Logfile.pm 33 KB

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