Logfile.pm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. parser => undef
  13. };
  14. # Import common functions in Pisg::Common
  15. require Pisg::Common;
  16. Pisg::Common->import();
  17. bless($self, $type);
  18. # Pick our parser.
  19. $self->{parser} = $self->_choose_format($self->{cfg}->{format});
  20. return $self;
  21. }
  22. # The function to choose which module to use.
  23. sub _choose_format
  24. {
  25. my $self = shift;
  26. my $format = shift;
  27. $self->{parser} = undef;
  28. eval <<_END;
  29. use lib '$self->{cfg}->{modules_dir}';
  30. use Pisg::Parser::Format::$format;
  31. \$self->{parser} = new Pisg::Parser::Format::$format(
  32. cfg => \$self->{cfg},
  33. );
  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. _uniquify_nicks(\%stats);
  66. my ($sec,$min,$hour) = gmtime(time() - $starttime);
  67. my $processtime = sprintf('%02d hours, %02d minutes and %02d seconds', $hour, $min, $sec);
  68. $stats{processtime}{hours} = sprintf('%02d', $hour);
  69. $stats{processtime}{mins} = sprintf('%02d', $min);
  70. $stats{processtime}{secs} = sprintf('%02d', $sec);
  71. print "Channel analyzed succesfully in $processtime on ",
  72. scalar localtime(time()), "\n"
  73. unless ($self->{cfg}->{silent});
  74. return \%stats;
  75. } else {
  76. print STDERR "Skipping channel '$self->{cfg}->{channel}' due to lack of parser.\n";
  77. return undef
  78. }
  79. # Shouldn't get here.
  80. return undef;
  81. }
  82. sub _parse_dir
  83. {
  84. my $self = shift;
  85. my ($stats, $lines) = @_;
  86. # Add trailing slash when it's not there..
  87. $self->{cfg}->{logdir} =~ s/([^\/])$/$1\//;
  88. print "Going into $self->{cfg}->{logdir} and parsing all files there...\n\n"
  89. unless ($self->{cfg}->{silent});
  90. my @filesarray;
  91. opendir(LOGDIR, $self->{cfg}->{logdir}) or
  92. die("Can't opendir $self->{cfg}->{logdir}: $!");
  93. @filesarray = grep {
  94. /^[^\.]/ && /^$self->{cfg}->{logprefix}/ && -f "$self->{cfg}->{logdir}/$_"
  95. } readdir(LOGDIR) or
  96. die("No files in \"$self->{cfg}->{logdir}\" matched prefix \"$self->{cfg}->{logprefix}\"");
  97. closedir(LOGDIR);
  98. my %state = (
  99. lastnick => '',
  100. monocount => 0,
  101. oldtime => 24
  102. );
  103. if ($self->{cfg}->{logsuffix} ne '') {
  104. my @temparray;
  105. my %months = (
  106. 'jan' => '0',
  107. 'feb' => '1',
  108. 'mar' => '2',
  109. 'apr' => '3',
  110. 'may' => '4',
  111. 'jun' => '5',
  112. 'jul' => '6',
  113. 'aug' => '7',
  114. 'sep' => '8',
  115. 'oct' => '9',
  116. 'nov' => '10',
  117. 'dec' => '11',
  118. );
  119. my ($mreg, $dreg, $yreg) = split(/\|\|/, $self->{cfg}->{logsuffix});
  120. my (@month, @day, @year);
  121. for my $file (@filesarray) {
  122. LOOPSTART:
  123. if ($file =~ /$mreg/) {
  124. my $month = $1;
  125. $month = lc $month;
  126. $month = $months{$month}
  127. if (defined $months{$month});
  128. push @month, $month;
  129. } else {
  130. splice(@filesarray,$#month + 1, 1);
  131. $file = $filesarray[$#month + 1];
  132. goto LOOPSTART;
  133. }
  134. if ($file =~ /$dreg/) {
  135. push @day, $1;
  136. } else {
  137. splice(@filesarray,$#day + 1, 1);
  138. splice(@month,$#day + 1);
  139. $file = $filesarray[$#day + 1];
  140. goto LOOPSTART;
  141. }
  142. if ($file =~ /$yreg/) {
  143. push @year, $1;
  144. } else {
  145. splice(@filesarray,$#year + 1, 1);
  146. splice(@month,$#year + 1);
  147. splice(@day,$#year + 1);
  148. $file = $filesarray[$#year + 1];
  149. goto LOOPSTART;
  150. }
  151. }
  152. my @newarray = @filesarray[ sort {
  153. $year[$a] <=> $year[$b]
  154. ||
  155. $month[$a] <=> $month[$b]
  156. ||
  157. $day[$a] <=> $day[$b]
  158. } 0..$#filesarray ];
  159. @filesarray = @newarray;
  160. } else {
  161. @filesarray = sort {lc($a) cmp lc($b)} @filesarray;
  162. }
  163. foreach my $file (@filesarray) {
  164. $file = $self->{cfg}->{logdir} . $file;
  165. $self->_parse_file($stats, $lines, $file, \%state);
  166. }
  167. }
  168. # This parses the file...
  169. sub _parse_file
  170. {
  171. my $self = shift;
  172. my ($stats, $lines, $file, $state) = @_;
  173. print "Analyzing log($file) in '$self->{cfg}->{format}' format...\n"
  174. unless ($self->{cfg}->{silent});
  175. if ($file =~ /.bz2?$/ && -f $file) {
  176. open (LOGFILE, "bunzip2 -c $file |") or
  177. die("$0: Unable to open logfile($file): $!\n");
  178. } elsif ($file =~ /.gz$/ && -f $file) {
  179. open (LOGFILE, "gunzip -c $file |") or
  180. die("$0: Unable to open logfile($file): $!\n");
  181. } else {
  182. open (LOGFILE, $file) or
  183. die("$0: Unable to open logfile($file): $!\n");
  184. }
  185. my $lastnormal = '';
  186. while(my $line = <LOGFILE>) {
  187. $line = _strip_mirccodes($line);
  188. $line =~ s/\r+$//; # Strip DOS Formatting
  189. my $hashref;
  190. # Match normal lines.
  191. if ($hashref = $self->{parser}->normalline($line, $.)) {
  192. my $repeated = 0;
  193. if (defined $hashref->{repeated}) {
  194. $repeated = $hashref->{repeated};
  195. }
  196. my ($hour, $nick, $saying, $i);
  197. for ($i = 0; $i <= $repeated; $i++) {
  198. if ($i > 0) {
  199. $hashref = $self->{parser}->normalline($lastnormal, $.);
  200. #Increment number of lines for repeated lines
  201. }
  202. $hour = $self->_adjusttimeoffset($hashref->{hour});
  203. $nick = find_alias($hashref->{nick});
  204. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  205. $saying = $hashref->{saying};
  206. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  207. $state->{oldtime} = $hour;
  208. if (!is_ignored($nick)) {
  209. $stats->{totallines}++;
  210. # Timestamp collecting
  211. $stats->{times}{$hour}++;
  212. $stats->{lines}{$nick}++;
  213. $stats->{lastvisited}{$nick} = $stats->{days};
  214. $stats->{line_times}{$nick}[int($hour/6)]++;
  215. # Count up monologues
  216. if ($state->{lastnick} eq $nick) {
  217. $state->{monocount}++;
  218. if ($state->{monocount} == 5) {
  219. $stats->{monologues}{$nick}++;
  220. }
  221. } else {
  222. $state->{monocount} = 0;
  223. }
  224. $state->{lastnick} = $nick;
  225. my $len = length($saying);
  226. if ($len > $self->{cfg}->{minquote} && $len < $self->{cfg}->{maxquote}) {
  227. push @{ $lines->{sayings}{$nick} }, $saying;
  228. } elsif (!$lines->{sayings}{$nick}) {
  229. # Just fill the users first saying in if he hasn't
  230. # said anything yet, to get rid of empty quotes.
  231. push @{ $lines->{sayings}{$nick} }, substr($saying, 0, $self->{cfg}->{maxquote});
  232. }
  233. $stats->{lengths}{$nick} += $len;
  234. $stats->{questions}{$nick}++
  235. if (index($saying, '?') > -1);
  236. $stats->{shouts}{$nick}++
  237. if (index($saying, '!') > -1);
  238. if ($saying !~ /[a-z]/o && $saying =~ /[A-Z]/o) {
  239. # Ignore single smileys on a line. eg. '<user> :P'
  240. if ($saying !~ /^[8;:=][ ^-o]?[)pPD}\]>]$/o) {
  241. $stats->{allcaps}{$nick}++;
  242. push @{ $lines->{allcaplines}{$nick} }, $line;
  243. }
  244. }
  245. if (my @foul = $saying =~ /(\b$self->{cfg}->{foulwords}|$self->{cfg}->{foulwords}\b)/io) {
  246. $stats->{foul}{$nick} += scalar @foul;
  247. push @{ $lines->{foullines}{$nick} }, $line;
  248. }
  249. # Who smiles the most?
  250. # A regex matching al lot of smilies
  251. $stats->{smiles}{$nick}++
  252. if ($saying =~ /[8;:=][ ^-o]?[)pPD}\]>]/o);
  253. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/{]/o and
  254. $saying !~ /\w+:\/\//o) {
  255. $stats->{frowns}{$nick}++;
  256. }
  257. # Find URLs
  258. if (my @urls = match_urls($saying)) {
  259. foreach my $url (@urls) {
  260. if(!url_is_ignored($url)) {
  261. $url =~ s/&/&amp;/g;
  262. $stats->{urlcounts}{$url}++;
  263. $stats->{urlnicks}{$url} = $nick;
  264. }
  265. }
  266. }
  267. _parse_words($stats, $saying, $nick, $self->{cfg}->{ignoreword}, $hour);
  268. }
  269. }
  270. $lastnormal = $line;
  271. $repeated = 0;
  272. }
  273. # Match action lines.
  274. elsif ($hashref = $self->{parser}->actionline($line, $.)) {
  275. $stats->{totallines}++;
  276. my ($hour, $nick, $saying);
  277. $hour = $self->_adjusttimeoffset($hashref->{hour});
  278. $nick = find_alias($hashref->{nick});
  279. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  280. $saying = $hashref->{saying};
  281. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  282. $state->{oldtime} = $hour;
  283. if (!is_ignored($nick)) {
  284. # Timestamp collecting
  285. $stats->{times}{$hour}++;
  286. $stats->{actions}{$nick}++;
  287. push @{ $lines->{actionlines}{$nick} }, $line;
  288. $stats->{lines}{$nick}++;
  289. $stats->{lastvisited}{$nick} = $stats->{days};
  290. $stats->{line_times}{$nick}[int($hour/6)]++;
  291. if ($saying =~ /^($self->{cfg}->{violentwords}) (\S+)(.*)/o) {
  292. my $victim;
  293. unless ($victim = is_nick($2)) {
  294. foreach my $trynick (split(/\s+/, $3)) {
  295. last if ($victim = is_nick($trynick));
  296. }
  297. unless ($victim) {
  298. $victim = $2;
  299. }
  300. }
  301. if (!is_ignored($victim)) {
  302. $stats->{violence}{$nick}++;
  303. $stats->{attacked}{$victim}++;
  304. push @{ $lines->{violencelines}{$nick} }, $line;
  305. push @{ $lines->{attackedlines}{$victim} }, $line;
  306. }
  307. }
  308. $stats->{lengths}{$nick} += length($saying);
  309. _parse_words($stats, $saying, $nick, $self->{cfg}->{ignoreword}, $hour);
  310. }
  311. }
  312. # Match *** lines.
  313. elsif (($hashref = $self->{parser}->thirdline($line, $.)) and $hashref->{nick}) {
  314. $stats->{totallines}++;
  315. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  316. my ($newnick);
  317. $hour = $self->_adjusttimeoffset($hashref->{hour});
  318. $min = $hashref->{min};
  319. $nick = find_alias($hashref->{nick});
  320. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  321. $kicker = find_alias($hashref->{kicker})
  322. if ($hashref->{kicker});
  323. $newtopic = $hashref->{newtopic};
  324. $newmode = $hashref->{newmode};
  325. $newjoin = $hashref->{newjoin};
  326. $newnick = $hashref->{newnick};
  327. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  328. $state->{oldtime} = $hour;
  329. if (!is_ignored($nick)) {
  330. # Timestamp collecting
  331. $stats->{times}{$hour}++;
  332. $stats->{lastvisited}{$nick} = $stats->{days};
  333. if (defined($kicker)) {
  334. if (!is_ignored($kicker)) {
  335. $stats->{kicked}{$kicker}++;
  336. $stats->{gotkicked}{$nick}++;
  337. push @{ $lines->{kicklines}{$nick} }, $line;
  338. }
  339. } elsif (defined($newtopic) && $newtopic ne '') {
  340. _topic_change($stats, $newtopic, $nick, $hour, $min);
  341. } elsif (defined($newmode)) {
  342. _modechanges($stats, $newmode, $nick);
  343. } elsif (defined($newjoin)) {
  344. $stats->{joins}{$nick}++;
  345. } elsif (defined($newnick) and ($self->{cfg}->{nicktracking} == 1)) {
  346. # Resolve new nick to the correct alias (this will create a hard-alias if it is using a regex)
  347. $newnick = find_alias($newnick);
  348. add_alias($nick, $newnick);
  349. checkname($nick, $newnick, $stats) if ($self->{cfg}->{showmostnicks});
  350. }
  351. }
  352. }
  353. }
  354. close(LOGFILE);
  355. print "Finished analyzing log, $stats->{days} days total.\n"
  356. unless ($self->{cfg}->{silent});
  357. }
  358. sub _topic_change
  359. {
  360. my $stats = shift;
  361. my $newtopic = shift;
  362. my $nick = shift;
  363. my $hour = shift;
  364. my $min = shift;
  365. my $tcount = 0;
  366. if (defined $stats->{topics}) {
  367. $tcount = @{ $stats->{topics} };
  368. }
  369. $stats->{topics}[$tcount]{topic} = $newtopic;
  370. $stats->{topics}[$tcount]{nick} = $nick;
  371. $stats->{topics}[$tcount]{hour} = $hour;
  372. $stats->{topics}[$tcount]{min} = $min;
  373. }
  374. sub _modechanges
  375. {
  376. my $stats = shift;
  377. my $newmode = shift;
  378. my $nick = shift;
  379. my (@voice, @halfops, @ops, $plus);
  380. foreach (split(//, $newmode)) {
  381. if ($_ eq 'o') {
  382. $ops[$plus]++;
  383. } elsif ($_ eq 'h') {
  384. $halfops[$plus]++;
  385. } elsif ($_ eq 'v') {
  386. $voice[$plus]++;
  387. } elsif ($_ eq '+') {
  388. $plus = 0;
  389. } elsif ($_ eq '-') {
  390. $plus = 1;
  391. }
  392. }
  393. $stats->{gaveops}{$nick} += $ops[0] if $ops[0];
  394. $stats->{tookops}{$nick} += $ops[1] if $ops[1];
  395. $stats->{gavehalfops}{$nick} += $halfops[0] if $halfops[0];
  396. $stats->{tookhalfops}{$nick} += $halfops[1] if $halfops[1];
  397. $stats->{gavevoice}{$nick} += $voice[0] if $voice[0];
  398. $stats->{tookvoice}{$nick} += $voice[1] if $voice[1];
  399. }
  400. sub _parse_words
  401. {
  402. my ($stats, $saying, $nick, $ignoreword, $hour) = @_;
  403. # Cache time of day
  404. my $tod = int($hour/6);
  405. foreach my $word (split(/[\s,!?.:;)(\"]+/o, $saying)) {
  406. $stats->{words}{$nick}++;
  407. $stats->{word_times}{$nick}[$tod]++;
  408. # remove uninteresting words
  409. next if ($ignoreword->{$word});
  410. # ignore contractions
  411. next if ($word =~ m/'.{1,2}$/o);
  412. # Also ignore stuff from URLs.
  413. next if ($word =~ m/^https?$|^\/\//o);
  414. $stats->{wordcounts}{$word}++;
  415. $stats->{wordnicks}{$word} = $nick;
  416. }
  417. }
  418. sub _pick_random_lines
  419. {
  420. my ($stats, $lines) = @_;
  421. foreach my $key (keys %{ $lines }) {
  422. foreach my $nick (keys %{ $lines->{$key} }) {
  423. $stats->{$key}{$nick} =
  424. @{ $lines->{$key}{$nick} }[rand@{ $lines->{$key}{$nick} }];
  425. }
  426. }
  427. }
  428. sub _uniquify_nicks {
  429. my ($stats) = @_;
  430. foreach my $word (keys %{ $stats->{wordcounts} }) {
  431. if (is_nick($word)) {
  432. my $realnick = find_alias($word);
  433. # The lc() is an attempt at being case insensitive.
  434. if (lc($realnick) ne lc($word)) {
  435. $stats->{wordcounts}{$realnick} += $stats->{wordcounts}{$word};
  436. $stats->{wordnicks}{$realnick} = $stats->{wordnicks}{$word};
  437. delete $stats->{wordcounts}{$word};
  438. delete $stats->{wordnicks}{$word};
  439. }
  440. }
  441. }
  442. }
  443. sub _strip_mirccodes
  444. {
  445. my $line = shift;
  446. # boldcode = chr(2) = oct 001
  447. # colorcode = chr(3) = oct 003
  448. # plaincode = chr(15) = oct 017
  449. # reversecode = chr(22) = oct 026
  450. # underlinecode = chr(31) = oct 037
  451. # Strip mIRC color codes
  452. $line =~ s/\003\d{1,2},\d{1,2}//go;
  453. $line =~ s/\003\d{0,2}//go;
  454. # Strip mIRC bold, plain, reverse and underline codes
  455. $line =~ s/[\002\017\026\037]//go;
  456. return $line;
  457. }
  458. sub checkname {
  459. # This function tracks nickchanges and puts them all in a hash->array,
  460. # so we can show all nicks that I user had later (only works properly
  461. # when nicktracking is enabled)
  462. my ($nick, $newnick, $stats) = @_;
  463. $stats->{nicks}{$newnick}{lc($nick)} = $nick;
  464. }
  465. sub _adjusttimeoffset
  466. {
  467. my ($self, $hour) = @_;
  468. if ($self->{cfg}{timeoffset} != 0) {
  469. # Adjust time
  470. $hour += $self->{cfg}{timeoffset};
  471. $hour = $hour % 24;
  472. }
  473. return sprintf('%02d', $hour);
  474. }
  475. 1;
  476. __END__
  477. =head1 NAME
  478. Pisg::Parser::Logfile - class to parse a normal logfile
  479. =head1 DESCRIPTION
  480. C<Pisg::Parser::Logfile> parses a logfile using the configuration variables set in the 'cfg' option passed to the constructor.
  481. =head1 SYNOPSIS
  482. use Pisg::Parser::Logfile;
  483. $analyzer = new Pisg::Parser::Logfile(
  484. cfg => $cfg,
  485. );
  486. =head1 CONSTRUCTOR
  487. =over 4
  488. =item new ( [ OPTIONS ] )
  489. 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.
  490. Possible options are:
  491. B<cfg> - hashref containing configuration variables, created by the Pisg module.
  492. =back
  493. =head1 AUTHOR
  494. Morten Brix Pedersen <morten@wtf.dk>
  495. =head1 COPYRIGHT
  496. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  497. This program is free software; you can redistribute it and/or modify it
  498. under the terms of the GPL, license is included with the distribution of
  499. this file.
  500. =cut