Logfile.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package Pisg::Parser::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::$format;
  26. \$parser = new Pisg::Parser::Format::$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 = (
  79. lastnick => "",
  80. monocount => 0,
  81. oldtime => 24
  82. );
  83. foreach my $file (sort @filesarray) {
  84. $file = $conf->{logdir} . $file;
  85. parse_file($stats, $lines, $file, \%state);
  86. }
  87. }
  88. # This parses the file...
  89. sub parse_file {
  90. my ($stats, $lines, $file, $state) = @_;
  91. print "Analyzing log($file) in '$conf->{format}' format...\n";
  92. if ($file =~ /.bz2?$/ && -f $file) {
  93. open (LOGFILE, "bunzip2 -c $file |") or
  94. die("$0: Unable to open logfile($file): $!\n");
  95. } elsif ($file =~ /.gz$/ && -f $file) {
  96. open (LOGFILE, "gunzip -c $file |") or
  97. die("$0: Unable to open logfile($file): $!\n");
  98. } else {
  99. open (LOGFILE, $file) or
  100. die("$0: Unable to open logfile($file): $!\n");
  101. }
  102. my $linecount = 0;
  103. my $lastnormal = "";
  104. my $repeated;
  105. while(my $line = <LOGFILE>) {
  106. $line = strip_mirccodes($line);
  107. $linecount++;
  108. my $hashref;
  109. # Match normal lines.
  110. if ($hashref = $parser->normalline($line, $linecount)) {
  111. if (defined $hashref->{repeated}) {
  112. $repeated = $hashref->{repeated};
  113. } else {
  114. $repeated = 0;
  115. }
  116. my ($hour, $nick, $saying, $i);
  117. for ($i = 0; $i <= $repeated; $i++) {
  118. if ($i > 0) {
  119. $hashref = $parser->normalline($lastnormal, $linecount);
  120. #Increment number of lines for repeated lines
  121. $linecount++;
  122. }
  123. $hour = $hashref->{hour};
  124. $nick = find_alias($hashref->{nick});
  125. $saying = $hashref->{saying};
  126. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  127. $state->{oldtime} = $hour;
  128. unless (is_ignored($nick)) {
  129. $stats->{totallines}++;
  130. # Timestamp collecting
  131. $stats->{times}{$hour}++;
  132. $stats->{lines}{$nick}++;
  133. $stats->{line_times}{$nick}[int($hour/6)]++;
  134. # Count up monologues
  135. if ($state->{lastnick} eq $nick) {
  136. $state->{monocount}++;
  137. if ($state->{monocount} == 5) {
  138. $stats->{monologues}{$nick}++;
  139. }
  140. } else {
  141. $state->{monocount} = 0;
  142. }
  143. $state->{lastnick} = $nick;
  144. my $len = length($saying);
  145. if ($len > $conf->{minquote} && $len < $conf->{maxquote}) {
  146. push @{ $lines->{sayings}{$nick} }, $saying;
  147. }
  148. $stats->{lengths}{$nick} += $len;
  149. $stats->{questions}{$nick}++
  150. if ($saying =~ /\?/);
  151. $stats->{shouts}{$nick}++
  152. if ($saying =~ /!/);
  153. if ($saying !~ /[a-z]/ && $saying =~ /[A-Z]/) {
  154. $stats->{allcaps}{$nick}++;
  155. push @{ $lines->{allcaplines}{$nick} }, $line;
  156. }
  157. $stats->{foul}{$nick}++
  158. if ($saying =~ /$conf->{foul}/i);
  159. # Who smiles the most?
  160. # A regex matching al lot of smilies
  161. $stats->{smiles}{$nick}++
  162. if ($saying =~ /[8;:=][ ^-o]?[)pPD}\]>]/);
  163. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/{]/ and
  164. $saying !~ /\w+:\/\//) {
  165. $stats->{frowns}{$nick}++;
  166. }
  167. if (my $url = match_url($saying)) {
  168. $stats->{urlcounts}{$url}++;
  169. $stats->{urlnicks}{$url} = $nick;
  170. }
  171. parse_words($stats, $saying, $nick);
  172. }
  173. }
  174. $lastnormal = $line;
  175. $repeated = 0;
  176. }
  177. # Match action lines.
  178. elsif ($hashref = $parser->actionline($line, $linecount)) {
  179. $stats->{totallines}++;
  180. my ($hour, $nick, $saying);
  181. $hour = $hashref->{hour};
  182. $nick = find_alias($hashref->{nick});
  183. $saying = $hashref->{saying};
  184. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  185. $state->{oldtime} = $hour;
  186. unless (is_ignored($nick)) {
  187. # Timestamp collecting
  188. $stats->{times}{$hour}++;
  189. $stats->{actions}{$nick}++;
  190. push @{ $lines->{actionlines}{$nick} }, $line;
  191. $stats->{lines}{$nick}++;
  192. $stats->{line_times}{$nick}[int($hour/6)]++;
  193. if ($saying =~ /^($conf->{violent}) (\S+)/) {
  194. my $victim = find_alias($2);
  195. $stats->{violence}{$nick}++;
  196. $stats->{attacked}{$victim}++;
  197. push @{ $lines->{violencelines}{$nick} }, $line;
  198. push @{ $lines->{attackedlines}{$victim} }, $line;
  199. }
  200. my $len = length($saying);
  201. $stats->{lengths}{$nick} += $len;
  202. parse_words($stats, $saying, $nick);
  203. }
  204. }
  205. # Match *** lines.
  206. elsif (($hashref = $parser->thirdline($line, $linecount)) and
  207. $hashref->{nick}) {
  208. $stats->{totallines}++;
  209. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin);
  210. my ($newnick);
  211. $hour = $hashref->{hour};
  212. $min = $hashref->{min};
  213. $nick = find_alias($hashref->{nick});
  214. $kicker = find_alias($hashref->{kicker})
  215. if ($hashref->{kicker});
  216. $newtopic = $hashref->{newtopic};
  217. $newmode = $hashref->{newmode};
  218. $newjoin = $hashref->{newjoin};
  219. $newnick = $hashref->{newnick};
  220. if ($hour < $state->{oldtime}) { $stats->{days}++ }
  221. $state->{oldtime} = $hour;
  222. unless (is_ignored($nick)) {
  223. # Timestamp collecting
  224. $stats->{times}{$hour}++;
  225. if (defined($kicker)) {
  226. $stats->{kicked}{$kicker}++;
  227. $stats->{gotkicked}{$nick}++;
  228. push @{ $lines->{kicklines}{$nick} }, $line;
  229. } elsif (defined($newtopic)) {
  230. unless ($newtopic eq '') {
  231. my $tcount;
  232. if (defined $stats->{topics}) {
  233. $tcount = @{ $stats->{topics} };
  234. } else {
  235. $tcount = 0;
  236. }
  237. $stats->{topics}[$tcount]{topic} = $newtopic;
  238. $stats->{topics}[$tcount]{nick} = $nick;
  239. $stats->{topics}[$tcount]{hour} = $hour;
  240. $stats->{topics}[$tcount]{min} = $min;
  241. }
  242. } elsif (defined($newmode)) {
  243. my @opchange = opchanges($newmode);
  244. $stats->{gaveops}{$nick} += $opchange[0] if $opchange[0];
  245. $stats->{tookops}{$nick} += $opchange[1] if $opchange[1];
  246. } elsif (defined($newjoin)) {
  247. $stats->{joins}{$nick}++;
  248. } elsif (defined($newnick) and ($conf->{nicktracking} == 1)) {
  249. add_alias($nick, $newnick);
  250. }
  251. }
  252. }
  253. }
  254. close(LOGFILE);
  255. print "Finished analyzing log, $stats->{days} days total.\n";
  256. }
  257. sub opchanges {
  258. my (@ops, $plus);
  259. foreach (split(//, $_[0])) {
  260. if ($_ eq "o") {
  261. $ops[$plus]++;
  262. } elsif ($_ eq "+") {
  263. $plus = 0;
  264. } elsif ($_ eq "-") {
  265. $plus = 1;
  266. }
  267. }
  268. return @ops;
  269. }
  270. sub parse_words {
  271. my ($stats, $saying, $nick) = @_;
  272. foreach my $word (split(/[\s,!?.:;)(\"]+/, $saying)) {
  273. $stats->{words}{$nick}++;
  274. # remove uninteresting words
  275. next unless (length($word) >= $conf->{wordlength});
  276. next if ($conf->{ignoreword}{$word});
  277. # ignore contractions
  278. next if ($word =~ m/'..?$/);#'
  279. # Also ignore stuff from URLs.
  280. next if ($word =~ m{https?|^//});
  281. $stats->{wordcounts}{$word}++;
  282. $stats->{wordnicks}{$word} = $nick;
  283. }
  284. }
  285. sub pick_random_lines {
  286. my ($stats, $lines) = @_;
  287. foreach my $key (keys %{ $lines }) {
  288. foreach my $nick (keys %{ $lines->{$key} }) {
  289. $stats->{$key}{$nick} =
  290. @{ $lines->{$key}{$nick} }[rand@{ $lines->{$key}{$nick} }];
  291. }
  292. }
  293. }
  294. sub strip_mirccodes {
  295. my $line = shift;
  296. # boldcode = chr(2) = oct 001
  297. # colorcode = chr(3) = oct 003
  298. # plaincode = chr(15) = oct 017
  299. # reversecode = chr(22) = oct 026
  300. # underlinecode = chr(31) = oct 037
  301. # Strip mIRC color codes
  302. $line =~ s/\003\d{1,2},\d{1,2}//go;
  303. $line =~ s/\003\d{0,2}//go;
  304. # Strip mIRC bold, plain, reverse and underline codes
  305. $line =~ s/[\002\017\026\037]//go;
  306. return $line;
  307. }
  308. 1;