Pisg.pm 19 KB

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