pircbot.pm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. . '|' .
  21. '^(\d+)\s(>>>)PRIVMSG\s(#\S+)\s:([^' . $ctcpchr . '].*)$',
  22. actionline => '^(\d+)\s:([^!]+)!([^@]+)@(\S+)\sPRIVMSG\s(#\S+)\s:' . $ctcpchr . 'ACTION (.+)' . $ctcpchr . '\s*$'
  23. . '|' .
  24. '^(\d+)\s(>>>)PRIVMSG\s(#\S+)\s:' . $ctcpchr . 'ACTION (.+)' . $ctcpchr . '\s*$',
  25. thirdline => '^(\d+)\s:([^!]+)![^@]+@\S+\s(.+)$'
  26. . '|' .
  27. '^(\d+)\s(>>>)([^P]\S+)\s+(.+)$',
  28. };
  29. bless($self, $type);
  30. return $self;
  31. }
  32. # Parse a normal line - returns a hash with 'hour', 'nick' and 'saying'
  33. sub normalline
  34. {
  35. my ($self, $line, $lines) = @_;
  36. my %hash;
  37. if ($line =~ /$self->{normalline}/o) {
  38. if (defined($8)) {
  39. return unless (lc($7) eq lc($self->{cfg}->{channel}));
  40. my @time = localtime($5 / 1000);
  41. $hash{hour} = $time[2];
  42. $hash{nick} = $6;
  43. $hash{saying} = $8;
  44. } else {
  45. return unless (lc($3) eq lc($self->{cfg}->{channel}));
  46. my @time = localtime($1 / 1000);
  47. $hash{hour} = $time[2];
  48. $hash{nick} = $2;
  49. $hash{saying} = $4;
  50. }
  51. $hash{nick} = $self->{cfg}->{maintainer}
  52. if ($hash{nick} eq '>>>');
  53. return \%hash;
  54. } else {
  55. return;
  56. }
  57. }
  58. # Parse an action line - returns a hash with 'hour', 'nick' and 'saying'
  59. sub actionline
  60. {
  61. my ($self, $line, $lines) = @_;
  62. my %hash;
  63. if ($line =~ /$self->{actionline}/o) {
  64. if (defined($8)) {
  65. return unless (lc($7) eq lc($self->{cfg}->{channel}));
  66. my @time = localtime($5 / 1000);
  67. $hash{hour} = $time[2];
  68. $hash{nick} = $6;
  69. $hash{saying} = $8;
  70. } else {
  71. return unless (lc($3) eq lc($self->{cfg}->{channel}));
  72. my @time = localtime($1 / 1000);
  73. $hash{hour} = $time[2];
  74. $hash{nick} = $2;
  75. $hash{saying} = $4;
  76. }
  77. $hash{nick} = $self->{cfg}->{maintainer}
  78. if ($hash{nick} eq '>>>');
  79. return \%hash;
  80. } else {
  81. return;
  82. }
  83. }
  84. # Parses the 'third' line - (the third line is everything else, like
  85. # topic changes, mode changes, kicks, etc.)
  86. # thirdline() has to return a hash with the following keys, for
  87. # every format:
  88. # hour - the hour we're in (for timestamp logging)
  89. # min - the minute we're in (for timestamp logging)
  90. # nick - the nick
  91. # kicker - the nick which kicked somebody (if any)
  92. # newtopic - the new topic (if any)
  93. # newmode - deops or ops, must be '+o' or '-o', or '+ooo'
  94. # newjoin - a new nick which has joined the channel
  95. # newnick - a person has changed nick and this is the new nick
  96. #
  97. # The hash may also have a "repeated" key indicating the number of times
  98. # the line was repeated. (Used by eggdrops log for example.)
  99. sub thirdline
  100. {
  101. my ($self, $line, $lines) = @_;
  102. my %hash;
  103. if ($line =~ /$self->{thirdline}/o) {
  104. my ($time, @line);
  105. if (defined($6)) {
  106. my @time = localtime($4 / 1000);
  107. $hash{hour} = $time[2];
  108. $hash{min} = $time[1];
  109. $hash{nick} = $self->{cfg}->{maintainer};
  110. @line = split(/\s+/, $6);
  111. } else {
  112. my @time = localtime($1 / 1000);
  113. $hash{hour} = $time[2];
  114. $hash{min} = $time[1];
  115. $hash{nick} = $2;
  116. @line = split(/\s+/, $3);
  117. }
  118. if ($line[0] eq 'KICK') {
  119. return unless (lc($line[1]) eq lc($self->{cfg}->{channel}));
  120. $hash{kicker} = $hash{nick};
  121. $hash{nick} = $line[2];
  122. } elsif ($line[0] eq 'TOPIC') {
  123. return unless (lc($line[1]) eq lc($self->{cfg}->{channel}));
  124. $hash{newtopic} = join(' ', @line[2..$#line]);
  125. $hash{newtopic} =~ s/^://;
  126. } elsif ($line[0] eq 'MODE') {
  127. return unless (lc($line[1]) eq lc($self->{cfg}->{channel}));
  128. $hash{newmode} = join(' ', @line[2..$#line]);
  129. } elsif ($line[0] eq 'JOIN') {
  130. return unless (lc($line[1]) eq ':' . lc($self->{cfg}->{channel}));
  131. $hash{newjoin} = $hash{nick};
  132. } elsif ($line[0] eq 'NICK') {
  133. $hash{newnick} = $line[1];
  134. $hash{newnick} =~ s/^://;
  135. }
  136. return \%hash;
  137. } else {
  138. return;
  139. }
  140. }
  141. 1;