Logfile.pm 14 KB

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