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

Legend:
Removed from v.7  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26