4
0

Template.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # This is a template for creating your own logfile parser. You can also look
  2. # in the other .pm files in this directory as good examples.
  3. package Pisg::Parser::Format::Template;
  4. use strict;
  5. $^W = 1;
  6. # The 3 variables in the new subrountine, 'normalline', 'actionline' and
  7. # 'thirdline' represents regular expressions for extracting information from
  8. # the logfile. normalline is for lines where the person merely said
  9. # something, actionline is for lines where the person performed an action,
  10. # and thirdline matches everything else, including things like kicks, nick
  11. # changes, and op grants. See the thirdline subroutine for a list of
  12. # everything it should match.
  13. sub new
  14. {
  15. my $type = shift;
  16. my $self = {
  17. debug => $_[0],
  18. normalline => '',
  19. actionline => '',
  20. thirdline => '',
  21. };
  22. bless($self, $type);
  23. return $self;
  24. }
  25. # Parse a normal line - returns a hash with 'hour', 'nick' and 'saying'
  26. sub normalline
  27. {
  28. my ($self, $line, $lines) = @_;
  29. my %hash;
  30. if ($line =~ /$self->{normalline}/) {
  31. $self->{debug}->("[$lines] Normal: $1 $2 $3");
  32. # Most log formats are regular enough that you can just match the
  33. # appropriate things with parentheses in the regular expression.
  34. $hash{hour} = $1;
  35. $hash{nick} = $2;
  36. $hash{saying} = $3;
  37. return \%hash;
  38. } else {
  39. return;
  40. }
  41. }
  42. # Parse an action line - returns a hash with 'hour', 'nick' and 'saying'
  43. sub actionline
  44. {
  45. my ($self, $line, $lines) = @_;
  46. my %hash;
  47. if ($line =~ /$self->{actionline}/) {
  48. $self->{debug}->("[$lines] Action: $1 $2 $3");
  49. # Most log formats are regular enough that you can just match the
  50. # appropriate things with parentheses in the regular expression.
  51. $hash{hour} = $1;
  52. $hash{nick} = $2;
  53. $hash{saying} = $3;
  54. return \%hash;
  55. } else {
  56. return;
  57. }
  58. }
  59. # Parses the 'third' line - (the third line is everything else, like
  60. # topic changes, mode changes, kicks, etc.)
  61. # thirdline() has to return a hash with the following keys, for
  62. # every format:
  63. # hour - the hour we're in (for timestamp logging)
  64. # min - the minute we're in (for timestamp logging)
  65. # nick - the nick
  66. # kicker - the nick which kicked somebody (if any)
  67. # newtopic - the new topic (if any)
  68. # newmode - deops or ops, must be '+o' or '-o', or '+ooo'
  69. # newjoin - a new nick which has joined the channel
  70. # newnick - a person has changed nick and this is the new nick
  71. #
  72. # The hash may also have a "repeated" key indicating the number of times
  73. # the line was repeated. (Used by eggdrops log for example.)
  74. sub thirdline
  75. {
  76. my ($self, $line, $lines) = @_;
  77. my %hash;
  78. if ($line =~ /$self->{thirdline}/) {
  79. $self->{debug}->("[$lines] ***: $1 $2 $3 $4 $5 $6 $7 $8 $9");
  80. $hash{hour} = $1;
  81. $hash{min} = $2;
  82. $hash{nick} = $3;
  83. # Format-specific stuff goes here.
  84. return \%hash;
  85. } else {
  86. return;
  87. }
  88. }
  89. 1;