Logfile.pm 12 KB

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