Pisg.pm 20 KB

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