Logfile.pm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. 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 (my @foul = $saying =~ /($self->{cfg}->{foulwords})/io) {
  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. # Find URLs
  328. if (my @urls = match_urls($saying)) {
  329. foreach my $url (@urls) {
  330. if(!url_is_ignored($url)) {
  331. $url =~ s/&/&amp;/g;
  332. $stats->{urlcounts}{$url}++;
  333. $stats->{urlnicks}{$url} = $nick;
  334. }
  335. }
  336. }
  337. _parse_words($stats, $saying, $nick, $self->{cfg}->{ignorewords}, $hour);
  338. }
  339. }
  340. $lastnormal = $line;
  341. $repeated = 0;
  342. }
  343. # Match action lines.
  344. elsif ($hashref = $self->{parser}->actionline($line, $.)) {
  345. $stats->{parsedlines}++;
  346. my ($hour, $nick, $saying);
  347. $hour = $self->_adjusttimeoffset($hashref->{hour});
  348. $nick = find_alias($hashref->{nick});
  349. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  350. $saying = $hashref->{saying};
  351. if ($hour < $state->{oldtime}) {
  352. $stats->{days}++;
  353. $stats->{day_times}{$stats->{days}}[0] = 0;
  354. $stats->{day_times}{$stats->{days}}[1] = 0;
  355. $stats->{day_times}{$stats->{days}}[2] = 0;
  356. $stats->{day_times}{$stats->{days}}[3] = 0;
  357. $stats->{day_lines}{$stats->{days}} = 0;
  358. }
  359. $state->{oldtime} = $hour;
  360. if (!is_ignored($nick)) {
  361. # Timestamp collecting
  362. $stats->{times}{$hour}++;
  363. $stats->{day_times}{$stats->{days}}[int($hour/6)]++;
  364. $stats->{day_lines}{$stats->{days}}++;
  365. $stats->{actions}{$nick}++;
  366. push @{ $lines->{actionlines}{$nick} }, $line;
  367. $stats->{lines}{$nick}++;
  368. $stats->{lastvisited}{$nick} = $stats->{days};
  369. $stats->{line_times}{$nick}[int($hour/6)]++;
  370. if ($saying =~ /^($self->{cfg}->{violentwords}) (\S+)(.*)/o) {
  371. my $victim;
  372. unless ($victim = is_nick($2)) {
  373. foreach my $trynick (split(/\s+/, $3)) {
  374. last if ($victim = is_nick($trynick));
  375. }
  376. unless ($victim) {
  377. $victim = $2;
  378. }
  379. }
  380. if (!is_ignored($victim)) {
  381. $stats->{violence}{$nick}++;
  382. $stats->{attacked}{$victim}++;
  383. push @{ $lines->{violencelines}{$nick} }, $line;
  384. push @{ $lines->{attackedlines}{$victim} }, $line;
  385. }
  386. }
  387. $stats->{lengths}{$nick} += length($saying);
  388. _parse_words($stats, $saying, $nick, $self->{cfg}->{ignorewords}, $hour);
  389. }
  390. }
  391. # Match *** lines.
  392. elsif (($hashref = $self->{parser}->thirdline($line, $.)) and $hashref->{nick}) {
  393. $stats->{parsedlines}++;
  394. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  395. my ($newnick);
  396. $hour = $self->_adjusttimeoffset($hashref->{hour});
  397. $min = $hashref->{min};
  398. $nick = find_alias($hashref->{nick});
  399. checkname($hashref->{nick}, $nick, $stats) if ($self->{cfg}->{showmostnicks});
  400. $kicker = find_alias($hashref->{kicker})
  401. if ($hashref->{kicker});
  402. $newtopic = $hashref->{newtopic};
  403. $newmode = $hashref->{newmode};
  404. $newjoin = $hashref->{newjoin};
  405. $newnick = $hashref->{newnick};
  406. if ($hour < $state->{oldtime}) {
  407. $stats->{days}++;
  408. $stats->{day_times}{$stats->{days}}[0]=0;
  409. $stats->{day_times}{$stats->{days}}[1]=0;
  410. $stats->{day_times}{$stats->{days}}[2]=0;
  411. $stats->{day_times}{$stats->{days}}[3]=0;
  412. $stats->{day_lines}{$stats->{days}}=0;
  413. }
  414. $state->{oldtime} = $hour;
  415. if (!is_ignored($nick)) {
  416. # Timestamp collecting
  417. $stats->{times}{$hour}++;
  418. $stats->{day_times}{$stats->{days}}[int($hour/6)]++;
  419. $stats->{day_lines}{$stats->{days}}++;
  420. $stats->{lastvisited}{$nick} = $stats->{days};
  421. if (defined($kicker)) {
  422. if (!is_ignored($kicker)) {
  423. $stats->{kicked}{$kicker}++;
  424. $stats->{gotkicked}{$nick}++;
  425. push @{ $lines->{kicklines}{$nick} }, $line;
  426. }
  427. } elsif (defined($newtopic) && $newtopic ne '') {
  428. _topic_change($stats, $newtopic, $nick, $hour, $min);
  429. } elsif (defined($newmode)) {
  430. _modechanges($stats, $newmode, $nick);
  431. } elsif (defined($newjoin)) {
  432. $stats->{joins}{$nick}++;
  433. } elsif (defined($newnick) and ($self->{cfg}->{nicktracking} == 1)) {
  434. # Resolve new nick to the correct alias (this will create a hard-alias if it is using a regex)
  435. $newnick = find_alias($newnick);
  436. add_alias($nick, $newnick);
  437. checkname($nick, $newnick, $stats) if ($self->{cfg}->{showmostnicks});
  438. }
  439. }
  440. }
  441. }
  442. $stats->{totallines} = $.;
  443. close(LOGFILE);
  444. print "Finished analyzing log, $stats->{days} days, $stats->{parsedlines} lines total\n"
  445. unless ($self->{cfg}->{silent});
  446. }
  447. sub _topic_change
  448. {
  449. my $stats = shift;
  450. my $newtopic = shift;
  451. my $nick = shift;
  452. my $hour = shift;
  453. my $min = shift;
  454. my $tcount = 0;
  455. if (defined $stats->{topics}) {
  456. $tcount = @{ $stats->{topics} };
  457. }
  458. $stats->{topics}[$tcount]{topic} = $newtopic;
  459. $stats->{topics}[$tcount]{nick} = $nick;
  460. $stats->{topics}[$tcount]{hour} = $hour;
  461. $stats->{topics}[$tcount]{min} = $min;
  462. }
  463. sub _modechanges
  464. {
  465. my $stats = shift;
  466. my $newmode = shift;
  467. my $nick = shift;
  468. my (@voice, @halfops, @ops, $plus);
  469. foreach (split(//, $newmode)) {
  470. if ($_ eq 'o') {
  471. $ops[$plus]++;
  472. } elsif ($_ eq 'h') {
  473. $halfops[$plus]++;
  474. } elsif ($_ eq 'v') {
  475. $voice[$plus]++;
  476. } elsif ($_ eq '+') {
  477. $plus = 0;
  478. } elsif ($_ eq '-') {
  479. $plus = 1;
  480. }
  481. }
  482. $stats->{gaveops}{$nick} += $ops[0] if $ops[0];
  483. $stats->{tookops}{$nick} += $ops[1] if $ops[1];
  484. $stats->{gavehalfops}{$nick} += $halfops[0] if $halfops[0];
  485. $stats->{tookhalfops}{$nick} += $halfops[1] if $halfops[1];
  486. $stats->{gavevoice}{$nick} += $voice[0] if $voice[0];
  487. $stats->{tookvoice}{$nick} += $voice[1] if $voice[1];
  488. }
  489. sub _parse_words
  490. {
  491. my ($stats, $saying, $nick, $ignorewords, $hour) = @_;
  492. # Cache time of day
  493. my $tod = int($hour/6);
  494. foreach my $word (split(/[\s,!?.:;)(\"]+/o, $saying)) {
  495. $stats->{words}{$nick}++;
  496. $stats->{word_times}{$nick}[$tod]++;
  497. # remove uninteresting words
  498. next if $ignorewords and $word =~ m/$ignorewords/i;
  499. # ignore contractions
  500. next if ($word =~ m/'.{1,2}$/o);
  501. # Also ignore stuff from URLs.
  502. next if ($word =~ m/^https?$|^\/\//o);
  503. # uniquify nicks
  504. if (my $realnick = is_nick($word)) {
  505. $stats->{wordcounts}{$realnick}++;
  506. $stats->{wordnicks}{$realnick} = $nick;
  507. } else {
  508. $stats->{wordcounts}{$word}++;
  509. $stats->{wordnicks}{$word} = $nick;
  510. }
  511. }
  512. }
  513. sub _pick_random_lines
  514. {
  515. my ($self, $stats, $lines) = @_;
  516. foreach my $key (keys %{ $lines }) {
  517. foreach my $nick (keys %{ $lines->{$key} }) {
  518. $stats->{$key}{$nick} = $self->_random_line($stats, $lines, $key, $nick, 0);
  519. }
  520. }
  521. }
  522. sub _random_line
  523. {
  524. my ($self, $stats, $lines, $key, $nick, $count) = @_;
  525. my $random = ${ $lines->{$key}{$nick} }[rand@{ $lines->{$key}{$nick} }];
  526. if ($self->{cfg}->{noignoredquotes} && $random =~ /$self->{cfg}->{ignorewords}/io) {
  527. return '' if ($count > 20);
  528. return $self->_random_line($stats, $lines, $key, $nick, ++$count);
  529. } else {
  530. return $random;
  531. }
  532. }
  533. sub _uniquify_nicks {
  534. my ($stats) = @_;
  535. foreach my $word (keys %{ $stats->{wordcounts} }) {
  536. if (my $realnick = is_nick($word)) {
  537. # The lc() is an attempt at being case insensitive.
  538. if (lc($realnick) ne lc($word)) {
  539. $stats->{wordcounts}{$realnick} += $stats->{wordcounts}{$word};
  540. delete $stats->{wordcounts}{$word};
  541. delete $stats->{wordnicks}{$word};
  542. }
  543. }
  544. }
  545. }
  546. sub _strip_mirccodes
  547. {
  548. my $line = shift;
  549. # boldcode = chr(2) = oct 001
  550. # colorcode = chr(3) = oct 003
  551. # plaincode = chr(15) = oct 017
  552. # reversecode = chr(22) = oct 026
  553. # underlinecode = chr(31) = oct 037
  554. # Strip mIRC color codes
  555. $line =~ s/\003\d{1,2},\d{1,2}//go;
  556. $line =~ s/\003\d{0,2}//go;
  557. # Strip mIRC bold, plain, reverse and underline codes
  558. $line =~ s/[\002\017\026\037]//go;
  559. return $line;
  560. }
  561. sub checkname {
  562. # This function tracks nickchanges and puts them all in a hash->array,
  563. # so we can show all nicks that I user had later (only works properly
  564. # when nicktracking is enabled)
  565. my ($nick, $newnick, $stats) = @_;
  566. $stats->{nicks}{$newnick}{lc($nick)} = $nick;
  567. }
  568. sub _adjusttimeoffset
  569. {
  570. my ($self, $hour) = @_;
  571. if ($self->{cfg}{timeoffset} != 0) {
  572. # Adjust time
  573. $hour += $self->{cfg}{timeoffset};
  574. $hour = $hour % 24;
  575. }
  576. return sprintf('%02d', $hour);
  577. }
  578. 1;
  579. __END__
  580. =head1 NAME
  581. Pisg::Parser::Logfile - class to parse a normal logfile
  582. =head1 DESCRIPTION
  583. C<Pisg::Parser::Logfile> parses a logfile using the configuration variables set in the 'cfg' option passed to the constructor.
  584. =head1 SYNOPSIS
  585. use Pisg::Parser::Logfile;
  586. $analyzer = new Pisg::Parser::Logfile(
  587. cfg => $cfg,
  588. );
  589. =head1 CONSTRUCTOR
  590. =over 4
  591. =item new ( [ OPTIONS ] )
  592. 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.
  593. Possible options are:
  594. B<cfg> - hashref containing configuration variables, created by the Pisg module.
  595. =back
  596. =head1 AUTHOR
  597. Morten Brix Pedersen <morten@wtf.dk>
  598. =head1 COPYRIGHT
  599. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  600. This program is free software; you can redistribute it and/or modify it
  601. under the terms of the GPL, license is included with the distribution of
  602. this file.
  603. =cut