Logfile.pm 19 KB

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