Logfile.pm 33 KB

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