Pisg.pm 14 KB

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