Logfile.pm 12 KB

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