Pisg.pm 19 KB

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