From aac69fe568fc97f041048df415de68120ecd134f Mon Sep 17 00:00:00 2001 From: octo Date: Wed, 13 Apr 2005 08:53:55 +0000 Subject: [PATCH] - Documented new config options in the config file - Surpressed warnings in Onis::Html.. (Number of lines parsed not yet done in Onis::Data::Core.. - Changed config options for Onis::Data::Persistent::Storable to more general names.. - Minor changes in Onis::Data::Persistent::None to work nicely with Onis::Data::Persistent::Storable --- config | 35 ++- lib/Onis/Data/Persistent/None.pm | 7 +- lib/Onis/Data/Persistent/Storable.pm | 30 ++- lib/Onis/Html.pm | 428 +++++++++++++++++++++++++++++++++++ 4 files changed, 487 insertions(+), 13 deletions(-) create mode 100644 lib/Onis/Html.pm diff --git a/config b/config index dec78a8..9a0a7f1 100644 --- a/config +++ b/config @@ -129,8 +129,39 @@ soliloquies_count: 5; # not wish onis to write and/or use such a file you can disable it here. #use_persistency: "true"; -# Sets the file which onis will dump it's state into. -persistency_file: "persistency.data"; + +# +# Storage / Persistency options +# + +# First, set the storage module to use. This depends on the perl-modules you +# have installed. If you don't know, ``Dbm'' is a wise choice. +# None: +# Don't use any storage module. Data is not stored at the end of each run and +# you will have to re-parse all logs again. This is the default. +# Dbm: +# This storage-module uses DBM-files. The Perl-module ``AnyDBM_File'' is used +# which is part of most Perl-distributions, i.e. it should work almost +# anywhere. +# Storable: +# Uses the ``Storable'' module to simply copy internal variables to a file. +# This may result in more memory usage. No benchmarks have been run yet, +# though.. +storage_module: "Dbm"; + +# Sets the filename to use for storing the data. This filename is relative to +# ``storage_dir'' (see below). This option applies to the storage module +# ``Storable''. +storage_file: "storage.dat"; + +# Sets the directory in which the storage file(s) should be kept. This is used +# by ``Dbm'' and ``Storable''. +storage_dir: "var/"; + + +# +# Output options +# # If set to ``print'' prints out all color-codes. If set to ``ignore'' # color-codes will not be printed. Default is to ignore colors.. diff --git a/lib/Onis/Data/Persistent/None.pm b/lib/Onis/Data/Persistent/None.pm index c8b77fd..a5440c8 100644 --- a/lib/Onis/Data/Persistent/None.pm +++ b/lib/Onis/Data/Persistent/None.pm @@ -43,15 +43,12 @@ sub new my $id = $caller . ':' . $name; - if (exists ($TREE->{$id})) + if (!exists ($TREE->{$id})) { - print STDERR $/, __FILE__, ": Name $name has been used in context $caller before."; - return (undef); + $TREE->{$id} = {}; } - $TREE->{$id} = {}; $obj->{'data'} = $TREE->{$id}; - $obj->{'key'} = $key; $obj->{'fields'} = [@fields]; $obj->{'num_fields'} = scalar (@fields); diff --git a/lib/Onis/Data/Persistent/Storable.pm b/lib/Onis/Data/Persistent/Storable.pm index 67597fe..704cd65 100644 --- a/lib/Onis/Data/Persistent/Storable.pm +++ b/lib/Onis/Data/Persistent/Storable.pm @@ -26,19 +26,37 @@ file after everything has been done. =over 4 -=item B: IfileE> +=item B: "I"; -Sets the file to use for storable. +Sets the file storable will write it's data to. + +=item B: "I"; + +Sets the directory in which B can be found. =back =cut -our $StorableFile = get_config ('storable_file') || 'var/storable.dat'; +our $StorageFile = get_config ('storage_file') || 'storage.dat'; +our $StorageDir = get_config ('storage_dir') || 'var'; + +$StorageDir =~ s#/+$##; + +if (!-d $StorageDir) +{ + print STDERR $/, __FILE__, ':', < $file")) + { + print STDERR $/, __FILE__, ": Unable to open file ``$file'': $!"; + return (undef); + } + + unless (flock ($fh, LOCK_EX)) + { + print STDERR $/, __FILE__, ": Unable to exclusive lock file ``$file'': $!"; + close ($fh); + return (undef); + } + + print_head (); + + return ($fh); +} + +# Generates the HTML header including the CSS information. +# Doesn't take any arguments +sub print_head +{ + my $generated_time = scalar (localtime ($time_start)); + my $trans; + + my $stylesheet = 'style.css'; + if (get_config ('stylesheet')) + { + $stylesheet = get_config ('stylesheet'); + } + + my $encoding = 'iso-8859-1'; + if (get_config ('encoding')) + { + $encoding = get_config ('encoding'); + } + + my $user = 'onis'; + if (get_config ('user')) + { + $user = get_config ('user'); + } + elsif (defined ($ENV{'USER'})) + { + $user = $ENV{'USER'}; + } + + my $channel = get_channel (); + + my @images = get_config ('horizontal_images'); + if (!@images) + { + @images = qw#images/hor0n.png images/hor1n.png images/hor2n.png images/hor3n.png#; + } + + $trans = translate ('%s statistics created by %s'); + my $title = sprintf ($trans, $channel, $user); + + + print $fh < + + + + + $title + + + + + + +
+EOF + + $trans = translate ('%s stats by %s'); + $title = sprintf ($trans, $channel, $user); + + $trans = translate ('Statistics generated on %s'); + my $time_msg = sprintf ($trans, $generated_time); + + $trans = translate ('Hours'); + + print $fh <$title +

$time_msg

+ + + + + + + + +
Red
$trans 0-5
Green
$trans 6-11
Blue
$trans 12-17
Red
$trans 18-24
+ +EOF +} + +# this routine adds a box to the end of the html- +# page with onis' homepage URL, the author's name +# and email-address. Feel free to uncomment the +# creation of this box if it's appereance nags +# you.. +sub close_file +{ + my $runtime = time () - $time_start; + my $now = scalar (localtime ()); + my $total_lines = get_total_lines () || 0; + my $lines_per_sec = 'infinite'; + + my $hp = translate ("onis' homepage"); + my $gen = translate ('This page was generated on %s with %s'); + my $stats = translate ('%u lines processed in %u seconds (%s lines per second)'); + my $by = translate ('onis is written %s by %s'); + my $link = translate ('Get the latest version from %s'); + + my $lps = translate ('infinite'); + if ($runtime) + { + $lps = sprintf ("%.1f", ($total_lines / $runtime)); + } + + print $fh < + + + + +EOF + print $fh ' \n +
'; + printf $fh ($gen, $now, "onis $::VERSION ("onis not irc stats")"); + print $fh "
\n "; + printf $fh ($stats, $total_lines, $runtime, $lps); + print $fh qq#\n
\n #; + printf $fh ($by, '2000-2004', 'Florian octo Forster <octo@nospam.verplant.org>'); + print $fh qq## if ($PUBLIC_PAGE); + print $fh "
\n "; + printf $fh ($link, sprintf (qq#%s#, $hp)); + + print $fh < +
+ + + +EOF +} + +sub html_escape +{ + my @retval = (); + + foreach (@_) + { + my $esc = escape_uris ($_); + push (@retval, $esc); + } + + if (wantarray ()) + { + return @retval; + } + else + { + return join ("\n", @retval); + } +} + +sub escape_uris +{ + my $text = shift; + my $retval = ''; + + return ('') if (!defined ($text)); + + #if ($text =~ m#(?:(?:ftp|https?)://|www\.)[\w\.-]+\.[A-Za-z]{2,4}(?::\d+)?(?:/[\w\d\.\%/-~]+)?(?=\W|$)#i) + if ($text =~ m#(?:(?:ftp|https?)://|www\.)[\w\.-]+\.[A-Za-z]{2,4}(?::\d+)?(?:/[\w\d\.\%\/\-\~]*(?:\?[\+\w\&\%\=]+)?)?(?=\W|$)#i) + { + my $orig_match = $&; + my $prematch = $`; + my $postmatch = $'; + + my $match = $orig_match; + if ($match =~ /^www/i) { $match = 'http://' . $match; } + if ($match !~ m#://.+/#) { $match .= '/'; } + + if ((length ($orig_match) > 50) and ($orig_match =~ m#^http://#)) + { + $orig_match =~ s#^http://##; + } + if (length ($orig_match) > 50) + { + my $len = length ($orig_match) - 47; + substr ($orig_match, 47, $len, '...'); + } + + $retval = escape_normal ($prematch); + $retval .= qq($orig_match); + $retval .= escape_uris ($postmatch); + } + else + { + $retval = escape_normal ($text); + } + + return ($retval); +} + +sub escape_normal +{ + my $text = shift; + + return ('') if (!defined ($text)); + + $text =~ s/\&/\&/g; + $text =~ s/"/\"/g; + $text =~ s//\>/g; + + # german umlauts + $text =~ s/ä/\ä/g; + $text =~ s/ö/\ö/g; + $text =~ s/ü/\ü/g; + $text =~ s/Ä/\Ä/g; + $text =~ s/Ü/\Ö/g; + $text =~ s/Ö/\Ü/g; + $text =~ s/ß/\ß/g; + + if ($WANT_COLOR) + { + $text = find_colors ($text); + } + else + { + $text =~ s/[\cB\c_\cV\cO]|\cC(?:\d+(?:,\d+)?)?//g; + } + + return ($text); +} + +sub find_colors +{ + my $string = shift; + my $open_spans = 0; + + my $code_ref; + + my %flags = + ( + span_open => 0, + fg_color => -1, + bg_color => -1, + bold => 0, + underline => 0, + 'reverse' => 0 + ); + + while ($string =~ m/([\cB\c_\cV\cO])|(\cC)(?:(\d+)(?:,(\d+))?)?/g) + { + my $controlchar = $1 ? $1 : $2; + my $fg = defined ($3) ? $3 : -1; + my $bg = defined ($4) ? $4 : -1; + + my $prematch = $`; + my $postmatch = $'; + + my $newspan = ""; + + # Close open spans first + if ($flags{'span_open'}) + { + $newspan .= ""; + $flags{'span_open'} = 0; + } + + # To catch `\cC' without anything following.. + if (($controlchar eq "\cC") and ($fg == -1) and ($bg == -1)) + { + $flags{'fg_color'} = -1; + $flags{'bg_color'} = -1; + } + elsif ($controlchar eq "\cC") + { + if ($fg != -1) + { + $flags{'fg_color'} = $fg % scalar (@mirc_colors); + } + if ($bg != -1) + { + $flags{'bg_color'} = $bg % scalar (@mirc_colors); + } + } + elsif ($controlchar eq "\cB") + { + $flags{'bold'} = 1 - $flags{'bold'}; + } + elsif ($controlchar eq "\c_") + { + $flags{'underline'} = 1 - $flags{'underline'}; + } + elsif ($controlchar eq "\cV") + { + $flags{'reverse'} = 1 - $flags{'reverse'}; + } + # reset + elsif ($controlchar eq "\cO") + { + $flags{'fg_color'} = -1; + $flags{'bg_color'} = -1; + $flags{'bold'} = 0; + $flags{'underline'} = 0; + $flags{'reverse'} = 0; + } + + # build the new span-tag + if (($flags{'fg_color'} != -1) || ($flags{'bg_color'} != -1) + || $flags{'bold'} || $flags{'underline'}) + { + my $fg = $flags{'fg_color'}; + my $bg = $flags{'bg_color'}; + my @style = (); + + if ($flags{'reverse'} and ($bg != -1)) + { + $fg = $flags{'bg_color'}; + $bg = $flags{'fg_color'}; + } + + if ($fg != -1) + { + push (@style, 'color: ' . $mirc_colors[$fg] . ';'); + } + if ($bg != -1) + { + push (@style, 'background-color: ' . $mirc_colors[$bg] . ';'); + } + if ($flags{'bold'}) + { + push (@style, 'font-weight: bold;'); + } + if ($flags{'underline'}) + { + push (@style, 'text-decoration: underline;'); + } + + $newspan .= ''; + $flags{'span_open'} = 1; + } + + $string = $prematch . $newspan . $postmatch; + } + + if ($flags{'span_open'}) + { + $string .= ""; + $flags{'span_open'} = 0; + } + + return ($string); +} -- 2.11.0