--- trunk/httpd.pl 2004/05/07 23:35:39 20 +++ trunk/httpd.pl 2004/05/08 20:34:26 27 @@ -11,23 +11,36 @@ 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, }, EVAL_PERL => 1, }); +my $static_html = $mws->{config}->val('global', 'static_html'); + print "Web server ready at: ", $d->url, "\n"; @@ -73,6 +86,14 @@ # XXX LOG print $r->method," ",$url,"\n",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'; @@ -110,36 +131,49 @@ yyyy => $yyyy, mm => $mm, dd => $dd, + date_limit => $date_limit, }; - if ($date_limit) { - $param->{'search'} .= "and " if ($param->{'search'}); - $param->{'search'} .= $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} = "date desc"; } - # show search results - # ?search=foo:bar - if ($param->{'search'}) { + # ?show_id=XXXXxxxx___message_id___xxxxXXXX + if ($param->{'show_id'}) { - print STDERR "search: ",$param->{'search'},"\n"; + $mws->reset_counters; + my $row = $mws->fetch_result_by_id($param->{'show_id'}); + $tpl_var->{message} = $row; + } elsif ($param->{'search'} || $date_limit) { - my $results = $mws->search($param->{'search'}); - my @res = $mws->fetch_all_results(); + # show search results + # ?search=foo:bar - $tpl_var->{results} = \@res if (@res); - $tpl_var->{total_hits} = $mws->{total_hits}; + my @search; + push @search, $param->{'search'} if ($param->{'search'}); + if ($date_limit) { + push @search, "and" if (@search); + push @search, "date:\"$date_limit\""; + } - # - # ?show_id=XXXXxxxx___message_id___xxxxXXXX - } elsif ($param->{'show_id'}) { + 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; - $mws->reset_counters; - my $row = $mws->fetch_result_by_id($param->{'show_id'}); - $tpl_var->{message} = $row; } -print Dumper($mws->{counter}); # push counters to template foreach my $f (qw(from to cc bcc)) { @@ -190,15 +224,3 @@ return $text; } -sub subject_search_filter { - my $s = shift; - # remove re: fdw: [list] preffixes from e-mail - while ( $s =~ s/^\s*\[(?:re|fwd|fw):\s+(.+)\]\s*$/$1/ig || - $s =~ s/^\s*(?:re|fwd|fw):\s+(.+?)\s*$/$1/ig || - $s =~ s/^\[\S+\]\s*//ig || - $s =~ s/^\[[^@]+@\w+\.\w+\s*:\s+(.+)\s*\]\s*$/$1/g || - $s =~ s/\(fwd\)\s*$//ig || - $s =~ s/\"//g - ) { }; - return $s; -}