]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
update copyright headers and email addresses
[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 # 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                 searcher($ctx);
83                 return PublicInbox::ExtMsg::ext_msg($ctx);
84         }
85         r(404, 'Not Found');
86 }
87
88 # simple response for errors
89 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
90
91 # returns undef if valid, array ref response if invalid
92 sub invalid_list {
93         my ($ctx, $listname) = @_;
94         my $git_dir = $pi_config->get($listname, "mainrepo");
95         if (defined $git_dir) {
96                 $ctx->{git_dir} = $git_dir;
97                 $ctx->{listname} = $listname;
98                 return;
99         }
100         r404();
101 }
102
103 # returns undef if valid, array ref response if invalid
104 sub invalid_list_mid {
105         my ($ctx, $listname, $mid) = @_;
106         my $ret = invalid_list($ctx, $listname, $mid);
107         return $ret if $ret;
108
109         $ctx->{mid} = $mid = uri_unescape($mid);
110         if ($mid =~ /\A[a-f0-9]{40}\z/) {
111                 if ($mid = mid2blob($ctx)) {
112                         require Email::Simple;
113                         use PublicInbox::MID qw/mid_clean/;
114                         $mid = Email::Simple->new($mid);
115                         $ctx->{mid} = mid_clean($mid->header('Message-ID'));
116                 }
117         }
118         undef;
119 }
120
121 # /$LISTNAME/new.atom                     -> Atom feed, includes replies
122 sub get_atom {
123         my ($ctx) = @_;
124         require PublicInbox::Feed;
125         PublicInbox::Feed::generate($ctx);
126 }
127
128 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
129 sub get_index {
130         my ($ctx) = @_;
131         require PublicInbox::Feed;
132         my $srch = searcher($ctx);
133         my $q = $ctx->{cgi}->param('q');
134         footer($ctx);
135         if (defined $q) {
136                 require PublicInbox::SearchView;
137                 PublicInbox::SearchView::sres_top_html($ctx, $q);
138         } else {
139                 PublicInbox::Feed::generate_html_index($ctx);
140         }
141 }
142
143 # just returns a string ref for the blob in the current ctx
144 sub mid2blob {
145         my ($ctx) = @_;
146         require PublicInbox::MID;
147         my $path = PublicInbox::MID::mid2path($ctx->{mid});
148         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
149                         qw(cat-file blob), "HEAD:$path");
150         my $pid = open my $fh, '-|';
151         defined $pid or die "fork failed: $!\n";
152         if ($pid == 0) {
153                 open STDERR, '>', '/dev/null'; # ignore errors
154                 exec @cmd or die "exec failed: $!\n";
155         } else {
156                 my $blob = eval { local $/; <$fh> };
157                 close $fh;
158                 $? == 0 ? \$blob : undef;
159         }
160 }
161
162 # /$LISTNAME/$MESSAGE_ID/raw                    -> raw mbox
163 sub get_mid_txt {
164         my ($ctx) = @_;
165         my $x = mid2blob($ctx) or return r404($ctx);
166         require PublicInbox::Mbox;
167         PublicInbox::Mbox::emit1($x);
168 }
169
170 # /$LISTNAME/$MESSAGE_ID/                   -> HTML content (short quotes)
171 sub get_mid_html {
172         my ($ctx) = @_;
173         my $x = mid2blob($ctx) or return r404($ctx);
174
175         require PublicInbox::View;
176         my $foot = footer($ctx);
177         require Email::MIME;
178         my $mime = Email::MIME->new($x);
179         searcher($ctx);
180         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
181           [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
182 }
183
184 # /$LISTNAME/$MESSAGE_ID/f/                   -> HTML content (fullquotes)
185 sub get_full_html {
186         my ($ctx) = @_;
187         my $x = mid2blob($ctx) or return r404($ctx);
188
189         require PublicInbox::View;
190         my $foot = footer($ctx);
191         require Email::MIME;
192         my $mime = Email::MIME->new($x);
193         searcher($ctx);
194         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
195           [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
196 }
197
198 # /$LISTNAME/$MESSAGE_ID/t/
199 sub get_thread {
200         my ($ctx, $flat) = @_;
201         my $srch = searcher($ctx) or return need_search($ctx);
202         require PublicInbox::View;
203         my $foot = footer($ctx);
204         $ctx->{flat} = $flat;
205         PublicInbox::View::thread_html($ctx, $foot, $srch);
206 }
207
208 sub self_url {
209         my ($cgi) = @_;
210         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
211 }
212
213 sub ctx_get {
214         my ($ctx, $key) = @_;
215         my $val = $ctx->{$key};
216         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
217         $val;
218 }
219
220 sub try_cat {
221         my ($path) = @_;
222         my $rv;
223         if (open(my $fh, '<', $path)) {
224                 local $/;
225                 $rv = <$fh>;
226                 close $fh;
227         }
228         $rv;
229 }
230
231 sub footer {
232         my ($ctx) = @_;
233         return '' unless $ctx;
234         my $git_dir = ctx_get($ctx, 'git_dir');
235
236         # favor user-supplied footer
237         my $footer = try_cat("$git_dir/public-inbox/footer.html");
238         if (defined $footer) {
239                 chomp $footer;
240                 $ctx->{footer} = $footer;
241                 return $footer;
242         }
243
244         # auto-generate a footer
245         my $listname = ctx_get($ctx, 'listname');
246         my $desc = try_cat("$git_dir/description");
247         $desc = '$GIT_DIR/description missing' unless defined $desc;
248         chomp $desc;
249
250         my $urls = try_cat("$git_dir/cloneurl");
251         my @urls = split(/\r?\n/, $urls || '');
252         my $nurls = scalar @urls;
253         if ($nurls == 0) {
254                 $urls = '($GIT_DIR/cloneurl missing)';
255         } elsif ($nurls == 1) {
256                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
257                         '">ssoma</a>: ' . $urls[0];
258         } else {
259                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
260                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
261         }
262
263         my $addr = $pi_config->get($listname, 'address');
264         if (ref($addr) eq 'ARRAY') {
265                 $addr = $addr->[0]; # first address is primary
266         }
267
268         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
269
270         $ctx->{footer} = join("\n",
271                 '- ' . $desc,
272                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
273                         'anybody may post in plain-text (not HTML):',
274                 $addr,
275                 $urls
276         );
277 }
278
279 # search support is optional, returns undef if Xapian is not installed
280 # or not configured for the given GIT_DIR
281 sub searcher {
282         my ($ctx) = @_;
283         eval {
284                 require PublicInbox::Search;
285                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
286         };
287 }
288
289 sub need_search {
290         my ($ctx) = @_;
291         my $msg = <<EOF;
292 <html><head><title>Search not available for this
293 public-inbox</title><body><pre>Search is not available for this public-inbox
294 <a href="../">Return to index</a></pre></body></html>
295 EOF
296         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
297 }
298
299 # /$LISTNAME/$MESSAGE_ID/t.mbox           -> thread as mbox
300 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
301 # note: I'm not a big fan of other compression formats since they're
302 # significantly more expensive on CPU than gzip and less-widely available,
303 # especially on older systems.  Stick to zlib since that's what git uses.
304 sub get_thread_mbox {
305         my ($ctx, $sfx) = @_;
306         my $srch = searcher($ctx) or return need_search($ctx);
307         require PublicInbox::Mbox;
308         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
309 }
310
311
312 # /$LISTNAME/$MESSAGE_ID/t.atom           -> thread as Atom feed
313 sub get_thread_atom {
314         my ($ctx) = @_;
315         searcher($ctx) or return need_search($ctx);
316         $ctx->{self_url} = self_url($ctx->{cgi});
317         require PublicInbox::Feed;
318         PublicInbox::Feed::generate_thread_atom($ctx);
319 }
320
321 sub legacy_redirects {
322         my ($ctx, $path_info) = @_;
323
324         # single-message pages
325         if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
326                 r301($ctx, $1, $2);
327         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
328                 r301($ctx, $1, $2, 'raw');
329
330         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
331                 r301($ctx, $1, $2, 'f/');
332
333         # thread display
334         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
335                 r301($ctx, $1, $2, 't/#u');
336
337         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
338                 r301($ctx, $1, $2, "t.mbox$3");
339
340         # even older legacy redirects
341         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
342                 r301($ctx, $1, $2);
343
344         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
345                 r301($ctx, $1, $2, 't/#u');
346
347         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
348                 r301($ctx, $1, $2, 'f/');
349
350         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
351                 r301($ctx, $1, $2, 'raw');
352
353         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
354                 r301($ctx, $1, $2, "t$3");
355
356         # legacy convenience redirects, order still matters
357         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
358                 r301($ctx, $1, $2);
359         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
360                 r301($ctx, $1, $2, 't/#u');
361         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
362                 r301($ctx, $1, $2, 'f/');
363
364         # some Message-IDs have slashes in them and the HTTP server
365         # may try to be clever and unescape them :<
366         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
367                 msg_page($ctx, $1, $2, $3);
368
369         # in case people leave off the trailing slash:
370         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
371                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
372         } else {
373                 r404();
374         }
375 }
376
377 sub r301 {
378         my ($ctx, $listname, $mid, $suffix) = @_;
379         my $cgi = $ctx->{cgi};
380         my $url;
381         if (ref($cgi) eq 'CGI') {
382                 $url = $cgi->url(-base) . '/';
383         } else {
384                 $url = $cgi->base->as_string;
385         }
386
387         $url .= $listname . '/';
388         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
389         $url .= $suffix if (defined $suffix);
390
391         [ 301,
392           [ Location => $url, 'Content-Type' => 'text/plain' ],
393           [ "Redirecting to $url\n" ] ]
394 }
395
396 sub msg_page {
397         my ($ctx, $list, $mid, $e) = @_;
398         unless (invalid_list_mid($ctx, $list, $mid)) {
399                 '' eq $e and return get_mid_html($ctx);
400                 't/' eq $e and return get_thread($ctx);
401                 't.atom' eq $e and return get_thread_atom($ctx);
402                 't.mbox' eq $e and return get_thread_mbox($ctx);
403                 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
404                 'T/' eq $e and return get_thread($ctx, 1);
405                 'raw' eq $e and return get_mid_txt($ctx);
406                 'f/' eq $e and return get_full_html($ctx);
407         }
408         r404($ctx);
409 }
410
411 1;