Logfile.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. if (defined $self->{cfg}->{logsuffix}) {
  105. my @temparray;
  106. my %months = (
  107. 'jan' => '0',
  108. 'feb' => '1',
  109. 'mar' => '2',
  110. 'apr' => '3',
  111. 'may' => '4',
  112. 'jun' => '5',
  113. 'jul' => '6',
  114. 'aug' => '7',
  115. 'sep' => '8',
  116. 'oct' => '9',
  117. 'nov' => '10',
  118. 'dec' => '11',
  119. );
  120. my ($mreg, $dreg, $yreg) = split(/\|\|/, $self->{cfg}->{logsuffix});
  121. my (@month, @day, @year);
  122. for my $file (@filesarray) {
  123. $file =~ /$mreg/;
  124. my $month = $1;
  125. $month = lc $month;
  126. $month = $months{$month}
  127. if (defined $months{$month});
  128. push @month, $month;
  129. $file =~ /$dreg/;
  130. push @day, $1;
  131. $file =~ /$yreg/;
  132. push @year, $1;
  133. }
  134. my @newarray = @filesarray[ sort {
  135. $year[$a] <=> $year[$b]
  136. ||
  137. $month[$a] <=> $month[$b]
  138. ||
  139. $day[$a] <=> $day[$b]
  140. } 0..$#filesarray ];
  141. @filesarray = @newarray;
  142. } else {
  143. @filesarray = sort @filesarray;
  144. }
  145. foreach my $file (@filesarray) {
  146. $file = $self->{cfg}->{logdir} . $file;
  147. $self->_parse_file($stats, $lines, $file, \%state);
  148. }
  149. }
  150. # This parses the file...
  151. sub _parse_file
  152. {
  153. my $self = shift;
  154. my ($stats, $lines, $file, $state) = @_;
  155. print "Analyzing log($file) in '$self->{cfg}->{format}' format...\n"
  156. unless ($self->{cfg}->{silent});
  157. if ($file =~ /.bz2?$/ && -f $file) {
  158. open (LOGFILE, "bunzip2 -c $file |") or
  159. die("$0: Unable to open logfile($file): $!\n");
  160. } elsif ($file =~ /.gz$/ && -f $file) {
  161. open (LOGFILE, "gunzip -c $file |") or
  162. die("$0: Unable to open logfile($file): $!\n");
  163. } else {
  164. open (LOGFILE, $file) or
  165. die("$0: Unable to open logfile($file): $!\n");
  166. }
  167. my $linecount = 0;
  168. my $lastnormal = "";
  169. my $repeated;
  170. while(my $line = <LOGFILE>) {
  171. $line = _strip_mirccodes($line);
  172. $linecount++;
  173. my $hashref;
  174. # Match normal lines.
  175. if ($hashref = $self->{parser}->normalline($line, $linecount)) {
  176. if (defined $hashref->{repeated}) {
  177. $repeated = $hashref->{repeated};
  178. } else {
  179. $repeated = 0;
  180. }
  181. my ($hour, $nick, $saying, $i);
  182. for ($i = 0; $i <= $repeated; $i++) {
  183. if ($i > 0) {
  184. $hashref = $self->{parser}->normalline($lastnormal, $linecount);
  185. #Increment number of lines for repeated lines
  186. $linecount++;
  187. }
  188. $hour = $hashref->{hour};
  189. $nick = find_alias($hashref->{nick});
  190. $saying = $hashref->{saying};
  191. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  192. $state->{oldtime} = $hour;
  193. unless (is_ignored($nick)) {
  194. $stats->{totallines}++;
  195. # Timestamp collecting
  196. $stats->{times}{$hour}++;
  197. $stats->{lines}{$nick}++;
  198. $stats->{lastvisited}{$nick} = $stats->{days};
  199. $stats->{line_times}{$nick}[int($hour/6)]++;
  200. # Count up monologues
  201. if ($state->{lastnick} eq $nick) {
  202. $state->{monocount}++;
  203. if ($state->{monocount} == 5) {
  204. $stats->{monologues}{$nick}++;
  205. }
  206. } else {
  207. $state->{monocount} = 0;
  208. }
  209. $state->{lastnick} = $nick;
  210. my $len = length($saying);
  211. if ($len > $self->{cfg}->{minquote} && $len < $self->{cfg}->{maxquote}) {
  212. push @{ $lines->{sayings}{$nick} }, $saying;
  213. } elsif (!$lines->{sayings}{$nick}) {
  214. # Just fill the users first saying in if he hasn't
  215. # said anything yet, to get rid of empty quotes.
  216. push @{ $lines->{sayings}{$nick} }, $saying;
  217. }
  218. $stats->{lengths}{$nick} += $len;
  219. $stats->{questions}{$nick}++
  220. if ($saying =~ /\?/);
  221. $stats->{shouts}{$nick}++
  222. if ($saying =~ /!/);
  223. if ($saying !~ /[a-z]/ && $saying =~ /[A-Z]/) {
  224. # Ignore single smileys on a line. eg. '<user> :P'
  225. if ($saying !~ /^[8;:=][ ^-o]?[)pPD}\]>]$/) {
  226. $stats->{allcaps}{$nick}++;
  227. push @{ $lines->{allcaplines}{$nick} }, $line;
  228. }
  229. }
  230. $stats->{foul}{$nick}++
  231. if ($saying =~ /$self->{cfg}->{foul}/i);
  232. # Who smiles the most?
  233. # A regex matching al lot of smilies
  234. $stats->{smiles}{$nick}++
  235. if ($saying =~ /[8;:=][ ^-o]?[)pPD}\]>]/);
  236. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/{]/ and
  237. $saying !~ /\w+:\/\//) {
  238. $stats->{frowns}{$nick}++;
  239. }
  240. if (my $url = match_url($saying)) {
  241. unless(url_is_ignored($url)) {
  242. $stats->{urlcounts}{$url}++;
  243. $stats->{urlnicks}{$url} = $nick;
  244. }
  245. }
  246. $self->_parse_words($stats, $saying, $nick);
  247. }
  248. }
  249. $lastnormal = $line;
  250. $repeated = 0;
  251. }
  252. # Match action lines.
  253. elsif ($hashref = $self->{parser}->actionline($line, $linecount)) {
  254. $stats->{totallines}++;
  255. my ($hour, $nick, $saying);
  256. $hour = $hashref->{hour};
  257. $nick = find_alias($hashref->{nick});
  258. $saying = $hashref->{saying};
  259. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  260. $state->{oldtime} = $hour;
  261. unless (is_ignored($nick)) {
  262. # Timestamp collecting
  263. $stats->{times}{$hour}++;
  264. $stats->{actions}{$nick}++;
  265. push @{ $lines->{actionlines}{$nick} }, $line;
  266. $stats->{lines}{$nick}++;
  267. $stats->{lastvisited}{$nick} = $stats->{days};
  268. $stats->{line_times}{$nick}[int($hour/6)]++;
  269. if ($saying =~ /^($self->{cfg}->{violent}) (\S+)/) {
  270. my $victim = find_alias($2);
  271. unless (is_ignored($victim)) {
  272. $stats->{violence}{$nick}++;
  273. $stats->{attacked}{$victim}++;
  274. push @{ $lines->{violencelines}{$nick} }, $line;
  275. push @{ $lines->{attackedlines}{$victim} }, $line;
  276. }
  277. }
  278. my $len = length($saying);
  279. $stats->{lengths}{$nick} += $len;
  280. $self->_parse_words($stats, $saying, $nick);
  281. }
  282. }
  283. # Match *** lines.
  284. elsif (($hashref = $self->{parser}->thirdline($line, $linecount)) and
  285. $hashref->{nick}) {
  286. $stats->{totallines}++;
  287. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  288. my ($newnick);
  289. $hour = $hashref->{hour};
  290. $min = $hashref->{min};
  291. $nick = find_alias($hashref->{nick});
  292. $kicker = find_alias($hashref->{kicker})
  293. if ($hashref->{kicker});
  294. $newtopic = $hashref->{newtopic};
  295. $newmode = $hashref->{newmode};
  296. $newjoin = $hashref->{newjoin};
  297. $newnick = $hashref->{newnick};
  298. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  299. $state->{oldtime} = $hour;
  300. unless (is_ignored($nick)) {
  301. # Timestamp collecting
  302. $stats->{times}{$hour}++;
  303. $stats->{lastvisited}{$nick} = $stats->{days};
  304. if (defined($kicker)) {
  305. unless (is_ignored($kicker)) {
  306. $stats->{kicked}{$kicker}++;
  307. $stats->{gotkicked}{$nick}++;
  308. push @{ $lines->{kicklines}{$nick} }, $line;
  309. }
  310. } elsif (defined($newtopic)) {
  311. unless ($newtopic eq '') {
  312. my $tcount;
  313. if (defined $stats->{topics}) {
  314. $tcount = @{ $stats->{topics} };
  315. } else {
  316. $tcount = 0;
  317. }
  318. $stats->{topics}[$tcount]{topic} = $newtopic;
  319. $stats->{topics}[$tcount]{nick} = $nick;
  320. $stats->{topics}[$tcount]{hour} = $hour;
  321. $stats->{topics}[$tcount]{min} = $min;
  322. }
  323. } elsif (defined($newmode)) {
  324. _modechanges($stats, $newmode, $nick);
  325. } elsif (defined($newjoin)) {
  326. $stats->{joins}{$nick}++;
  327. } elsif (defined($newnick) and ($self->{cfg}->{nicktracking} == 1)) {
  328. add_alias($nick, $newnick);
  329. }
  330. }
  331. }
  332. }
  333. close(LOGFILE);
  334. print "Finished analyzing log, $stats->{days} days total.\n"
  335. unless ($self->{cfg}->{silent});
  336. }
  337. sub _modechanges
  338. {
  339. my $stats = shift;
  340. my $newmode = shift;
  341. my $nick = shift;
  342. my (@voice, @ops, $plus);
  343. foreach (split(//, $newmode)) {
  344. if ($_ eq "o") {
  345. $ops[$plus]++;
  346. } elsif ($_ eq "v") {
  347. $voice[$plus]++;
  348. } elsif ($_ eq "+") {
  349. $plus = 0;
  350. } elsif ($_ eq "-") {
  351. $plus = 1;
  352. }
  353. }
  354. $stats->{gaveops}{$nick} += $ops[0] if $ops[0];
  355. $stats->{tookops}{$nick} += $ops[1] if $ops[1];
  356. $stats->{gavevoice}{$nick} += $voice[0] if $voice[0];
  357. $stats->{tookvoice}{$nick} += $voice[1] if $voice[1];
  358. }
  359. sub _parse_words
  360. {
  361. my $self = shift;
  362. my ($stats, $saying, $nick) = @_;
  363. foreach my $word (split(/[\s,!?.:;)(\"]+/, $saying)) {
  364. $stats->{words}{$nick}++;
  365. # remove uninteresting words
  366. # next unless (length($word) >= $self->{cfg}->{wordlength});
  367. next if ($self->{cfg}->{ignoreword}{$word});
  368. # ignore contractions
  369. next if ($word =~ m/'..?$/);#'
  370. # Also ignore stuff from URLs.
  371. next if ($word =~ m{https?|^//});
  372. $stats->{wordcounts}{$word}++;
  373. $stats->{wordnicks}{$word} = $nick;
  374. }
  375. }
  376. sub _pick_random_lines
  377. {
  378. my ($stats, $lines) = @_;
  379. foreach my $key (keys %{ $lines }) {
  380. foreach my $nick (keys %{ $lines->{$key} }) {
  381. $stats->{$key}{$nick} =
  382. @{ $lines->{$key}{$nick} }[rand@{ $lines->{$key}{$nick} }];
  383. }
  384. }
  385. }
  386. sub _strip_mirccodes
  387. {
  388. my $line = shift;
  389. # boldcode = chr(2) = oct 001
  390. # colorcode = chr(3) = oct 003
  391. # plaincode = chr(15) = oct 017
  392. # reversecode = chr(22) = oct 026
  393. # underlinecode = chr(31) = oct 037
  394. # Strip mIRC color codes
  395. $line =~ s/\003\d{1,2},\d{1,2}//go;
  396. $line =~ s/\003\d{0,2}//go;
  397. # Strip mIRC bold, plain, reverse and underline codes
  398. $line =~ s/[\002\017\026\037]//go;
  399. return $line;
  400. }
  401. 1;
  402. __END__
  403. =head1 NAME
  404. Pisg::Parser::Logfile - class to parse a normal logfile
  405. =head1 DESCRIPTION
  406. C<Pisg::Parser::Logfile> parses a logfile using the configuration variables set in the 'cfg' option passed to the constructor.
  407. =head1 SYNOPSIS
  408. use Pisg::Parser::Logfile;
  409. $analyzer = new Pisg::Parser::Logfile(
  410. cfg => $cfg,
  411. debug => $debug,
  412. );
  413. =head1 CONSTRUCTOR
  414. =over 4
  415. =item new ( [ OPTIONS ] )
  416. 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.
  417. Possible options are:
  418. B<cfg> - hashref containing configuration variables, created by the Pisg module.
  419. B<debug> - reference to a sub routine to send the debug information.
  420. =back
  421. =head1 AUTHOR
  422. Morten Brix Pedersen <morten@wtf.dk>
  423. =head1 COPYRIGHT
  424. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  425. This program is free software; you can redistribute it and/or modify it
  426. under the terms of the GPL, license is included with the distribution of
  427. this file.
  428. =cut