Pisg.pm 18 KB

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