Pisg.pm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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.67",
  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. next if ($_ =~ /^#/);
  214. last if ($_ =~ /<\/lang>/i);
  215. # Get 'template = "Text"' in language file:
  216. if ($_ =~ /^(\w+)\s*=\s*"(.*)"\s*$/) {
  217. warn "duplicate translation $1 -> $2"
  218. if $self->{tmps}->{$current_lang}{$1} and !$self->{cfg}->{silent};
  219. $self->{tmps}->{$current_lang}{$1} = $2;
  220. }
  221. }
  222. }
  223. }
  224. close(FILE);
  225. }
  226. sub init_config
  227. {
  228. my $self = shift;
  229. my $fh = shift;
  230. while (my $line = <$fh>)
  231. {
  232. next if ($line =~ /^\s*#/);
  233. chomp $line;
  234. if ($line =~ /<user.*>/) {
  235. my $nick;
  236. if ($line =~ /nick=(["'])(.+?)\1/) {
  237. $nick = $2;
  238. add_alias($nick, $nick);
  239. } else {
  240. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No nick specified\n";
  241. next;
  242. }
  243. if ($line =~ /alias=(["'])(.+?)\1/) {
  244. my @thisalias = split(/\s+/, lc($2));
  245. foreach (@thisalias) {
  246. if ($self->{cfg}->{regexpaliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
  247. add_aliaswild($nick, $_);
  248. } elsif (not $self->{cfg}->{regexpaliases} and s/\*/\.\*/g) {
  249. # quote it if it is a wildcard
  250. s/([\|\[\]\{\}\(\)\?\+\^\\])/\\$1/g;
  251. add_aliaswild($nick, $_);
  252. } else {
  253. add_alias($nick, $_);
  254. }
  255. }
  256. }
  257. if ($line =~ /pic=(["'])(.+?)\1/) {
  258. $self->{users}->{userpics}{$nick} = $2;
  259. }
  260. if ($line =~ /bigpic=(["'])(.+?)\1/) {
  261. $self->{users}->{biguserpics}{$nick} = $2;
  262. }
  263. if ($line =~ /link=(["'])(.+?)\1/) {
  264. $self->{users}->{userlinks}{$nick} = $2;
  265. }
  266. if ($line =~ /ignore=(["'])Y\1/i) {
  267. add_ignore($nick);
  268. }
  269. if ($line =~ /sex=(["'])([MmFfBb])\1/) {
  270. $self->{users}->{sex}{$nick} = lc($2);
  271. }
  272. } elsif ($line =~ /<link(.*)>/) {
  273. if ($line =~ /url=(["'])(.+?)\1/) {
  274. my $url = $2;
  275. if ($line =~ /ignore="Y"/i) {
  276. add_url_ignore($url);
  277. }
  278. } else {
  279. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No URL specified\n";
  280. }
  281. } elsif ($line =~ /<set(.*)>/) {
  282. my $settings = $1;
  283. if ($settings !~ /=["'](.*)["']/ || $settings =~ /(\w)>/ ) {
  284. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing or wrong quotes near $1\n";
  285. }
  286. while ($settings =~ s/[ \t]([^=]+?)=(["'])(.*?)\2//) {
  287. my $var = lc($1);
  288. my $val = $3;
  289. $var =~ s/ //; # Remove whitespace
  290. if (!defined($self->{cfg}->{$var})) {
  291. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No such configuration option: '$var'\n";
  292. next;
  293. }
  294. unless (($self->{cfg}->{$var} eq $val) || $self->{override_cfg}->{$var}) {
  295. $self->{cfg}->{$var} = $val;
  296. }
  297. }
  298. } elsif ($line =~ /<channel=(['"])(.+?)\1(.*)>/i) {
  299. my ($channel, $settings, $tmp) = ($2, $3, {});
  300. $tmp->{$channel}->{channel} = $channel;
  301. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
  302. while ($settings =~ s/\s([^=]+)=(["'])(.*?)\2//) {
  303. my $var = lc($1);
  304. my $val = $3;
  305. if ($var eq "logdir" || $var eq "logfile") {
  306. push(@{$tmp->{$channel}{$var}}, $val);
  307. } else {
  308. $tmp->{$channel}{$var} = $val;
  309. }
  310. }
  311. while (<$fh>) {
  312. next if /^\s*#/;
  313. if ($_ =~ /<\/*channel>/i) {
  314. push @{ $self->{chans} }, $tmp;
  315. last;
  316. }
  317. if ($_ =~ /^\s*(\w+)\s*=\s*(["'])(.*?)\2/) {
  318. my $var = lc($1);
  319. my $val = $3;
  320. unless ((($var eq "logdir" || $var eq "logfile") && scalar(@{$self->{override_cfg}->{$var}}) > 0) || (($var ne "logdir" && $var ne "logfile") && $self->{override_cfg}->{$var})) {
  321. if($var eq "logdir" || $var eq "logfile") {
  322. push @{$tmp->{$channel}{$var}}, $val;
  323. } else {
  324. $tmp->{$channel}{$var} = $val;
  325. }
  326. }
  327. } elsif ($_ !~ /^$/) {
  328. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line: $_";
  329. }
  330. }
  331. } elsif ($line =~ /<include\s*=\s*(["'])(.+?)\1\s*>/) {
  332. my $include_cfg = $2;
  333. my $backup_cfg = $self->{cfg}->{configfile};
  334. $self->{cfg}->{configfile} = $include_cfg;
  335. my $r;
  336. if ((open(INCLUDE, $self->{cfg}->{configfile})
  337. or open(INCLUDE, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
  338. $r = $self->init_config(\*INCLUDE);
  339. } else {
  340. print STDERR "Warning: $backup_cfg, line $.: $self->{cfg}->{configfile}: $!\n";
  341. }
  342. print "Included config file: $self->{cfg}->{configfile}\n\n"
  343. if ($r && !$self->{cfg}->{silent});
  344. $self->{cfg}->{configfile} = $backup_cfg;
  345. } elsif ($line =~ /<(\w+)?.*[^>]$/) {
  346. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing end on element <$1 (probably multi-line?)\n";
  347. } elsif ($line =~ /\S/) {
  348. $line =~ s/\n//;
  349. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line: $line\n";
  350. }
  351. }
  352. close($fh);
  353. }
  354. sub init_pisg
  355. {
  356. my $self = shift;
  357. my $timestamp = time();
  358. $self->{cfg}->{start} = time();
  359. if ($self->{cfg}->{timeoffset} =~ /\+(\d+)/) {
  360. # We must plus some hours to the time
  361. $timestamp += 3600 * $1; # 3600 seconds per hour
  362. } elsif ($self->{cfg}->{timeoffset} =~ /-(\d+)/) {
  363. # We must remove some hours from the time
  364. $timestamp -= 3600 * $1; # 3600 seconds per hour
  365. }
  366. $self->{cfg}->{timestamp} = $timestamp;
  367. # convert wordlists
  368. $self->{cfg}->{foulwords} = wordlist_regexp($self->{cfg}->{foulwords}, $self->{cfg}->{regexpaliases});
  369. $self->{cfg}->{ignorewords} = wordlist_regexp($self->{cfg}->{ignorewords}, $self->{cfg}->{regexpaliases});
  370. $self->{cfg}->{violentwords} = wordlist_regexp($self->{cfg}->{violentwords}, $self->{cfg}->{regexpaliases});
  371. # Add trailing slash when it's not there..
  372. $self->{cfg}->{imagepath} =~ s/([^\/])$/$1\//;
  373. # Set ImageGlobPath default
  374. $self->{cfg}->{imageglobpath} ||= $self->{cfg}->{imagepath};
  375. $self->{cfg}->{imageglobpath} =~ s/([^\/])$/$1\//;
  376. # Set number of picture columns to show
  377. if ($self->{cfg}->{userpics} =~ /^n/i) {
  378. $self->{cfg}->{userpics} = 0;
  379. } elsif ($self->{cfg}->{userpics} =~ /^y/i) {
  380. $self->{cfg}->{userpics} = 1;
  381. } elsif ($self->{cfg}->{userpics} !~ /^\d+$/) {
  382. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Invalid UserPics setting\n";
  383. }
  384. unless ($self->{cfg}->{silent}) {
  385. print "Statistics for channel $self->{cfg}->{channel} \@ $self->{cfg}->{network} by $self->{cfg}->{maintainer}\n\n";
  386. }
  387. }
  388. sub do_channel
  389. {
  390. my $self = shift;
  391. if (!$self->{cfg}->{channel}) {
  392. print STDERR "No channels defined.\n";
  393. } elsif ((!@{$self->{cfg}->{logfile}}) && (!@{$self->{cfg}->{logdir}})) {
  394. print STDERR "No logfile or logdir defined for " . $self->{cfg}->{channel} . "\n";
  395. } elsif (!$self->{cfg}->{format}) {
  396. print STDERR "No format defined for $self->{cfg}->{channel}.\n";
  397. } else {
  398. $self->init_pisg(); # Init some general things
  399. store_aliases(); # Save the aliases so we can restore them
  400. # later, we don't want to add the aliases
  401. # for this channel to the next channel
  402. # Pick our stats generator.
  403. my $analyzer;
  404. eval <<_END;
  405. use Pisg::Parser::$self->{cfg}->{logtype};
  406. \$analyzer = new Pisg::Parser::$self->{cfg}->{logtype}(
  407. { cfg => \$self->{cfg}, users => \$self->{users} }
  408. );
  409. _END
  410. if ($@) {
  411. print STDERR "Could not load stats analyzer for '$self->{cfg}->{logtype}': $@\n";
  412. return undef;
  413. }
  414. my $stats = $analyzer->analyze();
  415. $self->{cfg}->{analyzer} = $analyzer; # we need the parser in _format_line
  416. # Initialize HTMLGenerator object
  417. my $generator;
  418. eval <<_END;
  419. use Pisg::HTMLGenerator;
  420. \$generator = new Pisg::HTMLGenerator(
  421. cfg => \$self->{cfg},
  422. stats => \$stats,
  423. users => \$self->{users},
  424. tmps => \$self->{tmps}
  425. );
  426. _END
  427. if ($@) {
  428. print STDERR "Could not load stats generator (Pisg::HTMLGenerator): $@\n";
  429. return undef;
  430. }
  431. # Create our HTML page if the logfile has any data.
  432. if (defined $stats) {
  433. if ($stats->{parsedlines} > 0) {
  434. foreach my $lang (split /\s*,\s*/, uc $self->{cfg}->{lang}) {
  435. $lang =~ s/-/_/g; # PT_BR was called PT-BR before
  436. die sprintf "No such language: %s\n", $_ unless $self->{tmps}->{$lang};
  437. $generator->create_output($lang);
  438. }
  439. } else {
  440. print STDERR <<_END unless $self->{cfg}->{silent};
  441. No parseable lines found in logfile ($stats->{totallines} total lines). Skipping.
  442. -> You might be using the wrong format.
  443. -> A common error is that the logs do not contain timestamps for each line.
  444. _END
  445. }
  446. }
  447. restore_aliases();
  448. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1;
  449. }
  450. }
  451. sub parse_channels
  452. {
  453. my $self = shift;
  454. my %origcfg = %{ $self->{cfg} };
  455. # make a list of channels to do
  456. my @chanlist;
  457. if (scalar @ {$self->{cfg}->{cchannels} } > 0) {
  458. foreach my $channel (@{ $self->{cfg}->{cchannels} }) {
  459. my $hits = 0;
  460. foreach ( @{ $self->{chans} }) {
  461. my $chan = (keys %{ $_ })[0];
  462. if ($channel =~ m/^$chan$/i) {
  463. push @chanlist, $_;
  464. $hits++;
  465. }
  466. }
  467. if ($hits < 1) {
  468. print STDERR "Channel $channel not in config file, ignoring\n";
  469. }
  470. }
  471. } else {
  472. push @chanlist, $_ foreach (@{ $self->{chans} });
  473. }
  474. foreach my $channel (@chanlist) {
  475. foreach my $chan (keys %{ $channel }) { # import channel specific config
  476. $self->{cfg}->{$_} = $channel->{$chan}->{$_} foreach (keys %{ $channel->{$chan} });
  477. }
  478. $self->do_channel();
  479. $origcfg{chan_done} = $self->{cfg}->{chan_done};
  480. %{ $self->{cfg} } = %origcfg;
  481. }
  482. }
  483. 1;
  484. __END__
  485. =head1 NAME
  486. Pisg - Perl IRC Statistics Generator main module
  487. =head1 SYNOPSIS
  488. use Pisg;
  489. $pisg = new Pisg(
  490. use_configfile => '1',
  491. override_cfg => { network => 'MyNetwork', format => 'eggdrop' }
  492. );
  493. $pisg->run();
  494. =head1 DESCRIPTION
  495. C<Pisg> is a statistic generator for IRC logfiles or the like, delivering
  496. the results in a HTML page.
  497. =head1 CONSTRUCTOR
  498. =over 4
  499. =item new ( [ OPTIONS ] )
  500. This is the constructor for a new Pisg object. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  501. Possible options are:
  502. B<use_configfile> - When set to 1, pisg will look up it's channels in it's
  503. configuration file, defined by the configuration option 'configfile'.
  504. B<override_cfg> - This defines whichever configuration variables you want to
  505. override from the configuration file. If you set use_configfile to 0, then
  506. you'll have to set at least channel and logfile here.
  507. 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.
  508. =back
  509. =head1 AUTHOR
  510. Morten Brix Pedersen <morten@wtf.dk>
  511. =head1 COPYRIGHT
  512. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  513. This program is free software; you can redistribute it and/or modify it
  514. under the terms of the GPL, license is included with the distribution of
  515. this file.
  516. =cut