Pisg.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. tmps => {},
  32. };
  33. # Set the default configuration settings.
  34. get_default_config_settings($self);
  35. # Import common functions in Pisg::Common
  36. require Pisg::Common;
  37. Pisg::Common->import();
  38. bless($self, $type);
  39. return $self;
  40. }
  41. sub run
  42. {
  43. my $self = shift;
  44. print "pisg v$self->{cfg}->{version} - Perl IRC Statistics Generator\n\n"
  45. unless ($self->{cfg}->{silent});
  46. # Init the configuration file (aliases, ignores, channels, etc)
  47. my $r = $self->init_config()
  48. if ($self->{use_configfile});
  49. print "Using config file: $self->{cfg}->{configfile}\n\n"
  50. if ($r && !$self->{cfg}->{silent});
  51. $self->init_words() # Init words. (Foulwords, ignorewords, etc.)
  52. if ($self->{use_configfile});
  53. # Get translations from langfile
  54. $self->get_language_templates();
  55. # Parse any channels in <channel> statements
  56. $self->parse_channels();
  57. # Optionaly parse the channel we were given in override_cfg.
  58. $self->do_channel()
  59. if (!$self->{cfg}->{chan_done}{$self->{cfg}->{channel}});
  60. }
  61. sub get_default_config_settings
  62. {
  63. my $self = shift;
  64. # This is all the default settings of pisg. They can be overriden by the
  65. # pisg.cfg file, or by using the override_cfg argument to the new
  66. # constructor.
  67. $self->{cfg} = {
  68. channel => '',
  69. logtype => 'Logfile',
  70. logfile => '',
  71. format => 'mIRC',
  72. network => 'SomeIRCNet',
  73. outputfile => 'index.html',
  74. maintainer => 'MAINTAINER',
  75. pagehead => 'none',
  76. pagefoot => 'none',
  77. configfile => 'pisg.cfg',
  78. imagepath => '',
  79. default_pic => '',
  80. logdir => '',
  81. lang => 'EN',
  82. langfile => 'lang.txt',
  83. prefix => '',
  84. silent => 0,
  85. userpics => 'y',
  86. # Colors / Layout
  87. bgcolor => '#dedeee',
  88. bgpic => '',
  89. text => 'black',
  90. hbgcolor => '#666699',
  91. hcolor => 'white',
  92. hicell => '#BABADD',
  93. hicell2 => '#CCCCCC',
  94. tdcolor => 'black',
  95. tdtop => '#C8C8DD',
  96. link => '#0b407a',
  97. vlink => '#0b407a',
  98. hlink => '#0b407a',
  99. bg_link => '#0b407a',
  100. bg_vlink => '#0b407a',
  101. bg_hlink => '#0b407a',
  102. headline => '#000000',
  103. rankc => '#CCCCCC',
  104. hi_rankc => '#AAAAAA',
  105. pic_width => '',
  106. pic_height => '',
  107. pic_v_0 => 'blue-v.png',
  108. pic_v_6 => 'green-v.png',
  109. pic_v_12 => 'yellow-v.png',
  110. pic_v_18 => 'red-v.png',
  111. pic_h_0 => 'blue-h.png',
  112. pic_h_6 => 'green-h.png',
  113. pic_h_12 => 'yellow-h.png',
  114. pic_h_18 => 'red-h.png',
  115. pic_loc => '.',
  116. # Stats settings
  117. show_activetimes => 1,
  118. show_bignumbers => 1,
  119. show_topics => 1,
  120. show_linetime => 0,
  121. show_time => 1,
  122. show_words => 0,
  123. show_wpl => 0,
  124. show_cpl => 0,
  125. show_lastseen => 0,
  126. show_legend => 1,
  127. show_kickline => 1,
  128. show_actionline => 1,
  129. show_shoutline => 1,
  130. show_violentlines => 1,
  131. show_randquote => 1,
  132. show_muw => 1,
  133. show_mrn => 1,
  134. show_mru => 1,
  135. show_voices => 0,
  136. # Less important things
  137. timeoffset => '+0',
  138. use_activetime_alt => 0,
  139. minquote => 25,
  140. maxquote => 65,
  141. wordlength => 5,
  142. activenicks => 25,
  143. activenicks2 => 30,
  144. topichistory => 3,
  145. urlhistory => 5,
  146. nickhistory => 5,
  147. wordhistory => 10,
  148. nicktracking => 0,
  149. charset => 'iso-8859-1',
  150. # Misc settings
  151. foul => 'ass fuck bitch shit scheisse scheiße kacke arsch ficker ficken schlampe',
  152. violent => 'slaps beats smacks',
  153. ignorewords => '',
  154. tablewidth => 614,
  155. regexp_aliases => 0,
  156. # Developer stuff
  157. version => "0.35-cvs",
  158. };
  159. # Parse the optional overriden configuration variables
  160. foreach my $key (keys %{$self->{override_cfg}}) {
  161. if ($self->{override_cfg}->{$key}) {
  162. $self->{cfg}->{$key} = $self->{override_cfg}->{$key};
  163. }
  164. }
  165. }
  166. sub get_language_templates
  167. {
  168. my $self = shift;
  169. 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");
  170. while (my $line = <FILE>)
  171. {
  172. next if ($line =~ /^#/);
  173. if ($line =~ /<lang name=\"([^"]+)\">/) {
  174. # Found start tag, setting the current language
  175. my $current_lang = $1;
  176. while (<FILE>) {
  177. last if ($_ =~ /<\/lang>/i);
  178. # Get 'template = "Text"' in language file:
  179. if ($_ =~ /(\w+)\s+=\s+"(.*)"\s*$/) {
  180. $self->{tmps}->{$current_lang}{$1} = $2;
  181. }
  182. }
  183. }
  184. }
  185. close(FILE);
  186. }
  187. sub init_words
  188. {
  189. my $self = shift;
  190. $self->{cfg}->{foul} =~ s/(^\s+|\s+$)//g;
  191. $self->{cfg}->{foul} =~ s/\s+/|/g;
  192. foreach (split /\s+/, $self->{cfg}->{ignorewords}) {
  193. $self->{cfg}->{ignoreword}{$_} = 1;
  194. }
  195. $self->{cfg}->{violent} =~ s/(^\s+|\s+$)//g;
  196. $self->{cfg}->{violent} =~ s/\s+/|/g;
  197. }
  198. sub init_config
  199. {
  200. my $self = shift;
  201. if ((open(CONFIG, $self->{cfg}->{configfile}) or open(CONFIG, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
  202. while (my $line = <CONFIG>)
  203. {
  204. next if ($line =~ /^#/);
  205. chomp $line;
  206. if ($line =~ /<user.*>/) {
  207. my $nick;
  208. if ($line =~ /nick="([^"]+)"/) {
  209. $nick = $1;
  210. add_alias($nick, $nick);
  211. } else {
  212. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No nick specified\n";
  213. next;
  214. }
  215. if ($line =~ /alias="([^"]+)"/) {
  216. my @thisalias = split(/\s+/, lc($1));
  217. foreach (@thisalias) {
  218. if ($self->{cfg}->{regexp_aliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
  219. add_aliaswild($nick, $_);
  220. } elsif (not $self->{cfg}->{regexp_aliases} and s/\*/\.\*/g) {
  221. # quote it if it is a wildcard
  222. s/([\|\[\]\{\}\(\)\?\+\^\\])/\\$1/g;
  223. add_aliaswild($nick, $_);
  224. } else {
  225. add_alias($nick, $_);
  226. }
  227. }
  228. }
  229. if ($line =~ /pic="([^"]+)"/) {
  230. $self->{users}->{userpics}{$nick} = $1;
  231. }
  232. if ($line =~ /bigpic="([^"]+)"/) {
  233. $self->{users}->{biguserpics}{$nick} = $1;
  234. }
  235. if ($line =~ /link="([^"]+)"/) {
  236. $self->{users}->{userlinks}{$nick} = $1;
  237. }
  238. if ($line =~ /ignore="Y"/i) {
  239. add_ignore($nick);
  240. }
  241. if ($line =~ /sex="([MmFf])"/i) {
  242. $self->{users}->{sex}{$nick} = lc($1);
  243. }
  244. } elsif ($line =~ /<link(.*)>/) {
  245. my $url;
  246. if ($line =~ /url="([^"]+)"/) {
  247. $url = $1;
  248. if ($line =~ /ignore="Y"/i) {
  249. add_url_ignore($url);
  250. }
  251. } else {
  252. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No URL specified\n";
  253. }
  254. } elsif ($line =~ /<set(.*)>/) {
  255. my $settings = $1;
  256. if ($settings !~ /=["'](.*)["']/ || $settings =~ /(\w)>/ ) {
  257. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing or wrong quotes near $1\n";
  258. }
  259. while ($settings =~ s/[ \t]([^=]+)=["']([^"']*)["']//) {
  260. my $var = lc($1);
  261. $var =~ s/ //; # Remove whitespace
  262. if (!defined($self->{cfg}->{$var})) {
  263. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No such configuration option: '$var'\n";
  264. next;
  265. }
  266. unless (($self->{cfg}->{$var} eq $2) || $self->{override_cfg}->{$var}) {
  267. $self->{cfg}->{$var} = $2;
  268. }
  269. }
  270. } elsif ($line =~ /<channel=['"]([^'"]+)['"](.*)>/i) {
  271. my ($channel, $settings) = ($1, $2);
  272. $self->{chans}->{$channel}->{channel} = $channel;
  273. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
  274. while ($settings =~ s/\s([^=]+)=["']([^"']*)["']//) {
  275. my $var = lc($1);
  276. $self->{chans}->{$channel}{$var} = $2;
  277. }
  278. while (<CONFIG>) {
  279. last if ($_ =~ /<\/*channel>/i);
  280. if ($_ =~ /^\s*(\w+)\s*=\s*["']([^"']*)["']/) {
  281. my $var = lc($1);
  282. unless ($self->{override_cfg}->{$var}) {
  283. $self->{chans}->{$channel}{$var} = $2;
  284. }
  285. } else {
  286. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
  287. }
  288. }
  289. } elsif ($line =~ /<(\w+)?.*[^>]$/) {
  290. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing end on element <$1 (probably multi-line?)\n";
  291. } elsif ($line =~ /\S/) {
  292. $line =~ s/\n//;
  293. print "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
  294. }
  295. }
  296. close(CONFIG);
  297. }
  298. }
  299. sub init_pisg
  300. {
  301. my $self = shift;
  302. my $timestamp = time();
  303. $self->{cfg}->{start} = time();
  304. if ($self->{cfg}->{timeoffset} =~ /\+(\d+)/) {
  305. # We must plus some hours to the time
  306. $timestamp += 3600 * $1; # 3600 seconds per hour
  307. } elsif ($self->{cfg}->{timeoffset} =~ /-(\d+)/) {
  308. # We must remove some hours from the time
  309. $timestamp -= 3600 * $1; # 3600 seconds per hour
  310. }
  311. $self->{cfg}->{timestamp} = $timestamp;
  312. # Add trailing slash when it's not there..
  313. $self->{cfg}->{imagepath} =~ s/([^\/])$/$1\//;
  314. print "Using language template: $self->{cfg}->{lang}\n\n" if ($self->{cfg}->{lang} ne 'EN' && !$self->{cfg}->{silent});
  315. print "Statistics for channel $self->{cfg}->{channel} \@ $self->{cfg}->{network} by $self->{cfg}->{maintainer}\n\n"
  316. unless ($self->{cfg}->{silent});
  317. }
  318. sub do_channel
  319. {
  320. my $self = shift;
  321. if (!$self->{cfg}->{channel}) {
  322. print STDERR "No channels defined.\n";
  323. } elsif ((!$self->{cfg}->{logfile}) && (!$self->{cfg}->{logdir})) {
  324. print STDERR "No logfile or logdir defined for " . $self->{cfg}->{channel} . "\n";
  325. } else {
  326. $self->init_pisg(); # Init some general things
  327. # Pick our stats generator.
  328. my $analyzer;
  329. eval <<_END;
  330. use Pisg::Parser::$self->{cfg}->{logtype};
  331. \$analyzer = new Pisg::Parser::$self->{cfg}->{logtype}(
  332. cfg => \$self->{cfg}
  333. );
  334. _END
  335. if ($@) {
  336. print STDERR "Could not load stats analyzer for '$self->{cfg}->{logtype}': $@\n";
  337. return undef;
  338. }
  339. my $stats = $analyzer->analyze();
  340. # Initialize HTMLGenerator object
  341. my $generator;
  342. eval <<_END;
  343. use Pisg::HTMLGenerator;
  344. \$generator = new Pisg::HTMLGenerator(
  345. cfg => \$self->{cfg},
  346. stats => \$stats,
  347. users => \$self->{users},
  348. tmps => \$self->{tmps}
  349. );
  350. _END
  351. if ($@) {
  352. print STDERR "Could not load stats generator (Pisg::HTMLGenerator): $@\n";
  353. return undef;
  354. }
  355. # Create our HTML page if the logfile has any data.
  356. if (defined $stats and $stats->{totallines} > 0) {
  357. $generator->create_html();
  358. } elsif ($stats->{totallines} == 0) {
  359. print STDERR "No lines found in logfile.. skipping.\n";
  360. }
  361. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1;
  362. }
  363. }
  364. sub parse_channels
  365. {
  366. my $self = shift;
  367. my %origcfg = %{ $self->{cfg} };
  368. foreach my $channel (keys %{ $self->{chans} }) {
  369. foreach (keys %{ $self->{chans}->{$channel} }) {
  370. $self->{cfg}->{$_} = $self->{chans}->{$channel}{$_};
  371. }
  372. $self->do_channel();
  373. $origcfg{chan_done} = $self->{cfg}->{chan_done};
  374. %{ $self->{cfg} } = %origcfg;
  375. }
  376. }
  377. 1;
  378. __END__
  379. =head1 NAME
  380. Pisg - Perl IRC Statistics Generator main module
  381. =head1 SYNOPSIS
  382. use Pisg;
  383. $pisg = new Pisg(
  384. use_configfile => '1',
  385. override_cfg => { network => 'MyNetwork', format => 'eggdrop' }
  386. );
  387. $pisg->run();
  388. =head1 DESCRIPTION
  389. C<Pisg> is a statistic generator for IRC logfiles or the like, delivering
  390. the results in a HTML page.
  391. =head1 CONSTRUCTOR
  392. =over 4
  393. =item new ( [ OPTIONS ] )
  394. This is the constructor for a new Pisg object. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  395. Possible options are:
  396. B<use_configfile> - When set to 1, pisg will look up it's channels in it's
  397. configuration file, defined by the configuration option 'configfile'.
  398. B<override_cfg> - This defines whichever configuration variables you want to
  399. override from the configuration file. If you set use_configfile to 0, then
  400. you'll have to set at least channel and logfile here.
  401. 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.
  402. =back
  403. =head1 AUTHOR
  404. Morten Brix Pedersen <morten@wtf.dk>
  405. =head1 COPYRIGHT
  406. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  407. This program is free software; you can redistribute it and/or modify it
  408. under the terms of the GPL, license is included with the distribution of
  409. this file.
  410. =cut