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