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