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