Logfile.pm 15 KB

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