Pisg.pm 19 KB

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