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