#!/usr/local/bin/perl -Tw
use strict;
use CGI qw(:standard);
$ENV{PATH} = '/bin:/usr/bin';
### Set globals
my $cgi = 'logreader.cgi';
my $logfile = '/home/yourname/data/scriber.log';
my $message = '#channel log';
my $password = 'changeme';
my $crypted = crypt($password,'sc');
my $maxresults = 5000;
my $length = param('length') || 300;
$length =~ /(\d+)/;
$length = $1;
$length = $maxresults if ($length > $maxresults);
my $results = param('results') || 50;
$results =~ /(\d+)/;
$results = $1;
$results = $maxresults if ($results > $maxresults);
my $context = param('count') || 100;
$context =~ /(\d+)/;
$context = $1;
$context = $maxresults if ($context > $maxresults);
my $headfoot = <
HF
### Process cookie and form
if (cookie('scriberlog') eq $crypted) {
if (my $action = param('action')) {
if ($action eq 'logout') {
&print_logout;
}
elsif ($action eq 'search') {
my $term = param('term');
$term =~ /^([\w\s\.\:\/\-\_]+)$/;
$term = $1;
my $lines;
if ($term ne '') {
$lines = `grep -i '$term' $logfile | tail -$results`;
}
my $count = $lines=~tr/\n//;
$message = "Search results: $count matches for \"$term\"";
print_html(get_table(\$lines, Context => 1));
}
elsif ($action eq 'context') {
my $time = param('time');
$time =~ /(\d+)/;
$time = $1;
my $lines = `grep -A $context $time $logfile`;
print_html(get_table(\$lines));
}
else {
print "Bad Action.";
}
}
else {
$message = "Last $length lines from #channel";
my $lines = `tail -$length $logfile`;
print_html(get_table(\$lines));
}
}
elsif (param('i_like_bacon') eq $password) {
&print_redirect;
}
else {
&print_login;
}
### HTML subroutines
sub get_table {
my $table;
my $shade = 1;
my ($line_r, %opt) = @_;
my $href_chars = '[\w\;\\@:\#\%\!\-\.\_\,\/\?\&\+\=\|\~\$\{\}]';
for my $line (split('\n',$$line_r)) {
next if ($line =~ /^\s*$/);
my $style = $shade?' class="shaded"':'';
my ($etime,$line) = split("\t",$line,2);
my $time = localtime($etime);;
if ($opt{'Context'}) {
$time = qq($time);
}
$line =~ s/</gs;
$line =~ s/>/>/gs;
$line =~ s/^<(.+?)>/<$1<\/b>>/;
$line =~ s/^\* ([\]\[\w-]+) /\* $1<\/b> /;
# $line =~ s|(https?://\S+)|$1|gis;
$line =~ s|(https?://$href_chars+)|$1|gis;
$line =~ s|mailto:(\S+\@\S+)|$1|;
$table .= qq(| $time | $line |
);
$shade = !$shade;
}
return $table;
}
sub print_html {
my $table = shift;
print <
$message
$headfoot
$message : (bottom)
$headfoot
HTML
}
sub print_login {
print <Knock, knock...
what's the secret word?
HTML
}
sub print_logout {
print <