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