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