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; |
|
print "Please contact me at: <URL:", $d->url, ">\n"; |
|
19 |
|
|
20 |
|
my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; |
21 |
my $cgi = new CGI::Lite; |
my $cgi = new CGI::Lite; |
22 |
my $mws = MWS->new('global.conf'); |
my $mws = MWS->new('global.conf'); |
23 |
my $tt = Template->new({ |
my $tt = Template->new({ |
25 |
FILTERS => { |
FILTERS => { |
26 |
'body5' => \&body5_filter, |
'body5' => \&body5_filter, |
27 |
}, |
}, |
28 |
|
EVAL_PERL => 1, |
29 |
}); |
}); |
30 |
|
|
31 |
|
my $static_html = $mws->{config}->val('global', 'static_html'); |
32 |
|
|
33 |
|
print "Web server ready at: ", $d->url, "\n"; |
34 |
|
|
35 |
|
|
36 |
while ( my $c = $d->accept ) { |
while ( my $c = $d->accept ) { |
37 |
while ( my $r = $c->get_request ) { |
while ( my $r = $c->get_request ) { |
73 |
my $url = $r->url->path; |
my $url = $r->url->path; |
74 |
|
|
75 |
# XXX LOG |
# XXX LOG |
76 |
print $r->method," ",$url,Dumper($param); |
print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug); |
77 |
|
|
78 |
|
# is this static page? |
79 |
|
if ($static_html && -f "$static_html/$url") { |
80 |
|
print "static file: $static_html/$url\n" if ($debug); |
81 |
|
$c->send_file_response("$static_html/$url"); |
82 |
|
$c->close; |
83 |
|
next; |
84 |
|
} |
85 |
|
|
86 |
# template file name (use ?format=html as default) |
# template file name (use ?format=html as default) |
87 |
my $tpl_file = 'master.'; |
my $tpl_file = 'master.'; |
88 |
$tpl_file .= $param->{'format'} || 'html'; |
$tpl_file .= $param->{'format'} || 'html'; |
89 |
|
|
90 |
|
# parse date from url |
91 |
|
my ($yyyy,$mm,$dd) = $mws->yyyymmdd; |
92 |
|
|
93 |
|
my $yyyymm; |
94 |
|
|
95 |
|
my $date_limit; |
96 |
|
|
97 |
|
if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) { |
98 |
|
($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3); |
99 |
|
$date_limit = "$yyyy-$mm-$dd"; |
100 |
|
} elsif ($url =~ m,^/(\d{4})[/-](\d+),) { |
101 |
|
($yyyy,$mm) = $mws->fmtdate($1,$2); |
102 |
|
$date_limit = "$yyyy-$mm"; |
103 |
|
} elsif ($url =~ m,^/(\d{4}),) { |
104 |
|
$date_limit = $mws->fmtdate($1); |
105 |
|
} |
106 |
|
|
107 |
# |
# |
108 |
# implement functionality and generate HTML |
# implement functionality and generate HTML |
109 |
# |
# |
110 |
my $html; |
my $html; |
111 |
|
|
112 |
# show search results |
if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) { |
113 |
# ?search=foo:bar |
$param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'}; |
114 |
if ($param->{'search'}) { |
} elsif ($param->{'search'}) { |
115 |
|
($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2); |
116 |
print STDERR "search: ",$param->{'search'},"\n"; |
} |
|
|
|
|
my $results = $mws->search($param->{'search'}); |
|
|
my @res = $mws->fetch_all_results(); |
|
117 |
|
|
118 |
$tt->process($tpl_file, { |
my $tpl_var = { |
119 |
query => $param->{'search'}, |
param => $param, |
120 |
results => \@res, |
yyyy => $yyyy, |
121 |
param => $param, |
mm => $mm, |
122 |
}, \$html) || die $tt->error(); |
dd => $dd, |
123 |
|
date_limit => $date_limit, |
124 |
|
}; |
125 |
|
|
126 |
# |
# |
127 |
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
128 |
} elsif ($param->{'show_id'}) { |
if ($param->{'show_id'}) { |
129 |
|
|
130 |
|
$mws->reset_counters; |
131 |
my $row = $mws->fetch_result_by_id($param->{'show_id'}); |
my $row = $mws->fetch_result_by_id($param->{'show_id'}); |
132 |
$tt->process($tpl_file, { |
$tpl_var->{message} = $row; |
133 |
message => $row, |
} elsif ($param->{'search'} || $date_limit) { |
134 |
}, \$html) || die $tt->error(); |
|
135 |
|
# show search results |
136 |
|
# ?search=foo:bar |
137 |
|
|
138 |
|
my @search = ( $param->{'search'} ); |
139 |
|
|
140 |
|
if ($date_limit) { |
141 |
|
push @search, "and" if (@search); |
142 |
|
push @search, "date:\"$date_limit\""; |
143 |
|
} |
144 |
|
|
145 |
|
if ($param->{sort_by}) { |
146 |
|
push @search, "sort:".$param->{sort_by}; |
147 |
|
} |
148 |
|
|
149 |
|
print STDERR "search: ",join(" ",@search),"\n"; |
150 |
|
|
151 |
|
my $results = $mws->search(@search); |
152 |
|
my @res = $mws->fetch_all_results(); |
153 |
|
|
154 |
|
$tpl_var->{results} = \@res if (@res); |
155 |
|
$tpl_var->{total_hits} = $mws->{total_hits} || 0; |
156 |
|
|
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
# push counters to template |
161 |
|
foreach my $f (qw(from to cc bcc)) { |
162 |
|
my $h = $mws->counter($f) || next; |
163 |
|
my @a; |
164 |
|
foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) { |
165 |
|
push @a, $h->{$k}; |
166 |
|
} |
167 |
|
$tpl_var->{counters}->{$f} = [ @a ] if (@a); |
168 |
} |
} |
169 |
|
|
170 |
|
# push calendar in template |
171 |
|
$tpl_var->{calendar} = $mws->counter('calendar'); |
172 |
|
|
173 |
|
$tt->process($tpl_file, $tpl_var, \$html) || die $tt->error(); |
174 |
|
|
175 |
# |
# |
176 |
# send HTMLto client |
# send HTMLto client |
177 |
# |
# |
178 |
|
|
179 |
my $res = HTTP::Response->new(RC_OK); |
my $res = HTTP::Response->new(RC_OK); |
180 |
$res->header( 'Content-type' => 'text/html; charset=iso-8859-2' ); |
$res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' ); |
181 |
$res->content($html); |
$res->content($html); |
182 |
$c->send_response($res); |
$c->send_response($res); |
183 |
|
|
188 |
|
|
189 |
# template toolkit filter |
# template toolkit filter |
190 |
|
|
191 |
|
#use Text::Context::EitherSide; |
192 |
|
|
193 |
sub body5_filter { |
sub body5_filter { |
194 |
my $text = shift; |
my $text = shift; |
195 |
$text =~ s/^\s+//gs; |
$text =~ s/^\s+//gs; |
196 |
$text =~ s/[\n\r]+[\>:\|=]+\s*.*?[\n\r]+//sg; # remove quoted text |
$text =~ s/^[\>:\|=]+\s*.*?$//msg; # remove quoted text |
197 |
$text =~ s,^((?:.*?[\n\r]){5}).*$,$1<span style="color:#808080">--8<--[cut]--8<--</span>,s; |
$text =~ s/[\n\r]+/\n/gs; # compress cr/lf |
198 |
|
if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { |
199 |
|
$text =~ s/[\n\r]*$/ .../; |
200 |
|
} |
201 |
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
202 |
|
|
203 |
|
# my $context = Text::Context::EitherSide->new($text, context => 5); |
204 |
|
# return $context->as_string("perl"); |
205 |
|
|
206 |
return $text; |
return $text; |
207 |
} |
} |
208 |
|
|