Logfile.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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}->{prefix}/ && -f "$self->{cfg}->{logdir}/$_"
  95. } readdir(LOGDIR) or
  96. die("No files in \"$self->{cfg}->{logdir}\" matched prefix \"$self->{cfg}->{prefix}\"");
  97. closedir(LOGDIR);
  98. my %state = (
  99. lastnick => "",
  100. monocount => 0,
  101. oldtime => 24
  102. );
  103. if (defined $self->{cfg}->{logsuffix}) {
  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 @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 = $hashref->{hour};
  183. $nick = find_alias($hashref->{nick});
  184. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{show_mostnicks});
  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]/ && $saying =~ /[A-Z]/) {
  219. # Ignore single smileys on a line. eg. '<user> :P'
  220. if ($saying !~ /^[8;:=][ ^-o]?[)pPD}\]>]$/) {
  221. $stats->{allcaps}{$nick}++;
  222. push @{ $lines->{allcaplines}{$nick} }, $line;
  223. }
  224. }
  225. $stats->{foul}{$nick}++
  226. if ($saying =~ /$self->{cfg}->{foul}/io);
  227. # Who smiles the most?
  228. # A regex matching al lot of smilies
  229. $stats->{smiles}{$nick}++
  230. if ($saying =~ /[8;:=][ ^-o]?[)pPD}\]>]/);
  231. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/{]/ and
  232. $saying !~ /\w+:\/\//) {
  233. $stats->{frowns}{$nick}++;
  234. }
  235. # Find URLs
  236. if (my @urls = match_urls($saying)) {
  237. foreach my $url (@urls) {
  238. if(!url_is_ignored($url)) {
  239. $url =~ s/&/&amp;/g;
  240. $stats->{urlcounts}{$url}++;
  241. $stats->{urlnicks}{$url} = $nick;
  242. }
  243. }
  244. }
  245. _parse_words($stats, $saying, $nick, $self->{cfg}->{ignoreword});
  246. }
  247. }
  248. $lastnormal = $line;
  249. $repeated = 0;
  250. }
  251. # Match action lines.
  252. elsif ($hashref = $self->{parser}->actionline($line, $.)) {
  253. $stats->{totallines}++;
  254. my ($hour, $nick, $saying);
  255. $hour = $hashref->{hour};
  256. $nick = find_alias($hashref->{nick});
  257. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{show_mostnicks});
  258. $saying = $hashref->{saying};
  259. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  260. $state->{oldtime} = $hour;
  261. if (!is_ignored($nick)) {
  262. # Timestamp collecting
  263. $stats->{times}{$hour}++;
  264. $stats->{actions}{$nick}++;
  265. push @{ $lines->{actionlines}{$nick} }, $line;
  266. $stats->{lines}{$nick}++;
  267. $stats->{lastvisited}{$nick} = $stats->{days};
  268. $stats->{line_times}{$nick}[int($hour/6)]++;
  269. if ($saying =~ /^($self->{cfg}->{violent}) (\S+)/o) {
  270. my $victim = find_alias($2);
  271. if (!is_ignored($victim)) {
  272. $stats->{violence}{$nick}++;
  273. $stats->{attacked}{$victim}++;
  274. push @{ $lines->{violencelines}{$nick} }, $line;
  275. push @{ $lines->{attackedlines}{$victim} }, $line;
  276. }
  277. }
  278. my $len = length($saying);
  279. $stats->{lengths}{$nick} += $len;
  280. _parse_words($stats, $saying, $nick, $self->{cfg}->{ignoreword});
  281. }
  282. }
  283. # Match *** lines.
  284. elsif (($hashref = $self->{parser}->thirdline($line, $.)) and $hashref->{nick}) {
  285. $stats->{totallines}++;
  286. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  287. my ($newnick);
  288. $hour = $hashref->{hour};
  289. $min = $hashref->{min};
  290. $nick = find_alias($hashref->{nick});
  291. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{show_mostnicks});
  292. $kicker = find_alias($hashref->{kicker})
  293. if ($hashref->{kicker});
  294. $newtopic = $hashref->{newtopic};
  295. $newmode = $hashref->{newmode};
  296. $newjoin = $hashref->{newjoin};
  297. $newnick = $hashref->{newnick};
  298. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  299. $state->{oldtime} = $hour;
  300. if (!is_ignored($nick)) {
  301. # Timestamp collecting
  302. $stats->{times}{$hour}++;
  303. $stats->{lastvisited}{$nick} = $stats->{days};
  304. if (defined($kicker)) {
  305. if (!is_ignored($kicker)) {
  306. $stats->{kicked}{$kicker}++;
  307. $stats->{gotkicked}{$nick}++;
  308. push @{ $lines->{kicklines}{$nick} }, $line;
  309. }
  310. } elsif (defined($newtopic) && $newtopic ne '') {
  311. _topic_change($stats, $newtopic, $nick, $hour, $min);
  312. } elsif (defined($newmode)) {
  313. _modechanges($stats, $newmode, $nick);
  314. } elsif (defined($newjoin)) {
  315. $stats->{joins}{$nick}++;
  316. } elsif (defined($newnick) and ($self->{cfg}->{nicktracking} == 1)) {
  317. add_alias($nick, $newnick);
  318. checkname($nick, $newnick, $stats) if ($self->{cfg}->{show_mostnicks});
  319. }
  320. }
  321. }
  322. }
  323. close(LOGFILE);
  324. print "Finished analyzing log, $stats->{days} days total.\n"
  325. unless ($self->{cfg}->{silent});
  326. }
  327. sub _topic_change
  328. {
  329. my $stats = shift;
  330. my $newtopic = shift;
  331. my $nick = shift;
  332. my $hour = shift;
  333. my $min = shift;
  334. my $tcount = 0;
  335. if (defined $stats->{topics}) {
  336. $tcount = @{ $stats->{topics} };
  337. }
  338. $stats->{topics}[$tcount]{topic} = $newtopic;
  339. $stats->{topics}[$tcount]{nick} = $nick;
  340. $stats->{topics}[$tcount]{hour} = $hour;
  341. $stats->{topics}[$tcount]{min} = $min;
  342. }
  343. sub _modechanges
  344. {
  345. my $stats = shift;
  346. my $newmode = shift;
  347. my $nick = shift;
  348. my (@voice, @ops, $plus);
  349. foreach (split(//, $newmode)) {
  350. if ($_ eq "o") {
  351. $ops[$plus]++;
  352. } elsif ($_ eq "v") {
  353. $voice[$plus]++;
  354. } elsif ($_ eq "+") {
  355. $plus = 0;
  356. } elsif ($_ eq "-") {
  357. $plus = 1;
  358. }
  359. }
  360. $stats->{gaveops}{$nick} += $ops[0] if $ops[0];
  361. $stats->{tookops}{$nick} += $ops[1] if $ops[1];
  362. $stats->{gavevoice}{$nick} += $voice[0] if $voice[0];
  363. $stats->{tookvoice}{$nick} += $voice[1] if $voice[1];
  364. }
  365. sub _parse_words
  366. {
  367. my ($stats, $saying, $nick, $ignoreword) = @_;
  368. foreach my $word (split(/[\s,!?.:;)(\"]+/, $saying)) {
  369. $stats->{words}{$nick}++;
  370. # remove uninteresting words
  371. next if ($ignoreword->{$word});
  372. # ignore contractions
  373. next if ($word =~ m/'..?$/);
  374. # Also ignore stuff from URLs.
  375. next if ($word =~ m/https?|^\/\//);
  376. $stats->{wordcounts}{$word}++;
  377. $stats->{wordnicks}{$word} = $nick;
  378. }
  379. }
  380. sub _pick_random_lines
  381. {
  382. my ($stats, $lines) = @_;
  383. foreach my $key (keys %{ $lines }) {
  384. foreach my $nick (keys %{ $lines->{$key} }) {
  385. $stats->{$key}{$nick} =
  386. @{ $lines->{$key}{$nick} }[rand@{ $lines->{$key}{$nick} }];
  387. }
  388. }
  389. }
  390. sub _uniquify_nicks {
  391. my ($stats) = @_;
  392. foreach my $word (keys %{ $stats->{wordcounts} }) {
  393. my $realnick = find_alias($word);
  394. # The lc() is an attempt at being case insensitive.
  395. if (lc($realnick) ne lc($word)) {
  396. $stats->{wordcounts}{$realnick} += $stats->{wordcounts}{$word};
  397. $stats->{wordnicks}{$realnick} = $stats->{wordnicks}{$word};
  398. delete $stats->{wordcounts}{$word};
  399. delete $stats->{wordnicks}{$word};
  400. }
  401. }
  402. }
  403. sub _strip_mirccodes
  404. {
  405. my $line = shift;
  406. # boldcode = chr(2) = oct 001
  407. # colorcode = chr(3) = oct 003
  408. # plaincode = chr(15) = oct 017
  409. # reversecode = chr(22) = oct 026
  410. # underlinecode = chr(31) = oct 037
  411. # Strip mIRC color codes
  412. $line =~ s/\003\d{1,2},\d{1,2}//go;
  413. $line =~ s/\003\d{0,2}//go;
  414. # Strip mIRC bold, plain, reverse and underline codes
  415. $line =~ s/[\002\017\026\037]//go;
  416. return $line;
  417. }
  418. sub checkname {
  419. # This function tracks nickchanges and puts them all in a hash->array,
  420. # so we can show all nicks that I user had later (only works properly
  421. # when nicktracking is enabled)
  422. my ($nick, $newnick, $stats) = @_;
  423. foreach (@{ $stats->{nicks}{$newnick}}) {
  424. if (lc($_) eq lc($nick)) {
  425. return;
  426. }
  427. }
  428. push (@{ $stats->{nicks}{$newnick} }, $nick);
  429. }
  430. 1;
  431. __END__
  432. =head1 NAME
  433. Pisg::Parser::Logfile - class to parse a normal logfile
  434. =head1 DESCRIPTION
  435. C<Pisg::Parser::Logfile> parses a logfile using the configuration variables set in the 'cfg' option passed to the constructor.
  436. =head1 SYNOPSIS
  437. use Pisg::Parser::Logfile;
  438. $analyzer = new Pisg::Parser::Logfile(
  439. cfg => $cfg,
  440. );
  441. =head1 CONSTRUCTOR
  442. =over 4
  443. =item new ( [ OPTIONS ] )
  444. 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.
  445. Possible options are:
  446. B<cfg> - hashref containing configuration variables, created by the Pisg module.
  447. =back
  448. =head1 AUTHOR
  449. Morten Brix Pedersen <morten@wtf.dk>
  450. =head1 COPYRIGHT
  451. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  452. This program is free software; you can redistribute it and/or modify it
  453. under the terms of the GPL, license is included with the distribution of
  454. this file.
  455. =cut