Logfile.pm 32 KB

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