pircbot.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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::pircbot;
  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 $ctcpchr = chr(1);
  17. my $self = {
  18. cfg => $args{cfg},
  19. normalline => '^(\d+)\s:([^!]+)!([^@]+)@(\S+)\sPRIVMSG\s(#\S+)\s:([^' . $ctcpchr . '].*)$',
  20. actionline => '^(\d+)\s:([^!]+)!([^@]+)@(\S+)\sPRIVMSG\s(#\S+)\s:' . $ctcpchr . 'ACTION (.+)' . $ctcpchr . '\s*$',
  21. thirdline => '^(\d+)\s:([^!]+)!([^@]+)@(\S+)\s(.+)$',
  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}/o) {
  32. return unless (lc($5) eq lc($self->{cfg}->{channel}));
  33. # Most log formats are regular enough that you can just match the
  34. # appropriate things with parentheses in the regular expression.
  35. my @time = localtime($1 / 1000);
  36. $hash{hour} = $time[2];
  37. $hash{nick} = $2;
  38. $hash{saying} = $6;
  39. return \%hash;
  40. } else {
  41. return;
  42. }
  43. }
  44. # Parse an action line - returns a hash with 'hour', 'nick' and 'saying'
  45. sub actionline
  46. {
  47. my ($self, $line, $lines) = @_;
  48. my %hash;
  49. if ($line =~ /$self->{actionline}/o) {
  50. return unless (lc($5) eq lc($self->{cfg}->{channel}));
  51. # Most log formats are regular enough that you can just match the
  52. # appropriate things with parentheses in the regular expression.
  53. my @time = localtime($1 / 1000);
  54. $hash{hour} = $time[2];
  55. $hash{nick} = $2;
  56. $hash{saying} = $6;
  57. return \%hash;
  58. } else {
  59. return;
  60. }
  61. }
  62. # Parses the 'third' line - (the third line is everything else, like
  63. # topic changes, mode changes, kicks, etc.)
  64. # thirdline() has to return a hash with the following keys, for
  65. # every format:
  66. # hour - the hour we're in (for timestamp logging)
  67. # min - the minute we're in (for timestamp logging)
  68. # nick - the nick
  69. # kicker - the nick which kicked somebody (if any)
  70. # newtopic - the new topic (if any)
  71. # newmode - deops or ops, must be '+o' or '-o', or '+ooo'
  72. # newjoin - a new nick which has joined the channel
  73. # newnick - a person has changed nick and this is the new nick
  74. #
  75. # The hash may also have a "repeated" key indicating the number of times
  76. # the line was repeated. (Used by eggdrops log for example.)
  77. sub thirdline
  78. {
  79. my ($self, $line, $lines) = @_;
  80. my %hash;
  81. if ($line =~ /$self->{thirdline}/o) {
  82. my @time = localtime($1 / 1000);
  83. $hash{hour} = $time[2];
  84. $hash{min} = $time[1];
  85. $hash{nick} = $2;
  86. my @line = split(/\s+/, "$5");
  87. if ($line[0] eq 'KICK') {
  88. return unless (lc($line[1]) eq lc($self->{cfg}->{channel}));
  89. $hash{kicker} = $hash{nick};
  90. $hash{nick} = $line[2];
  91. } elsif ($line[0] eq 'TOPIC') {
  92. return unless (lc($line[1]) eq lc($self->{cfg}->{channel}));
  93. $hash{newtopic} = join(' ', @line[2..$#line]);
  94. $hash{newtopic} =~ s/^://;
  95. } elsif ($line[0] eq 'MODE') {
  96. return unless (lc($line[1]) eq lc($self->{cfg}->{channel}));
  97. $hash{newmode} = join(' ', @line[2..$#line]);
  98. } elsif ($line[0] eq 'JOIN') {
  99. return unless (lc($line[1]) eq ':' . lc($self->{cfg}->{channel}));
  100. $hash{newjoin} = $hash{nick};
  101. } elsif ($line[0] eq 'NICK') {
  102. $hash{newnick} = $line[1];
  103. $hash{newnick} =~ s/^://;
  104. }
  105. return \%hash;
  106. } else {
  107. return;
  108. }
  109. }
  110. 1;