Pisg.pm 18 KB

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