Logfile.pm 31 KB

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