pircbot.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. print "$line\n";
  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. # Most log formats are regular enough that you can just match the
  51. # appropriate things with parentheses in the regular expression.
  52. my @time = localtime($1 / 1000);
  53. $hash{hour} = $time[2];
  54. $hash{nick} = $2;
  55. $hash{saying} = $6;
  56. return \%hash;
  57. } else {
  58. return;
  59. }
  60. }
  61. # Parses the 'third' line - (the third line is everything else, like
  62. # topic changes, mode changes, kicks, etc.)
  63. # thirdline() has to return a hash with the following keys, for
  64. # every format:
  65. # hour - the hour we're in (for timestamp logging)
  66. # min - the minute we're in (for timestamp logging)
  67. # nick - the nick
  68. # kicker - the nick which kicked somebody (if any)
  69. # newtopic - the new topic (if any)
  70. # newmode - deops or ops, must be '+o' or '-o', or '+ooo'
  71. # newjoin - a new nick which has joined the channel
  72. # newnick - a person has changed nick and this is the new nick
  73. #
  74. # The hash may also have a "repeated" key indicating the number of times
  75. # the line was repeated. (Used by eggdrops log for example.)
  76. sub thirdline
  77. {
  78. my ($self, $line, $lines) = @_;
  79. my %hash;
  80. if ($line =~ /$self->{thirdline}/o) {
  81. my @time = localtime($1 / 1000);
  82. $hash{hour} = $time[2];
  83. $hash{min} = $time[1];
  84. $hash{nick} = $2;
  85. my @line = split(/\s+/, "$5");
  86. if ($line[0] eq 'KICK') {
  87. $hash{kicker} = $hash{nick};
  88. $hash{nick} = $line[2];
  89. } elsif ($line[0] eq 'TOPIC') {
  90. $hash{newtopic} = join(' ', @line[2..$#line]);
  91. $hash{newtopic} =~ s/^://;
  92. } elsif ($line[0] eq 'MODE') {
  93. $hash{newmode} = join(' ', @line[2..$#line]);
  94. } elsif ($line[0] eq 'JOIN') {
  95. $hash{newjoin} = $hash{nick};
  96. } elsif ($line[0] eq 'NICK') {
  97. $hash{newnick} = $line[1];
  98. $hash{newnick} =~ s/^://;
  99. }
  100. return \%hash;
  101. } else {
  102. return;
  103. }
  104. }
  105. 1;