/[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 20 by dpavlin, Fri May 7 23:35:39 2004 UTC revision 28 by dpavlin, Sat May 8 22:18:38 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 73  while ( my $c = $d->accept ) { Line 87  while ( my $c = $d->accept ) {
87                  # XXX LOG                  # XXX LOG
88                  print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug);                  print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug);
89    
90                    # is this static page?
91                    if ($static_html && -f "$static_html/$url") {
92                            print "static file: $static_html/$url\n" if ($debug);
93                            $c->send_file_response("$static_html/$url");
94                            $c->close;
95                            next;
96                    }
97    
98                  # template file name (use ?format=html as default)                  # template file name (use ?format=html as default)
99                  my $tpl_file = 'master.';                  my $tpl_file = 'master.';
100                  $tpl_file .= $param->{'format'} || 'html';                  $tpl_file .= $param->{'format'} || 'html';
# Line 110  while ( my $c = $d->accept ) { Line 132  while ( my $c = $d->accept ) {
132                          yyyy    => $yyyy,                          yyyy    => $yyyy,
133                          mm      => $mm,                          mm      => $mm,
134                          dd      => $dd,                          dd      => $dd,
135                            date_limit => $date_limit,
136                  };                  };
137    
138                  if ($date_limit) {                  # is this access to root of web server?
139                          $param->{'search'} .= "and " if ($param->{'search'});                  if ($url eq "/" && !$param->{'search'}) {
140                          $param->{'search'} .= $date_limit;                          # if first access, go to current year
141                            $date_limit = $mws->fmtdate($yyyy);
142                            $param->{sort_by} = "date desc";
143                  }                  }
144    
145                  # show search results                  # ?show_id=XXXXxxxx___message_id___xxxxXXXX
146                  # ?search=foo:bar                  if ($param->{'show_id'}) {
                 if ($param->{'search'}) {  
147    
148                          print STDERR "search: ",$param->{'search'},"\n";                          $mws->reset_counters;
149                            my $row = $mws->fetch_result_by_id($param->{'show_id'});
150                            $tpl_var->{message} = $row;
151                    } elsif ($param->{'search'} || $date_limit) {
152    
153                          my $results = $mws->search($param->{'search'});                          # show search results
154                          my @res = $mws->fetch_all_results();                          # ?search=foo:bar
155    
156                          $tpl_var->{results} = \@res if (@res);                          my @search;
157                          $tpl_var->{total_hits} = $mws->{total_hits};                          push @search, $param->{'search'} if ($param->{'search'});
158    
159                            if ($date_limit) {
160                                    push @search, "and" if (@search);
161                                    push @search, "date:\"$date_limit\"";
162                            }
163    
164                  #                          if ($param->{sort_by}) {
165                  # ?show_id=XXXXxxxx___message_id___xxxxXXXX                                  push @search, "sort:".$param->{sort_by};
166                  } elsif ($param->{'show_id'}) {                          }
167    
168                            print STDERR "search: ",join(" ",@search),"\n";
169    
170                            my $results = $mws->search(@search);
171                            my @res = $mws->fetch_all_results();
172    
173                            $tpl_var->{results} = \@res if (@res);
174                            $tpl_var->{total_hits} = $mws->{total_hits} || 0;
175    
                         $mws->reset_counters;  
                         my $row = $mws->fetch_result_by_id($param->{'show_id'});  
                         $tpl_var->{message} = $row;  
176                  }                  }
177    
 print Dumper($mws->{counter});  
178    
179                  # push counters to template                  # push counters to template
180                  foreach my $f (qw(from to cc bcc)) {                  foreach my $f (qw(from to cc bcc)) {
# Line 172  print Dumper($mws->{counter}); Line 207  print Dumper($mws->{counter});
207    
208  # template toolkit filter  # template toolkit filter
209    
210    sub html_escape($) {
211            my $text = shift;
212    
213            # Escape <, >, & and ", and to produce valid XML
214            my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
215            my $escape_re  = join '|' => keys %escape;
216    
217            $text =~ s/($escape_re)/$escape{$1}/gs;
218            return $text;
219    }
220    
221  #use Text::Context::EitherSide;  #use Text::Context::EitherSide;
222    
223  sub body5_filter {  sub body5_filter {
224          my $text = shift;          my $text = shift;
225          $text =~ s/^\s+//gs;  
226          $text =~ s/^[\>:\|=]+\s*.*?$//msg;      # remove quoted text          # remove quote
227          $text =~ s/[\n\r]+/\n/gs;               # compress cr/lf          $text =~ s/^[\>:\|=]+\s*.*?$/#-q-#/msg;
228            # remove quote author
229            $text =~ s/[\n\r]+[^\n\r]+:\s*(:?#-q-#[\n\r*])+//gs;
230            $text =~ s/^[^\n\r]+:\s*(:?#-q-#[\n\r]*)+//gs;
231            # outlook quoting
232            $text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si;
233            $text =~ s/(\s*--+\s*Forwarded\s+message\s*from\s+.+\s*--+.*)$//si;
234    
235            # remove signature
236            $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
237    
238            # compress cr/lf
239            $text =~ s/[\n\r]+/\n/gs;
240    
241            # remove whitespaces
242            $text =~ s/^\n+//gs;
243            $text =~ s/[\s\n]+$//gs;
244    
245            # cut to 5 lines;
246          if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {          if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {
247                  $text =~ s/[\n\r]*$/ .../;                  $text =~ s/[\n\r]*$/ .../;
248          }          }
         $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;  
249    
250  #       my $context = Text::Context::EitherSide->new($text, context => 5);  #       my $context = Text::Context::EitherSide->new($text, context => 5);
251  #       return $context->as_string("perl");  #       return $context->as_string("perl");
252    
253          return $text;          return html_escape($text);
254  }  }
255    
256  sub subject_search_filter {  sub body_filter {
257          my $s = shift;          my $text = shift;
258          # remove re: fdw: [list] preffixes from e-mail  
259          while ( $s =~ s/^\s*\[(?:re|fwd|fw):\s+(.+)\]\s*$/$1/ig ||          my $sig = '';
260                  $s =~ s/^\s*(?:re|fwd|fw):\s+(.+?)\s*$/$1/ig ||  
261                  $s =~ s/^\[\S+\]\s*//ig ||          # remove signature
262                  $s =~ s/^\[[^@]+@\w+\.\w+\s*:\s+(.+)\s*\]\s*$/$1/g ||          if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) {
263                  $s =~ s/\(fwd\)\s*$//ig ||                  $sig = "$1#-#signature##$2##signature#-#";
264                  $s =~ s/\"//g          }
265          ) { };  
266          return $s;          # find quoted text
267            $text =~ s/^([\>:\|=]+\s*.*?)$/#-#quote##$1##quote#-#/msg;
268            $text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote##$1##quote#-#/si || $text =~ s/(--+\s*Forwarded\s+message\s*from\s+.+\s*--+.*)$/#-#quote##$1##quote#-#/si;
269    
270            $text = html_escape($text . $sig);
271            $text =~ s/#-#(quote|signature)##(.+?)##(\1)#-#/<span class="$1">$2<\/span>/gs;
272            return $text;
273  }  }
274    

Legend:
Removed from v.20  
changed lines
  Added in v.28

  ViewVC Help
Powered by ViewVC 1.1.26