Logfile.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package Pisg::Parser::Logfile;
  2. # Copyright and license, as well as documentation(POD) for this module is
  3. # found at the end of the file.
  4. use strict;
  5. $^W = 1;
  6. sub new
  7. {
  8. my $type = shift;
  9. my %args = @_;
  10. my $self = {
  11. cfg => $args{cfg},
  12. debug => $args{debug},
  13. parser => undef
  14. };
  15. # Import common functions in Pisg::Common
  16. require Pisg::Common;
  17. Pisg::Common->import();
  18. bless($self, $type);
  19. # Pick our parser.
  20. $self->{parser} = $self->_choose_format($self->{cfg}->{format});
  21. return $self;
  22. }
  23. # The function to choose which module to use.
  24. sub _choose_format
  25. {
  26. my $self = shift;
  27. my $format = shift;
  28. $self->{parser} = undef;
  29. $self->{debug}->("Loading module for log format $format");
  30. eval <<_END;
  31. use lib '$self->{cfg}->{modules_dir}';
  32. use Pisg::Parser::Format::$format;
  33. \$self->{parser} = new Pisg::Parser::Format::$format(\$self->{debug});
  34. _END
  35. if ($@) {
  36. print STDERR "Could not load parser for '$format': $@\n";
  37. return undef;
  38. }
  39. return $self->{parser};
  40. }
  41. sub analyze
  42. {
  43. my $self = shift;
  44. my (%stats, %lines);
  45. if (defined $self->{parser}) {
  46. my $starttime = time();
  47. if ($self->{cfg}->{logdir}) {
  48. # Run through all files in dir
  49. $self->_parse_dir(\%stats, \%lines);
  50. } else {
  51. # Run through the whole logfile
  52. my %state = (
  53. linecount => 0,
  54. lastnick => "",
  55. monocount => 0,
  56. lastnormal => "",
  57. oldtime => 24
  58. );
  59. $self->_parse_file(\%stats, \%lines, $self->{cfg}->{logfile}, \%state);
  60. }
  61. _pick_random_lines(\%stats, \%lines);
  62. my ($sec,$min,$hour) = gmtime(time() - $starttime);
  63. $stats{processtime} =
  64. sprintf("%02d hours, %02d minutes and %02d seconds", $hour, $min,
  65. $sec);
  66. print "Channel analyzed succesfully in $stats{processtime} on ",
  67. scalar localtime(time()), "\n";;
  68. return \%stats;
  69. } else {
  70. print STDERR "Skipping channel '$self->{cfg}->{channel}' due to lack of parser.\n";
  71. return undef
  72. }
  73. # Shouldn't get here.
  74. return undef;
  75. }
  76. sub _parse_dir
  77. {
  78. my $self = shift;
  79. my ($stats, $lines) = @_;
  80. # Add trailing slash when it's not there..
  81. $self->{cfg}->{logdir} =~ s/([^\/])$/$1\//;
  82. print "Going into $self->{cfg}->{logdir} and parsing all files there...\n\n";
  83. my @filesarray;
  84. opendir(LOGDIR, $self->{cfg}->{logdir}) or
  85. die("Can't opendir $self->{cfg}->{logdir}: $!");
  86. @filesarray = grep {
  87. /^[^\.]/ && /^$self->{cfg}->{prefix}/ && -f "$self->{cfg}->{logdir}/$_"
  88. } readdir(LOGDIR) or
  89. die("No files in \"$self->{cfg}->{logdir}\" matched prefix \"$self->{cfg}->{prefix}\"");
  90. closedir(LOGDIR);
  91. my %state = (
  92. lastnick => "",
  93. monocount => 0,
  94. oldtime => 24
  95. );
  96. foreach my $file (sort @filesarray) {
  97. $file = $self->{cfg}->{logdir} . $file;
  98. $self->_parse_file($stats, $lines, $file, \%state);
  99. }
  100. }
  101. # This parses the file...
  102. sub _parse_file
  103. {
  104. my $self = shift;
  105. my ($stats, $lines, $file, $state) = @_;
  106. print "Analyzing log($file) in '$self->{cfg}->{format}' format...\n";
  107. if ($file =~ /.bz2?$/ && -f $file) {
  108. open (LOGFILE, "bunzip2 -c $file |") or
  109. die("$0: Unable to open logfile($file): $!\n");
  110. } elsif ($file =~ /.gz$/ && -f $file) {
  111. open (LOGFILE, "gunzip -c $file |") or
  112. die("$0: Unable to open logfile($file): $!\n");
  113. } else {
  114. open (LOGFILE, $file) or
  115. die("$0: Unable to open logfile($file): $!\n");
  116. }
  117. my $linecount = 0;
  118. my $lastnormal = "";
  119. my $repeated;
  120. $stats->{days} = 0;
  121. $stats->{totallines} = 0;
  122. while(my $line = <LOGFILE>) {
  123. $line = _strip_mirccodes($line);
  124. $linecount++;
  125. my $hashref;
  126. # Match normal lines.
  127. if ($hashref = $self->{parser}->normalline($line, $linecount)) {
  128. if (defined $hashref->{repeated}) {
  129. $repeated = $hashref->{repeated};
  130. } else {
  131. $repeated = 0;
  132. }
  133. my ($hour, $nick, $saying, $i);
  134. for ($i = 0; $i <= $repeated; $i++) {
  135. if ($i > 0) {
  136. $hashref = $self->{parser}->normalline($lastnormal, $linecount);
  137. #Increment number of lines for repeated lines
  138. $linecount++;
  139. }
  140. $hour = $hashref->{hour};
  141. $nick = find_alias($hashref->{nick});
  142. $saying = $hashref->{saying};
  143. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  144. $state->{oldtime} = $hour;
  145. unless (is_ignored($nick)) {
  146. $stats->{totallines}++;
  147. # Timestamp collecting
  148. $stats->{times}{$hour}++;
  149. $stats->{lines}{$nick}++;
  150. $stats->{line_times}{$nick}[int($hour/6)]++;
  151. # Count up monologues
  152. if ($state->{lastnick} eq $nick) {
  153. $state->{monocount}++;
  154. if ($state->{monocount} == 5) {
  155. $stats->{monologues}{$nick}++;
  156. }
  157. } else {
  158. $state->{monocount} = 0;
  159. }
  160. $state->{lastnick} = $nick;
  161. my $len = length($saying);
  162. if ($len > $self->{cfg}->{minquote} && $len < $self->{cfg}->{maxquote}) {
  163. push @{ $lines->{sayings}{$nick} }, $saying;
  164. } elsif (!$lines->{sayings}{$nick}) {
  165. # Just fill the users first saying in if he hasn't
  166. # said anything yet, to get rid of empty quotes.
  167. push @{ $lines->{sayings}{$nick} }, $saying;
  168. }
  169. $stats->{lengths}{$nick} += $len;
  170. $stats->{questions}{$nick}++
  171. if ($saying =~ /\?/);
  172. $stats->{shouts}{$nick}++
  173. if ($saying =~ /!/);
  174. if ($saying !~ /[a-z]/ && $saying =~ /[A-Z]/) {
  175. $stats->{allcaps}{$nick}++;
  176. push @{ $lines->{allcaplines}{$nick} }, $line;
  177. }
  178. $stats->{foul}{$nick}++
  179. if ($saying =~ /$self->{cfg}->{foul}/i);
  180. # Who smiles the most?
  181. # A regex matching al lot of smilies
  182. $stats->{smiles}{$nick}++
  183. if ($saying =~ /[8;:=][ ^-o]?[)pPD}\]>]/);
  184. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/{]/ and
  185. $saying !~ /\w+:\/\//) {
  186. $stats->{frowns}{$nick}++;
  187. }
  188. if (my $url = match_url($saying)) {
  189. $stats->{urlcounts}{$url}++;
  190. $stats->{urlnicks}{$url} = $nick;
  191. }
  192. $self->_parse_words($stats, $saying, $nick);
  193. }
  194. }
  195. $lastnormal = $line;
  196. $repeated = 0;
  197. }
  198. # Match action lines.
  199. elsif ($hashref = $self->{parser}->actionline($line, $linecount)) {
  200. $stats->{totallines}++;
  201. my ($hour, $nick, $saying);
  202. $hour = $hashref->{hour};
  203. $nick = find_alias($hashref->{nick});
  204. $saying = $hashref->{saying};
  205. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  206. $state->{oldtime} = $hour;
  207. unless (is_ignored($nick)) {
  208. # Timestamp collecting
  209. $stats->{times}{$hour}++;
  210. $stats->{actions}{$nick}++;
  211. push @{ $lines->{actionlines}{$nick} }, $line;
  212. $stats->{lines}{$nick}++;
  213. $stats->{line_times}{$nick}[int($hour/6)]++;
  214. if ($saying =~ /^($self->{cfg}->{violent}) (\S+)/) {
  215. my $victim = find_alias($2);
  216. $stats->{violence}{$nick}++;
  217. $stats->{attacked}{$victim}++;
  218. push @{ $lines->{violencelines}{$nick} }, $line;
  219. push @{ $lines->{attackedlines}{$victim} }, $line;
  220. }
  221. my $len = length($saying);
  222. $stats->{lengths}{$nick} += $len;
  223. $self->_parse_words($stats, $saying, $nick);
  224. }
  225. }
  226. # Match *** lines.
  227. elsif (($hashref = $self->{parser}->thirdline($line, $linecount)) and
  228. $hashref->{nick}) {
  229. $stats->{totallines}++;
  230. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  231. my ($newnick);
  232. $hour = $hashref->{hour};
  233. $min = $hashref->{min};
  234. $nick = find_alias($hashref->{nick});
  235. $kicker = find_alias($hashref->{kicker})
  236. if ($hashref->{kicker});
  237. $newtopic = $hashref->{newtopic};
  238. $newmode = $hashref->{newmode};
  239. $newjoin = $hashref->{newjoin};
  240. $newnick = $hashref->{newnick};
  241. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  242. $state->{oldtime} = $hour;
  243. unless (is_ignored($nick)) {
  244. # Timestamp collecting
  245. $stats->{times}{$hour}++;
  246. if (defined($kicker)) {
  247. $stats->{kicked}{$kicker}++;
  248. $stats->{gotkicked}{$nick}++;
  249. push @{ $lines->{kicklines}{$nick} }, $line;
  250. } elsif (defined($newtopic)) {
  251. unless ($newtopic eq '') {
  252. my $tcount;
  253. if (defined $stats->{topics}) {
  254. $tcount = @{ $stats->{topics} };
  255. } else {
  256. $tcount = 0;
  257. }
  258. $stats->{topics}[$tcount]{topic} = $newtopic;
  259. $stats->{topics}[$tcount]{nick} = $nick;
  260. $stats->{topics}[$tcount]{hour} = $hour;
  261. $stats->{topics}[$tcount]{min} = $min;
  262. }
  263. } elsif (defined($newmode)) {
  264. my @opchange = _opchanges($newmode);
  265. $stats->{gaveops}{$nick} += $opchange[0] if $opchange[0];
  266. $stats->{tookops}{$nick} += $opchange[1] if $opchange[1];
  267. } elsif (defined($newjoin)) {
  268. $stats->{joins}{$nick}++;
  269. } elsif (defined($newnick) and ($self->{cfg}->{nicktracking} == 1)) {
  270. add_alias($nick, $newnick);
  271. }
  272. }
  273. }
  274. }
  275. close(LOGFILE);
  276. print "Finished analyzing log, $stats->{days} days total.\n";
  277. }
  278. sub _opchanges
  279. {
  280. my (@ops, $plus);
  281. foreach (split(//, $_[0])) {
  282. if ($_ eq "o") {
  283. $ops[$plus]++;
  284. } elsif ($_ eq "+") {
  285. $plus = 0;
  286. } elsif ($_ eq "-") {
  287. $plus = 1;
  288. }
  289. }
  290. return @ops;
  291. }
  292. sub _parse_words
  293. {
  294. my $self = shift;
  295. my ($stats, $saying, $nick) = @_;
  296. foreach my $word (split(/[\s,!?.:;)(\"]+/, $saying)) {
  297. $stats->{words}{$nick}++;
  298. # remove uninteresting words
  299. next unless (length($word) >= $self->{cfg}->{wordlength});
  300. next if ($self->{cfg}->{ignoreword}{$word});
  301. # ignore contractions
  302. next if ($word =~ m/'..?$/);#'
  303. # Also ignore stuff from URLs.
  304. next if ($word =~ m{https?|^//});
  305. $stats->{wordcounts}{$word}++;
  306. $stats->{wordnicks}{$word} = $nick;
  307. }
  308. }
  309. sub _pick_random_lines
  310. {
  311. my ($stats, $lines) = @_;
  312. foreach my $key (keys %{ $lines }) {
  313. foreach my $nick (keys %{ $lines->{$key} }) {
  314. $stats->{$key}{$nick} =
  315. @{ $lines->{$key}{$nick} }[rand@{ $lines->{$key}{$nick} }];
  316. }
  317. }
  318. }
  319. sub _strip_mirccodes
  320. {
  321. my $line = shift;
  322. # boldcode = chr(2) = oct 001
  323. # colorcode = chr(3) = oct 003
  324. # plaincode = chr(15) = oct 017
  325. # reversecode = chr(22) = oct 026
  326. # underlinecode = chr(31) = oct 037
  327. # Strip mIRC color codes
  328. $line =~ s/\003\d{1,2},\d{1,2}//go;
  329. $line =~ s/\003\d{0,2}//go;
  330. # Strip mIRC bold, plain, reverse and underline codes
  331. $line =~ s/[\002\017\026\037]//go;
  332. return $line;
  333. }
  334. 1;
  335. __END__
  336. =head1 NAME
  337. Pisg::Parser::Logfile - class to parse a normal logfile
  338. =head1 DESCRIPTION
  339. C<Pisg::Parser::Logfile> parses a logfile using the configuration variables set in the 'cfg' option passed to the constructor.
  340. =head1 SYNOPSIS
  341. use Pisg::Parser::Logfile;
  342. $analyzer = new Pisg::Parser::Logfile(
  343. cfg => $cfg,
  344. debug => $debug,
  345. );
  346. =head1 CONSTRUCTOR
  347. =over 4
  348. =item new ( [ OPTIONS ] )
  349. This is the constructor for a new Pisg::Parser::Logfile object. C<OPTIONS> are passed in a hash like fashion using key and value pairs.
  350. Possible options are:
  351. B<cfg> - hashref containing configuration variables, created by the Pisg module.
  352. B<debug> - reference to a sub routine to send the debug information.
  353. =back
  354. =head1 AUTHOR
  355. Morten Brix Pedersen <morten@wtf.dk>
  356. =head1 COPYRIGHT
  357. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  358. This program is free software; you can redistribute it and/or modify it
  359. under the terms of the GPL, license is included with the distribution of
  360. this file.
  361. =cut