Revision 54 (by dpavlin, 2004/08/01 22:30:53) small fix, this module moved
#!/usr/bin/perl

my $basedir = '';
BEGIN {
	$basedir = $0;
	my $loop = 0;	# to prevent symlink loops
	while (-l $basedir && $loop++ < 20) {
		$basedir = readlink($basedir);
	}
	$basedir =~ s#/+[^/]+$#/lib#;
}
if ($basedir) {
	use lib "$basedir";
}

=head1 NAME

httpd.pl - http server for Mail::Box Web Search

=head1 SYNOPSYS

 httpd.pl [local.conf]

=head1 DESCRIPTION

This script implements user interface for Mail::Box Web Search as
a small single-user http server.

=head1 SEE ALSO

C<MWS> perl modules which are part of this package
C<HTTP::Daemon::Simple> module which implements the server itself

=cut

use strict;
use warnings;
use MWS::SWISH;
#use MWS::Plucene;
use HTTP::Daemon::Simple;
use Template;
use URI::Escape;

use Data::Dumper;

my $debug = 1;

my $config_file = shift @ARGV || 'global.conf';

if (! -f $config_file) {
	print qq{Usage: $0 [/path/to/local.conf]

If local.conf is not specified, global.conf in current directory will
be used.
};
	exit 1;
}

my $mws = MWS::SWISH->new(config_file => $config_file);
#my $mws = MWS::Plucene->new(config_file => $config_file, debug => $debug);

my $tt = Template->new({
	INCLUDE_PATH => $mws->{config}->val('global', 'templates'),
	FILTERS => {
		'body5' => \&body5_filter,
		'body' => \&body_filter,
	},
	EVAL_PERL => 1,
});

my $d = new HTTP::Daemon::Simple(
	'listen' => $mws->{config}->val('global', 'listen'),
	'static_html' => $mws->{config}->val('global', 'static_html'),
	'debug' => $debug,
) || die "can't create HTTP::Daemon::Simple: $!";


print "Web server ready at: ", $d->url, "\n";

$d->run_server( \&request );

sub request($$) {
	my ($url,$param) = @_;

	print Dumper($param,$mws->{counter}),"\n" if ($debug);

	# template file name (use ?format=html as default)
	my $tpl_file = 'master.';
	$tpl_file .= $param->{'format'} || 'html';

	# parse date from url
	my ($yyyy,$mm,$dd) = $mws->yyyymmdd;

	my $yyyymm;

	my $date_limit;

	if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) {
		($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3);
		 $date_limit = "$yyyy-$mm-$dd";
	} elsif ($url =~ m,^/(\d{4})[/-](\d+),) {
		($yyyy,$mm) = $mws->fmtdate($1,$2);
		$date_limit = "$yyyy-$mm";
	} elsif ($url =~ m,^/(\d{4}),) {
		$date_limit = $mws->fmtdate($1);
	}

	#
	# implement functionality and generate HTML
	#
	my $html;

	if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
		$param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
	} elsif ($param->{'search'}) {
		($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2);
	}

	my $tpl_var = {
		param	=> $param,
		yyyy	=> $yyyy,
		mm	=> $mm,
		dd	=> $dd,
		date_limit => $date_limit,
	};

	# is this access to root of web server?
	if ($url eq "/" && !$param->{'search'}) {
		# if first access, go to current year
		$date_limit = $mws->fmtdate($yyyy);
		$param->{sort_by} = "date desc";
	}

	# ?show_id=XXXXxxxx___message_id___xxxxXXXX
	if ($param->{'show_id'}) {

		$mws->reset_counters;
		my $row = $mws->fetch_result_by_id($param->{'show_id'});
		$tpl_var->{message} = $row;
	} elsif ($param->{'search'} || $date_limit) {

		# show search results
		# ?search=foo:bar

		my @search;
		push @search, $param->{'search'} if ($param->{'search'});

		if ($date_limit) {
			push @search, "and" if (@search);
			push @search, "date:\"$date_limit\"";
		}

		if ($param->{sort_by}) {
			push @search, "sort:".$param->{sort_by};
		}

		print STDERR "search: ",join(" ",@search),"\n";

		my $results = $mws->search(@search);
		my @res = $mws->fetch_all_results();

		$tpl_var->{results} = \@res if (@res);
		$tpl_var->{total_hits} = $mws->{total_hits} || 0;

		# no hits, offer suggestions
		if (! $tpl_var->{results} && $param->{'search_fld'} && $param->{'search_val'}) {
			@{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});
		}

	}

	# push counters to template
	foreach my $f (qw(from to cc bcc folder)) {
		my $h = $mws->counter($f) || next;
		my @a;
		foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {
			push @a, $h->{$k};
		}
		$tpl_var->{counters}->{$f} = [ @a ] if (@a);
	}

	# push calendar in template
	$tpl_var->{calendar} = $mws->counter('calendar');

	$tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();
	return $html;
};

# template toolkit filter

sub html_escape($) {
	my $text = shift || return;

	# don't re-escape html
	#return $text if ($text =~ /&(?:lt|gt|amp|quot);/);

	# Escape <, >, & and ", and to produce valid XML
	my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
	my $escape_re  = join '|' => keys %escape;

	$text =~ s/($escape_re)/$escape{$1}/gs;

	while ($text =~ s/#-#(quote|signature)(\d*)##(.+?)##\1\2#-#/<span class="$1">$3<\/span>/gs) { } ;

	return $text;
}

#use Text::Context::EitherSide;

sub body5_filter {
	my $text = shift;

	# remove quote
	$text =~ s/^[\>:\|=]+[^\n\r]*[\n\r]*$/#-q-#/msg;
	# remove quote author
	$text =~ s/[\n\r]+[^\n\r]+:\s*(?:#-q-#[\n\r*])+//gs;
	$text =~ s/^[^\n\r]+:\s*(?:#-q-#[\n\r]*)+//gs;
	$text =~ s/#-q-#[\n\r]*//gs;
	# outlook quoting
	$text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si;
	$text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si;

	# remove signature
	$text =~ s/(?:^|[\n\r]+)*--\s*[\n\r]+.*$//s;
	$text =~ s/(?:^|[\n\r]+)*_____+[\n\r]+.*$//s;

	# compress cr/lf
	$text =~ s/[\n\r]+/\n/gs;

	# remove whitespaces
	$text =~ s/^\n+//gs;
	$text =~ s/[\s\n]+$//gs;

	if ($text eq "") {
		$text="#-#quote##forwarded message##quote#-#";
	}

	# cut to 5 lines;
	if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {
		$text =~ s/[\n\r]*$/ .../;
	}

#	my $context = Text::Context::EitherSide->new($text, context => 5);
#	return $context->as_string("perl");

	return html_escape($text);
}

sub body_filter {
	my $text = shift;

	my $sig = '';

	# remove signature
	if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) {
		$sig = "$1#-#signature##$2##signature#-#";
	} elsif ($text =~s/(^|[\n\r]+)*(_____+[\n\r]+.*)$//s) {
		$sig = "$1#-#signature##$2##signature#-#";
	}

	# find quoted text
	$text =~ s/^([\>:\|=]+[^\n\r]*[\n\r]*)$/#-#quote1##$1##quote1#-#/mg;
	$text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote2##$1##quote2#-#/si;
	$text =~ s/(--+\s*Forwarded\s+message.+\s*--+.*)$/#-#quote3##$1##quote3#-#/si;

	$text = html_escape($text . $sig);
	return $text;
}