Logfile.pm 31 KB

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