]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
view: preliminary HTML search interface
[public-inbox.git] / lib / PublicInbox / WWW.pm
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # We focus on the lowest common denominators here:
5 # - targeted at text-only console browsers (w3m, links, etc..)
6 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
7 # - No JavaScript, graphics or icons allowed.
8 # - Must not rely on static content
9 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
10 package PublicInbox::WWW;
11 use 5.008;
12 use strict;
13 use warnings;
14 use PublicInbox::Config;
15 use URI::Escape qw(uri_escape_utf8 uri_unescape);
16 use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
17 use constant PI_URL => 'http://public-inbox.org/';
18 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
19 our $MID_RE = qr!([^/]+)!;
20 our $END_RE = qr!(f/|T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
21 our $pi_config;
22
23 sub run {
24         my ($cgi, $method) = @_;
25         $pi_config ||= PublicInbox::Config->new;
26         my $ctx = { cgi => $cgi, pi_config => $pi_config };
27         if ($method !~ /\AGET|HEAD\z/) {
28                 return r(405, 'Method Not Allowed');
29         }
30         my $path_info = $cgi->path_info;
31
32         # top-level indices and feeds
33         if ($path_info eq '/') {
34                 r404();
35         } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
36                 invalid_list($ctx, $1) || r301($ctx, $1);
37         } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
38                 invalid_list($ctx, $1) || get_index($ctx);
39         } elsif ($path_info =~ m!$LISTNAME_RE/(?:atom\.xml|new\.atom)\z!o) {
40                 invalid_list($ctx, $1) || get_atom($ctx);
41
42         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/$END_RE\z!o) {
43                 msg_page($ctx, $1, $2, $3);
44
45         # in case people leave off the trailing slash:
46         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t)\z!o) {
47                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
48
49         # convenience redirects order matters
50         } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
51                 r301($ctx, $1, $2);
52
53         } else {
54                 legacy_redirects($ctx, $path_info);
55         }
56 }
57
58 # for CoW-friendliness, MOOOOO!
59 sub preload {
60         require PublicInbox::Feed;
61         require PublicInbox::View;
62         require PublicInbox::Thread;
63         require PublicInbox::GitCatFile;
64         require Email::MIME;
65         require Digest::SHA;
66         require POSIX;
67
68         eval {
69                 require PublicInbox::Search;
70                 require PublicInbox::SearchView;
71                 require PublicInbox::Mbox;
72                 require IO::Compress::Gzip;
73         };
74 }
75
76 # private functions below
77
78 sub r404 {
79         my ($ctx) = @_;
80         if ($ctx && $ctx->{mid}) {
81                 require PublicInbox::ExtMsg;
82                 return PublicInbox::ExtMsg::ext_msg($ctx);
83         }
84         r(404, 'Not Found');
85 }
86
87 # simple response for errors
88 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
89
90 # returns undef if valid, array ref response if invalid
91 sub invalid_list {
92         my ($ctx, $listname) = @_;
93         my $git_dir = $pi_config->get($listname, "mainrepo");
94         if (defined $git_dir) {
95                 $ctx->{git_dir} = $git_dir;
96                 $ctx->{listname} = $listname;
97                 return;
98         }
99         r404();
100 }
101
102 # returns undef if valid, array ref response if invalid
103 sub invalid_list_mid {
104         my ($ctx, $listname, $mid) = @_;
105         my $ret = invalid_list($ctx, $listname, $mid);
106         return $ret if $ret;
107
108         $ctx->{mid} = $mid = uri_unescape($mid);
109         if ($mid =~ /\A[a-f0-9]{40}\z/) {
110                 if ($mid = mid2blob($ctx)) {
111                         require Email::Simple;
112                         use PublicInbox::MID qw/mid_clean/;
113                         $mid = Email::Simple->new($mid);
114                         $ctx->{mid} = mid_clean($mid->header('Message-ID'));
115                 }
116         }
117         undef;
118 }
119
120 # /$LISTNAME/new.atom                     -> Atom feed, includes replies
121 sub get_atom {
122         my ($ctx) = @_;
123         require PublicInbox::Feed;
124         PublicInbox::Feed::generate($ctx);
125 }
126
127 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
128 sub get_index {
129         my ($ctx) = @_;
130         require PublicInbox::Feed;
131         my $srch = searcher($ctx);
132         my $q = $ctx->{cgi}->param('q');
133         footer($ctx);
134         if (defined $q) {
135                 require PublicInbox::SearchView;
136                 PublicInbox::SearchView::sres_top_html($ctx, $q);
137         } else {
138                 PublicInbox::Feed::generate_html_index($ctx);
139         }
140 }
141
142 # just returns a string ref for the blob in the current ctx
143 sub mid2blob {
144         my ($ctx) = @_;
145         require PublicInbox::MID;
146         my $path = PublicInbox::MID::mid2path($ctx->{mid});
147         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
148                         qw(cat-file blob), "HEAD:$path");
149         my $pid = open my $fh, '-|';
150         defined $pid or die "fork failed: $!\n";
151         if ($pid == 0) {
152                 open STDERR, '>', '/dev/null'; # ignore errors
153                 exec @cmd or die "exec failed: $!\n";
154         } else {
155                 my $blob = eval { local $/; <$fh> };
156                 close $fh;
157                 $? == 0 ? \$blob : undef;
158         }
159 }
160
161 # /$LISTNAME/$MESSAGE_ID/raw                    -> raw mbox
162 sub get_mid_txt {
163         my ($ctx) = @_;
164         my $x = mid2blob($ctx) or return r404($ctx);
165         require PublicInbox::Mbox;
166         PublicInbox::Mbox::emit1($x);
167 }
168
169 # /$LISTNAME/$MESSAGE_ID/                   -> HTML content (short quotes)
170 sub get_mid_html {
171         my ($ctx) = @_;
172         my $x = mid2blob($ctx) or return r404($ctx);
173
174         require PublicInbox::View;
175         my $foot = footer($ctx);
176         require Email::MIME;
177         my $mime = Email::MIME->new($x);
178         searcher($ctx);
179         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
180           [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
181 }
182
183 # /$LISTNAME/$MESSAGE_ID/f/                   -> HTML content (fullquotes)
184 sub get_full_html {
185         my ($ctx) = @_;
186         my $x = mid2blob($ctx) or return r404($ctx);
187
188         require PublicInbox::View;
189         my $foot = footer($ctx);
190         require Email::MIME;
191         my $mime = Email::MIME->new($x);
192         searcher($ctx);
193         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
194           [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
195 }
196
197 # /$LISTNAME/$MESSAGE_ID/t/
198 sub get_thread {
199         my ($ctx, $flat) = @_;
200         my $srch = searcher($ctx) or return need_search($ctx);
201         require PublicInbox::View;
202         my $foot = footer($ctx);
203         $ctx->{flat} = $flat;
204         PublicInbox::View::thread_html($ctx, $foot, $srch);
205 }
206
207 sub self_url {
208         my ($cgi) = @_;
209         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
210 }
211
212 sub ctx_get {
213         my ($ctx, $key) = @_;
214         my $val = $ctx->{$key};
215         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
216         $val;
217 }
218
219 sub try_cat {
220         my ($path) = @_;
221         my $rv;
222         if (open(my $fh, '<', $path)) {
223                 local $/;
224                 $rv = <$fh>;
225                 close $fh;
226         }
227         $rv;
228 }
229
230 sub footer {
231         my ($ctx) = @_;
232         return '' unless $ctx;
233         my $git_dir = ctx_get($ctx, 'git_dir');
234
235         # favor user-supplied footer
236         my $footer = try_cat("$git_dir/public-inbox/footer.html");
237         if (defined $footer) {
238                 chomp $footer;
239                 $ctx->{footer} = $footer;
240                 return $footer;
241         }
242
243         # auto-generate a footer
244         my $listname = ctx_get($ctx, 'listname');
245         my $desc = try_cat("$git_dir/description");
246         $desc = '$GIT_DIR/description missing' unless defined $desc;
247         chomp $desc;
248
249         my $urls = try_cat("$git_dir/cloneurl");
250         my @urls = split(/\r?\n/, $urls || '');
251         my $nurls = scalar @urls;
252         if ($nurls == 0) {
253                 $urls = '($GIT_DIR/cloneurl missing)';
254         } elsif ($nurls == 1) {
255                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
256                         '">ssoma</a>: ' . $urls[0];
257         } else {
258                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
259                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
260         }
261
262         my $addr = $pi_config->get($listname, 'address');
263         if (ref($addr) eq 'ARRAY') {
264                 $addr = $addr->[0]; # first address is primary
265         }
266
267         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
268
269         $ctx->{footer} = join("\n",
270                 '- ' . $desc,
271                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
272                         'anybody may post in plain-text (not HTML):',
273                 $addr,
274                 $urls
275         );
276 }
277
278 # search support is optional, returns undef if Xapian is not installed
279 # or not configured for the given GIT_DIR
280 sub searcher {
281         my ($ctx) = @_;
282         eval {
283                 require PublicInbox::Search;
284                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
285         };
286 }
287
288 sub need_search {
289         my ($ctx) = @_;
290         my $msg = <<EOF;
291 <html><head><title>Search not available for this
292 public-inbox</title><body><pre>Search is not available for this public-inbox
293 <a href="../">Return to index</a></pre></body></html>
294 EOF
295         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
296 }
297
298 # /$LISTNAME/$MESSAGE_ID/t.mbox           -> thread as mbox
299 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
300 # note: I'm not a big fan of other compression formats since they're
301 # significantly more expensive on CPU than gzip and less-widely available,
302 # especially on older systems.  Stick to zlib since that's what git uses.
303 sub get_thread_mbox {
304         my ($ctx, $sfx) = @_;
305         my $srch = searcher($ctx) or return need_search($ctx);
306         require PublicInbox::Mbox;
307         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
308 }
309
310
311 # /$LISTNAME/$MESSAGE_ID/t.atom           -> thread as Atom feed
312 sub get_thread_atom {
313         my ($ctx) = @_;
314         searcher($ctx) or return need_search($ctx);
315         $ctx->{self_url} = self_url($ctx->{cgi});
316         require PublicInbox::Feed;
317         PublicInbox::Feed::generate_thread_atom($ctx);
318 }
319
320 sub legacy_redirects {
321         my ($ctx, $path_info) = @_;
322
323         # single-message pages
324         if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
325                 r301($ctx, $1, $2);
326         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
327                 r301($ctx, $1, $2, 'raw');
328
329         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
330                 r301($ctx, $1, $2, 'f/');
331
332         # thread display
333         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
334                 r301($ctx, $1, $2, 't/#u');
335
336         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
337                 r301($ctx, $1, $2, "t.mbox$3");
338
339         # even older legacy redirects
340         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
341                 r301($ctx, $1, $2);
342
343         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
344                 r301($ctx, $1, $2, 't/#u');
345
346         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
347                 r301($ctx, $1, $2, 'f/');
348
349         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
350                 r301($ctx, $1, $2, 'raw');
351
352         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
353                 r301($ctx, $1, $2, "t$3");
354
355         # legacy convenience redirects, order still matters
356         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
357                 r301($ctx, $1, $2);
358         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
359                 r301($ctx, $1, $2, 't/#u');
360         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
361                 r301($ctx, $1, $2, 'f/');
362
363         # some Message-IDs have slashes in them and the HTTP server
364         # may try to be clever and unescape them :<
365         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
366                 msg_page($ctx, $1, $2, $3);
367
368         # in case people leave off the trailing slash:
369         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
370                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
371         } else {
372                 r404();
373         }
374 }
375
376 sub r301 {
377         my ($ctx, $listname, $mid, $suffix) = @_;
378         my $cgi = $ctx->{cgi};
379         my $url;
380         if (ref($cgi) eq 'CGI') {
381                 $url = $cgi->url(-base) . '/';
382         } else {
383                 $url = $cgi->base->as_string;
384         }
385
386         $url .= $listname . '/';
387         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
388         $url .= $suffix if (defined $suffix);
389
390         [ 301,
391           [ Location => $url, 'Content-Type' => 'text/plain' ],
392           [ "Redirecting to $url\n" ] ]
393 }
394
395 sub msg_page {
396         my ($ctx, $list, $mid, $e) = @_;
397         unless (invalid_list_mid($ctx, $list, $mid)) {
398                 '' eq $e and return get_mid_html($ctx);
399                 't/' eq $e and return get_thread($ctx);
400                 't.atom' eq $e and return get_thread_atom($ctx);
401                 't.mbox' eq $e and return get_thread_mbox($ctx);
402                 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
403                 'T/' eq $e and return get_thread($ctx, 1);
404                 'raw' eq $e and return get_mid_txt($ctx);
405                 'f/' eq $e and return get_full_html($ctx);
406         }
407         r404($ctx);
408 }
409
410 1;