Logfile.pm 17 KB

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