Logfile.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. # test for Text::Iconv
  7. my $have_iconv = 1;
  8. eval 'use Text::Iconv';
  9. $have_iconv = 0 if $@;
  10. sub new
  11. {
  12. my $type = shift;
  13. my $self = shift; # get cfg and users
  14. # Import common functions in Pisg::Common
  15. require Pisg::Common;
  16. Pisg::Common->import();
  17. bless($self, $type);
  18. # Pick our parser.
  19. $self->{parser} = $self->_choose_format($self->{cfg}->{format});
  20. if($self->{cfg}->{logcharsetfallback} and not $self->{cfg}->{logcharset}) {
  21. print "LogCharset undefined, assuming LogCharset = LogCharsetFallback\n"
  22. unless ($self->{cfg}->{silent});
  23. $self->{cfg}->{logcharset} = $self->{cfg}->{logcharsetfallback};
  24. }
  25. if($self->{cfg}->{logcharset}) {
  26. if($have_iconv) {
  27. # use converter if charsets differ or there is a fallback charset
  28. # (in the latter case the converter is also used to test if the
  29. # line is in the proper charset)
  30. if(($self->{cfg}->{logcharset} ne $self->{cfg}->{charset}) or $self->{cfg}->{logcharsetfallback}) {
  31. $self->{iconv} = Text::Iconv->new($self->{cfg}->{logcharset}, $self->{cfg}->{charset});
  32. }
  33. if($self->{cfg}->{logcharsetfallback}) {
  34. $self->{iconvfallback} = Text::Iconv->new($self->{cfg}->{logcharsetfallback}, $self->{cfg}->{charset});
  35. }
  36. } else {
  37. print "Text::Iconv is not installed, skipping charset conversion of logfiles\n"
  38. unless ($self->{cfg}->{silent});
  39. }
  40. }
  41. # precompile the regexps used (we can't use /o since the config might be different per channel)
  42. $self->{foulwords_regexp} = qr/($self->{cfg}->{foulwords})/i if $self->{cfg}->{foulwords};
  43. $self->{ignorewords_regexp} = qr/$self->{cfg}->{ignorewords}/i if $self->{cfg}->{ignorewords};
  44. $self->{violentwords_regexp} = qr/^($self->{cfg}->{violentwords}) (\S+)(.*)/i if $self->{cfg}->{violentwords};
  45. return $self;
  46. }
  47. # The function to choose which module to use.
  48. sub _choose_format
  49. {
  50. my $self = shift;
  51. my $format = shift;
  52. $self->{parser} = undef;
  53. eval <<_END;
  54. use lib '$self->{cfg}->{modules_dir}';
  55. use Pisg::Parser::Format::$format;
  56. \$self->{parser} = new Pisg::Parser::Format::$format(
  57. cfg => \$self->{cfg},
  58. );
  59. _END
  60. if ($@) {
  61. print STDERR "Could not load parser for '$format': $@\n";
  62. return undef;
  63. }
  64. return $self->{parser};
  65. }
  66. sub analyze
  67. {
  68. my $self = shift;
  69. my (%stats, %lines);
  70. if (defined $self->{parser}) {
  71. my $starttime = time();
  72. # Just initialize these to 0
  73. $stats{days} = 0;
  74. $stats{parsedlines} = 0;
  75. $stats{totallines} = 0;
  76. if (scalar(@{$self->{cfg}->{logdir}}) > 0) {
  77. # Run through all files in dir
  78. $self->_parse_dir(\%stats, \%lines);
  79. } else {
  80. my %state = (
  81. linecount => 0,
  82. lastnick => '',
  83. monocount => 0,
  84. lastnormal => '',
  85. oldtime => 24
  86. );
  87. foreach my $logfile (@{$self->{cfg}->{logfile}}) {
  88. # Run through the whole logfile
  89. $self->_parse_file(\%stats, \%lines, $logfile, \%state);
  90. }
  91. }
  92. $self->_pick_random_lines(\%stats, \%lines);
  93. _uniquify_nicks(\%stats);
  94. my ($sec,$min,$hour) = gmtime(time() - $starttime);
  95. my $processtime = sprintf('%02d hours, %02d minutes and %02d seconds', $hour, $min, $sec);
  96. $stats{processtime}{hours} = sprintf('%02d', $hour);
  97. $stats{processtime}{mins} = sprintf('%02d', $min);
  98. $stats{processtime}{secs} = sprintf('%02d', $sec);
  99. print "Channel analyzed successfully in $processtime on ",
  100. scalar localtime(time()), "\n"
  101. unless ($self->{cfg}->{silent});
  102. return \%stats;
  103. } else {
  104. print STDERR "Skipping channel '$self->{cfg}->{channel}' due to lack of parser.\n";
  105. return undef
  106. }
  107. # Shouldn't get here.
  108. return undef;
  109. }
  110. sub _parse_dir
  111. {
  112. my $self = shift;
  113. my ($stats, $lines) = @_;
  114. # Loop through each logdir we were given
  115. foreach my $logdir (@{$self->{cfg}->{logdir}}) {
  116. # Add trailing slash when it's not there..
  117. $logdir =~ s/([^\/])$/$1\//;
  118. unless ($self->{cfg}->{silent}) {
  119. unless ($self->{cfg}->{nfiles} > 0) {
  120. print "Going into $logdir and parsing all files there...\n\n"
  121. } else {
  122. print "Going into $logdir and parsing the last $self->{cfg}->{nfiles} file(s) there...\n\n"
  123. }
  124. }
  125. my @filesarray;
  126. opendir(LOGDIR, $logdir) or
  127. die("Can't opendir ${logdir}: $!");
  128. @filesarray = grep {
  129. /^[^\.]/ && /^$self->{cfg}->{logprefix}/ && -f "$logdir/$_"
  130. } readdir(LOGDIR) or
  131. die("No files in \"$logdir\" matched prefix \"$self->{cfg}->{logprefix}\"");
  132. closedir(LOGDIR);
  133. my %state = (
  134. lastnick => '',
  135. monocount => 0,
  136. oldtime => 24
  137. );
  138. if ($self->{cfg}->{logsuffix} ne '') {
  139. my @temparray;
  140. my %months = (
  141. 'jan' => '0',
  142. 'feb' => '1',
  143. 'mar' => '2',
  144. 'apr' => '3',
  145. 'may' => '4',
  146. 'jun' => '5',
  147. 'jul' => '6',
  148. 'aug' => '7',
  149. 'sep' => '8',
  150. 'oct' => '9',
  151. 'nov' => '10',
  152. 'dec' => '11',
  153. );
  154. my ($mreg, $dreg, $yreg) = split(/\|\|/, $self->{cfg}->{logsuffix});
  155. my (@month, @day, @year);
  156. for my $file (@filesarray) {
  157. LOOPSTART:
  158. if ($file =~ /$mreg/) {
  159. my $month = $1;
  160. $month = lc $month;
  161. $month = $months{$month}
  162. if (defined $months{$month});
  163. push @month, $month;
  164. } else {
  165. splice(@filesarray,$#month + 1, 1);
  166. if ($file = $filesarray[$#month + 1]) {
  167. goto LOOPSTART;
  168. } else {
  169. last;
  170. }
  171. }
  172. if ($file =~ /$dreg/) {
  173. push @day, $1;
  174. } else {
  175. splice(@filesarray,$#day + 1, 1);
  176. splice(@month,$#day + 1);
  177. if ($file = $filesarray[$#day + 1]) {
  178. goto LOOPSTART;
  179. } else {
  180. last;
  181. }
  182. }
  183. if ($file =~ /$yreg/) {
  184. push @year, $1;
  185. } else {
  186. splice(@filesarray,$#year + 1, 1);
  187. splice(@month,$#year + 1);
  188. splice(@day,$#year + 1);
  189. if ($file = $filesarray[$#year + 1]) {
  190. goto LOOPSTART;
  191. } else {
  192. last;
  193. }
  194. }
  195. }
  196. my @newarray = @filesarray[ sort {
  197. $year[$a] <=> $year[$b]
  198. ||
  199. $month[$a] <=> $month[$b]
  200. ||
  201. $day[$a] <=> $day[$b]
  202. } 0..$#filesarray ];
  203. @filesarray = @newarray;
  204. } else {
  205. @filesarray = sort {lc($a) cmp lc($b)} @filesarray;
  206. }
  207. if($self->{cfg}->{nfiles} > 0) {
  208. my $shift = @filesarray - $self->{cfg}->{nfiles};
  209. splice(@filesarray, 0, $shift) if $shift > 0;
  210. }
  211. foreach my $file (@filesarray) {
  212. $file = $logdir . $file;
  213. $self->_parse_file($stats, $lines, $file, \%state);
  214. }
  215. }
  216. }
  217. # This parses the file...
  218. sub _parse_file
  219. {
  220. my $self = shift;
  221. my ($stats, $lines, $file, $state) = @_;
  222. print "Analyzing log $file in '$self->{cfg}->{format}' format...\n"
  223. unless ($self->{cfg}->{silent});
  224. if ($file =~ /.bz2?$/ && -f $file) {
  225. open (LOGFILE, "bunzip2 -c $file |") or
  226. die("$0: Unable to open logfile($file): $!\n");
  227. } elsif ($file =~ /.gz$/ && -f $file) {
  228. open (LOGFILE, "gunzip -c $file |") or
  229. die("$0: Unable to open logfile($file): $!\n");
  230. } else {
  231. open (LOGFILE, $file) or
  232. die("$0: Unable to open logfile($file): $!\n");
  233. }
  234. my $lastnormal = '';
  235. while(my $line = <LOGFILE>) {
  236. $line = _strip_mirccodes($line);
  237. $line =~ s/\r+$//; # Strip DOS Formatting
  238. if($self->{iconv}) { # iconv is defined only if LogCharset is set
  239. my $line2 = $self->{iconv}->convert($line);
  240. if(not $line2 and $self->{iconvfallback}) {
  241. $line2 = $self->{iconvfallback}->convert($line);
  242. }
  243. if($line2) {
  244. $line = $line2;
  245. } else {
  246. print "Charset conversion failed for '$line'\n"
  247. unless ($self->{cfg}->{silent});
  248. }
  249. }
  250. my $hashref;
  251. # Match normal lines.
  252. if ($hashref = $self->{parser}->normalline($line, $.)) {
  253. my $repeated = 0;
  254. if (defined $hashref->{repeated}) {
  255. $repeated = $hashref->{repeated};
  256. }
  257. my ($hour, $nick, $saying, $i);
  258. for ($i = 0; $i <= $repeated; $i++) {
  259. if ($i > 0) {
  260. $hashref = $self->{parser}->normalline($lastnormal, $.);
  261. #Increment number of lines for repeated lines
  262. }
  263. $hour = $self->_adjusttimeoffset($hashref->{hour});
  264. $nick = find_alias($hashref->{nick});
  265. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  266. $saying = $hashref->{saying};
  267. if ($hour < $state->{oldtime}) {
  268. $stats->{days}++;
  269. $stats->{day_times}{$stats->{days}}[0] = 0;
  270. $stats->{day_times}{$stats->{days}}[1] = 0;
  271. $stats->{day_times}{$stats->{days}}[2] = 0;
  272. $stats->{day_times}{$stats->{days}}[3] = 0;
  273. $stats->{day_lines}{$stats->{days}} = 0;
  274. }
  275. $state->{oldtime} = $hour;
  276. if (!is_ignored($nick)) {
  277. $stats->{parsedlines}++;
  278. # Timestamp collecting
  279. $stats->{times}{$hour}++;
  280. $stats->{day_times}{$stats->{days}}[int($hour/6)]++;
  281. $stats->{day_lines}{$stats->{days}}++;
  282. $stats->{lines}{$nick}++;
  283. $stats->{lastvisited}{$nick} = $stats->{days};
  284. $stats->{line_times}{$nick}[int($hour/6)]++;
  285. # Count up monologues
  286. if ($state->{lastnick} eq $nick) {
  287. $state->{monocount}++;
  288. if ($state->{monocount} == 5) {
  289. $stats->{monologues}{$nick}++;
  290. }
  291. } else {
  292. $state->{monocount} = 0;
  293. }
  294. $state->{lastnick} = $nick;
  295. my $len = length($saying);
  296. if ($len > $self->{cfg}->{minquote} && $len < $self->{cfg}->{maxquote}) {
  297. push @{ $lines->{sayings}{$nick} }, $saying;
  298. } elsif (!$lines->{sayings}{$nick}) {
  299. # Just fill the users first saying in if he hasn't
  300. # said anything yet, to get rid of empty quotes.
  301. push @{ $lines->{sayings}{$nick} }, substr($saying, 0, $self->{cfg}->{maxquote});
  302. }
  303. $stats->{lengths}{$nick} += $len;
  304. $stats->{questions}{$nick}++
  305. if (index($saying, '?') > -1);
  306. $stats->{shouts}{$nick}++
  307. if (index($saying, '!') > -1);
  308. if ($saying !~ /[a-z]/o && $saying =~ /[A-Z]/o) {
  309. # Ignore single smileys on a line. eg. '<user> :P'
  310. if ($saying !~ /^[8;:=][ ^-o]?[)pPD}\]>]$/o) {
  311. $stats->{allcaps}{$nick}++;
  312. push @{ $lines->{allcaplines}{$nick} }, $line;
  313. }
  314. }
  315. if ($self->{foulwords_regexp} and my @foul = $saying =~ /$self->{foulwords_regexp}/) {
  316. $stats->{foul}{$nick} += scalar @foul;
  317. push @{ $lines->{foullines}{$nick} }, $line;
  318. }
  319. # Who smiles the most?
  320. # A regex matching al lot of smilies
  321. $stats->{smiles}{$nick}++
  322. if ($saying =~ /[8;:=][ ^-o]?[)pPD}\]>]/o);
  323. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/{]/o and
  324. $saying !~ /\w+:\/\//o) {
  325. $stats->{frowns}{$nick}++;
  326. }
  327. if ($self->{cfg}->{showkarma}) {
  328. # require 2 chars (catches C++), nick must not end in [+=-]
  329. if ($saying =~ /^(\S*\w\S*\w\S*(?<![+=-]))(\+\+|==|--)$/o) {
  330. my $thing = lc $1;
  331. my $k = $2 eq "++" ? 1 : ($2 eq "==" ? 0 : -1);
  332. $stats->{karma}{$thing}{$nick} = $k
  333. if !is_ignored($thing) and $thing ne lc($nick);
  334. }
  335. }
  336. # Find URLs
  337. if (my @urls = match_urls($saying)) {
  338. foreach my $url (@urls) {
  339. if(!url_is_ignored($url)) {
  340. $stats->{urlcounts}{$url}++;
  341. $stats->{urlnicks}{$url} = $nick;
  342. }
  343. }
  344. }
  345. if (my $s = $self->{users}->{sex}{$nick}) {
  346. $stats->{sex_lines}{$s}++;
  347. $stats->{sex_line_times}{$s}[int($hour/6)]++;
  348. }
  349. _parse_words($stats, $saying, $nick, $self->{ignorewords_regexp}, $hour);
  350. }
  351. }
  352. $lastnormal = $line;
  353. $repeated = 0;
  354. }
  355. # Match action lines.
  356. elsif ($hashref = $self->{parser}->actionline($line, $.)) {
  357. $stats->{parsedlines}++;
  358. my ($hour, $nick, $saying);
  359. $hour = $self->_adjusttimeoffset($hashref->{hour});
  360. $nick = find_alias($hashref->{nick});
  361. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  362. $saying = $hashref->{saying};
  363. if ($hour < $state->{oldtime}) {
  364. $stats->{days}++;
  365. $stats->{day_times}{$stats->{days}}[0] = 0;
  366. $stats->{day_times}{$stats->{days}}[1] = 0;
  367. $stats->{day_times}{$stats->{days}}[2] = 0;
  368. $stats->{day_times}{$stats->{days}}[3] = 0;
  369. $stats->{day_lines}{$stats->{days}} = 0;
  370. }
  371. $state->{oldtime} = $hour;
  372. if (!is_ignored($nick)) {
  373. # Timestamp collecting
  374. $stats->{times}{$hour}++;
  375. $stats->{day_times}{$stats->{days}}[int($hour/6)]++;
  376. $stats->{day_lines}{$stats->{days}}++;
  377. $stats->{actions}{$nick}++;
  378. push @{ $lines->{actionlines}{$nick} }, $line;
  379. $stats->{lines}{$nick}++;
  380. $stats->{lastvisited}{$nick} = $stats->{days};
  381. $stats->{line_times}{$nick}[int($hour/6)]++;
  382. if ($self->{violentwords_regexp} and $saying =~ /$self->{violentwords_regexp}/) {
  383. my $victim;
  384. unless ($victim = is_nick($2)) {
  385. foreach my $trynick (split(/\s+/, $3)) {
  386. last if ($victim = is_nick($trynick));
  387. }
  388. unless ($victim) {
  389. $victim = $2;
  390. }
  391. }
  392. if (!is_ignored($victim)) {
  393. $stats->{violence}{$nick}++;
  394. $stats->{attacked}{$victim}++;
  395. push @{ $lines->{violencelines}{$nick} }, $line;
  396. push @{ $lines->{attackedlines}{$victim} }, $line;
  397. }
  398. }
  399. $stats->{lengths}{$nick} += length($saying);
  400. if (my $s = $self->{users}->{sex}{$nick}) {
  401. $stats->{sex_lines}{$s}++;
  402. $stats->{sex_line_times}{$s}[int($hour/6)]++;
  403. }
  404. _parse_words($stats, $saying, $nick, $self->{ignorewords_regexp}, $hour);
  405. }
  406. }
  407. # Match *** lines.
  408. elsif (($hashref = $self->{parser}->thirdline($line, $.)) and $hashref->{nick}) {
  409. $stats->{parsedlines}++;
  410. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  411. my ($newnick);
  412. $hour = $self->_adjusttimeoffset($hashref->{hour});
  413. $min = $hashref->{min};
  414. $nick = find_alias($hashref->{nick});
  415. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  416. $kicker = find_alias($hashref->{kicker})
  417. if ($hashref->{kicker});
  418. $newtopic = $hashref->{newtopic};
  419. $newmode = $hashref->{newmode};
  420. $newjoin = $hashref->{newjoin};
  421. $newnick = $hashref->{newnick};
  422. if ($hour < $state->{oldtime}) {
  423. $stats->{days}++;
  424. $stats->{day_times}{$stats->{days}}[0]=0;
  425. $stats->{day_times}{$stats->{days}}[1]=0;
  426. $stats->{day_times}{$stats->{days}}[2]=0;
  427. $stats->{day_times}{$stats->{days}}[3]=0;
  428. $stats->{day_lines}{$stats->{days}}=0;
  429. }
  430. $state->{oldtime} = $hour;
  431. if (!is_ignored($nick)) {
  432. # Timestamp collecting
  433. $stats->{times}{$hour}++;
  434. $stats->{day_times}{$stats->{days}}[int($hour/6)]++;
  435. $stats->{day_lines}{$stats->{days}}++;
  436. $stats->{lastvisited}{$nick} = $stats->{days};
  437. if (defined($kicker)) {
  438. if (!is_ignored($kicker)) {
  439. $stats->{kicked}{$kicker}++;
  440. $stats->{gotkicked}{$nick}++;
  441. push @{ $lines->{kicklines}{$nick} }, $line;
  442. }
  443. } elsif (defined($newtopic) && $newtopic ne '') {
  444. _topic_change($stats, $newtopic, $nick, $hour, $min);
  445. } elsif (defined($newmode)) {
  446. _modechanges($stats, $newmode, $nick);
  447. } elsif (defined($newjoin)) {
  448. $stats->{joins}{$nick}++;
  449. } elsif (defined($newnick) and ($self->{cfg}->{nicktracking} == 1)) {
  450. # Resolve new nick to the correct alias (this will create a hard-alias if it is using a regex)
  451. $newnick = find_alias($newnick);
  452. add_alias($nick, $newnick);
  453. checkname($nick, $newnick, $stats) if ($self->{cfg}->{showmostnicks});
  454. }
  455. }
  456. }
  457. }
  458. $stats->{totallines} = $.;
  459. close(LOGFILE);
  460. print "Finished analyzing log, $stats->{days} days, $stats->{parsedlines} lines total\n"
  461. unless ($self->{cfg}->{silent});
  462. }
  463. sub _topic_change
  464. {
  465. my $stats = shift;
  466. my $newtopic = shift;
  467. my $nick = shift;
  468. my $hour = shift;
  469. my $min = shift;
  470. my $tcount = 0;
  471. if (defined $stats->{topics}) {
  472. $tcount = @{ $stats->{topics} };
  473. }
  474. $stats->{topics}[$tcount]{topic} = $newtopic;
  475. $stats->{topics}[$tcount]{nick} = $nick;
  476. $stats->{topics}[$tcount]{hour} = $hour;
  477. $stats->{topics}[$tcount]{min} = $min;
  478. }
  479. sub _modechanges
  480. {
  481. my $stats = shift;
  482. my $newmode = shift;
  483. my $nick = shift;
  484. my (@voice, @halfops, @ops, $plus);
  485. foreach (split(//, $newmode)) {
  486. if ($_ eq 'o') {
  487. $ops[$plus]++;
  488. } elsif ($_ eq 'h') {
  489. $halfops[$plus]++;
  490. } elsif ($_ eq 'v') {
  491. $voice[$plus]++;
  492. } elsif ($_ eq '+') {
  493. $plus = 0;
  494. } elsif ($_ eq '-') {
  495. $plus = 1;
  496. }
  497. }
  498. $stats->{gaveops}{$nick} += $ops[0] if $ops[0];
  499. $stats->{tookops}{$nick} += $ops[1] if $ops[1];
  500. $stats->{gavehalfops}{$nick} += $halfops[0] if $halfops[0];
  501. $stats->{tookhalfops}{$nick} += $halfops[1] if $halfops[1];
  502. $stats->{gavevoice}{$nick} += $voice[0] if $voice[0];
  503. $stats->{tookvoice}{$nick} += $voice[1] if $voice[1];
  504. }
  505. sub _parse_words
  506. {
  507. my ($stats, $saying, $nick, $ignorewords_regexp, $hour) = @_;
  508. # Cache time of day
  509. my $tod = int($hour/6);
  510. foreach my $word (split(/[\s,!?.:;)(\"]+/o, $saying)) {
  511. $stats->{words}{$nick}++;
  512. $stats->{word_times}{$nick}[$tod]++;
  513. # remove uninteresting words
  514. next if $ignorewords_regexp and $word =~ m/$ignorewords_regexp/i;
  515. # ignore contractions
  516. next if ($word =~ m/'.{1,2}$/o);
  517. # Also ignore stuff from URLs.
  518. next if ($word =~ m/^https?$|^\/\//o);
  519. # uniquify nicks
  520. if (my $realnick = is_nick($word)) {
  521. $stats->{wordcounts}{$realnick}++;
  522. $stats->{wordnicks}{$realnick} = $nick;
  523. } else {
  524. $stats->{wordcounts}{$word}++;
  525. $stats->{wordnicks}{$word} = $nick;
  526. }
  527. }
  528. }
  529. sub _pick_random_lines
  530. {
  531. my ($self, $stats, $lines) = @_;
  532. foreach my $key (keys %{ $lines }) {
  533. foreach my $nick (keys %{ $lines->{$key} }) {
  534. $stats->{$key}{$nick} = $self->_random_line($stats, $lines, $key, $nick, 0);
  535. }
  536. }
  537. }
  538. sub _random_line
  539. {
  540. my ($self, $stats, $lines, $key, $nick, $count) = @_;
  541. my $random = ${ $lines->{$key}{$nick} }[rand@{ $lines->{$key}{$nick} }];
  542. if ($self->{cfg}->{noignoredquotes} and $self->{ignorewords_regexp} and $random =~ /$self->{ignorewords_regexp}/i) {
  543. return '' if ($count > 20);
  544. return $self->_random_line($stats, $lines, $key, $nick, ++$count);
  545. } else {
  546. return $random;
  547. }
  548. }
  549. sub _uniquify_nicks {
  550. my ($stats) = @_;
  551. foreach my $word (keys %{ $stats->{wordcounts} }) {
  552. if (my $realnick = is_nick($word)) {
  553. # The lc() is an attempt at being case insensitive.
  554. if (lc($realnick) ne lc($word)) {
  555. $stats->{wordcounts}{$realnick} += $stats->{wordcounts}{$word};
  556. delete $stats->{wordcounts}{$word};
  557. delete $stats->{wordnicks}{$word};
  558. }
  559. }
  560. }
  561. }
  562. sub _strip_mirccodes
  563. {
  564. my $line = shift;
  565. # boldcode = chr(2) = oct 001
  566. # colorcode = chr(3) = oct 003
  567. # plaincode = chr(15) = oct 017
  568. # reversecode = chr(22) = oct 026
  569. # underlinecode = chr(31) = oct 037
  570. # Strip mIRC color codes
  571. $line =~ s/\003\d{1,2},\d{1,2}//go;
  572. $line =~ s/\003\d{0,2}//go;
  573. # Strip mIRC bold, plain, reverse and underline codes
  574. $line =~ s/[\002\017\026\037]//go;
  575. return $line;
  576. }
  577. sub checkname {
  578. # This function tracks nickchanges and puts them all in a hash->array,
  579. # so we can show all nicks that I user had later (only works properly
  580. # when nicktracking is enabled)
  581. my ($nick, $newnick, $stats) = @_;
  582. $stats->{nicks}{$newnick}{lc($nick)} = $nick;
  583. }
  584. sub _adjusttimeoffset
  585. {
  586. my ($self, $hour) = @_;
  587. if ($self->{cfg}{timeoffset} != 0) {
  588. # Adjust time
  589. $hour += $self->{cfg}{timeoffset};
  590. $hour = $hour % 24;
  591. }
  592. return sprintf('%02d', $hour);
  593. }
  594. 1;
  595. __END__
  596. =head1 NAME
  597. Pisg::Parser::Logfile - class to parse a normal logfile
  598. =head1 DESCRIPTION
  599. C<Pisg::Parser::Logfile> parses a logfile using the configuration variables set in the 'cfg' option passed to the constructor.
  600. =head1 SYNOPSIS
  601. use Pisg::Parser::Logfile;
  602. $analyzer = new Pisg::Parser::Logfile(
  603. { cfg => $self->{cfg}, users => $self->{users} }
  604. );
  605. =head1 CONSTRUCTOR
  606. =over 4
  607. =item new ( [ OPTIONS ] )
  608. This is the constructor for a new Pisg::Parser::Logfile object.
  609. The first option must be a reference to a hash containing the cfg and users structures.
  610. =back
  611. =head1 AUTHOR
  612. Morten Brix Pedersen <morten@wtf.dk>
  613. =head1 COPYRIGHT
  614. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  615. This program is free software; you can redistribute it and/or modify it
  616. under the terms of the GPL, license is included with the distribution of
  617. this file.
  618. =cut