4
0

Pisg.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package Pisg;
  2. # Copyright (C) 2001 <Morten Brix Pedersen> - morten@wtf.dk
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. =head1 NAME
  18. Pisg - Perl IRC Statistics Generator main module
  19. =cut
  20. use strict;
  21. $^W = 1;
  22. sub new
  23. {
  24. my $type = shift;
  25. my $self = {
  26. chans => {},
  27. users => {},
  28. conf => {},
  29. tmps => {},
  30. overriden_confs => $_[0],
  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->{conf}->{debug} or not $self->{conf}->{debugstarted}) {
  36. my $debugline = $_[0] . "\n";
  37. if ($self->{conf}->{debugstarted}) {
  38. print DEBUG $debugline;
  39. } else {
  40. $self->{conf}->{debugqueue} .= $debugline;
  41. }
  42. }
  43. };
  44. # Load the Common module from wherever it's configured to be.
  45. #push(@INC, $self->{conf}->{modules_dir});
  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. $self->get_default_config_settings();
  55. print "pisg $self->{conf}->{version} - Perl IRC Statistics Generator\n\n";
  56. $self->init_config(); # Init config. (Aliases, ignores, other options etc.)
  57. $self->init_debug()
  58. unless ($self->{conf}->{debugstarted}); # Init the debugging file
  59. $self->get_language_templates(); # Get translations from lang.txt
  60. $self->parse_channels(); # parse any channels in <channel> statements
  61. $self->do_channel()
  62. unless ($self->{conf}->{chan_done}{$self->{conf}->{channel}});
  63. $self->close_debug(); # Close the debugging file
  64. }
  65. sub get_default_config_settings
  66. {
  67. my $self = shift;
  68. $self->{conf} = {
  69. channel => "#channel",
  70. logtype => "Logfile",
  71. logfile => "channel.log",
  72. format => "mIRC",
  73. network => "SomeIRCNet",
  74. outputfile => "index.html",
  75. maintainer => "MAINTAINER",
  76. pagehead => "none",
  77. configfile => "pisg.cfg",
  78. imagepath => "",
  79. logdir => "",
  80. lang => 'EN',
  81. langfile => 'lang.txt',
  82. prefix => "",
  83. modules_dir => $FindBin::Bin . "/modules", # Module search path
  84. # Colors / Layout
  85. bgcolor => "#dedeee",
  86. bgpic => '',
  87. text => "black",
  88. hbgcolor => "#666699",
  89. hcolor => "white",
  90. hicell => "#BABADD",
  91. hicell2 => "#CCCCCC",
  92. tdcolor => "black",
  93. tdtop => "#C8C8DD",
  94. link => "#0b407a",
  95. vlink => "#0b407a",
  96. hlink => "#0b407a",
  97. headline => "#000000",
  98. rankc => "#CCCCCC",
  99. pic_v_0 => "blue-v.png",
  100. pic_v_6 => "green-v.png",
  101. pic_v_12 => "yellow-v.png",
  102. pic_v_18 => "red-v.png",
  103. pic_h_0 => "blue-h.png",
  104. pic_h_6 => "green-h.png",
  105. pic_h_12 => "yellow-h.png",
  106. pic_h_18 => "red-h.png",
  107. # Stats settings
  108. show_linetime => 0,
  109. show_time => 1,
  110. show_words => 0,
  111. show_wpl => 0,
  112. show_cpl => 0,
  113. show_legend => 1,
  114. show_kickline => 1,
  115. show_actionline => 1,
  116. show_shoutline => 1,
  117. show_violentlines => 1,
  118. # Less important things
  119. minquote => 25,
  120. maxquote => 65,
  121. wordlength => 5,
  122. activenicks => 25,
  123. activenicks2 => 30,
  124. topichistory => 3,
  125. nicktracking => 0,
  126. timeoffset => "+0",
  127. # Misc settings
  128. foul => 'ass fuck bitch shit scheisse scheiße kacke arsch ficker ficken schlampe',
  129. violent => 'slaps beats smacks',
  130. ignorewords => '',
  131. tablewidth => 614,
  132. regexp_aliases => 0,
  133. # Developer stuff
  134. debug => 0,
  135. debugfile => "debug.log",
  136. version => "v0.23-cvs",
  137. };
  138. }
  139. sub get_language_templates
  140. {
  141. my $self = shift;
  142. open(FILE, $self->{conf}->{langfile}) or open (FILE, $FindBin::Bin . "/$self->{conf}->{langfile}") or die("$0: Unable to open language file($self->{conf}->{langfile}): $!\n");
  143. while (my $line = <FILE>)
  144. {
  145. next if ($line =~ /^#/);
  146. if ($line =~ /<lang name=\"([^"]+)\">/) {
  147. # Found start tag, setting the current language
  148. my $current_lang = $1;
  149. while (<FILE>) {
  150. last if ($_ =~ /<\/lang>/i);
  151. # Get 'template = "Text"' in language file:
  152. if ($_ =~ /(\w+)\s+=\s+"(.*)"\s*$/) {
  153. $self->{tmps}->{$current_lang}{$1} = $2;
  154. }
  155. }
  156. }
  157. }
  158. close(FILE);
  159. }
  160. sub get_subst
  161. {
  162. my $self = shift;
  163. my ($m,$f,$hash) = @_;
  164. if ($hash->{nick} && $self->{users}->{sex}{$hash->{nick}}) {
  165. if ($self->{users}->{sex}{$hash->{nick}} eq 'm') {
  166. return $m;
  167. } elsif ($self->{users}->{sex}{$hash->{nick}} eq 'f') {
  168. return $f;
  169. }
  170. }
  171. return "$m/$f";
  172. }
  173. sub init_debug
  174. {
  175. my $self = shift;
  176. $self->{conf}->{debugstarted} = 1;
  177. if ($self->{conf}->{debug}) {
  178. print "[ Debugging => $self->{conf}->{debugfile} ]\n";
  179. open(DEBUG,"> $self->{conf}->{debugfile}") or print STDERR "$0: Unable to open debug
  180. file($self->{conf}->{debugfile}): $!\n";
  181. $self->{debug}->("*** pisg debug file for $self->{conf}->{logfile}\n");
  182. if ($self->{conf}->{debugqueue}) {
  183. print DEBUG $self->{conf}->{debugqueue};
  184. delete $self->{conf}->{debugqueue};
  185. }
  186. } else {
  187. $self->{debug} = sub {};
  188. }
  189. }
  190. sub close_debug
  191. {
  192. my $self = shift;
  193. if ($self->{conf}->{debug}) {
  194. close(DEBUG) or print STDERR "$0: Cannot close debugfile($self->{conf}->{debugfile}): $!\n";
  195. }
  196. }
  197. sub init_words
  198. {
  199. my $self = shift;
  200. $self->{conf}->{foul} =~ s/\s+/|/g;
  201. foreach (split /\s+/, $self->{conf}->{ignorewords}) {
  202. $self->{conf}->{ignoreword}{$_} = 1;
  203. }
  204. $self->{conf}->{violent} =~ s/\s+/|/g;
  205. }
  206. sub init_config
  207. {
  208. my $self = shift;
  209. # Parse the optional overriden configuration variables
  210. foreach my $key (keys %{$self->{overriden_confs}}) {
  211. $self->{conf}->{$key} = $self->{overriden_confs}->{$key};
  212. }
  213. if ((open(CONFIG, $self->{conf}->{configfile}) or open(CONFIG, $FindBin::Bin . "/$self->{conf}->{configfile}"))) {
  214. print "Using config file: $self->{conf}->{configfile}\n";
  215. my $lineno = 0;
  216. while (my $line = <CONFIG>)
  217. {
  218. $lineno++;
  219. next if ($line =~ /^#/);
  220. if ($line =~ /<user.*>/) {
  221. my $nick;
  222. if ($line =~ /nick="([^"]+)"/) {
  223. $nick = $1;
  224. add_alias($nick, $nick);
  225. } else {
  226. print STDERR "Warning: no nick specified in $self->{conf}->{configfile} on line $lineno\n";
  227. next;
  228. }
  229. if ($line =~ /alias="([^"]+)"/) {
  230. my @thisalias = split(/\s+/, lc($1));
  231. foreach (@thisalias) {
  232. if ($self->{conf}->{regexp_aliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
  233. add_aliaswild($nick, $_);
  234. } elsif (not $self->{conf}->{regexp_aliases} and s/\*/\.\*/g) {
  235. # quote it if it is a wildcard
  236. s/([\|\[\]\{\}\(\)\?\+\^\\])/\\$1/g;
  237. add_aliaswild($nick, $_);
  238. } else {
  239. add_alias($nick, $_);
  240. }
  241. }
  242. }
  243. if ($line =~ /pic="([^"]+)"/) {
  244. $self->{users}->{userpics}{$nick} = $1;
  245. }
  246. if ($line =~ /link="([^"]+)"/) {
  247. $self->{users}->{userlinks}{$nick} = $1;
  248. }
  249. if ($line =~ /ignore="Y"/i) {
  250. add_ignore($nick);
  251. }
  252. if ($line =~ /sex="([MmFf])"/i) {
  253. $self->{users}->{sex}{$nick} = lc($1);
  254. }
  255. } elsif ($line =~ /<set(.*)>/) {
  256. my $settings = $1;
  257. while ($settings =~ s/[ \t]([^=]+)=["']([^"']*)["']//) {
  258. my $var = lc($1); # Make the string lowercase
  259. unless (($self->{conf}->{$var} eq $2) || $self->{overriden_confs}->{$var}) {
  260. $self->{conf}->{$var} = $2;
  261. }
  262. $self->{debug}->("Conf: $var = $2");
  263. }
  264. } elsif ($line =~ /<channel=['"]([^'"]+)['"](.*)>/i) {
  265. my ($channel, $settings) = ($1, $2);
  266. $self->{chans}->{$channel}->{channel} = $channel;
  267. $self->{conf}->{chan_done}{$self->{conf}->{channel}} = 1; # don't parse channel in $self->{conf}->{channel} if a channel statement is present
  268. while ($settings =~ s/\s([^=]+)=["']([^"']*)["']//) {
  269. my $var = lc($1);
  270. $self->{chans}->{$channel}{$var} = $2;
  271. $self->{debug}->("Channel conf $channel: $var = $2");
  272. }
  273. while (<CONFIG>) {
  274. last if ($_ =~ /<\/*channel>/i);
  275. while ($_ =~ s/^\s*(\w+)\s*=\s*["']([^"']*)["']//) {
  276. my $var = lc($1);
  277. unless ($self->{overriden_confs}->{$var}) {
  278. $self->{chans}->{$channel}{$var} = $2;
  279. }
  280. $self->{debug}->("Conf $channel: $var = $2");
  281. }
  282. }
  283. }
  284. }
  285. close(CONFIG);
  286. }
  287. }
  288. sub init_pisg
  289. {
  290. my $self = shift;
  291. my $timestamp = time();
  292. $self->{conf}->{start} = time();
  293. if ($self->{conf}->{timeoffset} =~ /\+(\d+)/) {
  294. # We must plus some hours to the time
  295. $timestamp += 3600 * $1; # 3600 seconds per hour
  296. } elsif ($self->{conf}->{timeoffset} =~ /-(\d+)/) {
  297. # We must remove some hours from the time
  298. $timestamp -= 3600 * $1; # 3600 seconds per hour
  299. }
  300. $self->{conf}->{timestamp} = $timestamp;
  301. # Add trailing slash when it's not there..
  302. $self->{conf}->{imagepath} =~ s/([^\/])$/$1\//;
  303. print "Using language template: $self->{conf}->{lang}\n\n" if ($self->{conf}->{lang} ne 'EN');
  304. print "Statistics for channel $self->{conf}->{channel} \@ $self->{conf}->{network} by $self->{conf}->{maintainer}\n\n";
  305. }
  306. sub do_channel
  307. {
  308. my $self = shift;
  309. $self->init_pisg(); # Init commandline arguments and other things
  310. $self->init_words(); # Init words. (Foulwords etc)
  311. # Pick our stats generator.
  312. my $analyzer;
  313. eval <<_END;
  314. use Pisg::Parser::$self->{conf}->{logtype};
  315. \$analyzer = new Pisg::Parser::$self->{conf}->{logtype}(\$self->{conf}, \$self->{debug});
  316. _END
  317. if ($@) {
  318. print STDERR "Could not load stats generator for '$self->{conf}->{logtype}': $@\n";
  319. return undef;
  320. }
  321. my $stats = $analyzer->analyze();
  322. my $generator;
  323. eval <<_END;
  324. use Pisg::HTMLGenerator;
  325. \$generator = new Pisg::HTMLGenerator(\$self->{conf}, \$self->{debug}, \$stats, \$self->{users}, \$self->{tmps});
  326. _END
  327. if ($@) {
  328. print STDERR "Could not load stats html generator (Pisg::HTMLGenerator): $@\n";
  329. return undef;
  330. }
  331. # Create our HTML page if the logfile has any data.
  332. if (defined $stats and $stats->{totallines} > 0) {
  333. $generator->create_html();
  334. } elsif ($stats->{totallines} == 0) {
  335. print STDERR "No lines found in logfile.. skipping.\n";
  336. }
  337. $self->{conf}->{chan_done}{$self->{conf}->{channel}} = 1;
  338. }
  339. sub parse_channels
  340. {
  341. my $self = shift;
  342. my %origconf = %{ $self->{conf} };
  343. foreach my $channel (keys %{ $self->{chans} }) {
  344. foreach (keys %{ $self->{chans}->{$channel} }) {
  345. $self->{conf}->{$_} = $self->{chans}->{$channel}{$_};
  346. }
  347. $self->do_channel();
  348. $origconf{chan_done} = $self->{conf}->{chan_done};
  349. %{ $self->{conf} } = %origconf;
  350. }
  351. }
  352. 1;