Pisg.pm 17 KB

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