Logfile.pm 16 KB

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