Pisg.pm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. showkarma => 0,
  136. showmru => 1,
  137. showcharts => 0,
  138. showops => 1,
  139. showvoices => 0,
  140. showhalfops => 0,
  141. showmostnicks => 0,
  142. showactivegenders => 0,
  143. showmostactivebyhour => 0,
  144. showmostactivebyhourgraph => 1,
  145. showonlytop => 0,
  146. # Less important things
  147. timeoffset => '+0',
  148. minquote => 25,
  149. maxquote => 65,
  150. quotewidth => 80,
  151. bignumbersthreshold => 'sqrt',
  152. wordlength => 5,
  153. dailyactivity => 0,
  154. activenicks => 25,
  155. activenicks2 => 30,
  156. activenicksbyhour => 10,
  157. topichistory => 3,
  158. urlhistory => 5,
  159. chartshistory => 5,
  160. nickhistory => 5,
  161. karmahistory => 5,
  162. wordhistory => 10,
  163. mostnickshistory => 5,
  164. mostnicksverbose => 1,
  165. nicklimit => 10,
  166. nicktracking => 0,
  167. charset => 'iso-8859-1',
  168. logcharset => '',
  169. logcharsetfallback => '',
  170. # sorting
  171. sortbywords => 0,
  172. # Misc settings
  173. foulwords => 'ass fuck bitch shit scheisse scheiße kacke arsch ficker ficken schlampe',
  174. violentwords => 'slaps beats smacks',
  175. chartsregexp => '(?:np:|(?:now )?playing:? (?:MPEG stream from)?)\s*(.*)',
  176. ignorewords => '',
  177. noignoredquotes => 0,
  178. tablewidth => 574,
  179. regexpaliases => 0,
  180. botnicks => '', # Needed for DCpp format (non-irc)
  181. statsdump => '', # Debug option
  182. version => "0.65",
  183. };
  184. # This enables us to use the search_path in other modules
  185. $self->{cfg}->{search_path} = $self->{search_path};
  186. # Parse the optional overriden configuration variables
  187. foreach my $key (keys %{$self->{override_cfg}}) {
  188. if ($self->{override_cfg}->{$key}) {
  189. $self->{cfg}->{$key} = $self->{override_cfg}->{$key};
  190. }
  191. }
  192. }
  193. sub get_language_templates
  194. {
  195. my $self = shift;
  196. 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");
  197. while (my $line = <FILE>)
  198. {
  199. next if ($line =~ /^#/);
  200. if ($line =~ /<lang name=\"([^"]+)\"(?: charset=\"(.*)\")?>/i) {
  201. # Found start tag, setting the current language
  202. my $current_lang = uc($1);
  203. $self->{tmps}->{$current_lang}{lang_charset} = lc($2);
  204. while (<FILE>) {
  205. last if ($_ =~ /<\/lang>/i);
  206. # Get 'template = "Text"' in language file:
  207. if ($_ =~ /^(\w+)\s*=\s*"(.*)"\s*$/) {
  208. warn "duplicate translation $1 -> $2" if $self->{tmps}->{$current_lang}{$1};
  209. $self->{tmps}->{$current_lang}{$1} = $2;
  210. }
  211. }
  212. }
  213. }
  214. close(FILE);
  215. }
  216. sub init_config
  217. {
  218. my $self = shift;
  219. my $fh = shift;
  220. while (my $line = <$fh>)
  221. {
  222. next if ($line =~ /^\s*#/);
  223. chomp $line;
  224. if ($line =~ /<user.*>/) {
  225. my $nick;
  226. if ($line =~ /nick=(["'])(.+?)\1/) {
  227. $nick = $2;
  228. add_alias($nick, $nick);
  229. } else {
  230. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No nick specified\n";
  231. next;
  232. }
  233. if ($line =~ /alias=(["'])(.+?)\1/) {
  234. my @thisalias = split(/\s+/, lc($2));
  235. foreach (@thisalias) {
  236. if ($self->{cfg}->{regexpaliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
  237. add_aliaswild($nick, $_);
  238. } elsif (not $self->{cfg}->{regexpaliases} and s/\*/\.\*/g) {
  239. # quote it if it is a wildcard
  240. s/([\|\[\]\{\}\(\)\?\+\^\\])/\\$1/g;
  241. add_aliaswild($nick, $_);
  242. } else {
  243. add_alias($nick, $_);
  244. }
  245. }
  246. }
  247. if ($line =~ /pic=(["'])(.+?)\1/) {
  248. $self->{users}->{userpics}{$nick} = $2;
  249. }
  250. if ($line =~ /bigpic=(["'])(.+?)\1/) {
  251. $self->{users}->{biguserpics}{$nick} = $2;
  252. }
  253. if ($line =~ /link=(["'])(.+?)\1/) {
  254. $self->{users}->{userlinks}{$nick} = $2;
  255. }
  256. if ($line =~ /ignore=(["'])Y\1/i) {
  257. add_ignore($nick);
  258. }
  259. if ($line =~ /sex=(["'])([MmFfBb])\1/) {
  260. $self->{users}->{sex}{$nick} = lc($2);
  261. }
  262. } elsif ($line =~ /<link(.*)>/) {
  263. if ($line =~ /url=(["'])(.+?)\1/) {
  264. my $url = $2;
  265. if ($line =~ /ignore="Y"/i) {
  266. add_url_ignore($url);
  267. }
  268. } else {
  269. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No URL specified\n";
  270. }
  271. } elsif ($line =~ /<set(.*)>/) {
  272. my $settings = $1;
  273. if ($settings !~ /=["'](.*)["']/ || $settings =~ /(\w)>/ ) {
  274. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing or wrong quotes near $1\n";
  275. }
  276. while ($settings =~ s/[ \t]([^=]+?)=(["'])(.*?)\2//) {
  277. my $var = lc($1);
  278. $var =~ s/ //; # Remove whitespace
  279. if (!defined($self->{cfg}->{$var})) {
  280. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No such configuration option: '$var'\n";
  281. next;
  282. }
  283. unless (($self->{cfg}->{$var} eq $3) || $self->{override_cfg}->{$var}) {
  284. $self->{cfg}->{$var} = $3;
  285. }
  286. }
  287. } elsif ($line =~ /<channel=(['"])(.+?)\1(.*)>/i) {
  288. my ($channel, $settings) = ($2, $3);
  289. $self->{chans}->{$channel}->{channel} = $channel;
  290. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
  291. while ($settings =~ s/\s([^=]+)=(["'])(.+?)\2//) {
  292. my $var = lc($1);
  293. if ($var eq "logdir" || $var eq "logfile") {
  294. push(@{$self->{chans}->{$channel}{$var}}, $3);
  295. } else {
  296. $self->{chans}->{$channel}{$var} = $3;
  297. }
  298. }
  299. while (<$fh>) {
  300. next if /^\s*#/;
  301. last if ($_ =~ /<\/*channel>/i);
  302. if ($_ =~ /^\s*(\w+)\s*=\s*(["'])(.+?)\2/) {
  303. my $var = lc($1);
  304. unless ((($var eq "logdir" || $var eq "logfile") && scalar(@{$self->{override_cfg}->{$var}}) > 0) || (($var ne "logdir" && $var ne "logfile") && $self->{override_cfg}->{$var})) {
  305. if($var eq "logdir" || $var eq "logfile") {
  306. push @{$self->{chans}->{$channel}{$var}}, $3;
  307. } else {
  308. $self->{chans}->{$channel}{$var} = $3;
  309. }
  310. }
  311. } elsif ($_ !~ /^$/) {
  312. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line: $_";
  313. }
  314. }
  315. } elsif ($line =~ /<include\s*=\s*(["'])(.+?)\1\s*>/) {
  316. my $include_cfg = $2;
  317. my $backup_cfg = $self->{cfg}->{configfile};
  318. $self->{cfg}->{configfile} = $include_cfg;
  319. my $r;
  320. if ((open(INCLUDE, $self->{cfg}->{configfile})
  321. or open(INCLUDE, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
  322. $r = $self->init_config(\*INCLUDE);
  323. } else {
  324. print STDERR "Warning: $backup_cfg, line $.: $self->{cfg}->{configfile}: $!\n";
  325. }
  326. print "Included config file: $self->{cfg}->{configfile}\n\n"
  327. if ($r && !$self->{cfg}->{silent});
  328. $self->{cfg}->{configfile} = $backup_cfg;
  329. } elsif ($line =~ /<(\w+)?.*[^>]$/) {
  330. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing end on element <$1 (probably multi-line?)\n";
  331. } elsif ($line =~ /\S/) {
  332. $line =~ s/\n//;
  333. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line: $line\n";
  334. }
  335. }
  336. close($fh);
  337. }
  338. sub init_pisg
  339. {
  340. my $self = shift;
  341. my $timestamp = time();
  342. $self->{cfg}->{start} = time();
  343. if ($self->{cfg}->{timeoffset} =~ /\+(\d+)/) {
  344. # We must plus some hours to the time
  345. $timestamp += 3600 * $1; # 3600 seconds per hour
  346. } elsif ($self->{cfg}->{timeoffset} =~ /-(\d+)/) {
  347. # We must remove some hours from the time
  348. $timestamp -= 3600 * $1; # 3600 seconds per hour
  349. }
  350. $self->{cfg}->{timestamp} = $timestamp;
  351. $self->{cfg}->{lang} = uc $self->{cfg}->{lang};
  352. $self->{cfg}->{lang} =~ s/-/_/g; # PT_BR was called PT-BR before
  353. # convert wordlists
  354. $self->{cfg}->{foulwords} = wordlist_regexp($self->{cfg}->{foulwords}, $self->{cfg}->{regexpaliases});
  355. $self->{cfg}->{ignorewords} = wordlist_regexp($self->{cfg}->{ignorewords}, $self->{cfg}->{regexpaliases});
  356. $self->{cfg}->{violentwords} = wordlist_regexp($self->{cfg}->{violentwords}, $self->{cfg}->{regexpaliases});
  357. # Add trailing slash when it's not there..
  358. $self->{cfg}->{imagepath} =~ s/([^\/])$/$1\//;
  359. # Set ImageGlobPath default
  360. $self->{cfg}->{imageglobpath} ||= $self->{cfg}->{imagepath};
  361. $self->{cfg}->{imageglobpath} =~ s/([^\/])$/$1\//;
  362. # Set number of picture columns to show
  363. if ($self->{cfg}->{userpics} =~ /^n/i) {
  364. $self->{cfg}->{userpics} = 0;
  365. } elsif ($self->{cfg}->{userpics} =~ /^y/i) {
  366. $self->{cfg}->{userpics} = 1;
  367. } elsif ($self->{cfg}->{userpics} !~ /^\d+$/) {
  368. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Invalid UserPics setting\n";
  369. }
  370. unless ($self->{cfg}->{silent}) {
  371. print "Statistics for channel $self->{cfg}->{channel} \@ $self->{cfg}->{network} by $self->{cfg}->{maintainer}\n\n";
  372. die sprintf "No such language: %s\n", $self->{cfg}->{lang}
  373. unless $self->{tmps}->{$self->{cfg}->{lang}};
  374. print "Using language template: $self->{cfg}->{lang}\n\n" if $self->{cfg}->{lang} ne 'EN';
  375. }
  376. }
  377. sub do_channel
  378. {
  379. my $self = shift;
  380. if (!$self->{cfg}->{channel}) {
  381. print STDERR "No channels defined.\n";
  382. } elsif ((!@{$self->{cfg}->{logfile}}) && (!@{$self->{cfg}->{logdir}})) {
  383. print STDERR "No logfile or logdir defined for " . $self->{cfg}->{channel} . "\n";
  384. } elsif (!$self->{cfg}->{format}) {
  385. print STDERR "No format defined for $self->{cfg}->{channel}.\n";
  386. } else {
  387. $self->init_pisg(); # Init some general things
  388. store_aliases(); # Save the aliases so we can restore them
  389. # later, we don't want to add the aliases
  390. # for this channel to the next channel
  391. # Pick our stats generator.
  392. my $analyzer;
  393. eval <<_END;
  394. use Pisg::Parser::$self->{cfg}->{logtype};
  395. \$analyzer = new Pisg::Parser::$self->{cfg}->{logtype}(
  396. { cfg => \$self->{cfg}, users => \$self->{users} }
  397. );
  398. _END
  399. if ($@) {
  400. print STDERR "Could not load stats analyzer for '$self->{cfg}->{logtype}': $@\n";
  401. return undef;
  402. }
  403. my $stats = $analyzer->analyze();
  404. $self->{cfg}->{analyzer} = $analyzer; # we need the parser in _format_line
  405. # Initialize HTMLGenerator object
  406. my $generator;
  407. eval <<_END;
  408. use Pisg::HTMLGenerator;
  409. \$generator = new Pisg::HTMLGenerator(
  410. cfg => \$self->{cfg},
  411. stats => \$stats,
  412. users => \$self->{users},
  413. tmps => \$self->{tmps}
  414. );
  415. _END
  416. if ($@) {
  417. print STDERR "Could not load stats generator (Pisg::HTMLGenerator): $@\n";
  418. return undef;
  419. }
  420. # Create our HTML page if the logfile has any data.
  421. if (defined $stats) {
  422. if ($stats->{parsedlines} > 0) {
  423. $generator->create_output();
  424. } else {
  425. print STDERR <<_END unless $self->{cfg}->{silent};
  426. No parseable lines found in logfile ($stats->{totallines} total lines). Skipping.
  427. -> You might be using the wrong format.
  428. -> A common error is that the logs do not contain timestamps for each line.
  429. _END
  430. }
  431. }
  432. restore_aliases();
  433. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1;
  434. }
  435. }
  436. sub parse_channels
  437. {
  438. my $self = shift;
  439. my %origcfg = %{ $self->{cfg} };
  440. # make a list of channels to do
  441. my @chanlist;
  442. if (scalar @ {$self->{cfg}->{cchannels} } > 0) {
  443. @chanlist = @{ $self->{cfg}->{cchannels} };
  444. } else {
  445. @chanlist = keys %{ $self->{chans} };
  446. }
  447. foreach my $channel (@chanlist) {
  448. if (!defined $self->{chans}->{$channel}) {
  449. print STDERR "Channel $channel not in config file, ignoring\n";
  450. next;
  451. }
  452. foreach (keys %{ $self->{chans}->{$channel} }) { # import channel specific config
  453. $self->{cfg}->{$_} = $self->{chans}->{$channel}{$_};
  454. }
  455. $self->do_channel();
  456. $origcfg{chan_done} = $self->{cfg}->{chan_done};
  457. %{ $self->{cfg} } = %origcfg;
  458. }
  459. }
  460. 1;
  461. __END__
  462. =head1 NAME
  463. Pisg - Perl IRC Statistics Generator main module
  464. =head1 SYNOPSIS
  465. use Pisg;
  466. $pisg = new Pisg(
  467. use_configfile => '1',
  468. override_cfg => { network => 'MyNetwork', format => 'eggdrop' }
  469. );
  470. $pisg->run();
  471. =head1 DESCRIPTION
  472. C<Pisg> is a statistic generator for IRC logfiles or the like, delivering
  473. the results in a HTML page.
  474. =head1 CONSTRUCTOR
  475. =over 4
  476. =item new ( [ OPTIONS ] )
  477. This is the constructor for a new Pisg object. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  478. Possible options are:
  479. B<use_configfile> - When set to 1, pisg will look up it's channels in it's
  480. configuration file, defined by the configuration option 'configfile'.
  481. B<override_cfg> - This defines whichever configuration variables you want to
  482. override from the configuration file. If you set use_configfile to 0, then
  483. you'll have to set at least channel and logfile here.
  484. 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.
  485. =back
  486. =head1 AUTHOR
  487. Morten Brix Pedersen <morten@wtf.dk>
  488. =head1 COPYRIGHT
  489. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  490. This program is free software; you can redistribute it and/or modify it
  491. under the terms of the GPL, license is included with the distribution of
  492. this file.
  493. =cut