4
0

Logfile.pm 24 KB

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