Pisg.pm 17 KB

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