Logfile.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package Pisg::Logfile;
  2. use strict;
  3. $^W = 1;
  4. my ($conf, $debug, $parser);
  5. sub new {
  6. # The sole argument is the config hash
  7. my $self = shift;
  8. $conf = shift;
  9. $debug = shift;
  10. # Load the Common module from wherever it's configured to be.
  11. push @INC, $conf->{modules_dir};
  12. require Pisg::Common;
  13. Pisg::Common->import();
  14. # Pick our parser.
  15. $parser = choose_log_format($conf->{format});
  16. return bless {};
  17. }
  18. # The function to choose which module to use.
  19. sub choose_log_format {
  20. my $format = shift;
  21. my $parser = undef;
  22. $debug->("Loading module for log format $format");
  23. eval <<_END;
  24. use lib '$conf->{modules_dir}';
  25. use Pisg::Parser::$format;
  26. \$parser = new Pisg::Parser::$format(\$debug);
  27. _END
  28. if ($@) {
  29. print STDERR "Could not load parser for '$format': $@\n";
  30. return undef;
  31. }
  32. return $parser;
  33. }
  34. sub analyze {
  35. my (%stats, %lines);
  36. if (defined $parser) {
  37. my $starttime = time();
  38. if ($conf->{logdir}) {
  39. # Run through all files in dir
  40. parse_dir(\%stats, \%lines);
  41. } else {
  42. # Run through the whole logfile
  43. my %state = (linecount => 0,
  44. lastnick => "",
  45. monocount => 0,
  46. lastnormal => "",
  47. oldtime => 24);
  48. parse_file(\%stats, \%lines, $conf->{logfile}, \%state);
  49. }
  50. pick_random_lines(\%stats, \%lines);
  51. my ($sec,$min,$hour) = gmtime(time() - $starttime);
  52. $stats{processtime} =
  53. sprintf("%02d hours, %02d minutes and %02d seconds", $hour, $min,
  54. $sec);
  55. print "Channel analyzed succesfully in $stats{processtime} on ",
  56. scalar localtime(time()), "\n";;
  57. return \%stats;
  58. } else {
  59. print STDERR "Skipping channel '$conf->{channel}' due to lack of parser.\n";
  60. return undef
  61. }
  62. # Shouldn't get here.
  63. return undef;
  64. }
  65. sub parse_dir {
  66. my ($stats, $lines) = @_;
  67. # Add trailing slash when it's not there..
  68. $conf->{logdir} =~ s/([^\/])$/$1\//;
  69. print "Going into $conf->{logdir} and parsing all files there...\n\n";
  70. my @filesarray;
  71. opendir(LOGDIR, $conf->{logdir}) or
  72. die("Can't opendir $conf->{logdir}: $!");
  73. @filesarray = grep {
  74. /^[^\.]/ && /^$conf->{prefix}/ && -f "$conf->{logdir}/$_"
  75. } readdir(LOGDIR) or
  76. die("No files in \"$conf->{logdir}\" matched prefix \"$conf->{prefix}\"");
  77. closedir(LOGDIR);
  78. my %state = (lastnick => "",
  79. monocount => 0,
  80. oldtime => 24);
  81. foreach my $file (sort @filesarray) {
  82. $file = $conf->{logdir} . $file;
  83. parse_file($stats, $lines, $file, \%state);
  84. }
  85. }
  86. # This parses the file...
  87. sub parse_file {
  88. my ($stats, $lines, $file, $state) = @_;
  89. print "Analyzing log($file) in '$conf->{format}' format...\n";
  90. if ($file =~ /.bz2?$/ && -f $file) {
  91. open (LOGFILE, "bunzip2 -c $file |") or
  92. die("$0: Unable to open logfile($file): $!\n");
  93. } elsif ($file =~ /.gz$/ && -f $file) {
  94. open (LOGFILE, "gunzip -c $file |") or
  95. die("$0: Unable to open logfile($file): $!\n");
  96. } else {
  97. open (LOGFILE, $file) or
  98. die("$0: Unable to open logfile($file): $!\n");
  99. }
  100. my $linecount = 0;
  101. my $lastnormal = "";
  102. my $repeated;
  103. while(my $line = <LOGFILE>) {
  104. $line = strip_mirccodes($line);
  105. $linecount++;
  106. my $hashref;
  107. # Match normal lines.
  108. if ($hashref = $parser->normalline($line, $linecount)) {
  109. if (defined $hashref->{repeated}) {
  110. $repeated = $hashref->{repeated};
  111. } else {
  112. $repeated = 0;
  113. }
  114. my ($hour, $nick, $saying, $i);
  115. for ($i = 0; $i <= $repeated; $i++) {
  116. if ($i > 0) {
  117. $hashref = $parser->normalline($lastnormal, $linecount);
  118. #Increment number of lines for repeated lines
  119. $linecount++;
  120. }
  121. $hour = $hashref->{hour};
  122. $nick = find_alias($hashref->{nick});
  123. $saying = $hashref->{saying};
  124. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  125. $state->{oldtime} = $hour;
  126. unless (is_ignored($nick)) {
  127. $stats->{totallines}++;
  128. # Timestamp collecting
  129. $stats->{times}{$hour}++;
  130. $stats->{lines}{$nick}++;
  131. $stats->{line_times}{$nick}[int($hour/6)]++;
  132. # Count up monologues
  133. if ($state->{lastnick} eq $nick) {
  134. $state->{monocount}++;
  135. if ($state->{monocount} == 5) {
  136. $stats->{monologues}{$nick}++;
  137. }
  138. } else {
  139. $state->{monocount} = 0;
  140. }
  141. $state->{lastnick} = $nick;
  142. my $len = length($saying);
  143. if ($len > $conf->{minquote} && $len < $conf->{maxquote}) {
  144. push @{ $lines->{sayings}{$nick} }, $saying;
  145. }
  146. $stats->{lengths}{$nick} += $len;
  147. $stats->{questions}{$nick}++
  148. if ($saying =~ /\?/);
  149. $stats->{shouts}{$nick}++
  150. if ($saying =~ /!/);
  151. if ($saying !~ /[a-z]/ && $saying =~ /[A-Z]/) {
  152. $stats->{allcaps}{$nick}++;
  153. push @{ $lines->{allcaplines}{$nick} }, $line;
  154. }
  155. $stats->{foul}{$nick}++
  156. if ($saying =~ /$conf->{foul}/i);
  157. # Who smiles the most?
  158. # A regex matching al lot of smilies
  159. $stats->{smiles}{$nick}++
  160. if ($saying =~ /[8;:=][ ^-o]?[)pPD}\]>]/);
  161. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/{]/ and
  162. $saying !~ /\w+:\/\//) {
  163. $stats->{frowns}{$nick}++;
  164. }
  165. if (my $url = match_url($saying)) {
  166. $stats->{urlcounts}{$url}++;
  167. $stats->{urlnicks}{$url} = $nick;
  168. }
  169. parse_words($stats, $saying, $nick);
  170. }
  171. }
  172. $lastnormal = $line;
  173. $repeated = 0;
  174. }
  175. # Match action lines.
  176. elsif ($hashref = $parser->actionline($line, $linecount)) {
  177. $stats->{totallines}++;
  178. my ($hour, $nick, $saying);
  179. $hour = $hashref->{hour};
  180. $nick = find_alias($hashref->{nick});
  181. $saying = $hashref->{saying};
  182. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  183. $state->{oldtime} = $hour;
  184. unless (is_ignored($nick)) {
  185. # Timestamp collecting
  186. $stats->{times}{$hour}++;
  187. $stats->{actions}{$nick}++;
  188. push @{ $lines->{actionlines}{$nick} }, $line;
  189. $stats->{lines}{$nick}++;
  190. $stats->{line_times}{$nick}[int($hour/6)]++;
  191. if ($saying =~ /^($conf->{violent}) (\S+)/) {
  192. my $victim = find_alias($2);
  193. $stats->{violence}{$nick}++;
  194. $stats->{attacked}{$victim}++;
  195. push @{ $lines->{violencelines}{$nick} }, $line;
  196. push @{ $lines->{attackedlines}{$victim} }, $line;
  197. }
  198. my $len = length($saying);
  199. $stats->{lengths}{$nick} += $len;
  200. parse_words($stats, $saying, $nick);
  201. }
  202. }
  203. # Match *** lines.
  204. elsif (($hashref = $parser->thirdline($line, $linecount)) and
  205. $hashref->{nick}) {
  206. $stats->{totallines}++;
  207. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  208. my ($newnick);
  209. $hour = $hashref->{hour};
  210. $min = $hashref->{min};
  211. $nick = find_alias($hashref->{nick});
  212. $kicker = find_alias($hashref->{kicker})
  213. if ($hashref->{kicker});
  214. $newtopic = $hashref->{newtopic};
  215. $newmode = $hashref->{newmode};
  216. $newjoin = $hashref->{newjoin};
  217. $newnick = $hashref->{newnick};
  218. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  219. $state->{oldtime} = $hour;
  220. unless (is_ignored($nick)) {
  221. # Timestamp collecting
  222. $stats->{times}{$hour}++;
  223. if (defined($kicker)) {
  224. $stats->{kicked}{$kicker}++;
  225. $stats->{gotkicked}{$nick}++;
  226. push @{ $lines->{kicklines}{$nick} }, $line;
  227. } elsif (defined($newtopic)) {
  228. unless ($newtopic eq '') {
  229. my $tcount;
  230. if (defined $stats->{topics}) {
  231. $tcount = @{ $stats->{topics} };
  232. } else {
  233. $tcount = 0;
  234. }
  235. $stats->{topics}[$tcount]{topic} = $newtopic;
  236. $stats->{topics}[$tcount]{nick} = $nick;
  237. $stats->{topics}[$tcount]{hour} = $hour;
  238. $stats->{topics}[$tcount]{min} = $min;
  239. }
  240. } elsif (defined($newmode)) {
  241. my @opchange = opchanges($newmode);
  242. $stats->{gaveops}{$nick} += $opchange[0] if $opchange[0];
  243. $stats->{tookops}{$nick} += $opchange[1] if $opchange[1];
  244. } elsif (defined($newjoin)) {
  245. $stats->{joins}{$nick}++;
  246. } elsif (defined($newnick) and ($conf->{nicktracking} == 1)) {
  247. add_alias($nick, $newnick);
  248. }
  249. }
  250. }
  251. }
  252. close(LOGFILE);
  253. print "Finished analyzing log, $stats->{days} days total.\n";
  254. }
  255. sub opchanges {
  256. my (@ops, $plus);
  257. foreach (split(//, $_[0])) {
  258. if ($_ eq "o") {
  259. $ops[$plus]++;
  260. } elsif ($_ eq "+") {
  261. $plus = 0;
  262. } elsif ($_ eq "-") {
  263. $plus = 1;
  264. }
  265. }
  266. return @ops;
  267. }
  268. sub parse_words {
  269. my ($stats, $saying, $nick) = @_;
  270. foreach my $word (split(/[\s,!?.:;)(\"]+/, $saying)) {
  271. $stats->{words}{$nick}++;
  272. # remove uninteresting words
  273. next unless (length($word) >= $conf->{wordlength});
  274. next if ($conf->{ignoreword}{$word});
  275. # ignore contractions
  276. next if ($word =~ m/'..?$/);#'
  277. # Also ignore stuff from URLs.
  278. next if ($word =~ m{https?|^//});
  279. $stats->{wordcounts}{$word}++;
  280. $stats->{wordnicks}{$word} = $nick;
  281. }
  282. }
  283. sub pick_random_lines {
  284. my ($stats, $lines) = @_;
  285. foreach my $key (keys %{ $lines }) {
  286. foreach my $nick (keys %{ $lines->{$key} }) {
  287. $stats->{$key}{$nick} =
  288. @{ $lines->{$key}{$nick} }[rand@{ $lines->{$key}{$nick} }];
  289. }
  290. }
  291. }
  292. sub strip_mirccodes {
  293. my $line = shift;
  294. my $boldcode = chr(2);
  295. my $colorcode = chr(3);
  296. my $plaincode = chr(15);
  297. my $reversecode = chr(22);
  298. my $underlinecode = chr(31);
  299. # Strip mIRC color codes
  300. $line =~ s/$colorcode\d{1,2},\d{1,2}//go;
  301. $line =~ s/$colorcode\d{0,2}//go;
  302. # Strip mIRC bold, plain, reverse and underline codes
  303. $line =~ s/[$boldcode$underlinecode$reversecode$plaincode]//go;
  304. return $line;
  305. }
  306. 1;