Pisg.pm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. package Pisg;
  2. # Documentation(POD) for this module is found at the end of the file.
  3. # Copyright (C) 2001-2005 <Morten Brix Pedersen> - morten@wtf.dk
  4. # Copyright (C) 2003-2005 Christoph Berg <cb@df7cb.de>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. use strict;
  20. $^W = 1;
  21. sub new
  22. {
  23. my $type = shift;
  24. my %args = @_;
  25. my $self = {
  26. override_cfg => $args{override_cfg},
  27. use_configfile => $args{use_configfile},
  28. search_path => $args{search_path},
  29. chans => {},
  30. users => {},
  31. cfg => {},
  32. tmps => {},
  33. };
  34. # Set the default configuration settings.
  35. get_default_config_settings($self);
  36. # Import common functions in Pisg::Common
  37. require Pisg::Common;
  38. Pisg::Common->import();
  39. bless($self, $type);
  40. return $self;
  41. }
  42. sub run
  43. {
  44. my $self = shift;
  45. print "pisg v$self->{cfg}->{version} - Perl IRC Statistics Generator\n\n"
  46. unless ($self->{cfg}->{silent});
  47. # Init the configuration file (aliases, ignores, channels, etc)
  48. my $r;
  49. if ($self->{use_configfile}) {
  50. if ((open(CONFIG, $self->{cfg}->{configfile})
  51. or open(CONFIG, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
  52. $r = $self->init_config(\*CONFIG)
  53. }
  54. }
  55. print "Using config file: $self->{cfg}->{configfile}\n\n"
  56. if ($r && !$self->{cfg}->{silent});
  57. # Get translations from langfile
  58. $self->get_language_templates();
  59. # Parse any channels in <channel> statements
  60. $self->parse_channels();
  61. # Optionaly parse the channel we were given in override_cfg.
  62. $self->do_channel()
  63. if (!$self->{cfg}->{chan_done}{$self->{cfg}->{channel}});
  64. }
  65. sub get_default_config_settings
  66. {
  67. my $self = shift;
  68. # This is all the default settings of pisg. They can be overriden by the
  69. # pisg.cfg file, or by using the override_cfg argument to the new
  70. # constructor.
  71. $self->{cfg} = {
  72. channel => '',
  73. logtype => 'Logfile',
  74. logfile => [],
  75. format => '',
  76. network => 'SomeIRCNet',
  77. outputfile => 'index.html',
  78. outputtag => '',
  79. maintainer => 'MAINTAINER',
  80. pagehead => 'none',
  81. pagefoot => 'none',
  82. configfile => 'pisg.cfg',
  83. imagepath => '',
  84. imageglobpath => '',
  85. defaultpic => '',
  86. logdir => [],
  87. nfiles => 0,
  88. lang => 'EN',
  89. langfile => 'lang.txt',
  90. cssdir => 'layout/',
  91. colorscheme => 'default',
  92. altcolorscheme => 'none',
  93. logprefix => '',
  94. logsuffix => '',
  95. silent => 0,
  96. cachedir => '',
  97. userpics => 'y',
  98. # Colors / Layout
  99. hicell => '#BABADD', # FIXME
  100. hicell2 => '#CCCCCC', # FIXME
  101. picwidth => '',
  102. picheight => '',
  103. pic_v_0 => 'blue-v.png',
  104. pic_v_6 => 'green-v.png',
  105. pic_v_12 => 'yellow-v.png',
  106. pic_v_18 => 'red-v.png',
  107. pic_h_0 => 'blue-h.png',
  108. pic_h_6 => 'green-h.png',
  109. pic_h_12 => 'yellow-h.png',
  110. pic_h_18 => 'red-h.png',
  111. piclocation => '.',
  112. # Stats settings
  113. showactivetimes => 1,
  114. showactivenicks => 1,
  115. showbignumbers => 1,
  116. showtopics => 1,
  117. showlinetime => 0,
  118. showwordtime => 0,
  119. showlines => 1,
  120. showtime => 1,
  121. showwords => 0,
  122. showwpl => 0,
  123. showcpl => 0,
  124. showlastseen => 1,
  125. showlegend => 1,
  126. showkickline => 1,
  127. showactionline => 1,
  128. showfoulline => 0,
  129. showfouldecimals => 1,
  130. showshoutline => 1,
  131. showviolentlines => 1,
  132. showrandquote => 1,
  133. showmuw => 1,
  134. showmrn => 1,
  135. showsmileys => 0,
  136. showkarma => 0,
  137. showmru => 1,
  138. showcharts => 0,
  139. showops => 1,
  140. showvoices => 0,
  141. showhalfops => 0,
  142. showmostnicks => 0,
  143. showactivegenders => 0,
  144. showmostactivebyhour => 0,
  145. showmostactivebyhourgraph => 1,
  146. showonlytop => 0,
  147. # Less important things
  148. timeoffset => '+0',
  149. minquote => 25,
  150. maxquote => 65,
  151. quotewidth => 80,
  152. bignumbersthreshold => 'sqrt',
  153. wordlength => 5,
  154. dailyactivity => 0,
  155. activenicks => 25,
  156. activenicks2 => 30,
  157. activenicksbyhour => 10,
  158. topichistory => 3,
  159. urlhistory => 5,
  160. chartshistory => 5,
  161. nickhistory => 5,
  162. smileyhistory => 10,
  163. karmahistory => 5,
  164. wordhistory => 10,
  165. mostnickshistory => 5,
  166. mostnicksverbose => 1,
  167. nicklimit => 10,
  168. nicktracking => 0,
  169. charset => 'iso-8859-1',
  170. logcharset => '',
  171. logcharsetfallback => '',
  172. # sorting
  173. sortbywords => 0,
  174. # Misc settings
  175. foulwords => 'ass fuck bitch shit scheisse scheiße kacke arsch ficker ficken schlampe',
  176. violentwords => 'slaps beats smacks',
  177. chartsregexp => '(?:np:|(?:now )?playing:? (?:MPEG stream from)?)\s*(.*)',
  178. ignorewords => '',
  179. noignoredquotes => 0,
  180. tablewidth => 574,
  181. regexpaliases => 0,
  182. botnicks => '', # Needed for DCpp format (non-irc)
  183. statsdump => '', # Debug option
  184. modules_dir => '', # set in get_cmdline_options
  185. cchannels => '', # set in get_cmdline_options
  186. version => "0.66",
  187. };
  188. # This enables us to use the search_path in other modules
  189. $self->{cfg}->{search_path} = $self->{search_path};
  190. # Parse the optional overriden configuration variables
  191. foreach my $key (keys %{$self->{override_cfg}}) {
  192. if ($self->{override_cfg}->{$key}) {
  193. unless (defined($self->{cfg}->{$key})) {
  194. print STDERR "Warning: No such configuration option: -cfg $key\n";
  195. next;
  196. }
  197. $self->{cfg}->{$key} = $self->{override_cfg}->{$key};
  198. }
  199. }
  200. }
  201. sub get_language_templates
  202. {
  203. my $self = shift;
  204. open(FILE, $self->{cfg}->{langfile}) or open (FILE, $self->{search_path} . "/$self->{cfg}->{langfile}") or die("$0: Unable to open language file($self->{cfg}->{langfile}): $!\n");
  205. while (my $line = <FILE>)
  206. {
  207. next if ($line =~ /^#/);
  208. if ($line =~ /<lang name=\"([^"]+)\"(?: charset=\"(.*)\")?>/i) {
  209. # Found start tag, setting the current language
  210. my $current_lang = uc($1);
  211. $self->{tmps}->{$current_lang}{lang_charset} = lc($2);
  212. while (<FILE>) {
  213. last if ($_ =~ /<\/lang>/i);
  214. # Get 'template = "Text"' in language file:
  215. if ($_ =~ /^(\w+)\s*=\s*"(.*)"\s*$/) {
  216. warn "duplicate translation $1 -> $2"
  217. if $self->{tmps}->{$current_lang}{$1} and !$self->{cfg}->{silent};
  218. $self->{tmps}->{$current_lang}{$1} = $2;
  219. }
  220. }
  221. }
  222. }
  223. close(FILE);
  224. }
  225. sub init_config
  226. {
  227. my $self = shift;
  228. my $fh = shift;
  229. while (my $line = <$fh>)
  230. {
  231. next if ($line =~ /^\s*#/);
  232. chomp $line;
  233. if ($line =~ /<user.*>/) {
  234. my $nick;
  235. if ($line =~ /nick=(["'])(.+?)\1/) {
  236. $nick = $2;
  237. add_alias($nick, $nick);
  238. } else {
  239. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No nick specified\n";
  240. next;
  241. }
  242. if ($line =~ /alias=(["'])(.+?)\1/) {
  243. my @thisalias = split(/\s+/, lc($2));
  244. foreach (@thisalias) {
  245. if ($self->{cfg}->{regexpaliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
  246. add_aliaswild($nick, $_);
  247. } elsif (not $self->{cfg}->{regexpaliases} and s/\*/\.\*/g) {
  248. # quote it if it is a wildcard
  249. s/([\|\[\]\{\}\(\)\?\+\^\\])/\\$1/g;
  250. add_aliaswild($nick, $_);
  251. } else {
  252. add_alias($nick, $_);
  253. }
  254. }
  255. }
  256. if ($line =~ /pic=(["'])(.+?)\1/) {
  257. $self->{users}->{userpics}{$nick} = $2;
  258. }
  259. if ($line =~ /bigpic=(["'])(.+?)\1/) {
  260. $self->{users}->{biguserpics}{$nick} = $2;
  261. }
  262. if ($line =~ /link=(["'])(.+?)\1/) {
  263. $self->{users}->{userlinks}{$nick} = $2;
  264. }
  265. if ($line =~ /ignore=(["'])Y\1/i) {
  266. add_ignore($nick);
  267. }
  268. if ($line =~ /sex=(["'])([MmFfBb])\1/) {
  269. $self->{users}->{sex}{$nick} = lc($2);
  270. }
  271. } elsif ($line =~ /<link(.*)>/) {
  272. if ($line =~ /url=(["'])(.+?)\1/) {
  273. my $url = $2;
  274. if ($line =~ /ignore="Y"/i) {
  275. add_url_ignore($url);
  276. }
  277. } else {
  278. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No URL specified\n";
  279. }
  280. } elsif ($line =~ /<set(.*)>/) {
  281. my $settings = $1;
  282. if ($settings !~ /=["'](.*)["']/ || $settings =~ /(\w)>/ ) {
  283. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing or wrong quotes near $1\n";
  284. }
  285. while ($settings =~ s/[ \t]([^=]+?)=(["'])(.*?)\2//) {
  286. my $var = lc($1);
  287. $var =~ s/ //; # Remove whitespace
  288. if (!defined($self->{cfg}->{$var})) {
  289. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No such configuration option: '$var'\n";
  290. next;
  291. }
  292. unless (($self->{cfg}->{$var} eq $3) || $self->{override_cfg}->{$var}) {
  293. $self->{cfg}->{$var} = $3;
  294. }
  295. }
  296. } elsif ($line =~ /<channel=(['"])(.+?)\1(.*)>/i) {
  297. my ($channel, $settings) = ($2, $3);
  298. $self->{chans}->{$channel}->{channel} = $channel;
  299. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
  300. while ($settings =~ s/\s([^=]+)=(["'])(.+?)\2//) {
  301. my $var = lc($1);
  302. if ($var eq "logdir" || $var eq "logfile") {
  303. push(@{$self->{chans}->{$channel}{$var}}, $3);
  304. } else {
  305. $self->{chans}->{$channel}{$var} = $3;
  306. }
  307. }
  308. while (<$fh>) {
  309. next if /^\s*#/;
  310. last if ($_ =~ /<\/*channel>/i);
  311. if ($_ =~ /^\s*(\w+)\s*=\s*(["'])(.+?)\2/) {
  312. my $var = lc($1);
  313. unless ((($var eq "logdir" || $var eq "logfile") && scalar(@{$self->{override_cfg}->{$var}}) > 0) || (($var ne "logdir" && $var ne "logfile") && $self->{override_cfg}->{$var})) {
  314. if($var eq "logdir" || $var eq "logfile") {
  315. push @{$self->{chans}->{$channel}{$var}}, $3;
  316. } else {
  317. $self->{chans}->{$channel}{$var} = $3;
  318. }
  319. }
  320. } elsif ($_ !~ /^$/) {
  321. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line: $_";
  322. }
  323. }
  324. } elsif ($line =~ /<include\s*=\s*(["'])(.+?)\1\s*>/) {
  325. my $include_cfg = $2;
  326. my $backup_cfg = $self->{cfg}->{configfile};
  327. $self->{cfg}->{configfile} = $include_cfg;
  328. my $r;
  329. if ((open(INCLUDE, $self->{cfg}->{configfile})
  330. or open(INCLUDE, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
  331. $r = $self->init_config(\*INCLUDE);
  332. } else {
  333. print STDERR "Warning: $backup_cfg, line $.: $self->{cfg}->{configfile}: $!\n";
  334. }
  335. print "Included config file: $self->{cfg}->{configfile}\n\n"
  336. if ($r && !$self->{cfg}->{silent});
  337. $self->{cfg}->{configfile} = $backup_cfg;
  338. } elsif ($line =~ /<(\w+)?.*[^>]$/) {
  339. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing end on element <$1 (probably multi-line?)\n";
  340. } elsif ($line =~ /\S/) {
  341. $line =~ s/\n//;
  342. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line: $line\n";
  343. }
  344. }
  345. close($fh);
  346. }
  347. sub init_pisg
  348. {
  349. my $self = shift;
  350. my $timestamp = time();
  351. $self->{cfg}->{start} = time();
  352. if ($self->{cfg}->{timeoffset} =~ /\+(\d+)/) {
  353. # We must plus some hours to the time
  354. $timestamp += 3600 * $1; # 3600 seconds per hour
  355. } elsif ($self->{cfg}->{timeoffset} =~ /-(\d+)/) {
  356. # We must remove some hours from the time
  357. $timestamp -= 3600 * $1; # 3600 seconds per hour
  358. }
  359. $self->{cfg}->{timestamp} = $timestamp;
  360. $self->{cfg}->{lang} = uc $self->{cfg}->{lang};
  361. $self->{cfg}->{lang} =~ s/-/_/g; # PT_BR was called PT-BR before
  362. # convert wordlists
  363. $self->{cfg}->{foulwords} = wordlist_regexp($self->{cfg}->{foulwords}, $self->{cfg}->{regexpaliases});
  364. $self->{cfg}->{ignorewords} = wordlist_regexp($self->{cfg}->{ignorewords}, $self->{cfg}->{regexpaliases});
  365. $self->{cfg}->{violentwords} = wordlist_regexp($self->{cfg}->{violentwords}, $self->{cfg}->{regexpaliases});
  366. # Add trailing slash when it's not there..
  367. $self->{cfg}->{imagepath} =~ s/([^\/])$/$1\//;
  368. # Set ImageGlobPath default
  369. $self->{cfg}->{imageglobpath} ||= $self->{cfg}->{imagepath};
  370. $self->{cfg}->{imageglobpath} =~ s/([^\/])$/$1\//;
  371. # Set number of picture columns to show
  372. if ($self->{cfg}->{userpics} =~ /^n/i) {
  373. $self->{cfg}->{userpics} = 0;
  374. } elsif ($self->{cfg}->{userpics} =~ /^y/i) {
  375. $self->{cfg}->{userpics} = 1;
  376. } elsif ($self->{cfg}->{userpics} !~ /^\d+$/) {
  377. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Invalid UserPics setting\n";
  378. }
  379. unless ($self->{cfg}->{silent}) {
  380. print "Statistics for channel $self->{cfg}->{channel} \@ $self->{cfg}->{network} by $self->{cfg}->{maintainer}\n\n";
  381. die sprintf "No such language: %s\n", $self->{cfg}->{lang}
  382. unless $self->{tmps}->{$self->{cfg}->{lang}};
  383. print "Using language template: $self->{cfg}->{lang}\n\n" if $self->{cfg}->{lang} ne 'EN';
  384. }
  385. }
  386. sub do_channel
  387. {
  388. my $self = shift;
  389. if (!$self->{cfg}->{channel}) {
  390. print STDERR "No channels defined.\n";
  391. } elsif ((!@{$self->{cfg}->{logfile}}) && (!@{$self->{cfg}->{logdir}})) {
  392. print STDERR "No logfile or logdir defined for " . $self->{cfg}->{channel} . "\n";
  393. } elsif (!$self->{cfg}->{format}) {
  394. print STDERR "No format defined for $self->{cfg}->{channel}.\n";
  395. } else {
  396. $self->init_pisg(); # Init some general things
  397. store_aliases(); # Save the aliases so we can restore them
  398. # later, we don't want to add the aliases
  399. # for this channel to the next channel
  400. # Pick our stats generator.
  401. my $analyzer;
  402. eval <<_END;
  403. use Pisg::Parser::$self->{cfg}->{logtype};
  404. \$analyzer = new Pisg::Parser::$self->{cfg}->{logtype}(
  405. { cfg => \$self->{cfg}, users => \$self->{users} }
  406. );
  407. _END
  408. if ($@) {
  409. print STDERR "Could not load stats analyzer for '$self->{cfg}->{logtype}': $@\n";
  410. return undef;
  411. }
  412. my $stats = $analyzer->analyze();
  413. $self->{cfg}->{analyzer} = $analyzer; # we need the parser in _format_line
  414. # Initialize HTMLGenerator object
  415. my $generator;
  416. eval <<_END;
  417. use Pisg::HTMLGenerator;
  418. \$generator = new Pisg::HTMLGenerator(
  419. cfg => \$self->{cfg},
  420. stats => \$stats,
  421. users => \$self->{users},
  422. tmps => \$self->{tmps}
  423. );
  424. _END
  425. if ($@) {
  426. print STDERR "Could not load stats generator (Pisg::HTMLGenerator): $@\n";
  427. return undef;
  428. }
  429. # Create our HTML page if the logfile has any data.
  430. if (defined $stats) {
  431. if ($stats->{parsedlines} > 0) {
  432. $generator->create_output();
  433. } else {
  434. print STDERR <<_END unless $self->{cfg}->{silent};
  435. No parseable lines found in logfile ($stats->{totallines} total lines). Skipping.
  436. -> You might be using the wrong format.
  437. -> A common error is that the logs do not contain timestamps for each line.
  438. _END
  439. }
  440. }
  441. restore_aliases();
  442. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1;
  443. }
  444. }
  445. sub parse_channels
  446. {
  447. my $self = shift;
  448. my %origcfg = %{ $self->{cfg} };
  449. # make a list of channels to do
  450. my @chanlist;
  451. if (scalar @ {$self->{cfg}->{cchannels} } > 0) {
  452. @chanlist = @{ $self->{cfg}->{cchannels} };
  453. } else {
  454. @chanlist = keys %{ $self->{chans} };
  455. }
  456. foreach my $channel (@chanlist) {
  457. if (!defined $self->{chans}->{$channel}) {
  458. print STDERR "Channel $channel not in config file, ignoring\n";
  459. next;
  460. }
  461. foreach (keys %{ $self->{chans}->{$channel} }) { # import channel specific config
  462. $self->{cfg}->{$_} = $self->{chans}->{$channel}{$_};
  463. }
  464. $self->do_channel();
  465. $origcfg{chan_done} = $self->{cfg}->{chan_done};
  466. %{ $self->{cfg} } = %origcfg;
  467. }
  468. }
  469. 1;
  470. __END__
  471. =head1 NAME
  472. Pisg - Perl IRC Statistics Generator main module
  473. =head1 SYNOPSIS
  474. use Pisg;
  475. $pisg = new Pisg(
  476. use_configfile => '1',
  477. override_cfg => { network => 'MyNetwork', format => 'eggdrop' }
  478. );
  479. $pisg->run();
  480. =head1 DESCRIPTION
  481. C<Pisg> is a statistic generator for IRC logfiles or the like, delivering
  482. the results in a HTML page.
  483. =head1 CONSTRUCTOR
  484. =over 4
  485. =item new ( [ OPTIONS ] )
  486. This is the constructor for a new Pisg object. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  487. Possible options are:
  488. B<use_configfile> - When set to 1, pisg will look up it's channels in it's
  489. configuration file, defined by the configuration option 'configfile'.
  490. B<override_cfg> - This defines whichever configuration variables you want to
  491. override from the configuration file. If you set use_configfile to 0, then
  492. you'll have to set at least channel and logfile here.
  493. B<search_path> - This defines an optional search path. It's used when you want to hardcode an alternative path where pisg should look after its language and config file.
  494. =back
  495. =head1 AUTHOR
  496. Morten Brix Pedersen <morten@wtf.dk>
  497. =head1 COPYRIGHT
  498. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  499. This program is free software; you can redistribute it and/or modify it
  500. under the terms of the GPL, license is included with the distribution of
  501. this file.
  502. =cut