Pisg.pm 18 KB

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