4
0

Logfile.pm 19 KB

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