Logfile.pm 19 KB

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