--- trunk/httpd.pl 2004/05/06 16:53:40 13 +++ trunk/httpd.pl 2004/05/08 14:27:54 24 @@ -11,9 +11,12 @@ use CGI::Lite; use Template; use MWS; +use URI::Escape; use Data::Dumper; +my $debug = 1; + my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; my $cgi = new CGI::Lite; my $mws = MWS->new('global.conf'); @@ -22,6 +25,7 @@ FILTERS => { 'body5' => \&body5_filter, }, + EVAL_PERL => 1, }); print "Web server ready at: ", $d->url, "\n"; @@ -67,12 +71,29 @@ my $url = $r->url->path; # XXX LOG - print $r->method," ",$url,Dumper($param); + print $r->method," ",$url,"\n",Dumper($param),"\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 # @@ -80,32 +101,65 @@ 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 + param => $param, + yyyy => $yyyy, + mm => $mm, + dd => $dd, + date_limit => $date_limit, }; - # show search results - # ?search=foo:bar - if ($param->{'search'}) { + # + # ?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 = ( $param->{'search'} ); - print STDERR "search: ",$param->{'search'},"\n"; + 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($param->{'search'}); + my $results = $mws->search(@search); my @res = $mws->fetch_all_results(); - $tpl_var->{results} = \@res; + $tpl_var->{results} = \@res if (@res); + $tpl_var->{total_hits} = $mws->{total_hits}; + } - # - # ?show_id=XXXXxxxx___message_id___xxxxXXXX - } elsif ($param->{'show_id'}) { - - my $row = $mws->fetch_result_by_id($param->{'show_id'}); - $tpl_var->{message} = $row; + + # push counters to template + foreach my $f (qw(from to cc bcc)) { + 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(); # @@ -113,7 +167,7 @@ # my $res = HTTP::Response->new(RC_OK); - $res->header( 'Content-type' => 'text/html; charset=UTF-8' ); + $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' ); $res->content($html); $c->send_response($res); @@ -131,7 +185,9 @@ $text =~ s/^\s+//gs; $text =~ s/^[\>:\|=]+\s*.*?$//msg; # remove quoted text $text =~ s/[\n\r]+/\n/gs; # compress cr/lf - $text =~ s,^((?:.*?[\n\r]){5}).*$,$1--8<--[cut]--8<--,s; + if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { + $text =~ s/[\n\r]*$/ .../; + } $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; # my $context = Text::Context::EitherSide->new($text, context => 5);