Pisg.pm 19 KB

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