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