/[mws]/trunk/httpd.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/httpd.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 21 by dpavlin, Sat May 8 00:54:16 2004 UTC revision 34 by dpavlin, Sun May 9 10:16:51 2004 UTC
# Line 11  use IO::String; Line 11  use IO::String;
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 $debug = 1;  my $debug = 1;
19    
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 $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die;  my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die;
32  my $cgi = new CGI::Lite;  my $cgi = new CGI::Lite;
33  my $mws = MWS->new('global.conf');  my $mws = MWS->new($config_file);
34  my $tt = Template->new({  my $tt = Template->new({
35          INCLUDE_PATH => $mws->{config}->val('global', 'templates'),          INCLUDE_PATH => $mws->{config}->val('global', 'templates'),
36          FILTERS => {          FILTERS => {
37                  'body5' => \&body5_filter,                  'body5' => \&body5_filter,
38                  'subject_search' => \&subject_search_filter,                  'body' => \&body_filter,
39          },          },
40          EVAL_PERL => 1,          EVAL_PERL => 1,
41  });  });
42    
43    my $static_html = $mws->{config}->val('global', 'static_html');
44    
45  print "Web server ready at: ", $d->url, "\n";  print "Web server ready at: ", $d->url, "\n";
46    
47    
# Line 71  while ( my $c = $d->accept ) { Line 85  while ( my $c = $d->accept ) {
85                  my $url = $r->url->path;                  my $url = $r->url->path;
86    
87                  # XXX LOG                  # XXX LOG
88                  print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug);                  print $r->method," ",$url,"\n";
89                    print Dumper($param),"\n" if ($debug);
90    
91                    # is this static page?
92                    if ($static_html && -f "$static_html/$url") {
93                            print "static file: $static_html/$url\n" if ($debug);
94                            $c->send_file_response("$static_html/$url");
95                            $c->close;
96                            next;
97                    }
98    
99                  # template file name (use ?format=html as default)                  # template file name (use ?format=html as default)
100                  my $tpl_file = 'master.';                  my $tpl_file = 'master.';
# Line 113  while ( my $c = $d->accept ) { Line 136  while ( my $c = $d->accept ) {
136                          date_limit => $date_limit,                          date_limit => $date_limit,
137                  };                  };
138    
139                  #                  # is this access to root of web server?
140                    if ($url eq "/" && !$param->{'search'}) {
141                            # if first access, go to current year
142                            $date_limit = $mws->fmtdate($yyyy);
143                            $param->{sort_by} = "date desc";
144                    }
145    
146                  # ?show_id=XXXXxxxx___message_id___xxxxXXXX                  # ?show_id=XXXXxxxx___message_id___xxxxXXXX
147                  if ($param->{'show_id'}) {                  if ($param->{'show_id'}) {
148    
# Line 125  while ( my $c = $d->accept ) { Line 154  while ( my $c = $d->accept ) {
154                          # show search results                          # show search results
155                          # ?search=foo:bar                          # ?search=foo:bar
156    
157                          my @search = ( $param->{'search'} );                          my @search;
158                            push @search, $param->{'search'} if ($param->{'search'});
159    
160                          if ($date_limit) {                          if ($date_limit) {
161                                  push @search, "and" if (@search);                                  push @search, "and" if (@search);
162                                  push @search, "date:\"$date_limit\"";                                  push @search, "date:\"$date_limit\"";
163                          }                          }
164    
165                            if ($param->{sort_by}) {
166                                    push @search, "sort:".$param->{sort_by};
167                            }
168    
169                          print STDERR "search: ",join(" ",@search),"\n";                          print STDERR "search: ",join(" ",@search),"\n";
170    
171                          my $results = $mws->search(@search);                          my $results = $mws->search(@search);
172                          my @res = $mws->fetch_all_results();                          my @res = $mws->fetch_all_results();
173    
174                          $tpl_var->{results} = \@res if (@res);                          $tpl_var->{results} = \@res if (@res);
175                          $tpl_var->{total_hits} = $mws->{total_hits};                          $tpl_var->{total_hits} = $mws->{total_hits} || 0;
176    
177                            # no hits, offer suggestions
178                            if (! $tpl_var->{results}) {
179                                    @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});
180                            }
181    
182                  }                  }
183    
# Line 174  while ( my $c = $d->accept ) { Line 213  while ( my $c = $d->accept ) {
213    
214  # template toolkit filter  # template toolkit filter
215    
216    sub html_escape($) {
217            my $text = shift || return;
218    
219            # don't re-escape html
220            #return $text if ($text =~ /&(:?lt|gt|amp|quot);/);
221    
222            # Escape <, >, & and ", and to produce valid XML
223            my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
224            my $escape_re  = join '|' => keys %escape;
225    
226            $text =~ s/($escape_re)/$escape{$1}/gs;
227    
228            while ($text =~ s/#-#(quote|signature)(\d*)##(.+?)##\1\2#-#/<span class="$1">$3<\/span>/gs) { } ;
229    
230            return $text;
231    }
232    
233  #use Text::Context::EitherSide;  #use Text::Context::EitherSide;
234    
235  sub body5_filter {  sub body5_filter {
236          my $text = shift;          my $text = shift;
237          $text =~ s/^\s+//gs;  
238          $text =~ s/^[\>:\|=]+\s*.*?$//msg;      # remove quoted text          # remove quote
239          $text =~ s/[\n\r]+/\n/gs;               # compress cr/lf          $text =~ s/^[\>:\|=]+\s*.*?$/#-q-#/msg;
240            # remove quote author
241            $text =~ s/[\n\r]+[^\n\r]+:\s*(:?#-q-#[\n\r*])+//gs;
242            $text =~ s/^[^\n\r]+:\s*(:?#-q-#[\n\r]*)+//gs;
243            $text =~ s/#-q-#[\n\r]*//gs;
244            # outlook quoting
245            $text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si;
246            $text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si;
247    
248            # remove signature
249            $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
250    
251            # compress cr/lf
252            $text =~ s/[\n\r]+/\n/gs;
253    
254            # remove whitespaces
255            $text =~ s/^\n+//gs;
256            $text =~ s/[\s\n]+$//gs;
257    
258            if ($text eq "") {
259                    $text="#-#quote##forwarded message##quote#-#";
260            }
261    
262            # cut to 5 lines;
263          if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {          if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {
264                  $text =~ s/[\n\r]*$/ .../;                  $text =~ s/[\n\r]*$/ .../;
265          }          }
         $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;  
266    
267  #       my $context = Text::Context::EitherSide->new($text, context => 5);  #       my $context = Text::Context::EitherSide->new($text, context => 5);
268  #       return $context->as_string("perl");  #       return $context->as_string("perl");
269    
270          return $text;          return html_escape($text);
271  }  }
272    
273  sub subject_search_filter {  sub body_filter {
274          my $s = shift;          my $text = shift;
275          # remove re: fdw: [list] preffixes from e-mail  
276          while ( $s =~ s/^\s*\[(?:re|fwd|fw):\s+(.+)\]\s*$/$1/ig ||          my $sig = '';
277                  $s =~ s/^\s*(?:re|fwd|fw):\s+(.+?)\s*$/$1/ig ||  
278                  $s =~ s/^\[\S+\]\s*//ig ||          # remove signature
279                  $s =~ s/^\[[^@]+@\w+\.\w+\s*:\s+(.+)\s*\]\s*$/$1/g ||          if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) {
280                  $s =~ s/\(fwd\)\s*$//ig ||                  $sig = "$1#-#signature##$2##signature#-#";
281                  $s =~ s/\"//g          }
282          ) { };  
283          return $s;          # find quoted text
284            $text =~ s/^([\>:\|=]+\s*.*?)$/#-#quote1##$1##quote1#-#/msg;
285            $text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote2##$1##quote2#-#/si;
286            $text =~ s/(--+\s*Forwarded\s+message.+\s*--+.*)$/#-#quote3##$1##quote3#-#/si;
287    
288            $text = html_escape($text . $sig);
289            return $text;
290  }  }
291    

Legend:
Removed from v.21  
changed lines
  Added in v.34

  ViewVC Help
Powered by ViewVC 1.1.26