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