3 |
# based on post |
# based on post |
4 |
# http://www.mail-archive.com/libwww@perl.org/msg04750.html |
# http://www.mail-archive.com/libwww@perl.org/msg04750.html |
5 |
|
|
6 |
|
BEGIN { |
7 |
|
my $basedir = readlink($0) || $0; $basedir =~ s#/[^/]+$##; |
8 |
|
unshift(@INC, $basedir); |
9 |
|
} |
10 |
|
|
11 |
use strict; |
use strict; |
12 |
use warnings; |
use warnings; |
13 |
|
use MWS::SWISH; |
14 |
|
#use MWS::Plucene; |
15 |
use HTTP::Daemon; |
use HTTP::Daemon; |
16 |
use HTTP::Status; |
use HTTP::Status; |
17 |
use IO::String; |
use IO::String; |
18 |
use CGI::Lite; |
use CGI::Lite; |
19 |
use Template; |
use Template; |
20 |
use MWS; |
use URI::Escape; |
21 |
|
|
22 |
use Data::Dumper; |
use Data::Dumper; |
23 |
|
|
24 |
my $debug = 1; |
my $debug = 1; |
25 |
|
|
26 |
my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; |
my $config_file = shift @ARGV || 'global.conf'; |
27 |
|
|
28 |
|
if (! -f $config_file) { |
29 |
|
print qq{Usage: $0 [/path/to/local.conf] |
30 |
|
|
31 |
|
If local.conf is not specified, global.conf in current directory will |
32 |
|
be used. |
33 |
|
}; |
34 |
|
exit 1; |
35 |
|
} |
36 |
|
|
37 |
|
my $mws = MWS::SWISH->new(config_file => $config_file); |
38 |
|
#my $mws = MWS::Plucene->new(config_file => $config_file, debug => $debug); |
39 |
|
|
40 |
|
my ($local_addr,$local_port) = ('127.0.0.1',6969); |
41 |
|
|
42 |
|
my $listen = $mws->{config}->val('global', 'listen'); |
43 |
|
|
44 |
|
print STDERR "using listen $listen\n" if ($listen); |
45 |
|
|
46 |
|
if ($listen && $listen =~ m/:/) { |
47 |
|
($local_addr,$local_port) = split(/:/,$listen,2); |
48 |
|
} elsif ($listen) { |
49 |
|
$local_addr = $listen; |
50 |
|
} |
51 |
|
|
52 |
|
my $d = HTTP::Daemon->new( |
53 |
|
Reuse => 1, |
54 |
|
LocalAddr => $local_addr, |
55 |
|
LocalPort => $local_port, |
56 |
|
) || die "can't create HTTP::Daemon on $local_addr:$local_port: $!"; |
57 |
|
|
58 |
my $cgi = new CGI::Lite; |
my $cgi = new CGI::Lite; |
|
my $mws = MWS->new('global.conf'); |
|
59 |
my $tt = Template->new({ |
my $tt = Template->new({ |
60 |
INCLUDE_PATH => $mws->{config}->val('global', 'templates'), |
INCLUDE_PATH => $mws->{config}->val('global', 'templates'), |
61 |
FILTERS => { |
FILTERS => { |
62 |
'body5' => \&body5_filter, |
'body5' => \&body5_filter, |
63 |
'subject_search' => \&subject_search_filter, |
'body' => \&body_filter, |
64 |
}, |
}, |
65 |
EVAL_PERL => 1, |
EVAL_PERL => 1, |
66 |
}); |
}); |
67 |
|
|
68 |
|
my $static_html = $mws->{config}->val('global', 'static_html'); |
69 |
|
|
70 |
print "Web server ready at: ", $d->url, "\n"; |
print "Web server ready at: ", $d->url, "\n"; |
71 |
|
|
72 |
|
|
110 |
my $url = $r->url->path; |
my $url = $r->url->path; |
111 |
|
|
112 |
# XXX LOG |
# XXX LOG |
113 |
print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug); |
print $r->method," ",$url,"\n"; |
114 |
|
print Dumper($param,$mws->{counter}),"\n" if ($debug); |
115 |
|
|
116 |
|
# is this static page? |
117 |
|
if ($static_html && -f "$static_html/$url") { |
118 |
|
print "static file: $static_html/$url\n" if ($debug); |
119 |
|
$c->send_file_response("$static_html/$url"); |
120 |
|
$c->close; |
121 |
|
next; |
122 |
|
} |
123 |
|
|
124 |
# template file name (use ?format=html as default) |
# template file name (use ?format=html as default) |
125 |
my $tpl_file = 'master.'; |
my $tpl_file = 'master.'; |
161 |
date_limit => $date_limit, |
date_limit => $date_limit, |
162 |
}; |
}; |
163 |
|
|
164 |
# |
# is this access to root of web server? |
165 |
|
if ($url eq "/" && !$param->{'search'}) { |
166 |
|
# if first access, go to current year |
167 |
|
$date_limit = $mws->fmtdate($yyyy); |
168 |
|
$param->{sort_by} = "date desc"; |
169 |
|
} |
170 |
|
|
171 |
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
172 |
if ($param->{'show_id'}) { |
if ($param->{'show_id'}) { |
173 |
|
|
179 |
# show search results |
# show search results |
180 |
# ?search=foo:bar |
# ?search=foo:bar |
181 |
|
|
182 |
my @search = ( $param->{'search'} ); |
my @search; |
183 |
|
push @search, $param->{'search'} if ($param->{'search'}); |
184 |
|
|
185 |
if ($date_limit) { |
if ($date_limit) { |
186 |
push @search, "and" if (@search); |
push @search, "and" if (@search); |
187 |
push @search, "date:\"$date_limit\""; |
push @search, "date:\"$date_limit\""; |
188 |
} |
} |
189 |
|
|
190 |
|
if ($param->{sort_by}) { |
191 |
|
push @search, "sort:".$param->{sort_by}; |
192 |
|
} |
193 |
|
|
194 |
print STDERR "search: ",join(" ",@search),"\n"; |
print STDERR "search: ",join(" ",@search),"\n"; |
195 |
|
|
196 |
my $results = $mws->search(@search); |
my $results = $mws->search(@search); |
197 |
my @res = $mws->fetch_all_results(); |
my @res = $mws->fetch_all_results(); |
198 |
|
|
199 |
$tpl_var->{results} = \@res if (@res); |
$tpl_var->{results} = \@res if (@res); |
200 |
$tpl_var->{total_hits} = $mws->{total_hits}; |
$tpl_var->{total_hits} = $mws->{total_hits} || 0; |
201 |
|
|
202 |
|
# no hits, offer suggestions |
203 |
|
if (! $tpl_var->{results}) { |
204 |
|
@{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'}); |
205 |
|
} |
206 |
|
|
207 |
} |
} |
208 |
|
|
209 |
|
|
210 |
# push counters to template |
# push counters to template |
211 |
foreach my $f (qw(from to cc bcc)) { |
foreach my $f (qw(from to cc bcc folder)) { |
212 |
my $h = $mws->counter($f) || next; |
my $h = $mws->counter($f) || next; |
213 |
my @a; |
my @a; |
214 |
foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) { |
foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) { |
238 |
|
|
239 |
# template toolkit filter |
# template toolkit filter |
240 |
|
|
241 |
|
sub html_escape($) { |
242 |
|
my $text = shift || return; |
243 |
|
|
244 |
|
# don't re-escape html |
245 |
|
#return $text if ($text =~ /&(?:lt|gt|amp|quot);/); |
246 |
|
|
247 |
|
# Escape <, >, & and ", and to produce valid XML |
248 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
249 |
|
my $escape_re = join '|' => keys %escape; |
250 |
|
|
251 |
|
$text =~ s/($escape_re)/$escape{$1}/gs; |
252 |
|
|
253 |
|
while ($text =~ s/#-#(quote|signature)(\d*)##(.+?)##\1\2#-#/<span class="$1">$3<\/span>/gs) { } ; |
254 |
|
|
255 |
|
return $text; |
256 |
|
} |
257 |
|
|
258 |
#use Text::Context::EitherSide; |
#use Text::Context::EitherSide; |
259 |
|
|
260 |
sub body5_filter { |
sub body5_filter { |
261 |
my $text = shift; |
my $text = shift; |
262 |
$text =~ s/^\s+//gs; |
|
263 |
$text =~ s/^[\>:\|=]+\s*.*?$//msg; # remove quoted text |
# remove quote |
264 |
$text =~ s/[\n\r]+/\n/gs; # compress cr/lf |
$text =~ s/^[\>:\|=]+[^\n\r]*[\n\r]*$/#-q-#/msg; |
265 |
|
# remove quote author |
266 |
|
$text =~ s/[\n\r]+[^\n\r]+:\s*(?:#-q-#[\n\r*])+//gs; |
267 |
|
$text =~ s/^[^\n\r]+:\s*(?:#-q-#[\n\r]*)+//gs; |
268 |
|
$text =~ s/#-q-#[\n\r]*//gs; |
269 |
|
# outlook quoting |
270 |
|
$text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si; |
271 |
|
$text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si; |
272 |
|
|
273 |
|
# remove signature |
274 |
|
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
275 |
|
|
276 |
|
# compress cr/lf |
277 |
|
$text =~ s/[\n\r]+/\n/gs; |
278 |
|
|
279 |
|
# remove whitespaces |
280 |
|
$text =~ s/^\n+//gs; |
281 |
|
$text =~ s/[\s\n]+$//gs; |
282 |
|
|
283 |
|
if ($text eq "") { |
284 |
|
$text="#-#quote##forwarded message##quote#-#"; |
285 |
|
} |
286 |
|
|
287 |
|
# cut to 5 lines; |
288 |
if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { |
if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { |
289 |
$text =~ s/[\n\r]*$/ .../; |
$text =~ s/[\n\r]*$/ .../; |
290 |
} |
} |
|
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
|
291 |
|
|
292 |
# my $context = Text::Context::EitherSide->new($text, context => 5); |
# my $context = Text::Context::EitherSide->new($text, context => 5); |
293 |
# return $context->as_string("perl"); |
# return $context->as_string("perl"); |
294 |
|
|
295 |
return $text; |
return html_escape($text); |
296 |
} |
} |
297 |
|
|
298 |
sub subject_search_filter { |
sub body_filter { |
299 |
my $s = shift; |
my $text = shift; |
300 |
# remove re: fdw: [list] preffixes from e-mail |
|
301 |
while ( $s =~ s/^\s*\[(?:re|fwd|fw):\s+(.+)\]\s*$/$1/ig || |
my $sig = ''; |
302 |
$s =~ s/^\s*(?:re|fwd|fw):\s+(.+?)\s*$/$1/ig || |
|
303 |
$s =~ s/^\[\S+\]\s*//ig || |
# remove signature |
304 |
$s =~ s/^\[[^@]+@\w+\.\w+\s*:\s+(.+)\s*\]\s*$/$1/g || |
if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) { |
305 |
$s =~ s/\(fwd\)\s*$//ig || |
$sig = "$1#-#signature##$2##signature#-#"; |
306 |
$s =~ s/\"//g |
} |
307 |
) { }; |
|
308 |
return $s; |
# find quoted text |
309 |
|
$text =~ s/^([\>:\|=]+[^\n\r]*[\n\r]*)$/#-#quote1##$1##quote1#-#/mg; |
310 |
|
$text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote2##$1##quote2#-#/si; |
311 |
|
$text =~ s/(--+\s*Forwarded\s+message.+\s*--+.*)$/#-#quote3##$1##quote3#-#/si; |
312 |
|
|
313 |
|
$text = html_escape($text . $sig); |
314 |
|
return $text; |
315 |
} |
} |
316 |
|
|