Logfile.pm 14 KB

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