Pisg.pm 18 KB

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