Pisg.pm 14 KB

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