Logfile.pm 14 KB

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