Pisg.pm 19 KB

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