Pisg.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. picwidth => '',
  95. picheight => '',
  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. piclocation => '.',
  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. pic_loc => 'PicLocation',
  177. pic_width => 'PicWidth',
  178. pic_height => 'PicHeight'
  179. };
  180. # This enables us to use the search_path in other modules
  181. $self->{cfg}->{search_path} = $self->{search_path};
  182. # Parse the optional overriden configuration variables
  183. foreach my $key (keys %{$self->{override_cfg}}) {
  184. if ($self->{override_cfg}->{$key}) {
  185. $self->{cfg}->{$key} = $self->{override_cfg}->{$key};
  186. }
  187. }
  188. }
  189. sub get_language_templates
  190. {
  191. my $self = shift;
  192. 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");
  193. while (my $line = <FILE>)
  194. {
  195. next if ($line =~ /^#/);
  196. if ($line =~ /<lang name=\"([^"]+)\">/i) {
  197. # Found start tag, setting the current language
  198. my $current_lang = lc($1);
  199. while (<FILE>) {
  200. last if ($_ =~ /<\/lang>/i);
  201. # Get 'template = "Text"' in language file:
  202. if ($_ =~ /(\w+)\s+=\s+"(.*)"\s*$/) {
  203. $self->{tmps}->{$current_lang}{$1} = $2;
  204. }
  205. }
  206. }
  207. }
  208. close(FILE);
  209. }
  210. sub init_words
  211. {
  212. my $self = shift;
  213. $self->{cfg}->{foulwords} =~ s/(^\s+|\s+$)//g;
  214. $self->{cfg}->{foulwords} =~ s/\s+/|/g;
  215. foreach (split(/\s+/, $self->{cfg}->{ignorewords})) {
  216. $self->{cfg}->{ignoreword}{$_} = 1;
  217. }
  218. $self->{cfg}->{violentwords} =~ s/(^\s+|\s+$)//g;
  219. $self->{cfg}->{violentwords} =~ s/\s+/|/g;
  220. }
  221. sub init_config
  222. {
  223. my $self = shift;
  224. if ((open(CONFIG, $self->{cfg}->{configfile}) or open(CONFIG, $self->{search_path} . "/$self->{cfg}->{configfile}"))) {
  225. while (my $line = <CONFIG>)
  226. {
  227. next if ($line =~ /^#/);
  228. chomp $line;
  229. if ($line =~ /<user.*>/) {
  230. my $nick;
  231. if ($line =~ /nick="([^"]+)"/) {
  232. $nick = $1;
  233. add_alias($nick, $nick);
  234. } else {
  235. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No nick specified\n";
  236. next;
  237. }
  238. if ($line =~ /alias="([^"]+)"/) {
  239. my @thisalias = split(/\s+/, lc($1));
  240. foreach (@thisalias) {
  241. if ($self->{cfg}->{regexpaliases} and /[\|\[\]\{\}\(\)\?\+\.\*\^\\]/) {
  242. add_aliaswild($nick, $_);
  243. } elsif (not $self->{cfg}->{regexpaliases} and s/\*/\.\*/g) {
  244. # quote it if it is a wildcard
  245. s/([\|\[\]\{\}\(\)\?\+\^\\])/\\$1/g;
  246. add_aliaswild($nick, $_);
  247. } else {
  248. add_alias($nick, $_);
  249. }
  250. }
  251. }
  252. if ($line =~ /pic="([^"]+)"/) {
  253. $self->{users}->{userpics}{$nick} = $1;
  254. }
  255. if ($line =~ /bigpic="([^"]+)"/) {
  256. $self->{users}->{biguserpics}{$nick} = $1;
  257. }
  258. if ($line =~ /link="([^"]+)"/) {
  259. $self->{users}->{userlinks}{$nick} = $1;
  260. }
  261. if ($line =~ /ignore="Y"/i) {
  262. add_ignore($nick);
  263. }
  264. if ($line =~ /sex="([MmFf])"/i) {
  265. $self->{users}->{sex}{$nick} = lc($1);
  266. }
  267. } elsif ($line =~ /<link(.*)>/) {
  268. if ($line =~ /url="([^"]+)"/) {
  269. my $url = $1;
  270. if ($line =~ /ignore="Y"/i) {
  271. add_url_ignore($url);
  272. }
  273. } else {
  274. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No URL specified\n";
  275. }
  276. } elsif ($line =~ /<set(.*)>/) {
  277. my $settings = $1;
  278. if ($settings !~ /=["'](.*)["']/ || $settings =~ /(\w)>/ ) {
  279. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing or wrong quotes near $1\n";
  280. }
  281. while ($settings =~ s/[ \t]([^=]+)=["']([^"']*)["']//) {
  282. my $var = lc($1);
  283. $var =~ s/ //; # Remove whitespace
  284. if (!defined($self->{cfg}->{$var})) {
  285. if (defined($self->{bwd}->{$var})) {
  286. print "Using backwards compatibility option '$var'; you should change it to '$self->{bwd}->{$var}'\n";
  287. unless (($self->{cfg}->{lc($self->{bwd}->{$var})} eq $2) || $self->{override_cfg}->{lc($self->{bwd}->{$var})}) {
  288. $self->{cfg}->{$self->{bwd}->{$var}} = $2;
  289. }
  290. next;
  291. } else {
  292. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: No such configuration option: '$var'\n";
  293. next;
  294. }
  295. }
  296. unless (($self->{cfg}->{$var} eq $2) || $self->{override_cfg}->{$var}) {
  297. $self->{cfg}->{$var} = $2;
  298. }
  299. }
  300. } elsif ($line =~ /<channel=['"]([^'"]+)['"](.*)>/i) {
  301. my ($channel, $settings) = ($1, $2);
  302. $self->{chans}->{$channel}->{channel} = $channel;
  303. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
  304. while ($settings =~ s/\s([^=]+)=["']([^"']*)["']//) {
  305. my $var = lc($1);
  306. if (defined($self->{bwd}->{$var})) {
  307. print "Using backwards compatibility option '$var'; you should change it to '$self->{bwd}->{$var}'\n";
  308. $self->{chans}->{$channel}{lc($self->{bwd}->{$var})} = $2;
  309. } else {
  310. $self->{chans}->{$channel}{$var} = $2;
  311. }
  312. }
  313. while (<CONFIG>) {
  314. last if ($_ =~ /<\/*channel>/i);
  315. if ($_ =~ /^\s*(\w+)\s*=\s*["']([^"']*)["']/) {
  316. my $var = lc($1);
  317. unless ($self->{override_cfg}->{$var}) {
  318. if (defined($self->{bwd}->{$var})) {
  319. print "Using backwards compatibility option '$var'; you should change it to '$self->{bwd}->{$var}'\n";
  320. $self->{chans}->{$channel}{lc($self->{bwd}->{$var})} = $2;
  321. } else {
  322. $self->{chans}->{$channel}{$var} = $2;
  323. }
  324. }
  325. } elsif ($_ !~ /^$/) {
  326. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
  327. }
  328. }
  329. } elsif ($line =~ /<(\w+)?.*[^>]$/) {
  330. print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Missing end on element <$1 (probably multi-line?)\n";
  331. } elsif ($line =~ /\S/) {
  332. $line =~ s/\n//;
  333. print "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
  334. }
  335. }
  336. close(CONFIG);
  337. }
  338. }
  339. sub init_pisg
  340. {
  341. my $self = shift;
  342. my $timestamp = time();
  343. $self->{cfg}->{start} = time();
  344. if ($self->{cfg}->{timeoffset} =~ /\+(\d+)/) {
  345. # We must plus some hours to the time
  346. $timestamp += 3600 * $1; # 3600 seconds per hour
  347. } elsif ($self->{cfg}->{timeoffset} =~ /-(\d+)/) {
  348. # We must remove some hours from the time
  349. $timestamp -= 3600 * $1; # 3600 seconds per hour
  350. }
  351. $self->{cfg}->{timestamp} = $timestamp;
  352. # Add trailing slash when it's not there..
  353. $self->{cfg}->{imagepath} =~ s/([^\/])$/$1\//;
  354. print "Using language template: $self->{cfg}->{lang}\n\n" if ($self->{cfg}->{lang} ne 'EN' && !$self->{cfg}->{silent});
  355. print "Statistics for channel $self->{cfg}->{channel} \@ $self->{cfg}->{network} by $self->{cfg}->{maintainer}\n\n"
  356. unless ($self->{cfg}->{silent});
  357. }
  358. sub do_channel
  359. {
  360. my $self = shift;
  361. if (!$self->{cfg}->{channel}) {
  362. print STDERR "No channels defined.\n";
  363. } elsif ((!$self->{cfg}->{logfile}) && (!$self->{cfg}->{logdir})) {
  364. print STDERR "No logfile or logdir defined for " . $self->{cfg}->{channel} . "\n";
  365. } else {
  366. $self->init_pisg(); # Init some general things
  367. store_aliases(); # Save the aliases so we can restore them
  368. # later, we don't want to add the aliases
  369. # for this channel to the next channel
  370. # Pick our stats generator.
  371. my $analyzer;
  372. eval <<_END;
  373. use Pisg::Parser::$self->{cfg}->{logtype};
  374. \$analyzer = new Pisg::Parser::$self->{cfg}->{logtype}(
  375. cfg => \$self->{cfg}
  376. );
  377. _END
  378. if ($@) {
  379. print STDERR "Could not load stats analyzer for '$self->{cfg}->{logtype}': $@\n";
  380. return undef;
  381. }
  382. my $stats = $analyzer->analyze();
  383. # Initialize HTMLGenerator object
  384. my $generator;
  385. eval <<_END;
  386. use Pisg::HTMLGenerator;
  387. \$generator = new Pisg::HTMLGenerator(
  388. cfg => \$self->{cfg},
  389. stats => \$stats,
  390. users => \$self->{users},
  391. tmps => \$self->{tmps}
  392. );
  393. _END
  394. if ($@) {
  395. print STDERR "Could not load stats generator (Pisg::HTMLGenerator): $@\n";
  396. return undef;
  397. }
  398. # Create our HTML page if the logfile has any data.
  399. if (defined $stats and $stats->{totallines} > 0) {
  400. $generator->create_html();
  401. } elsif ($stats->{totallines} == 0) {
  402. print STDERR "No lines found in logfile.. skipping.\n";
  403. }
  404. restore_aliases();
  405. $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1;
  406. }
  407. }
  408. sub parse_channels
  409. {
  410. my $self = shift;
  411. my %origcfg = %{ $self->{cfg} };
  412. foreach my $channel (keys %{ $self->{chans} }) {
  413. foreach (keys %{ $self->{chans}->{$channel} }) {
  414. $self->{cfg}->{$_} = $self->{chans}->{$channel}{$_};
  415. }
  416. $self->do_channel();
  417. $origcfg{chan_done} = $self->{cfg}->{chan_done};
  418. %{ $self->{cfg} } = %origcfg;
  419. }
  420. }
  421. 1;
  422. __END__
  423. =head1 NAME
  424. Pisg - Perl IRC Statistics Generator main module
  425. =head1 SYNOPSIS
  426. use Pisg;
  427. $pisg = new Pisg(
  428. use_configfile => '1',
  429. override_cfg => { network => 'MyNetwork', format => 'eggdrop' }
  430. );
  431. $pisg->run();
  432. =head1 DESCRIPTION
  433. C<Pisg> is a statistic generator for IRC logfiles or the like, delivering
  434. the results in a HTML page.
  435. =head1 CONSTRUCTOR
  436. =over 4
  437. =item new ( [ OPTIONS ] )
  438. This is the constructor for a new Pisg object. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  439. Possible options are:
  440. B<use_configfile> - When set to 1, pisg will look up it's channels in it's
  441. configuration file, defined by the configuration option 'configfile'.
  442. B<override_cfg> - This defines whichever configuration variables you want to
  443. override from the configuration file. If you set use_configfile to 0, then
  444. you'll have to set at least channel and logfile here.
  445. 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.
  446. =back
  447. =head1 AUTHOR
  448. Morten Brix Pedersen <morten@wtf.dk>
  449. =head1 COPYRIGHT
  450. Copyright (C) 2001 Morten Brix Pedersen. All rights resereved.
  451. This program is free software; you can redistribute it and/or modify it
  452. under the terms of the GPL, license is included with the distribution of
  453. this file.
  454. =cut