Logfile.pm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. # the log cache
  7. use Data::Dumper;
  8. $Data::Dumper::Indent = 1;
  9. my $cache;
  10. # test for Text::Iconv
  11. my $have_iconv = 1;
  12. eval 'use Text::Iconv';
  13. $have_iconv = 0 if $@;
  14. sub new
  15. {
  16. my $type = shift;
  17. my $self = shift; # get cfg and users
  18. # Import common functions in Pisg::Common
  19. require Pisg::Common;
  20. Pisg::Common->import();
  21. bless($self, $type);
  22. # Pick our parser.
  23. $self->{parser} = $self->_choose_format($self->{cfg}->{format});
  24. if($self->{cfg}->{logcharsetfallback} and not $self->{cfg}->{logcharset}) {
  25. print "LogCharset undefined, assuming LogCharset = LogCharsetFallback\n"
  26. unless ($self->{cfg}->{silent});
  27. $self->{cfg}->{logcharset} = $self->{cfg}->{logcharsetfallback};
  28. }
  29. if($self->{cfg}->{logcharset}) {
  30. if($have_iconv) {
  31. # use converter if charsets differ or there is a fallback charset
  32. # (in the latter case the converter is also used to test if the
  33. # line is in the proper charset)
  34. if(($self->{cfg}->{logcharset} ne $self->{cfg}->{charset}) or $self->{cfg}->{logcharsetfallback}) {
  35. $self->{iconv} = Text::Iconv->new($self->{cfg}->{logcharset}, $self->{cfg}->{charset});
  36. }
  37. if($self->{cfg}->{logcharsetfallback}) {
  38. $self->{iconvfallback} = Text::Iconv->new($self->{cfg}->{logcharsetfallback}, $self->{cfg}->{charset});
  39. }
  40. } else {
  41. print "Text::Iconv is not installed, skipping charset conversion of logfiles\n"
  42. unless ($self->{cfg}->{silent});
  43. }
  44. }
  45. # precompile the regexps used (we can't use /o since the config might be different per channel)
  46. $self->{foulwords_regexp} = qr/($self->{cfg}->{foulwords})/i if $self->{cfg}->{foulwords};
  47. $self->{ignorewords_regexp} = qr/$self->{cfg}->{ignorewords}/i if $self->{cfg}->{ignorewords};
  48. $self->{violentwords_regexp} = qr/^($self->{cfg}->{violentwords}) (\S+)(.*)/i if $self->{cfg}->{violentwords};
  49. $self->{chartsregexp} = qr/^$self->{cfg}->{chartsregexp}/i if $self->{cfg}->{chartsregexp};
  50. return $self;
  51. }
  52. # The function to choose which module to use.
  53. sub _choose_format
  54. {
  55. my $self = shift;
  56. my $format = shift;
  57. $self->{parser} = undef;
  58. eval <<_END;
  59. use lib '$self->{cfg}->{modules_dir}';
  60. use Pisg::Parser::Format::$format;
  61. \$self->{parser} = new Pisg::Parser::Format::$format(
  62. cfg => \$self->{cfg},
  63. );
  64. _END
  65. if ($@) {
  66. print STDERR "Could not load parser for '$format': $@\n";
  67. return undef;
  68. }
  69. return $self->{parser};
  70. }
  71. sub analyze
  72. {
  73. my $self = shift;
  74. unless (defined $self->{parser}) {
  75. print STDERR "Skipping channel '$self->{cfg}->{channel}' due to lack of parser.\n";
  76. return undef
  77. }
  78. my $starttime = time();
  79. my @logfiles = @{$self->{cfg}->{logfile}};
  80. # expand wildcards
  81. @logfiles = map { if(/[\[*?]/) { glob; } else { $_; } } @logfiles;
  82. foreach my $logdir (@{$self->{cfg}->{logdir}}) {
  83. push @logfiles, $self->_parse_dir($logdir); # get all files in dir
  84. }
  85. my $count = @logfiles;
  86. my $shift = 0;
  87. if($self->{cfg}->{nfiles} > 0) { # chop list to maximal length
  88. $shift = @logfiles - $self->{cfg}->{nfiles};
  89. splice(@logfiles, 0, $shift) if $shift > 0;
  90. }
  91. unless ($self->{cfg}->{silent}) {
  92. my $msg = "";
  93. $msg = ", parsing the last $self->{cfg}->{nfiles}" if ($shift > 0);
  94. print "$count logfile(s) found$msg, using $self->{cfg}->{format} format...\n\n"
  95. }
  96. my (%stats, %lines);
  97. %stats = (
  98. oldtime => 24,
  99. days => 0,
  100. lastnick => '',
  101. monocount => 0,
  102. day_lines => [ undef ],
  103. day_times => [ undef ],
  104. parsedlines => 0,
  105. totallines => 0,
  106. );
  107. if ($self->{cfg}->{cachedir} and not -d $self->{cfg}->{cachedir}) {
  108. print STDERR "CacheDir \"$self->{cfg}->{cachedir}\" not found. Skipping caching.\n";
  109. delete $self->{cfg}->{cachedir};
  110. }
  111. foreach my $logfile (@logfiles) {
  112. # Run through the logfile
  113. print "Analyzing log $logfile... " unless ($self->{cfg}->{silent});
  114. my $s = {
  115. oldtime => 24,
  116. days => 0,
  117. firsttime => 0,
  118. lastnick => '',
  119. parsedlines => 0,
  120. totallines => 0,
  121. };
  122. my $l = {};
  123. unless ($self->{cfg}->{cachedir} and $self->_read_cache(\$s, \$l, $logfile)) {
  124. $self->_parse_file($s, $l, $logfile);
  125. if ($self->{cfg}->{cachedir}) {
  126. $self->_update_cache($s, $l, $logfile);
  127. }
  128. }
  129. $self->_merge_stats(\%stats, $s); # merge per-file stats into global stats
  130. $self->_merge_lines(\%lines, $l);
  131. print "$stats{days} days, $stats{parsedlines} lines total\n"
  132. unless ($self->{cfg}->{silent});
  133. }
  134. if ($self->{cfg}->{statsdump}) {
  135. open C, "> $self->{cfg}->{statsdump}" or die "$self->{cfg}->{statsdump}: $!";
  136. print C Data::Dumper->Dump([\%stats, \%lines], ["stats", "lines"]);
  137. close C;
  138. }
  139. $self->_pick_random_lines(\%stats, \%lines);
  140. _uniquify_nicks(\%stats);
  141. my ($sec,$min,$hour) = gmtime(time() - $starttime);
  142. my $processtime = sprintf('%02d hours, %02d minutes and %02d seconds', $hour, $min, $sec);
  143. $stats{processtime}{hours} = sprintf('%02d', $hour);
  144. $stats{processtime}{mins} = sprintf('%02d', $min);
  145. $stats{processtime}{secs} = sprintf('%02d', $sec);
  146. print "Channel analyzed successfully in $processtime on ",
  147. scalar localtime(time()), "\n\n"
  148. unless ($self->{cfg}->{silent});
  149. return \%stats;
  150. }
  151. sub _parse_dir
  152. {
  153. my $self = shift;
  154. my $logdir = shift;
  155. # Add trailing slash when it's not there..
  156. $logdir =~ s/([^\/])$/$1\//;
  157. unless ($self->{cfg}->{silent}) {
  158. print "Looking for logfiles in $logdir...\n\n"
  159. }
  160. my @filesarray;
  161. opendir(LOGDIR, $logdir) or
  162. die("Can't opendir ${logdir}: $!");
  163. unless(@filesarray = grep {
  164. /^[^\.]/ && /^$self->{cfg}->{logprefix}/ && -f "$logdir/$_"
  165. } readdir(LOGDIR)) {
  166. print ("No files in \"$logdir\" matched prefix \"$self->{cfg}->{logprefix}\"\n");
  167. return;
  168. }
  169. closedir(LOGDIR);
  170. if ($self->{cfg}->{logsuffix} ne '') {
  171. my @temparray;
  172. my %months = (
  173. 'jan' => '0',
  174. 'feb' => '1',
  175. 'mar' => '2',
  176. 'apr' => '3',
  177. 'may' => '4',
  178. 'jun' => '5',
  179. 'jul' => '6',
  180. 'aug' => '7',
  181. 'sep' => '8',
  182. 'oct' => '9',
  183. 'nov' => '10',
  184. 'dec' => '11',
  185. );
  186. my ($mreg, $dreg, $yreg) = split(/\|\|/, $self->{cfg}->{logsuffix});
  187. my (@month, @day, @year);
  188. for my $file (@filesarray) {
  189. LOOPSTART:
  190. if ($file =~ /$mreg/) {
  191. my $month = $1;
  192. $month = lc $month;
  193. $month = $months{$month}
  194. if (defined $months{$month});
  195. push @month, $month;
  196. } else {
  197. splice(@filesarray,$#month + 1, 1);
  198. if ($file = $filesarray[$#month + 1]) {
  199. goto LOOPSTART;
  200. } else {
  201. last;
  202. }
  203. }
  204. if ($file =~ /$dreg/) {
  205. push @day, $1;
  206. } else {
  207. splice(@filesarray,$#day + 1, 1);
  208. splice(@month,$#day + 1);
  209. if ($file = $filesarray[$#day + 1]) {
  210. goto LOOPSTART;
  211. } else {
  212. last;
  213. }
  214. }
  215. if ($file =~ /$yreg/) {
  216. push @year, $1;
  217. } else {
  218. splice(@filesarray,$#year + 1, 1);
  219. splice(@month,$#year + 1);
  220. splice(@day,$#year + 1);
  221. if ($file = $filesarray[$#year + 1]) {
  222. goto LOOPSTART;
  223. } else {
  224. last;
  225. }
  226. }
  227. }
  228. @filesarray = @filesarray[ sort {
  229. $year[$a] <=> $year[$b]
  230. ||
  231. $month[$a] <=> $month[$b]
  232. ||
  233. $day[$a] <=> $day[$b]
  234. } 0..$#filesarray ];
  235. } else {
  236. @filesarray = sort {lc($a) cmp lc($b)} @filesarray;
  237. }
  238. return map { "$logdir$_" } @filesarray;
  239. }
  240. # This parses the file...
  241. sub _parse_file
  242. {
  243. my $self = shift;
  244. my ($stats, $lines, $file) = @_;
  245. if ($file =~ /.bz2?$/ && -f $file) {
  246. open (LOGFILE, "bunzip2 -c $file |") or
  247. die("$0: Unable to open logfile($file): $!\n");
  248. } elsif ($file =~ /.gz$/ && -f $file) {
  249. open (LOGFILE, "gunzip -c $file |") or
  250. die("$0: Unable to open logfile($file): $!\n");
  251. } else {
  252. open (LOGFILE, $file) or
  253. die("$0: Unable to open logfile($file): $!\n");
  254. }
  255. while(my $line = <LOGFILE>) {
  256. $line = _strip_mirccodes($line);
  257. $line =~ s/\r+$//; # Strip DOS Formatting
  258. if($self->{iconv}) { # iconv is defined only if LogCharset is set
  259. my $line2 = $self->{iconv}->convert($line);
  260. if(not $line2 and $self->{iconvfallback}) {
  261. $line2 = $self->{iconvfallback}->convert($line);
  262. }
  263. if($line2) {
  264. $line = $line2;
  265. } else {
  266. print "Charset conversion failed for '$line'\n"
  267. unless ($self->{cfg}->{silent});
  268. }
  269. }
  270. my $hashref;
  271. # Match normal lines.
  272. if ($hashref = $self->{parser}->normalline($line, $.)) {
  273. my $repeated = 0;
  274. if (defined $hashref->{repeated}) {
  275. $repeated = $hashref->{repeated};
  276. }
  277. my ($hour, $nick, $saying, $i);
  278. for ($i = 0; $i <= $repeated; $i++) {
  279. if ($i > 0) {
  280. $hashref = $self->{parser}->normalline($stats->{lastnormal}, $.);
  281. #Increment number of lines for repeated lines
  282. }
  283. $hour = $self->_adjusttimeoffset($hashref->{hour});
  284. $nick = find_alias($hashref->{nick});
  285. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  286. $saying = $hashref->{saying};
  287. if ($hour < $stats->{oldtime}) {
  288. $stats->{firsttime} = $hour if $stats->{oldtime} == 24; # save stamp for merging
  289. $stats->{days}++;
  290. @{$stats->{day_times}[$stats->{days}]} = (0, 0, 0, 0);
  291. $stats->{day_lines}->[$stats->{days}] = 0;
  292. }
  293. $stats->{oldtime} = $hour;
  294. if (!is_ignored($nick)) {
  295. $stats->{parsedlines}++;
  296. # Timestamp collecting
  297. $stats->{times}{$hour}++;
  298. $stats->{day_times}[$stats->{days}][int($hour/6)]++;
  299. $stats->{day_lines}->[$stats->{days}]++;
  300. $stats->{lines}{$nick}++;
  301. $stats->{lastvisited}{$nick} = $stats->{days};
  302. $stats->{line_times}{$nick}[int($hour/6)]++;
  303. # Count up monologues
  304. if ($stats->{lastnick} eq $nick) {
  305. $stats->{monocount}++;
  306. if ($stats->{monocount} == 5) {
  307. $stats->{monologues}{$nick}++;
  308. }
  309. } else {
  310. $stats->{monocount} = 0;
  311. }
  312. $stats->{lastnick} = $nick;
  313. my $len = length($saying);
  314. if ($len > $self->{cfg}->{minquote} && $len < $self->{cfg}->{maxquote}) {
  315. push @{ $lines->{sayings}{$nick} }, $saying;
  316. } elsif (!$lines->{sayings}{$nick}) {
  317. # Just fill the users first saying in if he hasn't
  318. # said anything yet, to get rid of empty quotes.
  319. push @{ $lines->{sayings}{$nick} }, substr($saying, 0, $self->{cfg}->{maxquote});
  320. }
  321. $stats->{lengths}{$nick} += $len;
  322. $stats->{questions}{$nick}++
  323. if (index($saying, '?') > -1);
  324. $stats->{shouts}{$nick}++
  325. if (index($saying, '!') > -1);
  326. if ($saying !~ /[a-z]/o && $saying =~ /[A-Z]/o) {
  327. # Ignore single smileys on a line. eg. '<user> :P'
  328. if ($saying !~ /^[8;:=][ ^-o]?[)pPD\}\]>]$/o) {
  329. $stats->{allcaps}{$nick}++;
  330. push @{ $lines->{allcaplines}{$nick} }, $line;
  331. }
  332. }
  333. if ($self->{foulwords_regexp} and my @foul = $saying =~ /$self->{foulwords_regexp}/) {
  334. $stats->{foul}{$nick} += scalar @foul;
  335. push @{ $lines->{foullines}{$nick} }, $line;
  336. }
  337. # Who smiles the most?
  338. # A regex matching al lot of smilies
  339. $stats->{smiles}{$nick}++
  340. if ($saying =~ /[8;:=][ ^-o]?[)pPD\}\]>]/o);
  341. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/\{]/o and
  342. $saying !~ /\w+:\/\//o) {
  343. $stats->{frowns}{$nick}++;
  344. }
  345. if ($self->{cfg}->{showkarma}) {
  346. # require 2 chars (catches C++), nick must not end in [+=-]
  347. if ($saying =~ /^(\S*\w\S*\w\S*(?<![+=-]))(\+\+|==|--)$/o) {
  348. my $thing = lc $1;
  349. my $k = $2 eq "++" ? 1 : ($2 eq "==" ? 0 : -1);
  350. $stats->{karma}{$thing}{$nick} = $k
  351. if !is_ignored($thing) and $thing ne lc($nick);
  352. }
  353. }
  354. # Find URLs
  355. if (my @urls = match_urls($saying)) {
  356. foreach my $url (@urls) {
  357. if(!url_is_ignored($url)) {
  358. $stats->{urlcounts}{$url}++;
  359. $stats->{urlnicks}{$url} = $nick;
  360. }
  361. }
  362. }
  363. if ($saying =~ /$self->{chartsregexp}/i) {
  364. $self->_charts($1, $nick);
  365. }
  366. if (my $s = $self->{users}->{sex}{$nick}) {
  367. $stats->{sex_lines}{$s}++;
  368. $stats->{sex_line_times}{$s}[int($hour/6)]++;
  369. }
  370. _parse_words($stats, $saying, $nick, $self->{ignorewords_regexp}, $hour);
  371. } # ignored
  372. } # repeated
  373. $stats->{lastnormal} = $line;
  374. $repeated = 0;
  375. } # normal lines
  376. # Match action lines.
  377. elsif ($hashref = $self->{parser}->actionline($line, $.)) {
  378. $stats->{parsedlines}++;
  379. my ($hour, $nick, $saying);
  380. $hour = $self->_adjusttimeoffset($hashref->{hour});
  381. $nick = find_alias($hashref->{nick});
  382. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  383. $saying = $hashref->{saying};
  384. if ($hour < $stats->{oldtime}) {
  385. $stats->{firsttime} = $hour if $stats->{oldtime} == 24; # save stamp for merging
  386. $stats->{days}++;
  387. @{$stats->{day_times}[$stats->{days}]} = (0, 0, 0, 0);
  388. $stats->{day_lines}->[$stats->{days}] = 0;
  389. }
  390. $stats->{oldtime} = $hour;
  391. if (!is_ignored($nick)) {
  392. # Timestamp collecting
  393. $stats->{times}{$hour}++;
  394. $stats->{day_times}[$stats->{days}][int($hour/6)]++;
  395. $stats->{day_lines}->[$stats->{days}]++;
  396. $stats->{actions}{$nick}++;
  397. push @{ $lines->{actionlines}{$nick} }, $line;
  398. $stats->{lines}{$nick}++;
  399. $stats->{lastvisited}{$nick} = $stats->{days};
  400. $stats->{line_times}{$nick}[int($hour/6)]++;
  401. if ($self->{violentwords_regexp} and $saying =~ /$self->{violentwords_regexp}/) {
  402. my $victim;
  403. unless ($victim = is_nick($2)) {
  404. foreach my $trynick (split(/\s+/, $3)) {
  405. last if ($victim = is_nick($trynick));
  406. }
  407. unless ($victim) {
  408. $victim = $2;
  409. }
  410. }
  411. if (!is_ignored($victim)) {
  412. $stats->{violence}{$nick}++;
  413. $stats->{attacked}{$victim}++;
  414. push @{ $lines->{violencelines}{$nick} }, $line;
  415. push @{ $lines->{attackedlines}{$victim} }, $line;
  416. }
  417. }
  418. if ($saying =~ /$self->{chartsregexp}/i) {
  419. $self->_charts($1, $nick);
  420. }
  421. $stats->{lengths}{$nick} += length($saying);
  422. if (my $s = $self->{users}->{sex}{$nick}) {
  423. $stats->{sex_lines}{$s}++;
  424. $stats->{sex_line_times}{$s}[int($hour/6)]++;
  425. }
  426. _parse_words($stats, $saying, $nick, $self->{ignorewords_regexp}, $hour);
  427. } # ignored
  428. } # action lines
  429. # Match *** lines.
  430. elsif (($hashref = $self->{parser}->thirdline($line, $.)) and $hashref->{nick}) {
  431. $stats->{parsedlines}++;
  432. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  433. my ($newnick);
  434. $hour = $self->_adjusttimeoffset($hashref->{hour});
  435. $min = $hashref->{min};
  436. $nick = find_alias($hashref->{nick});
  437. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  438. $kicker = find_alias($hashref->{kicker})
  439. if ($hashref->{kicker});
  440. $newtopic = $hashref->{newtopic};
  441. $newmode = $hashref->{newmode};
  442. $newjoin = $hashref->{newjoin};
  443. $newnick = $hashref->{newnick};
  444. if ($hour < $stats->{oldtime}) {
  445. $stats->{firsttime} = $hour if $stats->{oldtime} == 24; # save stamp for merging
  446. $stats->{days}++;
  447. @{$stats->{day_times}[$stats->{days}]} = (0, 0, 0, 0);
  448. $stats->{day_lines}->[$stats->{days}] = 0;
  449. }
  450. $stats->{oldtime} = $hour;
  451. if (!is_ignored($nick)) {
  452. # Timestamp collecting
  453. $stats->{times}{$hour}++;
  454. $stats->{day_times}[$stats->{days}][int($hour/6)]++;
  455. $stats->{day_lines}->[$stats->{days}]++;
  456. $stats->{lastvisited}{$nick} = $stats->{days};
  457. if (defined($kicker)) {
  458. if (!is_ignored($kicker)) {
  459. $stats->{kicked}{$kicker}++;
  460. $stats->{gotkicked}{$nick}++;
  461. push @{ $lines->{kicklines}{$nick} }, $line;
  462. }
  463. } elsif (defined($newtopic) && $newtopic ne '') {
  464. push @{$stats->{topics}}, {
  465. topic => $newtopic,
  466. nick => $nick,
  467. hour => $hour,
  468. min => $min,
  469. days => $stats->{days},
  470. };
  471. } elsif (defined($newmode)) {
  472. _modechanges($stats, $newmode, $nick);
  473. } elsif (defined($newjoin)) {
  474. $stats->{joins}{$nick}++;
  475. } elsif (defined($newnick) and ($self->{cfg}->{nicktracking} == 1)) {
  476. # Resolve new nick to the correct alias (this will create a hard-alias if it is using a regex)
  477. $newnick = find_alias($newnick);
  478. add_alias($nick, $newnick);
  479. checkname($nick, $newnick, $stats) if ($self->{cfg}->{showmostnicks});
  480. }
  481. }
  482. } # *** lines
  483. unless ($stats->{parsedlines} % 10000) { # keep only recent quotes to save memory
  484. $self->_trim_lines($lines);
  485. }
  486. } # while(my $line = <LOGFILE>)
  487. $self->_trim_lines($lines);
  488. my $wordcount = sqrt(sqrt(keys %{$stats->{wordcounts}})); # remove less frequent words
  489. foreach my $word (keys %{$stats->{wordcounts}}) {
  490. if ($stats->{wordcounts}->{$word} < $wordcount) {
  491. delete $stats->{wordcounts}->{$word};
  492. delete $stats->{wordnicks}->{$word};
  493. delete $stats->{word_upcase}->{$word};
  494. }
  495. }
  496. $stats->{totallines} = $.;
  497. close(LOGFILE);
  498. }
  499. sub _modechanges
  500. {
  501. my $stats = shift;
  502. my $newmode = shift;
  503. my $nick = shift;
  504. my (@voice, @halfops, @ops, $plus);
  505. foreach (split(//, $newmode)) {
  506. if ($_ eq 'o') {
  507. $ops[$plus]++;
  508. } elsif ($_ eq 'h') {
  509. $halfops[$plus]++;
  510. } elsif ($_ eq 'v') {
  511. $voice[$plus]++;
  512. } elsif ($_ eq '+') {
  513. $plus = 0;
  514. } elsif ($_ eq '-') {
  515. $plus = 1;
  516. }
  517. }
  518. $stats->{gaveops}{$nick} += $ops[0] if $ops[0];
  519. $stats->{tookops}{$nick} += $ops[1] if $ops[1];
  520. $stats->{gavehalfops}{$nick} += $halfops[0] if $halfops[0];
  521. $stats->{tookhalfops}{$nick} += $halfops[1] if $halfops[1];
  522. $stats->{gavevoice}{$nick} += $voice[0] if $voice[0];
  523. $stats->{tookvoice}{$nick} += $voice[1] if $voice[1];
  524. }
  525. sub _parse_words
  526. {
  527. my ($stats, $saying, $nick, $ignorewords_regexp, $hour) = @_;
  528. # Cache time of day
  529. my $tod = int($hour/6);
  530. foreach my $word (split(/[\s,!?.:;)(\"]+/o, $saying)) {
  531. $stats->{words}{$nick}++;
  532. $stats->{word_times}{$nick}[$tod]++;
  533. # remove uninteresting words
  534. next if $ignorewords_regexp and $word =~ m/$ignorewords_regexp/i;
  535. # ignore contractions
  536. next if ($word =~ m/'.{1,2}$/o);
  537. # Also ignore stuff from URLs.
  538. next if ($word =~ m/^https?$|^\/\//o);
  539. my $lcword = lc $word;
  540. $stats->{wordcounts}{$lcword}++;
  541. $stats->{wordnicks}{$lcword} = $nick;
  542. $stats->{word_upcase}{$lcword} ||= $word; # remember first-seen case
  543. }
  544. }
  545. sub _charts
  546. {
  547. my ($stats, $Song, $nick) = @_;
  548. $Song =~ s/_/ /g;
  549. $Song =~ s/\d+ ?- ?//;
  550. $Song =~ s/\.mp3//g;
  551. $Song =~ s/ \.\.\.$//;
  552. $Song =~ s/ [^\w]+$//;
  553. my $song = lc $Song;
  554. $stats->{word_upcase}{$song} = $Song;
  555. $stats->{chartcounts}{$song}++;
  556. $stats->{chartnicks}{$song} = $nick;
  557. }
  558. sub _trim_lines
  559. {
  560. my ($self, $lines) = @_;
  561. foreach my $n (keys %{$lines->{sayings}}) {
  562. my $x = @{$lines->{sayings}->{$n}};
  563. splice(@{$lines->{sayings}->{$n}}, 0, ($x - 15)) if ($x > 30);
  564. }
  565. foreach my $n (keys %{$lines->{actionlines}}) {
  566. my $x = @{$lines->{actionlines}->{$n}};
  567. splice(@{$lines->{actionlines}->{$n}}, 0, ($x - 15)) if ($x > 30);
  568. }
  569. }
  570. sub _pick_random_lines
  571. {
  572. my ($self, $stats, $lines) = @_;
  573. foreach my $key (keys %{ $lines }) {
  574. foreach my $nick (keys %{ $lines->{$key} }) {
  575. $stats->{$key}{$nick} = $self->_random_line($lines, $key, $nick);
  576. }
  577. }
  578. }
  579. sub _random_line
  580. {
  581. my ($self, $lines, $key, $nick) = @_;
  582. my $count = 0;
  583. my ($random, $out, $out2) = ("", "", "");
  584. #warn "$nick did not say anything" unless @{ $lines->{$key}{$nick} };
  585. while (++$count < 20) {
  586. $random = ${ $lines->{$key}{$nick} }[rand @{ $lines->{$key}{$nick} }];
  587. if (length($random) < $self->{cfg}->{minquote} or length($random) > $self->{cfg}->{maxquote}) {
  588. $out2 = $random; # 2nd best choice
  589. next;
  590. }
  591. next if ($self->{cfg}->{noignoredquotes} and $self->{ignorewords_regexp} and
  592. $random =~ /$self->{ignorewords_regexp}/i);
  593. $out = $random;
  594. }
  595. return $out || $out2;
  596. }
  597. sub _uniquify_nicks {
  598. my ($stats) = @_;
  599. foreach my $word (keys %{ $stats->{wordcounts} }) {
  600. if (my $realnick = lc(is_nick($word))) {
  601. if ($realnick ne $word) { # word is always lc
  602. $stats->{wordcounts}{$realnick} += $stats->{wordcounts}{$word};
  603. $stats->{wordnicks}{$realnick} ||= $stats->{wordnicks}{$word};
  604. $stats->{word_upcase}{$realnick} ||= $stats->{word_upcase}{$word};
  605. delete $stats->{wordcounts}{$word};
  606. delete $stats->{wordnicks}{$word};
  607. delete $stats->{word_upcase}{$word};
  608. }
  609. }
  610. }
  611. }
  612. sub _strip_mirccodes
  613. {
  614. my $line = shift;
  615. # boldcode = chr(2) = oct 001
  616. # colorcode = chr(3) = oct 003
  617. # plaincode = chr(15) = oct 017
  618. # reversecode = chr(22) = oct 026
  619. # underlinecode = chr(31) = oct 037
  620. # Strip mIRC color codes
  621. $line =~ s/\003\d{1,2},\d{1,2}//go;
  622. $line =~ s/\003\d{0,2}//go;
  623. # Strip mIRC bold, plain, reverse and underline codes
  624. $line =~ s/[\002\017\026\037]//go;
  625. return $line;
  626. }
  627. sub checkname {
  628. # This function tracks nickchanges and puts them all in a hash->array,
  629. # so we can show all nicks that a user had later (only works properly
  630. # when nicktracking is enabled)
  631. my ($nick, $newnick, $stats) = @_;
  632. $stats->{nicks}{$newnick}{lc($nick)} = $nick;
  633. }
  634. sub _adjusttimeoffset
  635. {
  636. my ($self, $hour) = @_;
  637. if ($self->{cfg}{timeoffset} != 0) {
  638. # Adjust time
  639. $hour += $self->{cfg}{timeoffset};
  640. $hour = $hour % 24;
  641. }
  642. return sprintf('%02d', $hour);
  643. }
  644. sub _read_cache
  645. {
  646. my ($self, $statsref, $linesref, $logfile) = @_;
  647. my $mtime = (stat $logfile)[9];
  648. my $cachefile = $logfile;
  649. $cachefile =~ s/[^\w-]/_/g;
  650. $cachefile = "$self->{cfg}->{cachedir}/$cachefile";
  651. return undef unless -e $cachefile;
  652. open C, $cachefile or die "$cachefile: $!";
  653. local $/;
  654. my $str = <C>;
  655. close C;
  656. my ($stats, $lines);
  657. eval $str;
  658. return undef if $stats->{version} and $stats->{version} ne $self->{cfg}->{version};
  659. return undef unless $stats->{logfile} eq $logfile; # the name might be ambigous
  660. return undef if $stats->{logfile_mtime} != $mtime; # file has changed
  661. print "cached, " unless $self->{cfg}->{silent};
  662. $$statsref = $stats;
  663. $$linesref = $lines;
  664. return 1;
  665. }
  666. sub _update_cache
  667. {
  668. my ($self, $stats, $lines, $logfile) = @_;
  669. my $mtime = (stat $logfile)[9];
  670. my $cachefile = $logfile;
  671. $cachefile =~ s/[^\w-]/_/g;
  672. $cachefile = "$self->{cfg}->{cachedir}/$cachefile";
  673. #print "Writing cache $cachefile...";
  674. $stats->{logfile} = $logfile;
  675. $stats->{logfile_mtime} = $mtime;
  676. unless (open C, ">$cachefile") {
  677. die "$cachefile: $!";
  678. }
  679. $stats->{version} = $self->{cfg}->{version};
  680. print C Data::Dumper->Dump([$stats], ["stats"]);
  681. print C Data::Dumper->Dump([$lines], ["lines"]);
  682. close C;
  683. }
  684. sub _merge_stats
  685. {
  686. my ($self, $stats, $s) = @_;
  687. my $days_offset = $stats->{days};
  688. my $days_rollover = $stats->{oldtime} > $s->{firsttime};
  689. $stats->{days} += $s->{days} - 1 + $days_rollover;
  690. foreach my $key (keys %$s) {
  691. #print "$key -> $s->{$key}\n";
  692. if ($key =~ /^(logfile|firsttime|days|version)/) { # don't merge these
  693. next;
  694. } elsif ($key =~ /^(oldtime|lastnick|lastnormal|monocount)$/) { # {key} = int/str: copy
  695. $stats->{$key} = $s->{$key};
  696. } elsif ($key =~ /^(parsedlines|totallines)$/) { # {key} = int: add
  697. $stats->{$key} += $s->{$key};
  698. } elsif ($key =~ /^(wordnicks|word_upcase|urlnicks|chartnicks)$/) { # {key}->{} = str: copy
  699. foreach my $subkey (keys %{$s->{$key}}) {
  700. $stats->{$key}->{$subkey} = $s->{$key}->{$subkey};
  701. }
  702. } elsif ($key =~ /^(nicks|karma)$/) { # {key}->{}->{} = str: copy
  703. foreach my $subkey (keys %{$s->{$key}}) {
  704. foreach my $value (keys %{$s->{$key}->{$subkey}}) {
  705. $stats->{$key}->{$subkey}->{$value} = $s->{$key}->{$subkey}->{$value};
  706. }
  707. }
  708. } elsif ($key =~ /^(word|line|sex_line)_times$/) { # {key}->{}->[] = int: add
  709. foreach my $subkey (keys %{$s->{$key}}) {
  710. foreach my $pos (0 .. @{$s->{$key}->{$subkey}} - 1) {
  711. $stats->{$key}->{$subkey}->[$pos] += $s->{$key}->{$subkey}->[$pos]
  712. if $s->{$key}->{$subkey}->[$pos];
  713. }
  714. }
  715. } elsif ($key eq 'lastvisited') { # {key}->{} = int: copy
  716. foreach my $nick (keys %{$s->{lastvisited}}) {
  717. $stats->{lastvisited}->{$nick} =
  718. $days_offset + $s->{lastvisited}->{$nick} - 1 + $days_rollover;
  719. }
  720. } elsif ($s->{$key} =~ /^HASH/) { # {key}->{} = int: add
  721. foreach my $subkey (keys %{$s->{$key}}) {
  722. die "$key -> $subkey" unless $s->{$key}->{$subkey} =~ /^\d+/; # assert
  723. $stats->{$key}->{$subkey} += $s->{$key}->{$subkey};
  724. }
  725. } elsif ($key =~ /^topics$/) { # {key}->[] = topic hash: append
  726. push @{$stats->{$key}}, map {
  727. my %a = %$_; $a{days} += $days_offset - 1 + $days_rollover; \%a; # make new hash
  728. } @{$s->{$key}};
  729. } elsif ($key =~ /^day_lines$/) { # {key}->[] = int: append
  730. my @list = @{$s->{day_lines}};
  731. die if splice @list, 0, 1; # first element is always undef
  732. unless ($days_rollover) {
  733. $stats->{day_lines}->[$days_offset] += splice @list, 0, 1;
  734. }
  735. push @{$stats->{day_lines}}, @list;
  736. } elsif ($key =~ /^day_times$/) { # {key}->[]->[] = int: append outer list
  737. my @list = @{$s->{day_times}};
  738. die if splice @list, 0, 1;
  739. if (not $days_rollover) {
  740. my @first = @{splice @list, 0, 1};
  741. foreach my $pos (0 .. @first - 1) {
  742. $stats->{day_times}[$days_offset][$pos] += $first[$pos];
  743. }
  744. }
  745. push @{$stats->{day_times}}, map { my @a = @$_; \@a; } @list;
  746. } else {
  747. die "unknown key format $key -> $s->{$key}";
  748. }
  749. }
  750. }
  751. sub _merge_lines
  752. {
  753. my ($self, $lines, $l) = @_;
  754. foreach my $key (keys %$l) { # sayings, actionlines, etc.
  755. foreach my $subkey (keys %{$l->{$key}}) {
  756. push @{$lines->{$key}->{$subkey}}, @{$l->{$key}->{$subkey}};
  757. my $x = @{$lines->{$key}->{$subkey}};
  758. splice(@{$lines->{$key}->{$subkey}}, 0, ($x - 15)) if ($x > 30);
  759. }
  760. }
  761. }
  762. 1;
  763. __END__
  764. =head1 NAME
  765. Pisg::Parser::Logfile - class to parse a normal logfile
  766. =head1 DESCRIPTION
  767. C<Pisg::Parser::Logfile> parses a logfile using the configuration variables set in the 'cfg' option passed to the constructor.
  768. =head1 SYNOPSIS
  769. use Pisg::Parser::Logfile;
  770. $analyzer = new Pisg::Parser::Logfile(
  771. { cfg => $self->{cfg}, users => $self->{users} }
  772. );
  773. =head1 CONSTRUCTOR
  774. =over 4
  775. =item new ( [ OPTIONS ] )
  776. This is the constructor for a new Pisg::Parser::Logfile object.
  777. The first option must be a reference to a hash containing the cfg and users structures.
  778. =back
  779. =head1 AUTHOR
  780. Morten Brix Pedersen <morten@wtf.dk>
  781. =head1 COPYRIGHT
  782. Copyright (C) 2001-2005 Morten Brix Pedersen. All rights resereved.
  783. Copyright (C) 2003-2005 Christoph Berg <cb@df7cb.de>.
  784. This program is free software; you can redistribute it and/or modify it
  785. under the terms of the GPL, license is included with the distribution of
  786. this file.
  787. =cut