Logfile.pm 18 KB

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