11 |
use CGI::Lite; |
use CGI::Lite; |
12 |
use Template; |
use Template; |
13 |
use MWS; |
use MWS; |
14 |
|
use URI::Escape; |
15 |
|
|
16 |
use Data::Dumper; |
use Data::Dumper; |
17 |
|
|
18 |
my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; |
my $debug = 1; |
19 |
print "Please contact me at: <URL:", $d->url, ">\n"; |
|
20 |
|
my $config_file = shift @ARGV || 'global.conf'; |
21 |
|
|
22 |
|
if (! -f $config_file) { |
23 |
|
print qq{Usage: $0 [/path/to/local.conf] |
24 |
|
|
25 |
|
If local.conf is not specified, global.conf in current directory will |
26 |
|
be used. |
27 |
|
}; |
28 |
|
exit 1; |
29 |
|
} |
30 |
|
|
31 |
|
my $mws = MWS->new($config_file); |
32 |
|
|
33 |
|
my ($local_addr,$local_port) = ('127.0.0.1',6969); |
34 |
|
|
35 |
|
my $listen = $mws->{config}->val('global', 'listen'); |
36 |
|
if ($listen =~ m/:/) { |
37 |
|
($local_addr,$local_port) = split(/:/,$listen); |
38 |
|
} elsif ($listen) { |
39 |
|
$local_addr = $listen; |
40 |
|
} |
41 |
|
|
42 |
|
my $d = HTTP::Daemon->new( |
43 |
|
Reuse => 1, |
44 |
|
LocalAddr => $local_addr, |
45 |
|
LocalPort => $local_port, |
46 |
|
) || die "can't create HTTP::Daemon"; |
47 |
|
|
48 |
my $cgi = new CGI::Lite; |
my $cgi = new CGI::Lite; |
|
my $mws = MWS->new('global.conf'); |
|
49 |
my $tt = Template->new({ |
my $tt = Template->new({ |
50 |
INCLUDE_PATH => $mws->{config}->val('global', 'templates'), |
INCLUDE_PATH => $mws->{config}->val('global', 'templates'), |
51 |
FILTERS => { |
FILTERS => { |
52 |
'body5' => \&body5_filter, |
'body5' => \&body5_filter, |
53 |
|
'body' => \&body_filter, |
54 |
}, |
}, |
55 |
|
EVAL_PERL => 1, |
56 |
}); |
}); |
57 |
|
|
58 |
|
my $static_html = $mws->{config}->val('global', 'static_html'); |
59 |
|
|
60 |
|
print "Web server ready at: ", $d->url, "\n"; |
61 |
|
|
62 |
|
|
63 |
while ( my $c = $d->accept ) { |
while ( my $c = $d->accept ) { |
64 |
while ( my $r = $c->get_request ) { |
while ( my $r = $c->get_request ) { |
100 |
my $url = $r->url->path; |
my $url = $r->url->path; |
101 |
|
|
102 |
# XXX LOG |
# XXX LOG |
103 |
print $r->method," ",$url,Dumper($param); |
print $r->method," ",$url,"\n"; |
104 |
|
print Dumper($param),"\n" if ($debug); |
105 |
|
|
106 |
|
# is this static page? |
107 |
|
if ($static_html && -f "$static_html/$url") { |
108 |
|
print "static file: $static_html/$url\n" if ($debug); |
109 |
|
$c->send_file_response("$static_html/$url"); |
110 |
|
$c->close; |
111 |
|
next; |
112 |
|
} |
113 |
|
|
114 |
# template file name (use ?format=html as default) |
# template file name (use ?format=html as default) |
115 |
my $tpl_file = 'master.'; |
my $tpl_file = 'master.'; |
116 |
$tpl_file .= $param->{'format'} || 'html'; |
$tpl_file .= $param->{'format'} || 'html'; |
117 |
|
|
118 |
|
# parse date from url |
119 |
|
my ($yyyy,$mm,$dd) = $mws->yyyymmdd; |
120 |
|
|
121 |
|
my $yyyymm; |
122 |
|
|
123 |
|
my $date_limit; |
124 |
|
|
125 |
|
if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) { |
126 |
|
($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3); |
127 |
|
$date_limit = "$yyyy-$mm-$dd"; |
128 |
|
} elsif ($url =~ m,^/(\d{4})[/-](\d+),) { |
129 |
|
($yyyy,$mm) = $mws->fmtdate($1,$2); |
130 |
|
$date_limit = "$yyyy-$mm"; |
131 |
|
} elsif ($url =~ m,^/(\d{4}),) { |
132 |
|
$date_limit = $mws->fmtdate($1); |
133 |
|
} |
134 |
|
|
135 |
# |
# |
136 |
# implement functionality and generate HTML |
# implement functionality and generate HTML |
137 |
# |
# |
138 |
my $html; |
my $html; |
139 |
|
|
140 |
# show search results |
if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) { |
141 |
# ?search=foo:bar |
$param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'}; |
142 |
if ($param->{'search'}) { |
} elsif ($param->{'search'}) { |
143 |
|
($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2); |
144 |
|
} |
145 |
|
|
146 |
print STDERR "search: ",$param->{'search'},"\n"; |
my $tpl_var = { |
147 |
|
param => $param, |
148 |
|
yyyy => $yyyy, |
149 |
|
mm => $mm, |
150 |
|
dd => $dd, |
151 |
|
date_limit => $date_limit, |
152 |
|
}; |
153 |
|
|
154 |
|
# is this access to root of web server? |
155 |
|
if ($url eq "/" && !$param->{'search'}) { |
156 |
|
# if first access, go to current year |
157 |
|
$date_limit = $mws->fmtdate($yyyy); |
158 |
|
$param->{sort_by} = "date desc"; |
159 |
|
} |
160 |
|
|
161 |
my $results = $mws->search($param->{'search'}); |
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
162 |
|
if ($param->{'show_id'}) { |
163 |
|
|
164 |
|
$mws->reset_counters; |
165 |
|
my $row = $mws->fetch_result_by_id($param->{'show_id'}); |
166 |
|
$tpl_var->{message} = $row; |
167 |
|
} elsif ($param->{'search'} || $date_limit) { |
168 |
|
|
169 |
|
# show search results |
170 |
|
# ?search=foo:bar |
171 |
|
|
172 |
|
my @search; |
173 |
|
push @search, $param->{'search'} if ($param->{'search'}); |
174 |
|
|
175 |
|
if ($date_limit) { |
176 |
|
push @search, "and" if (@search); |
177 |
|
push @search, "date:\"$date_limit\""; |
178 |
|
} |
179 |
|
|
180 |
|
if ($param->{sort_by}) { |
181 |
|
push @search, "sort:".$param->{sort_by}; |
182 |
|
} |
183 |
|
|
184 |
|
print STDERR "search: ",join(" ",@search),"\n"; |
185 |
|
|
186 |
|
my $results = $mws->search(@search); |
187 |
my @res = $mws->fetch_all_results(); |
my @res = $mws->fetch_all_results(); |
188 |
|
|
189 |
$tt->process($tpl_file, { |
$tpl_var->{results} = \@res if (@res); |
190 |
query => $param->{'search'}, |
$tpl_var->{total_hits} = $mws->{total_hits} || 0; |
|
results => \@res, |
|
|
param => $param, |
|
|
}, \$html) || die $tt->error(); |
|
191 |
|
|
192 |
# |
# no hits, offer suggestions |
193 |
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
if (! $tpl_var->{results}) { |
194 |
} elsif ($param->{'show_id'}) { |
@{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'}); |
195 |
|
} |
196 |
my $row = $mws->fetch_result_by_id($param->{'show_id'}); |
|
197 |
$tt->process($tpl_file, { |
} |
198 |
message => $row, |
|
199 |
}, \$html) || die $tt->error(); |
|
200 |
|
# push counters to template |
201 |
|
foreach my $f (qw(from to cc bcc)) { |
202 |
|
my $h = $mws->counter($f) || next; |
203 |
|
my @a; |
204 |
|
foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) { |
205 |
|
push @a, $h->{$k}; |
206 |
|
} |
207 |
|
$tpl_var->{counters}->{$f} = [ @a ] if (@a); |
208 |
} |
} |
209 |
|
|
210 |
|
# push calendar in template |
211 |
|
$tpl_var->{calendar} = $mws->counter('calendar'); |
212 |
|
|
213 |
|
$tt->process($tpl_file, $tpl_var, \$html) || die $tt->error(); |
214 |
|
|
215 |
# |
# |
216 |
# send HTMLto client |
# send HTMLto client |
217 |
# |
# |
218 |
|
|
219 |
my $res = HTTP::Response->new(RC_OK); |
my $res = HTTP::Response->new(RC_OK); |
220 |
$res->header( 'Content-type' => 'text/html; charset=iso-8859-2' ); |
$res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' ); |
221 |
$res->content($html); |
$res->content($html); |
222 |
$c->send_response($res); |
$c->send_response($res); |
223 |
|
|
228 |
|
|
229 |
# template toolkit filter |
# template toolkit filter |
230 |
|
|
231 |
|
sub html_escape($) { |
232 |
|
my $text = shift || return; |
233 |
|
|
234 |
|
# don't re-escape html |
235 |
|
#return $text if ($text =~ /&(?:lt|gt|amp|quot);/); |
236 |
|
|
237 |
|
# Escape <, >, & and ", and to produce valid XML |
238 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
239 |
|
my $escape_re = join '|' => keys %escape; |
240 |
|
|
241 |
|
$text =~ s/($escape_re)/$escape{$1}/gs; |
242 |
|
|
243 |
|
while ($text =~ s/#-#(quote|signature)(\d*)##(.+?)##\1\2#-#/<span class="$1">$3<\/span>/gs) { } ; |
244 |
|
|
245 |
|
return $text; |
246 |
|
} |
247 |
|
|
248 |
|
#use Text::Context::EitherSide; |
249 |
|
|
250 |
sub body5_filter { |
sub body5_filter { |
251 |
my $text = shift; |
my $text = shift; |
252 |
$text =~ s/^\s+//gs; |
|
253 |
$text =~ s/[\n\r]+[\>:\|=]+\s*.*?[\n\r]+//sg; # remove quoted text |
# remove quote |
254 |
$text =~ s,^((?:.*?[\n\r]){5}).*$,$1<span style="color:#808080">--8<--[cut]--8<--</span>,s; |
$text =~ s/^[\>:\|=]+[^\n\r]*[\n\r]*$/#-q-#/msg; |
255 |
|
# remove quote author |
256 |
|
$text =~ s/[\n\r]+[^\n\r]+:\s*(?:#-q-#[\n\r*])+//gs; |
257 |
|
$text =~ s/^[^\n\r]+:\s*(?:#-q-#[\n\r]*)+//gs; |
258 |
|
$text =~ s/#-q-#[\n\r]*//gs; |
259 |
|
# outlook quoting |
260 |
|
$text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si; |
261 |
|
$text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si; |
262 |
|
|
263 |
|
# remove signature |
264 |
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
265 |
|
|
266 |
|
# compress cr/lf |
267 |
|
$text =~ s/[\n\r]+/\n/gs; |
268 |
|
|
269 |
|
# remove whitespaces |
270 |
|
$text =~ s/^\n+//gs; |
271 |
|
$text =~ s/[\s\n]+$//gs; |
272 |
|
|
273 |
|
if ($text eq "") { |
274 |
|
$text="#-#quote##forwarded message##quote#-#"; |
275 |
|
} |
276 |
|
|
277 |
|
# cut to 5 lines; |
278 |
|
if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { |
279 |
|
$text =~ s/[\n\r]*$/ .../; |
280 |
|
} |
281 |
|
|
282 |
|
# my $context = Text::Context::EitherSide->new($text, context => 5); |
283 |
|
# return $context->as_string("perl"); |
284 |
|
|
285 |
|
return html_escape($text); |
286 |
|
} |
287 |
|
|
288 |
|
sub body_filter { |
289 |
|
my $text = shift; |
290 |
|
|
291 |
|
my $sig = ''; |
292 |
|
|
293 |
|
# remove signature |
294 |
|
if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) { |
295 |
|
$sig = "$1#-#signature##$2##signature#-#"; |
296 |
|
} |
297 |
|
|
298 |
|
# find quoted text |
299 |
|
$text =~ s/^([\>:\|=]+[^\n\r]*[\n\r]*)$/#-#quote1##$1##quote1#-#/mg; |
300 |
|
$text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote2##$1##quote2#-#/si; |
301 |
|
$text =~ s/(--+\s*Forwarded\s+message.+\s*--+.*)$/#-#quote3##$1##quote3#-#/si; |
302 |
|
|
303 |
|
$text = html_escape($text . $sig); |
304 |
return $text; |
return $text; |
305 |
} |
} |
306 |
|
|