Template.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, %args) = @_;
  16. my $self = {
  17. cfg => $args{cfg},
  18. debug => $args{debug},
  19. normalline => '',
  20. actionline => '',
  21. thirdline => '',
  22. };
  23. bless($self, $type);
  24. return $self;
  25. }
  26. # Parse a normal line - returns a hash with 'hour', 'nick' and 'saying'
  27. sub normalline
  28. {
  29. my ($self, $line, $lines) = @_;
  30. my %hash;
  31. if ($line =~ /$self->{normalline}/) {
  32. $self->{debug}->("[$lines] Normal: $1 $2 $3");
  33. # Most log formats are regular enough that you can just match the
  34. # appropriate things with parentheses in the regular expression.
  35. $hash{hour} = $1;
  36. $hash{nick} = $2;
  37. $hash{saying} = $3;
  38. return \%hash;
  39. } else {
  40. return;
  41. }
  42. }
  43. # Parse an action line - returns a hash with 'hour', 'nick' and 'saying'
  44. sub actionline
  45. {
  46. my ($self, $line, $lines) = @_;
  47. my %hash;
  48. if ($line =~ /$self->{actionline}/) {
  49. $self->{debug}->("[$lines] Action: $1 $2 $3");
  50. # Most log formats are regular enough that you can just match the
  51. # appropriate things with parentheses in the regular expression.
  52. $hash{hour} = $1;
  53. $hash{nick} = $2;
  54. $hash{saying} = $3;
  55. return \%hash;
  56. } else {
  57. return;
  58. }
  59. }
  60. # Parses the 'third' line - (the third line is everything else, like
  61. # topic changes, mode changes, kicks, etc.)
  62. # thirdline() has to return a hash with the following keys, for
  63. # every format:
  64. # hour - the hour we're in (for timestamp logging)
  65. # min - the minute we're in (for timestamp logging)
  66. # nick - the nick
  67. # kicker - the nick which kicked somebody (if any)
  68. # newtopic - the new topic (if any)
  69. # newmode - deops or ops, must be '+o' or '-o', or '+ooo'
  70. # newjoin - a new nick which has joined the channel
  71. # newnick - a person has changed nick and this is the new nick
  72. #
  73. # The hash may also have a "repeated" key indicating the number of times
  74. # the line was repeated. (Used by eggdrops log for example.)
  75. sub thirdline
  76. {
  77. my ($self, $line, $lines) = @_;
  78. my %hash;
  79. if ($line =~ /$self->{thirdline}/) {
  80. $self->{debug}->("[$lines] ***: $1 $2 $3 $4 $5 $6 $7 $8 $9");
  81. $hash{hour} = $1;
  82. $hash{min} = $2;
  83. $hash{nick} = $3;
  84. # Format-specific stuff goes here.
  85. return \%hash;
  86. } else {
  87. return;
  88. }
  89. }
  90. 1;