Pisg.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. if (!$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. if (!$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. default_pic => '',
  96. logdir => '',
  97. lang => 'EN',
  98. langfile => 'lang.txt',
  99. prefix => '',
  100. silent => 0,
  101. userpics => 'y',
  102. # Colors / Layout
  103. bgcolor => '#dedeee',
  104. bgpic => '',
  105. text => 'black',
  106. hbgcolor => '#666699',
  107. hcolor => 'white',
  108. hicell => '#BABADD',
  109. hicell2 => '#CCCCCC',
  110. tdcolor => 'black',
  111. tdtop => '#C8C8DD',
  112. link => '#0b407a',
  113. vlink => '#0b407a',
  114. hlink => '#0b407a',
  115. bg_link => '#0b407a',
  116. bg_vlink => '#0b407a',
  117. bg_hlink => '#0b407a',
  118. headline => '#000000',
  119. rankc => '#CCCCCC',
  120. hi_rankc => '#AAAAAA',
  121. pic_width => '',
  122. pic_height => '',
  123. pic_v_0 => 'blue-v.png',
  124. pic_v_6 => 'green-v.png',
  125. pic_v_12 => 'yellow-v.png',
  126. pic_v_18 => 'red-v.png',
  127. pic_h_0 => 'blue-h.png',
  128. pic_h_6 => 'green-h.png',
  129. pic_h_12 => 'yellow-h.png',
  130. pic_h_18 => 'red-h.png',
  131. pic_loc => '.',
  132. # Stats settings
  133. show_activetimes => 1,
  134. show_bignumbers => 1,
  135. show_linetime => 0,
  136. show_time => 1,
  137. show_words => 0,
  138. show_wpl => 0,
  139. show_cpl => 0,
  140. show_lastseen => 0,
  141. show_legend => 1,
  142. show_kickline => 1,
  143. show_actionline => 1,
  144. show_shoutline => 1,
  145. show_violentlines => 1,
  146. show_randquote => 1,
  147. show_muw => 1,
  148. show_mrn => 1,
  149. show_mru => 1,
  150. show_voices => 0,
  151. # Less important things
  152. timeoffset => '+0',
  153. minquote => 25,
  154. maxquote => 65,
  155. wordlength => 5,
  156. activenicks => 25,
  157. activenicks2 => 30,
  158. topichistory => 3,
  159. urlhistory => 5,
  160. nicktracking => 0,
  161. charset => 'iso-8859-1',
  162. # Misc settings
  163. foul => 'ass fuck bitch shit scheisse scheiße kacke arsch ficker ficken schlampe',
  164. violent => 'slaps beats smacks',
  165. ignorewords => '',
  166. tablewidth => 614,
  167. regexp_aliases => 0,
  168. # Developer stuff
  169. debug => 0,
  170. debugfile => 'debug.log',
  171. version => "v0.32-cvs",
  172. };
  173. # Parse the optional overriden configuration variables
  174. foreach my $key (keys %{$self->{override_cfg}}) {
  175. if ($self->{override_cfg}->{$key}) {
  176. $self->{cfg}->{$key} = $self->{override_cfg}->{$key};
  177. }
  178. }
  179. }
  180. sub get_language_templates
  181. {
  182. my $self = shift;
  183. 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");
  184. while (my $line = <FILE>)
  185. {
  186. next if ($line =~ /^#/);
  187. if ($line =~ /<lang name=\"([^"]+)\">/) {
  188. # Found start tag, setting the current language
  189. my $current_lang = $1;
  190. while (<FILE>) {
  191. last if ($_ =~ /<\/lang>/i);
  192. # Get 'template = "Text"' in language file:
  193. if ($_ =~ /(\w+)\s+=\s+"(.*)"\s*$/) {
  194. $self->{tmps}->{$current_lang}{$1} = $2;
  195. }
  196. }
  197. }
  198. }
  199. close(FILE);
  200. }
  201. sub init_debug
  202. {
  203. my $self = shift;
  204. $self->{cfg}->{debugstarted} = 1;
  205. if ($self->{cfg}->{debug}) {
  206. print "[ Debugging => $self->{cfg}->{debugfile} ]\n"
  207. unless ($self->{cfg}->{silent});
  208. open(DEBUG,"> $self->{cfg}->{debugfile}") or print STDERR "$0: Unable to open debug
  209. file($self->{cfg}->{debugfile}): $!\n";
  210. $self->{debug}->("*** pisg debug file for $self->{cfg}->{logfile}\n");
  211. if ($self->{cfg}->{debugqueue}) {
  212. print DEBUG $self->{cfg}->{debugqueue};
  213. delete $self->{cfg}->{debugqueue};
  214. }
  215. } else {
  216. $self->{debug} = sub {};
  217. }
  218. }
  219. sub close_debug
  220. {
  221. my $self = shift;
  222. if ($self->{cfg}->{debug}) {
  223. close(DEBUG) or print STDERR "$0: Cannot close debugfile($self->{cfg}->{debugfile}): $!\n";
  224. }
  225. }
  226. sub init_words
  227. {
  228. my $self = shift;
  229. $self->{cfg}->{foul} =~ s/(^\s+|\s+$)//g;
  230. $self->{cfg}->{foul} =~ s/\s+/|/g;
  231. foreach (split /\s+/, $self->{cfg}->{ignorewords}) {
  232. $self->{cfg}->{ignoreword}{$_} = 1;
  233. }
  234. $self->{cfg}->{violent} =~ s/(^\s+|\s+$)//g;
  235. $self->{cfg}->{violent} =~ s/\s+/|/g;
  236. }
  237. sub init_config
  238. {
  239. my $self = shift;
  240. if ((open(CONFIG, $self->{cfg}->{configfile}) or open(CONFIG, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
  241. while (my $line = <CONFIG>)
  242. {
  243. next if ($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: No nick specified in $self->{cfg}->{configfile} on line $.\n";
  251. next;
  252. }
  253. if ($line =~ /alias="([^"]+)"/) {
  254. my @thisalias = split(/\s+/, lc($1));
  255. foreach (@thisalias) {
  256. if ($self->{cfg}->{regexp_aliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
  257. add_aliaswild($nick, $_);
  258. } elsif (not $self->{cfg}->{regexp_aliases} 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 =~ /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. my $url;
  281. if ($line =~ /url="([^"]+)"/) {
  282. $url = $1;
  283. if ($line =~ /ignore="Y"/i) {
  284. add_url_ignore($url);
  285. }
  286. } else {
  287. print STDERR "Warning: no URL specified in $self->{cfg}->{configfile} on line $.\n";
  288. }
  289. } elsif ($line =~ /<set(.*)>/) {
  290. my $settings = $1;
  291. while ($settings =~ s/[ \t]([^=]+)=["']([^"']*)["']//) {
  292. my $var = lc($1); # Make the string lowercase
  293. if (!defined($self->{cfg}->{$var})) {
  294. print STDERR "Warning: no such configuration option: $1\n";
  295. next;
  296. }
  297. unless (($self->{cfg}->{$var} eq $2) || $self->{override_cfg}->{$var}) {
  298. $self->{cfg}->{$var} = $2;
  299. }
  300. $self->{debug}->("cfg: $var = $2");
  301. }
  302. } elsif ($line =~ /<channel=['"]([^'"]+)['"](.*)>/i) {
  303. my ($channel, $settings) = ($1, $2);
  304. $self->{chans}->{$channel}->{channel} = $channel;
  305. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
  306. while ($settings =~ s/\s([^=]+)=["']([^"']*)["']//) {
  307. my $var = lc($1);
  308. $self->{chans}->{$channel}{$var} = $2;
  309. $self->{debug}->("Channel cfg $channel: $var = $2");
  310. }
  311. while (<CONFIG>) {
  312. last if ($_ =~ /<\/*channel>/i);
  313. while ($_ =~ s/^\s*(\w+)\s*=\s*["']([^"']*)["']//) {
  314. my $var = lc($1);
  315. unless ($self->{override_cfg}->{$var}) {
  316. $self->{chans}->{$channel}{$var} = $2;
  317. }
  318. $self->{debug}->("Conf $channel: $var = $2");
  319. }
  320. }
  321. } elsif ($line =~ /<set/) {
  322. print STDERR "Warning: Malformed <set ..> line in $self->{cfg}->{configfile} on line $.\n";
  323. } elsif ($line =~ /<channel/) {
  324. print STDERR "Warning: Malformed <channel ..> line in $self->{cfg}->{configfile} on line $.\n";
  325. } elsif ($line =~ /\S/) {
  326. $line =~ s/\n//;
  327. print "Warning: Unrecognized line in $self->{cfg}->{configfile} on line $.: '$line'\n";
  328. }
  329. }
  330. close(CONFIG);
  331. }
  332. }
  333. sub init_pisg
  334. {
  335. my $self = shift;
  336. my $timestamp = time();
  337. $self->{cfg}->{start} = time();
  338. if ($self->{cfg}->{timeoffset} =~ /\+(\d+)/) {
  339. # We must plus some hours to the time
  340. $timestamp += 3600 * $1; # 3600 seconds per hour
  341. } elsif ($self->{cfg}->{timeoffset} =~ /-(\d+)/) {
  342. # We must remove some hours from the time
  343. $timestamp -= 3600 * $1; # 3600 seconds per hour
  344. }
  345. $self->{cfg}->{timestamp} = $timestamp;
  346. # Add trailing slash when it's not there..
  347. $self->{cfg}->{imagepath} =~ s/([^\/])$/$1\//;
  348. print "Using language template: $self->{cfg}->{lang}\n\n" if ($self->{cfg}->{lang} ne 'EN' && !$self->{cfg}->{silent});
  349. print "Statistics for channel $self->{cfg}->{channel} \@ $self->{cfg}->{network} by $self->{cfg}->{maintainer}\n\n"
  350. unless ($self->{cfg}->{silent});
  351. }
  352. sub do_channel
  353. {
  354. my $self = shift;
  355. if (!$self->{cfg}->{channel}) {
  356. print STDERR "No channels defined.\n";
  357. } elsif ((!$self->{cfg}->{logfile}) && (!$self->{cfg}->{logdir})) {
  358. print STDERR "No logfile or logdir defined for " . $self->{cfg}->{channel} . "\n";
  359. } else {
  360. $self->init_pisg(); # Init some general things
  361. # Pick our stats generator.
  362. my $analyzer;
  363. eval <<_END;
  364. use Pisg::Parser::$self->{cfg}->{logtype};
  365. \$analyzer = new Pisg::Parser::$self->{cfg}->{logtype}(
  366. cfg => \$self->{cfg},
  367. debug => \$self->{debug}
  368. );
  369. _END
  370. if ($@) {
  371. print STDERR "Could not load stats analyzer for '$self->{cfg}->{logtype}': $@\n";
  372. return undef;
  373. }
  374. my $stats = $analyzer->analyze();
  375. # Initialize HTMLGenerator object
  376. my $generator;
  377. eval <<_END;
  378. use Pisg::HTMLGenerator;
  379. \$generator = new Pisg::HTMLGenerator(
  380. cfg => \$self->{cfg},
  381. debug => \$self->{debug},
  382. stats => \$stats,
  383. users => \$self->{users},
  384. tmps => \$self->{tmps}
  385. );
  386. _END
  387. if ($@) {
  388. print STDERR "Could not load stats generator (Pisg::HTMLGenerator): $@\n";
  389. return undef;
  390. }
  391. # Create our HTML page if the logfile has any data.
  392. if (defined $stats and $stats->{totallines} > 0) {
  393. $generator->create_html();
  394. } elsif ($stats->{totallines} == 0) {
  395. print STDERR "No lines found in logfile.. skipping.\n";
  396. }
  397. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1;
  398. }
  399. }
  400. sub parse_channels
  401. {
  402. my $self = shift;
  403. my %origcfg = %{ $self->{cfg} };
  404. foreach my $channel (keys %{ $self->{chans} }) {
  405. foreach (keys %{ $self->{chans}->{$channel} }) {
  406. $self->{cfg}->{$_} = $self->{chans}->{$channel}{$_};
  407. }
  408. $self->do_channel();
  409. $origcfg{chan_done} = $self->{cfg}->{chan_done};
  410. %{ $self->{cfg} } = %origcfg;
  411. }
  412. }
  413. 1;
  414. __END__
  415. =head1 NAME
  416. Pisg - Perl IRC Statistics Generator main module
  417. =head1 SYNOPSIS
  418. use Pisg;
  419. $pisg = new Pisg(
  420. use_configfile => '1',
  421. override_cfg => { network => 'MyNetwork', format => 'eggdrop' }
  422. );
  423. =head1 DESCRIPTION
  424. C<Pisg> is a statistic generator for IRC logfiles or the like, delivering
  425. the results in a HTML page.
  426. =head1 CONSTRUCTOR
  427. =over 4
  428. =item new ( [ OPTIONS ] )
  429. This is the constructor for a new Pisg object. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  430. Possible options are:
  431. B<use_configfile> - When set to 1, pisg will look up it's channels in it's
  432. configuration file, defined by the configuration option 'configfile'.
  433. B<override_cfg> - This defines whichever configuration variables you want to
  434. override from the configuration file. If you set use_configfile to 0, then
  435. you'll have to set at least channel and logfile here.
  436. 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.
  437. =back
  438. =head1 AUTHOR
  439. Morten Brix Pedersen <morten@wtf.dk>
  440. =head1 COPYRIGHT
  441. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  442. This program is free software; you can redistribute it and/or modify it
  443. under the terms of the GPL, license is included with the distribution of
  444. this file.
  445. =cut