Pisg.pm 19 KB

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