4
0

Pisg.pm 20 KB

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