1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
# based on post |
my $basedir = ''; |
4 |
# http://www.mail-archive.com/libwww@perl.org/msg04750.html |
BEGIN { |
5 |
|
$basedir = $0; |
6 |
|
my $loop = 0; # to prevent symlink loops |
7 |
|
while (-l $basedir && $loop++ < 20) { |
8 |
|
$basedir = readlink($basedir); |
9 |
|
} |
10 |
|
$basedir =~ s#/+[^/]+$#/lib#; |
11 |
|
} |
12 |
|
if ($basedir) { |
13 |
|
use lib "$basedir"; |
14 |
|
} |
15 |
|
|
16 |
|
=head1 NAME |
17 |
|
|
18 |
|
httpd.pl - http server for Mail::Box Web Search |
19 |
|
|
20 |
|
=head1 SYNOPSYS |
21 |
|
|
22 |
|
httpd.pl [local.conf] |
23 |
|
|
24 |
|
=head1 DESCRIPTION |
25 |
|
|
26 |
|
This script implements user interface for Mail::Box Web Search as |
27 |
|
a small single-user http server. |
28 |
|
|
29 |
|
=head1 SEE ALSO |
30 |
|
|
31 |
|
C<MWS> perl modules which are part of this package |
32 |
|
C<HTTP::Daemon::Simple> module which implements the server itself |
33 |
|
|
34 |
|
=cut |
35 |
|
|
36 |
use strict; |
use strict; |
37 |
use warnings; |
use warnings; |
38 |
use HTTP::Daemon; |
use MWS::SWISH; |
39 |
use HTTP::Status; |
#use MWS::Plucene; |
40 |
use IO::String; |
use HTTP::Daemon::Simple; |
|
use CGI::Lite; |
|
41 |
use Template; |
use Template; |
|
use MWS; |
|
42 |
use URI::Escape; |
use URI::Escape; |
43 |
|
|
44 |
use Data::Dumper; |
use Data::Dumper; |
56 |
exit 1; |
exit 1; |
57 |
} |
} |
58 |
|
|
59 |
my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; |
my $mws = MWS::SWISH->new(config_file => $config_file); |
60 |
my $cgi = new CGI::Lite; |
#my $mws = MWS::Plucene->new(config_file => $config_file, debug => $debug); |
61 |
my $mws = MWS->new($config_file); |
|
62 |
my $tt = Template->new({ |
my $tt = Template->new({ |
63 |
INCLUDE_PATH => $mws->{config}->val('global', 'templates'), |
INCLUDE_PATH => $mws->{config}->val('global', 'templates'), |
64 |
FILTERS => { |
FILTERS => { |
68 |
EVAL_PERL => 1, |
EVAL_PERL => 1, |
69 |
}); |
}); |
70 |
|
|
71 |
my $static_html = $mws->{config}->val('global', 'static_html'); |
my $d = new HTTP::Daemon::Simple( |
72 |
|
'listen' => $mws->{config}->val('global', 'listen'), |
73 |
|
'static_html' => $mws->{config}->val('global', 'static_html'), |
74 |
|
'debug' => $debug, |
75 |
|
) || die "can't create HTTP::Daemon::Simple: $!"; |
76 |
|
|
|
print "Web server ready at: ", $d->url, "\n"; |
|
77 |
|
|
78 |
|
print "Web server ready at: ", $d->url, "\n"; |
79 |
|
|
80 |
while ( my $c = $d->accept ) { |
$d->run_server( \&request ); |
|
while ( my $r = $c->get_request ) { |
|
|
|
|
|
# environs that a webserver should set. |
|
|
$ENV{'REQUEST_METHOD'} = $r->method; |
|
|
$ENV{'GATEWAY_INTERFACE'} = "CGI/1.0"; |
|
|
$ENV{'SERVER_PROTOCOL'} = $r->protocol; |
|
|
$ENV{'CONTENT_TYPE'} = $r->content_type; |
|
|
|
|
|
# this part is based on CGI::Lite |
|
|
|
|
|
$cgi->close_all_files(); |
|
|
$cgi->{web_data} = {}; |
|
|
$cgi->{ordered_keys} = []; |
|
|
$cgi->{all_handles} = []; |
|
|
$cgi->{error_status} = 0; |
|
|
$cgi->{error_message} = undef; |
|
|
|
|
|
if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) { |
|
|
my $query_string = $r->uri; |
|
|
$query_string =~ s/[^\?]+\?(.*)/$1/; |
|
|
$cgi->_decode_url_encoded_data (\$query_string, 'form'); |
|
|
|
|
|
} elsif ( $r->method eq 'POST' ) { |
|
|
|
|
|
if ($r->content_type eq 'application/x-www-form-urlencoded') { |
|
|
# local $^W = 0; |
|
|
$cgi->_decode_url_encoded_data (\$r->content, 'form'); |
|
|
} elsif ($r->content_type =~ /multipart\/form-data/) { |
|
|
my ($boundary) = $r->content_type =~ /boundary=(\S+)$/; |
|
|
$cgi->_parse_multipart_data ($r->content_length, $boundary); |
|
|
} |
|
|
} else { |
|
|
$c->send_error(RC_FORBIDDEN); |
|
|
} |
|
81 |
|
|
82 |
my $param = $cgi->{web_data}; |
sub request($$) { |
83 |
my $url = $r->url->path; |
my ($url,$param) = @_; |
84 |
|
|
85 |
# XXX LOG |
print Dumper($param,$mws->{counter}),"\n" if ($debug); |
|
print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug); |
|
86 |
|
|
87 |
# is this static page? |
# template file name (use ?format=html as default) |
88 |
if ($static_html && -f "$static_html/$url") { |
my $tpl_file = 'master.'; |
89 |
print "static file: $static_html/$url\n" if ($debug); |
$tpl_file .= $param->{'format'} || 'html'; |
|
$c->send_file_response("$static_html/$url"); |
|
|
$c->close; |
|
|
next; |
|
|
} |
|
90 |
|
|
91 |
# template file name (use ?format=html as default) |
# parse date from url |
92 |
my $tpl_file = 'master.'; |
my ($yyyy,$mm,$dd) = $mws->yyyymmdd; |
|
$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); |
|
|
} |
|
93 |
|
|
94 |
# |
my $yyyymm; |
|
# 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); |
|
|
} |
|
95 |
|
|
96 |
my $tpl_var = { |
my $date_limit; |
|
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"; |
|
|
} |
|
97 |
|
|
98 |
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) { |
99 |
if ($param->{'show_id'}) { |
($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3); |
100 |
|
$date_limit = "$yyyy-$mm-$dd"; |
101 |
|
} elsif ($url =~ m,^/(\d{4})[/-](\d+),) { |
102 |
|
($yyyy,$mm) = $mws->fmtdate($1,$2); |
103 |
|
$date_limit = "$yyyy-$mm"; |
104 |
|
} elsif ($url =~ m,^/(\d{4}),) { |
105 |
|
$date_limit = $mws->fmtdate($1); |
106 |
|
} |
107 |
|
|
108 |
$mws->reset_counters; |
# |
109 |
my $row = $mws->fetch_result_by_id($param->{'show_id'}); |
# implement functionality and generate HTML |
110 |
$tpl_var->{message} = $row; |
# |
111 |
} elsif ($param->{'search'} || $date_limit) { |
my $html; |
112 |
|
|
113 |
|
if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) { |
114 |
|
$param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'}; |
115 |
|
} elsif ($param->{'search'}) { |
116 |
|
($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2); |
117 |
|
} |
118 |
|
|
119 |
# show search results |
my $tpl_var = { |
120 |
# ?search=foo:bar |
param => $param, |
121 |
|
yyyy => $yyyy, |
122 |
|
mm => $mm, |
123 |
|
dd => $dd, |
124 |
|
date_limit => $date_limit, |
125 |
|
}; |
126 |
|
|
127 |
|
# is this access to root of web server? |
128 |
|
if ($url eq "/" && !$param->{'search'}) { |
129 |
|
# if first access, go to current year |
130 |
|
$date_limit = $mws->fmtdate($yyyy); |
131 |
|
$param->{sort_by} = "date desc"; |
132 |
|
} |
133 |
|
|
134 |
my @search; |
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
135 |
push @search, $param->{'search'} if ($param->{'search'}); |
if ($param->{'show_id'}) { |
136 |
|
|
137 |
if ($date_limit) { |
$mws->reset_counters; |
138 |
push @search, "and" if (@search); |
my $row = $mws->fetch_result_by_id($param->{'show_id'}); |
139 |
push @search, "date:\"$date_limit\""; |
$tpl_var->{message} = $row; |
140 |
} |
} elsif ($param->{'search'} || $date_limit) { |
141 |
|
|
142 |
|
# show search results |
143 |
|
# ?search=foo:bar |
144 |
|
|
145 |
|
my @search; |
146 |
|
push @search, $param->{'search'} if ($param->{'search'}); |
147 |
|
|
148 |
|
if ($date_limit) { |
149 |
|
push @search, "and" if (@search); |
150 |
|
push @search, "date:\"$date_limit\""; |
151 |
|
} |
152 |
|
|
153 |
if ($param->{sort_by}) { |
if ($param->{sort_by}) { |
154 |
push @search, "sort:".$param->{sort_by}; |
push @search, "sort:".$param->{sort_by}; |
155 |
} |
} |
156 |
|
|
157 |
print STDERR "search: ",join(" ",@search),"\n"; |
print STDERR "search: ",join(" ",@search),"\n"; |
158 |
|
|
159 |
my $results = $mws->search(@search); |
my $results = $mws->search(@search); |
160 |
my @res = $mws->fetch_all_results(); |
my @res = $mws->fetch_all_results(); |
161 |
|
|
162 |
$tpl_var->{results} = \@res if (@res); |
$tpl_var->{results} = \@res if (@res); |
163 |
$tpl_var->{total_hits} = $mws->{total_hits} || 0; |
$tpl_var->{total_hits} = $mws->{total_hits} || 0; |
164 |
|
|
165 |
|
# no hits, offer suggestions |
166 |
|
if (! $tpl_var->{results} && $param->{'search_fld'} && $param->{'search_val'}) { |
167 |
|
@{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'}); |
168 |
} |
} |
169 |
|
|
170 |
|
} |
171 |
|
|
172 |
# push counters to template |
# push counters to template |
173 |
foreach my $f (qw(from to cc bcc)) { |
foreach my $f (qw(from to cc bcc folder)) { |
174 |
my $h = $mws->counter($f) || next; |
my $h = $mws->counter($f) || next; |
175 |
my @a; |
my @a; |
176 |
foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) { |
foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) { |
177 |
push @a, $h->{$k}; |
push @a, $h->{$k}; |
|
} |
|
|
$tpl_var->{counters}->{$f} = [ @a ] if (@a); |
|
178 |
} |
} |
179 |
|
$tpl_var->{counters}->{$f} = [ @a ] if (@a); |
180 |
|
} |
181 |
|
|
182 |
# push calendar in template |
# push calendar in template |
183 |
$tpl_var->{calendar} = $mws->counter('calendar'); |
$tpl_var->{calendar} = $mws->counter('calendar'); |
|
|
|
|
$tt->process($tpl_file, $tpl_var, \$html) || die $tt->error(); |
|
|
|
|
|
# |
|
|
# send HTMLto client |
|
|
# |
|
|
|
|
|
my $res = HTTP::Response->new(RC_OK); |
|
|
$res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' ); |
|
|
$res->content($html); |
|
|
$c->send_response($res); |
|
184 |
|
|
185 |
$c->close; |
$tt->process($tpl_file, $tpl_var, \$html) || die $tt->error(); |
186 |
} |
return $html; |
187 |
undef($c); |
}; |
|
} |
|
188 |
|
|
189 |
# template toolkit filter |
# template toolkit filter |
190 |
|
|
191 |
sub html_escape($) { |
sub html_escape($) { |
192 |
my $text = shift; |
my $text = shift || return; |
193 |
|
|
194 |
|
# don't re-escape html |
195 |
|
#return $text if ($text =~ /&(?:lt|gt|amp|quot);/); |
196 |
|
|
197 |
# Escape <, >, & and ", and to produce valid XML |
# Escape <, >, & and ", and to produce valid XML |
198 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
199 |
my $escape_re = join '|' => keys %escape; |
my $escape_re = join '|' => keys %escape; |
200 |
|
|
201 |
$text =~ s/($escape_re)/$escape{$1}/gs; |
$text =~ s/($escape_re)/$escape{$1}/gs; |
202 |
|
|
203 |
|
while ($text =~ s/#-#(quote|signature)(\d*)##(.+?)##\1\2#-#/<span class="$1">$3<\/span>/gs) { } ; |
204 |
|
|
205 |
return $text; |
return $text; |
206 |
} |
} |
207 |
|
|
211 |
my $text = shift; |
my $text = shift; |
212 |
|
|
213 |
# remove quote |
# remove quote |
214 |
$text =~ s/^[\>:\|=]+\s*.*?$/#-q-#/msg; |
$text =~ s/^[\>:\|=]+[^\n\r]*[\n\r]*$/#-q-#/msg; |
215 |
# remove quote author |
# remove quote author |
216 |
$text =~ s/[\n\r]+[^\n\r]+:\s*(:?#-q-#[\n\r*])+//gs; |
$text =~ s/[\n\r]+[^\n\r]+:\s*(?:#-q-#[\n\r*])+//gs; |
217 |
$text =~ s/^[^\n\r]+:\s*(:?#-q-#[\n\r]*)+//gs; |
$text =~ s/^[^\n\r]+:\s*(?:#-q-#[\n\r]*)+//gs; |
218 |
|
$text =~ s/#-q-#[\n\r]*//gs; |
219 |
# outlook quoting |
# outlook quoting |
220 |
$text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si; |
$text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si; |
221 |
$text =~ s/(\s*--+\s*Forwarded\s+message\s*from\s+.+\s*--+.*)$//si; |
$text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si; |
222 |
|
|
223 |
# remove signature |
# remove signature |
224 |
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
$text =~ s/(?:^|[\n\r]+)*--\s*[\n\r]+.*$//s; |
225 |
|
$text =~ s/(?:^|[\n\r]+)*_____+[\n\r]+.*$//s; |
226 |
|
|
227 |
# compress cr/lf |
# compress cr/lf |
228 |
$text =~ s/[\n\r]+/\n/gs; |
$text =~ s/[\n\r]+/\n/gs; |
231 |
$text =~ s/^\n+//gs; |
$text =~ s/^\n+//gs; |
232 |
$text =~ s/[\s\n]+$//gs; |
$text =~ s/[\s\n]+$//gs; |
233 |
|
|
234 |
|
if ($text eq "") { |
235 |
|
$text="#-#quote##forwarded message##quote#-#"; |
236 |
|
} |
237 |
|
|
238 |
# cut to 5 lines; |
# cut to 5 lines; |
239 |
if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { |
if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { |
240 |
$text =~ s/[\n\r]*$/ .../; |
$text =~ s/[\n\r]*$/ .../; |
254 |
# remove signature |
# remove signature |
255 |
if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) { |
if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) { |
256 |
$sig = "$1#-#signature##$2##signature#-#"; |
$sig = "$1#-#signature##$2##signature#-#"; |
257 |
|
} elsif ($text =~s/(^|[\n\r]+)*(_____+[\n\r]+.*)$//s) { |
258 |
|
$sig = "$1#-#signature##$2##signature#-#"; |
259 |
} |
} |
260 |
|
|
261 |
# find quoted text |
# find quoted text |
262 |
$text =~ s/^([\>:\|=]+\s*.*?)$/#-#quote##$1##quote#-#/msg; |
$text =~ s/^([\>:\|=]+[^\n\r]*[\n\r]*)$/#-#quote1##$1##quote1#-#/mg; |
263 |
$text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote##$1##quote#-#/si || $text =~ s/(--+\s*Forwarded\s+message\s*from\s+.+\s*--+.*)$/#-#quote##$1##quote#-#/si; |
$text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote2##$1##quote2#-#/si; |
264 |
|
$text =~ s/(--+\s*Forwarded\s+message.+\s*--+.*)$/#-#quote3##$1##quote3#-#/si; |
265 |
|
|
266 |
$text = html_escape($text . $sig); |
$text = html_escape($text . $sig); |
|
$text =~ s/#-#(quote|signature)##(.+?)##(\1)#-#/<span class="$1">$2<\/span>/gs; |
|
267 |
return $text; |
return $text; |
268 |
} |
} |
269 |
|
|