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