Pisg.pm 18 KB

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