--- trunk/httpd.pl 2004/05/06 19:46:58 14 +++ trunk/httpd.pl 2004/05/09 10:16:51 34 @@ -11,20 +11,37 @@ use CGI::Lite; use Template; use MWS; +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 $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; my $cgi = new CGI::Lite; -my $mws = MWS->new('global.conf'); +my $mws = MWS->new($config_file); my $tt = Template->new({ INCLUDE_PATH => $mws->{config}->val('global', 'templates'), FILTERS => { 'body5' => \&body5_filter, - 'subject_search' => \&subject_search_filter, + 'body' => \&body_filter, }, + EVAL_PERL => 1, }); +my $static_html = $mws->{config}->val('global', 'static_html'); + print "Web server ready at: ", $d->url, "\n"; @@ -68,12 +85,38 @@ my $url = $r->url->path; # XXX LOG - print $r->method," ",$url,Dumper($param); + print $r->method," ",$url,"\n"; + print Dumper($param),"\n" if ($debug); + + # is this static page? + if ($static_html && -f "$static_html/$url") { + print "static file: $static_html/$url\n" if ($debug); + $c->send_file_response("$static_html/$url"); + $c->close; + next; + } # 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 # @@ -81,32 +124,77 @@ 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'}) { + # 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"; + } - print STDERR "search: ",$param->{'search'},"\n"; + # ?show_id=XXXXxxxx___message_id___xxxxXXXX + if ($param->{'show_id'}) { - my $results = $mws->search($param->{'search'}); + $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; + $tpl_var->{results} = \@res if (@res); + $tpl_var->{total_hits} = $mws->{total_hits} || 0; + # no hits, offer suggestions + if (! $tpl_var->{results}) { + @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'}); + } - # - # ?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(); # @@ -125,30 +213,79 @@ # 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 = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); + my $escape_re = join '|' => keys %escape; + + $text =~ s/($escape_re)/$escape{$1}/gs; + + while ($text =~ s/#-#(quote|signature)(\d*)##(.+?)##\1\2#-#/$3<\/span>/gs) { } ; + + return $text; +} + #use Text::Context::EitherSide; sub body5_filter { my $text = shift; - $text =~ s/^\s+//gs; - $text =~ s/^[\>:\|=]+\s*.*?$//msg; # remove quoted text - $text =~ s/[\n\r]+/\n/gs; # compress cr/lf + + # remove quote + $text =~ s/^[\>:\|=]+\s*.*?$/#-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; + + # 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]*$/ .../; } - $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; # my $context = Text::Context::EitherSide->new($text, context => 5); # return $context->as_string("perl"); - return $text; + return html_escape($text); } -sub subject_search_filter { - my $s = shift; - # remove re: fdw: [list] preffixes from e-mail - while ( $s =~ s/^\s*\[(?:re|fwd):\s+(.+)\]\s*$/$1/ig || - $s =~ s/^\s*(?:re|fwd):\s+(.+?)\s*$/$1/ig || - $s =~ s/^\[\w+\]\s*//ig - ) { }; - return $s; +sub body_filter { + my $text = shift; + + my $sig = ''; + + # remove signature + if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) { + $sig = "$1#-#signature##$2##signature#-#"; + } + + # find quoted text + $text =~ s/^([\>:\|=]+\s*.*?)$/#-#quote1##$1##quote1#-#/msg; + $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; } +