]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
rename most instances of "list" to "inbox"
[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 Plack::Request;
17 use PublicInbox::Config qw(try_cat);
18 use URI::Escape qw(uri_escape_utf8 uri_unescape);
19 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
20 use constant PI_URL => '//public-inbox.org/';
21 require PublicInbox::Git;
22 use PublicInbox::GitHTTPBackend;
23 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
24 our $MID_RE = qr!([^/]+)!;
25 our $END_RE = qr!(T/|t/|R/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
26
27 sub new {
28         my ($class, $pi_config) = @_;
29         $pi_config ||= PublicInbox::Config->new;
30         bless { pi_config => $pi_config }, $class;
31 }
32
33 # backwards compatibility, do not use
34 sub run {
35         my ($req, $method) = @_;
36         PublicInbox::WWW->new->call($req->env);
37 }
38
39 sub call {
40         my ($self, $env) = @_;
41         my $cgi = Plack::Request->new($env);
42         my $ctx = { cgi => $cgi, pi_config => $self->{pi_config} };
43         my $path_info = $cgi->path_info;
44
45         my $method = $cgi->method;
46         if ($method eq 'POST' &&
47                  $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
48                 my $path = $2;
49                 return (invalid_inbox($self, $ctx, $1) ||
50                         serve_git($cgi, $ctx->{git}, $path));
51         }
52         elsif ($method !~ /\AGET|HEAD\z/) {
53                 return r(405, 'Method Not Allowed');
54         }
55
56         # top-level indices and feeds
57         if ($path_info eq '/') {
58                 r404();
59         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
60                 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
61         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
62                 invalid_inbox($self, $ctx, $1) || get_index($ctx);
63         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
64                 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
65
66         } elsif ($path_info =~ m!$INBOX_RE/
67                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
68                 my $path = $2;
69                 invalid_inbox($self, $ctx, $1) ||
70                         serve_git($cgi, $ctx->{git}, $path);
71         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
72                 msg_page($self, $ctx, $1, $2, $3);
73
74         # in case people leave off the trailing slash:
75         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t|R)\z!o) {
76                 my ($inbox, $mid, $suffix) = ($1, $2, $3);
77                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
78                 r301($ctx, $inbox, $mid, $suffix);
79
80         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
81                 r301($ctx, $1, $2);
82
83         # convenience redirects order matters
84         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
85                 r301($ctx, $1, $2);
86
87         } else {
88                 legacy_redirects($self, $ctx, $path_info);
89         }
90 }
91
92 # for CoW-friendliness, MOOOOO!
93 sub preload {
94         require PublicInbox::Feed;
95         require PublicInbox::View;
96         require PublicInbox::Thread;
97         require Email::MIME;
98         require Digest::SHA;
99         require POSIX;
100
101         foreach (qw(PublicInbox::Search PublicInbox::SearchView
102                         PublicInbox::Mbox IO::Compress::Gzip
103                         PublicInbox::NewsWWW PublicInbox::NewsGroup)) {
104                 eval "require $_;";
105         }
106 }
107
108 # private functions below
109
110 sub r404 {
111         my ($ctx) = @_;
112         if ($ctx && $ctx->{mid}) {
113                 require PublicInbox::ExtMsg;
114                 searcher($ctx);
115                 return PublicInbox::ExtMsg::ext_msg($ctx);
116         }
117         r(404, 'Not Found');
118 }
119
120 # simple response for errors
121 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
122
123 # returns undef if valid, array ref response if invalid
124 sub invalid_inbox {
125         my ($self, $ctx, $inbox, $mid) = @_;
126         my $git_dir = $ctx->{pi_config}->get($inbox, "mainrepo");
127         if (defined $git_dir) {
128                 $ctx->{git_dir} = $git_dir;
129                 $ctx->{git} = PublicInbox::Git->new($git_dir);
130                 $ctx->{inbox} = $inbox;
131                 return;
132         }
133
134         # sometimes linkifiers (not ours!) screw up automatic link
135         # generation and link things intended for nntp:// to https?://,
136         # so try to infer links and redirect them to the appropriate
137         # list URL.
138         $self->news_www->call($ctx->{cgi}->{env});
139 }
140
141 # returns undef if valid, array ref response if invalid
142 sub invalid_inbox_mid {
143         my ($self, $ctx, $inbox, $mid) = @_;
144         my $ret = invalid_inbox($self, $ctx, $inbox, $mid);
145         return $ret if $ret;
146
147         $ctx->{mid} = $mid = uri_unescape($mid);
148         if ($mid =~ /\A[a-f0-9]{40}\z/) {
149                 # this is horiffically wasteful for legacy URLs:
150                 if ($mid = mid2blob($ctx)) {
151                         require Email::Simple;
152                         use PublicInbox::MID qw/mid_clean/;
153                         my $s = Email::Simple->new($mid);
154                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
155                 }
156         }
157         undef;
158 }
159
160 # /$INBOX/new.atom                     -> Atom feed, includes replies
161 sub get_atom {
162         my ($ctx) = @_;
163         require PublicInbox::Feed;
164         PublicInbox::Feed::generate($ctx);
165 }
166
167 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
168 sub get_index {
169         my ($ctx) = @_;
170         require PublicInbox::Feed;
171         my $srch = searcher($ctx);
172         footer($ctx);
173         if (defined $ctx->{cgi}->param('q')) {
174                 require PublicInbox::SearchView;
175                 PublicInbox::SearchView::sres_top_html($ctx);
176         } else {
177                 PublicInbox::Feed::generate_html_index($ctx);
178         }
179 }
180
181 # just returns a string ref for the blob in the current ctx
182 sub mid2blob {
183         my ($ctx) = @_;
184         require PublicInbox::MID;
185         my $path = PublicInbox::MID::mid2path($ctx->{mid});
186         $ctx->{git}->cat_file("HEAD:$path");
187 }
188
189 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
190 sub get_mid_txt {
191         my ($ctx) = @_;
192         my $x = mid2blob($ctx) or return r404($ctx);
193         require PublicInbox::Mbox;
194         PublicInbox::Mbox::emit1($ctx, $x);
195 }
196
197 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
198 sub get_mid_html {
199         my ($ctx) = @_;
200         my $x = mid2blob($ctx) or return r404($ctx);
201
202         require PublicInbox::View;
203         my $foot = footer($ctx);
204         require Email::MIME;
205         my $mime = Email::MIME->new($x);
206         searcher($ctx);
207         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
208           [ PublicInbox::View::msg_html($ctx, $mime, $foot) ] ];
209 }
210
211 # /$INBOX/$MESSAGE_ID/R/                   -> HTML content (fullquotes)
212 sub get_reply_html {
213         my ($ctx) = @_;
214         my $x = mid2blob($ctx) or return r404($ctx);
215
216         require PublicInbox::View;
217         my $foot = footer($ctx);
218         require Email::MIME;
219         my $hdr = Email::MIME->new($x)->header_obj;
220         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
221           [ PublicInbox::View::msg_reply($ctx, $hdr, $foot)] ];
222 }
223
224 # /$INBOX/$MESSAGE_ID/t/
225 sub get_thread {
226         my ($ctx, $flat) = @_;
227         my $srch = searcher($ctx) or return need_search($ctx);
228         require PublicInbox::View;
229         my $foot = footer($ctx);
230         $ctx->{flat} = $flat;
231         PublicInbox::View::thread_html($ctx, $foot, $srch);
232 }
233
234 sub ctx_get {
235         my ($ctx, $key) = @_;
236         my $val = $ctx->{$key};
237         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
238         $val;
239 }
240
241 sub footer {
242         my ($ctx) = @_;
243         return '' unless $ctx;
244         my $git_dir = ctx_get($ctx, 'git_dir');
245
246         # favor user-supplied footer
247         my $footer = try_cat("$git_dir/public-inbox/footer.html");
248         if (defined $footer) {
249                 chomp $footer;
250                 $ctx->{footer} = $footer;
251                 return $footer;
252         }
253
254         # auto-generate a footer
255         my $inbox = ctx_get($ctx, 'inbox');
256         my $desc = try_cat("$git_dir/description");
257         $desc = '$GIT_DIR/description missing' unless defined $desc;
258         chomp $desc;
259
260         my $urls = try_cat("$git_dir/cloneurl");
261         my @urls = split(/\r?\n/, $urls || '');
262         my %seen = map { $_ => 1 } @urls;
263         my $cgi = $ctx->{cgi};
264         my $http = $cgi->base->as_string . $inbox;
265         $seen{$http} or unshift @urls, $http;
266         my $ssoma_url = PublicInbox::Hval::prurl($cgi->{env}, SSOMA_URL);
267         if (scalar(@urls) == 1) {
268                 $urls = "URL for <a\nhref=\"" . $ssoma_url .
269                         qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
270         } else {
271                 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
272                         qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
273                         join("\n", map { "\tgit clone --mirror $_" } @urls);
274         }
275
276         my $addr = $ctx->{pi_config}->get($inbox, 'address');
277         if (ref($addr) eq 'ARRAY') {
278                 $addr = $addr->[0]; # first address is primary
279         }
280
281         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
282
283         $ctx->{footer} = join("\n",
284                 '- ' . $desc,
285                 "A <a\nhref=\"" .
286                         PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
287                         '">public-inbox</a>, ' .
288                         'anybody may post in plain-text (not HTML):',
289                 $addr,
290                 $urls
291         );
292 }
293
294 # search support is optional, returns undef if Xapian is not installed
295 # or not configured for the given GIT_DIR
296 sub searcher {
297         my ($ctx) = @_;
298         eval {
299                 require PublicInbox::Search;
300                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
301         };
302 }
303
304 sub need_search {
305         my ($ctx) = @_;
306         my $msg = <<EOF;
307 <html><head><title>Search not available for this
308 public-inbox</title><body><pre>Search is not available for this public-inbox
309 <a href="../">Return to index</a></pre></body></html>
310 EOF
311         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
312 }
313
314 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
315 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
316 # note: I'm not a big fan of other compression formats since they're
317 # significantly more expensive on CPU than gzip and less-widely available,
318 # especially on older systems.  Stick to zlib since that's what git uses.
319 sub get_thread_mbox {
320         my ($ctx, $sfx) = @_;
321         my $srch = searcher($ctx) or return need_search($ctx);
322         require PublicInbox::Mbox;
323         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
324 }
325
326
327 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
328 sub get_thread_atom {
329         my ($ctx) = @_;
330         searcher($ctx) or return need_search($ctx);
331         $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
332         require PublicInbox::Feed;
333         PublicInbox::Feed::generate_thread_atom($ctx);
334 }
335
336 sub legacy_redirects {
337         my ($self, $ctx, $path_info) = @_;
338
339         # single-message pages
340         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
341                 r301($ctx, $1, $2);
342         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
343                 r301($ctx, $1, $2, 'raw');
344
345         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
346                 r301($ctx, $1, $2);
347
348         # thread display
349         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
350                 r301($ctx, $1, $2, 't/#u');
351
352         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
353                 r301($ctx, $1, $2, "t.mbox$3");
354
355         # even older legacy redirects
356         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
357                 r301($ctx, $1, $2);
358
359         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
360                 r301($ctx, $1, $2, 't/#u');
361
362         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
363                 r301($ctx, $1, $2);
364
365         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
366                 r301($ctx, $1, $2, 'raw');
367
368         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
369                 r301($ctx, $1, $2, "t$3");
370
371         # legacy convenience redirects, order still matters
372         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
373                 r301($ctx, $1, $2);
374         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
375                 r301($ctx, $1, $2, 't/#u');
376         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
377                 r301($ctx, $1, $2);
378
379         # some Message-IDs have slashes in them and the HTTP server
380         # may try to be clever and unescape them :<
381         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
382                 msg_page($self, $ctx, $1, $2, $3);
383
384         # in case people leave off the trailing slash:
385         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
386                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
387         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
388                 r301($ctx, $1, $2);
389         } else {
390                 $self->news_www->call($ctx->{cgi}->{env});
391         }
392 }
393
394 sub r301 {
395         my ($ctx, $inbox, $mid, $suffix) = @_;
396         my $cgi = $ctx->{cgi};
397         my $url;
398         my $qs = $cgi->env->{QUERY_STRING};
399         $url = $cgi->base->as_string . $inbox . '/';
400         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
401         $url .= $suffix if (defined $suffix);
402         $url .= "?$qs" if $qs ne '';
403
404         [ 301,
405           [ Location => $url, 'Content-Type' => 'text/plain' ],
406           [ "Redirecting to $url\n" ] ]
407 }
408
409 sub msg_page {
410         my ($self, $ctx, $inbox, $mid, $e) = @_;
411         my $ret;
412         $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
413         '' eq $e and return get_mid_html($ctx);
414         't/' eq $e and return get_thread($ctx);
415         't.atom' eq $e and return get_thread_atom($ctx);
416         't.mbox' eq $e and return get_thread_mbox($ctx);
417         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
418         'T/' eq $e and return get_thread($ctx, 1);
419         'raw' eq $e and return get_mid_txt($ctx);
420
421         # legacy, but no redirect for compatibility:
422         'f/' eq $e and return get_mid_html($ctx);
423
424         'R/' eq $e and return get_reply_html($ctx);
425         r404($ctx);
426 }
427
428 sub serve_git {
429         my ($cgi, $git, $path) = @_;
430         PublicInbox::GitHTTPBackend::serve($cgi, $git, $path);
431 }
432
433 sub news_www {
434         my ($self) = @_;
435         my $nw = $self->{news_www};
436         return $nw if $nw;
437         require PublicInbox::NewsWWW;
438         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
439 }
440
441 1;