Pisg.pm 15 KB

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