Logfile.pm 20 KB

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