/[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 12 by dpavlin, Thu May 6 12:40:11 2004 UTC revision 27 by dpavlin, Sat May 8 20:34:26 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;
19  print "Please contact me at: <URL:", $d->url, ">\n";  
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;
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          },          },
39            EVAL_PERL => 1,
40  });  });
41    
42    my $static_html = $mws->{config}->val('global', 'static_html');
43    
44    print "Web server ready at: ", $d->url, "\n";
45    
46    
47  while ( my $c = $d->accept ) {  while ( my $c = $d->accept ) {
48          while ( my $r = $c->get_request ) {          while ( my $r = $c->get_request ) {
# Line 67  while ( my $c = $d->accept ) { Line 84  while ( my $c = $d->accept ) {
84                  my $url = $r->url->path;                  my $url = $r->url->path;
85    
86                  # XXX LOG                  # XXX LOG
87                  print $r->method," ",$url,Dumper($param);                  print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug);
88    
89                    # is this static page?
90                    if ($static_html && -f "$static_html/$url") {
91                            print "static file: $static_html/$url\n" if ($debug);
92                            $c->send_file_response("$static_html/$url");
93                            $c->close;
94                            next;
95                    }
96    
97                  # template file name (use ?format=html as default)                  # template file name (use ?format=html as default)
98                  my $tpl_file = 'master.';                  my $tpl_file = 'master.';
99                  $tpl_file .= $param->{'format'} || 'html';                  $tpl_file .= $param->{'format'} || 'html';
100    
101                    # parse date from url
102                    my ($yyyy,$mm,$dd) = $mws->yyyymmdd;
103    
104                    my $yyyymm;
105    
106                    my $date_limit;
107    
108                    if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) {
109                            ($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3);
110                             $date_limit = "$yyyy-$mm-$dd";
111                    } elsif ($url =~ m,^/(\d{4})[/-](\d+),) {
112                            ($yyyy,$mm) = $mws->fmtdate($1,$2);
113                            $date_limit = "$yyyy-$mm";
114                    } elsif ($url =~ m,^/(\d{4}),) {
115                            $date_limit = $mws->fmtdate($1);
116                    }
117    
118                  #                  #
119                  # implement functionality and generate HTML                  # implement functionality and generate HTML
120                  #                  #
# Line 80  while ( my $c = $d->accept ) { Line 122  while ( my $c = $d->accept ) {
122    
123                  if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {                  if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
124                          $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};                          $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
125                    } elsif ($param->{'search'}) {
126                            ($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2);
127                    }
128    
129                    my $tpl_var = {
130                            param   => $param,
131                            yyyy    => $yyyy,
132                            mm      => $mm,
133                            dd      => $dd,
134                            date_limit => $date_limit,
135                    };
136    
137                    # is this access to root of web server?
138                    if ($url eq "/" && !$param->{'search'}) {
139                            # if first access, go to current year
140                            $date_limit = $mws->fmtdate($yyyy);
141                            $param->{sort} = "date desc";
142                  }                  }
143    
144                  # show search results                  # ?show_id=XXXXxxxx___message_id___xxxxXXXX
145                  # ?search=foo:bar                  if ($param->{'show_id'}) {
146                  if ($param->{'search'}) {  
147                            $mws->reset_counters;
148                            my $row = $mws->fetch_result_by_id($param->{'show_id'});
149                            $tpl_var->{message} = $row;
150                    } elsif ($param->{'search'} || $date_limit) {
151    
152                            # show search results
153                            # ?search=foo:bar
154    
155                          print STDERR "search: ",$param->{'search'},"\n";                          my @search;
156                            push @search, $param->{'search'} if ($param->{'search'});
157    
158                            if ($date_limit) {
159                                    push @search, "and" if (@search);
160                                    push @search, "date:\"$date_limit\"";
161                            }
162    
163                          my $results = $mws->search($param->{'search'});                          if ($param->{sort_by}) {
164                                    push @search, "sort:".$param->{sort_by};
165                            }
166    
167                            print STDERR "search: ",join(" ",@search),"\n";
168    
169                            my $results = $mws->search(@search);
170                          my @res = $mws->fetch_all_results();                          my @res = $mws->fetch_all_results();
171    
172                          $tt->process($tpl_file, {                          $tpl_var->{results} = \@res if (@res);
173                                  query   => $param->{'search'},                          $tpl_var->{total_hits} = $mws->{total_hits} || 0;
                                 results => \@res,  
                                 param   => $param,  
                         }, \$html) || die $tt->error();  
174    
                 #  
                 # ?show_id=XXXXxxxx___message_id___xxxxXXXX  
                 } elsif ($param->{'show_id'}) {  
                           
                         my $row = $mws->fetch_result_by_id($param->{'show_id'});  
                         $tt->process($tpl_file, {  
                                 message => $row,  
                         }, \$html) || die $tt->error();  
175                  }                  }
176    
177    
178                    # push counters to template
179                    foreach my $f (qw(from to cc bcc)) {
180                            my $h = $mws->counter($f) || next;
181                            my @a;
182                            foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {
183                                    push @a, $h->{$k};
184                            }
185                            $tpl_var->{counters}->{$f} = [ @a ] if (@a);
186                    }
187    
188                    # push calendar in template
189                    $tpl_var->{calendar} = $mws->counter('calendar');
190    
191                    $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();
192    
193                  #                  #
194                  # send HTMLto client                  # send HTMLto client
195                  #                  #
196    
197                  my $res = HTTP::Response->new(RC_OK);                  my $res = HTTP::Response->new(RC_OK);
198                  $res->header( 'Content-type' => 'text/html; charset=iso-8859-2' );                  $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' );
199                  $res->content($html);                  $res->content($html);
200                  $c->send_response($res);                  $c->send_response($res);
201    
# Line 130  sub body5_filter { Line 213  sub body5_filter {
213          $text =~ s/^\s+//gs;          $text =~ s/^\s+//gs;
214          $text =~ s/^[\>:\|=]+\s*.*?$//msg;      # remove quoted text          $text =~ s/^[\>:\|=]+\s*.*?$//msg;      # remove quoted text
215          $text =~ s/[\n\r]+/\n/gs;               # compress cr/lf          $text =~ s/[\n\r]+/\n/gs;               # compress cr/lf
216          $text =~ s,^((?:.*?[\n\r]){5}).*$,$1<span style="color:#808080">--8&lt;--[cut]--8&lt;--</span>,s;          if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {
217                    $text =~ s/[\n\r]*$/ .../;
218            }
219          $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;          $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
220    
221  #       my $context = Text::Context::EitherSide->new($text, context => 5);  #       my $context = Text::Context::EitherSide->new($text, context => 5);

Legend:
Removed from v.12  
changed lines
  Added in v.27

  ViewVC Help
Powered by ViewVC 1.1.26