Logfile.pm 32 KB

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