Logfile.pm 18 KB

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