Logfile.pm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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<?|[;:][\)pPD\}\]\>]|\([;:]|\^[_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|[;:][\(\/]|[\)D]:|;_+;|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. next if defined $stats->{chartcounts}{$word};
  498. delete $stats->{wordcounts}->{$word};
  499. delete $stats->{wordnicks}->{$word};
  500. delete $stats->{word_upcase}->{$word};
  501. }
  502. }
  503. $stats->{totallines} = $.;
  504. close(LOGFILE);
  505. }
  506. sub _modechanges
  507. {
  508. my $stats = shift;
  509. my $newmode = shift;
  510. my $nick = shift;
  511. my (@voice, @halfops, @ops, $plus);
  512. foreach (split(//, $newmode)) {
  513. if ($_ eq 'o') {
  514. $ops[$plus]++;
  515. } elsif ($_ eq 'h') {
  516. $halfops[$plus]++;
  517. } elsif ($_ eq 'v') {
  518. $voice[$plus]++;
  519. } elsif ($_ eq '+') {
  520. $plus = 0;
  521. } elsif ($_ eq '-') {
  522. $plus = 1;
  523. }
  524. }
  525. $stats->{gaveops}{$nick} += $ops[0] if $ops[0];
  526. $stats->{tookops}{$nick} += $ops[1] if $ops[1];
  527. $stats->{gavehalfops}{$nick} += $halfops[0] if $halfops[0];
  528. $stats->{tookhalfops}{$nick} += $halfops[1] if $halfops[1];
  529. $stats->{gavevoice}{$nick} += $voice[0] if $voice[0];
  530. $stats->{tookvoice}{$nick} += $voice[1] if $voice[1];
  531. }
  532. sub _parse_words
  533. {
  534. my ($stats, $saying, $nick, $ignorewords_regexp, $hour) = @_;
  535. # Cache time of day
  536. my $tod = int($hour/6);
  537. foreach my $word (split(/[\s,!?.:;)(\"]+/o, $saying)) {
  538. # ignore if $word is empty
  539. next if $word eq "";
  540. $stats->{words}{$nick}++;
  541. $stats->{word_times}{$nick}[$tod]++;
  542. # remove uninteresting words
  543. next if $ignorewords_regexp and $word =~ m/$ignorewords_regexp/i;
  544. # ignore contractions
  545. next if ($word =~ m/'.{1,2}$/o);
  546. # Also ignore stuff from URLs.
  547. next if ($word =~ m/^https?$|^\/\//o);
  548. my $lcword = lc $word;
  549. $stats->{wordcounts}{$lcword}++;
  550. $stats->{wordnicks}{$lcword} = $nick;
  551. $stats->{word_upcase}{$lcword} ||= $word; # remember first-seen case
  552. }
  553. }
  554. sub _charts
  555. {
  556. my ($self, $stats, $Song, $nick) = @_;
  557. unless (defined $Song) {
  558. warn "Your ChartsRegexp is b0rked. Read the manual! This happened";
  559. return;
  560. }
  561. $Song =~ s/_/ /g;
  562. $Song =~ s/\d+ ?- ?//;
  563. $Song =~ s/\.(mp3|ogg|wma)//ig;
  564. $Song =~ s/\[[^\] ]*\]/ /g; # strip stuff in brackets [44kbps]
  565. $Song =~ s/^ *[^\w]* *| *[^\w]* *$//g;
  566. my $song = lc $Song;
  567. $stats->{word_upcase}{$song} = $Song;
  568. $stats->{chartcounts}{$song}++;
  569. $stats->{chartnicks}{$song} = $nick;
  570. }
  571. sub _trim_lines
  572. {
  573. my ($self, $lines) = @_;
  574. foreach my $n (keys %{$lines->{sayings}}) {
  575. my $x = @{$lines->{sayings}->{$n}};
  576. splice(@{$lines->{sayings}->{$n}}, 0, ($x - 15)) if ($x > 30);
  577. }
  578. foreach my $n (keys %{$lines->{actionlines}}) {
  579. my $x = @{$lines->{actionlines}->{$n}};
  580. splice(@{$lines->{actionlines}->{$n}}, 0, ($x - 15)) if ($x > 30);
  581. }
  582. }
  583. sub _pick_random_lines
  584. {
  585. my ($self, $stats, $lines) = @_;
  586. foreach my $key (keys %{ $lines }) {
  587. foreach my $nick (keys %{ $lines->{$key} }) {
  588. $stats->{$key}{$nick} = $self->_random_line($lines, $key, $nick);
  589. }
  590. }
  591. }
  592. sub _random_line
  593. {
  594. my ($self, $lines, $key, $nick) = @_;
  595. my $count = 0;
  596. my ($random, $out, $out2) = ("", "", "");
  597. #warn "$nick did not say anything" unless @{ $lines->{$key}{$nick} };
  598. while (++$count < 20) {
  599. $random = ${ $lines->{$key}{$nick} }[rand @{ $lines->{$key}{$nick} }];
  600. if (length($random) < $self->{cfg}->{minquote} or length($random) > $self->{cfg}->{maxquote}) {
  601. $out2 = $random; # 2nd best choice
  602. next;
  603. }
  604. next if ($self->{cfg}->{noignoredquotes} and $self->{ignorewords_regexp} and
  605. $random =~ /$self->{ignorewords_regexp}/i);
  606. $out = $random;
  607. }
  608. return $out || $out2;
  609. }
  610. sub _uniquify_nicks {
  611. my ($stats) = @_;
  612. foreach my $word (keys %{ $stats->{wordcounts} }) {
  613. if (my $realnick = lc(is_nick($word))) {
  614. if ($realnick ne $word) { # word is always lc
  615. $stats->{wordcounts}{$realnick} += $stats->{wordcounts}{$word};
  616. $stats->{wordnicks}{$realnick} ||= $stats->{wordnicks}{$word};
  617. $stats->{word_upcase}{$realnick} ||= $stats->{word_upcase}{$word};
  618. delete $stats->{wordcounts}{$word};
  619. delete $stats->{wordnicks}{$word};
  620. delete $stats->{word_upcase}{$word};
  621. }
  622. }
  623. }
  624. }
  625. sub _strip_mirccodes
  626. {
  627. my $line = shift;
  628. # boldcode = chr(2) = oct 001
  629. # colorcode = chr(3) = oct 003
  630. # plaincode = chr(15) = oct 017
  631. # reversecode = chr(22) = oct 026
  632. # underlinecode = chr(31) = oct 037
  633. # Strip mIRC color codes
  634. $line =~ s/\003\d{1,2},\d{1,2}//go;
  635. $line =~ s/\003\d{0,2}//go;
  636. # Strip mIRC bold, plain, reverse and underline codes
  637. $line =~ s/[\002\017\026\037]//go;
  638. return $line;
  639. }
  640. sub checkname {
  641. # This function tracks nickchanges and puts them all in a hash->array,
  642. # so we can show all nicks that a user had later (only works properly
  643. # when nicktracking is enabled)
  644. my ($nick, $newnick, $stats) = @_;
  645. $stats->{nicks}{$newnick}{lc($nick)} = $nick;
  646. }
  647. sub _adjusttimeoffset
  648. {
  649. my ($self, $hour) = @_;
  650. if ($self->{cfg}{timeoffset} != 0) {
  651. # Adjust time
  652. $hour += $self->{cfg}{timeoffset};
  653. $hour = $hour % 24;
  654. }
  655. return sprintf('%02d', $hour);
  656. }
  657. sub _read_cache
  658. {
  659. my ($self, $statsref, $linesref, $logfile) = @_;
  660. my $mtime = (stat $logfile)[9];
  661. my $cachefile = $logfile;
  662. $cachefile =~ s/[^\w-]/_/g;
  663. $cachefile = "$self->{cfg}->{cachedir}/$cachefile";
  664. return undef unless -e $cachefile;
  665. open C, $cachefile or die "$cachefile: $!";
  666. local $/;
  667. my $str = <C>;
  668. close C;
  669. my ($stats, $lines);
  670. eval $str;
  671. return undef if $stats->{version} and $stats->{version} ne $self->{cfg}->{version};
  672. return undef unless $stats->{logfile} eq $logfile; # the name might be ambigous
  673. return undef if $stats->{logfile_mtime} != $mtime; # file has changed
  674. print "cached, " unless $self->{cfg}->{silent};
  675. $$statsref = $stats;
  676. $$linesref = $lines;
  677. return 1;
  678. }
  679. sub _update_cache
  680. {
  681. my ($self, $stats, $lines, $logfile) = @_;
  682. my $mtime = (stat $logfile)[9];
  683. my $cachefile = $logfile;
  684. $cachefile =~ s/[^\w-]/_/g;
  685. $cachefile = "$self->{cfg}->{cachedir}/$cachefile";
  686. #print "Writing cache $cachefile...";
  687. $stats->{logfile} = $logfile;
  688. $stats->{logfile_mtime} = $mtime;
  689. unless (open C, ">$cachefile") {
  690. die "$cachefile: $!";
  691. }
  692. $stats->{version} = $self->{cfg}->{version};
  693. print C Data::Dumper->Dump([$stats], ["stats"]);
  694. print C Data::Dumper->Dump([$lines], ["lines"]);
  695. close C;
  696. }
  697. sub _merge_stats
  698. {
  699. my ($self, $stats, $s) = @_;
  700. my $days_offset = $stats->{days};
  701. my $days_rollover = $stats->{oldtime} > $s->{firsttime};
  702. $stats->{days} += $s->{days} - 1 + $days_rollover;
  703. foreach my $key (keys %$s) {
  704. #print "$key -> $s->{$key}\n";
  705. if ($key =~ /^(logfile|firsttime|days|version)/) { # don't merge these
  706. next;
  707. } elsif ($key =~ /^(oldtime|lastnick|lastnormal|monocount)$/) { # {key} = int/str: copy
  708. $stats->{$key} = $s->{$key};
  709. } elsif ($key =~ /^(parsedlines|totallines)$/) { # {key} = int: add
  710. $stats->{$key} += $s->{$key};
  711. } elsif ($key =~ /^(wordnicks|word_upcase|urlnicks|chartnicks|smileynicks)$/) { # {key}->{} = str: copy
  712. foreach my $subkey (keys %{$s->{$key}}) {
  713. $stats->{$key}->{$subkey} = $s->{$key}->{$subkey};
  714. }
  715. } elsif ($key =~ /^(nicks|karma)$/) { # {key}->{}->{} = str: copy
  716. foreach my $subkey (keys %{$s->{$key}}) {
  717. foreach my $value (keys %{$s->{$key}->{$subkey}}) {
  718. $stats->{$key}->{$subkey}->{$value} = $s->{$key}->{$subkey}->{$value};
  719. }
  720. }
  721. } elsif ($key =~ /^(word|line|sex_line)_times$/) { # {key}->{}->[] = int: add
  722. foreach my $subkey (keys %{$s->{$key}}) {
  723. foreach my $pos (0 .. @{$s->{$key}->{$subkey}} - 1) {
  724. $stats->{$key}->{$subkey}->[$pos] += $s->{$key}->{$subkey}->[$pos]
  725. if $s->{$key}->{$subkey}->[$pos];
  726. }
  727. }
  728. } elsif ($key eq 'lastvisited') { # {key}->{} = int: copy
  729. foreach my $nick (keys %{$s->{lastvisited}}) {
  730. $stats->{lastvisited}->{$nick} =
  731. $days_offset + $s->{lastvisited}->{$nick} - 1 + $days_rollover;
  732. }
  733. } elsif ($s->{$key} =~ /^HASH/) { # {key}->{} = int: add
  734. foreach my $subkey (keys %{$s->{$key}}) {
  735. die "$key -> $subkey" unless $s->{$key}->{$subkey} =~ /^\d+/; # assert
  736. $stats->{$key}->{$subkey} += $s->{$key}->{$subkey};
  737. }
  738. } elsif ($key =~ /^topics$/) { # {key}->[] = topic hash: append
  739. push @{$stats->{$key}}, map {
  740. my %a = %$_; $a{days} += $days_offset - 1 + $days_rollover; \%a; # make new hash
  741. } @{$s->{$key}};
  742. } elsif ($key =~ /^day_lines$/) { # {key}->[] = int: append
  743. my @list = @{$s->{day_lines}};
  744. die if splice @list, 0, 1; # first element is always undef
  745. unless ($days_rollover) {
  746. $stats->{day_lines}->[$days_offset] += splice @list, 0, 1;
  747. }
  748. push @{$stats->{day_lines}}, @list;
  749. } elsif ($key =~ /^day_times$/) { # {key}->[]->[] = int: append outer list
  750. my @list = @{$s->{day_times}};
  751. die if splice @list, 0, 1;
  752. if (not $days_rollover) {
  753. my @first = @{splice @list, 0, 1};
  754. foreach my $pos (0 .. @first - 1) {
  755. $stats->{day_times}[$days_offset][$pos] += $first[$pos];
  756. }
  757. }
  758. push @{$stats->{day_times}}, map { my @a = @$_; \@a; } @list;
  759. } else {
  760. die "unknown key format $key -> $s->{$key}";
  761. }
  762. }
  763. }
  764. sub _merge_lines
  765. {
  766. my ($self, $lines, $l) = @_;
  767. foreach my $key (keys %$l) { # sayings, actionlines, etc.
  768. foreach my $subkey (keys %{$l->{$key}}) {
  769. push @{$lines->{$key}->{$subkey}}, @{$l->{$key}->{$subkey}};
  770. my $x = @{$lines->{$key}->{$subkey}};
  771. splice(@{$lines->{$key}->{$subkey}}, 0, ($x - 15)) if ($x > 30);
  772. }
  773. }
  774. }
  775. 1;
  776. __END__
  777. =head1 NAME
  778. Pisg::Parser::Logfile - class to parse a normal logfile
  779. =head1 DESCRIPTION
  780. C<Pisg::Parser::Logfile> parses a logfile using the configuration variables set in the 'cfg' option passed to the constructor.
  781. =head1 SYNOPSIS
  782. use Pisg::Parser::Logfile;
  783. $analyzer = new Pisg::Parser::Logfile(
  784. { cfg => $self->{cfg}, users => $self->{users} }
  785. );
  786. =head1 CONSTRUCTOR
  787. =over 4
  788. =item new ( [ OPTIONS ] )
  789. This is the constructor for a new Pisg::Parser::Logfile object.
  790. The first option must be a reference to a hash containing the cfg and users structures.
  791. =back
  792. =head1 AUTHOR
  793. Morten Brix Pedersen <morten@wtf.dk>
  794. =head1 COPYRIGHT
  795. Copyright (C) 2001-2005 Morten Brix Pedersen. All rights resereved.
  796. Copyright (C) 2003-2005 Christoph Berg <cb@df7cb.de>.
  797. This program is free software; you can redistribute it and/or modify it
  798. under the terms of the GPL, license is included with the distribution of
  799. this file.
  800. =cut