]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: /t/$MESSAGE_ID.html for threads
[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 (lynx, w3m, 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, 0);
39         } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
40                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
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         # convenience redirects, order matters
57         } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t)/(\S+)\z!o) {
58                 my $pfx = $2;
59                 invalid_list_mid(\%ctx, $1, $3) ||
60                         redirect_mid(\%ctx, $cgi, $2);
61
62         } else {
63                 r404();
64         }
65 }
66
67 # for CoW-friendliness, MOOOOO!
68 sub preload {
69         require PublicInbox::Feed;
70         require PublicInbox::View;
71         require PublicInbox::Thread;
72         require Email::MIME;
73         require Digest::SHA;
74         require POSIX;
75         require XML::Atom::SimpleFeed;
76 }
77
78 # private functions below
79
80 sub r404 { r(404, 'Not Found') }
81
82 # simple response for errors
83 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
84
85 # returns undef if valid, array ref response if invalid
86 sub invalid_list {
87         my ($ctx, $listname) = @_;
88         my $git_dir = $pi_config->get($listname, "mainrepo");
89         if (defined $git_dir) {
90                 $ctx->{git_dir} = $git_dir;
91                 $ctx->{listname} = $listname;
92                 return;
93         }
94         r404();
95 }
96
97 # returns undef if valid, array ref response if invalid
98 sub invalid_list_mid {
99         my ($ctx, $listname, $mid) = @_;
100         my $ret = invalid_list($ctx, $listname, $mid);
101         $ctx->{mid} = uri_unescape($mid) unless $ret;
102         $ret;
103 }
104
105 # /$LISTNAME/atom.xml                       -> Atom feed, includes replies
106 sub get_atom {
107         my ($ctx, $cgi, $top) = @_;
108         require PublicInbox::Feed;
109         [ 200, [ 'Content-Type' => 'application/xml' ],
110           [ PublicInbox::Feed->generate({
111                         git_dir => $ctx->{git_dir},
112                         listname => $ctx->{listname},
113                         pi_config => $pi_config,
114                         cgi => $cgi,
115                         top => $top,
116                 }) ]
117         ];
118 }
119
120 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
121 sub get_index {
122         my ($ctx, $cgi, $top) = @_;
123         require PublicInbox::Feed;
124         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
125           [ PublicInbox::Feed->generate_html_index({
126                         git_dir => $ctx->{git_dir},
127                         listname => $ctx->{listname},
128                         pi_config => $pi_config,
129                         cgi => $cgi,
130                         footer => footer($ctx),
131                         top => $top,
132                 }) ]
133         ];
134 }
135
136 # just returns a string ref for the blob in the current ctx
137 sub mid2blob {
138         my ($ctx) = @_;
139         my $hex = $ctx->{mid};
140         my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
141
142         unless (defined $x38) {
143                 # compatibility with old links
144                 require Digest::SHA;
145                 $hex = Digest::SHA::sha1_hex($hex);
146                 ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
147                 defined $x38 or die "BUG: not a SHA-1 hex: $hex";
148         }
149
150         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
151                         qw(cat-file blob), "HEAD:$x2/$x38");
152         my $cmd = join(' ', @cmd);
153         my $pid = open my $fh, '-|';
154         defined $pid or die "fork failed: $!\n";
155         if ($pid == 0) {
156                 open STDERR, '>', '/dev/null'; # ignore errors
157                 exec @cmd or die "exec failed: $!\n";
158         } else {
159                 my $blob = eval { local $/; <$fh> };
160                 close $fh;
161                 $? == 0 ? \$blob : undef;
162         }
163 }
164
165 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
166 sub get_mid_txt {
167         my ($ctx, $cgi) = @_;
168         my $x = mid2blob($ctx);
169         $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
170 }
171
172 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
173 sub get_mid_html {
174         my ($ctx, $cgi) = @_;
175         my $x = mid2blob($ctx);
176         return r404() unless $x;
177
178         require PublicInbox::View;
179         my $pfx = msg_pfx($ctx);
180         my $foot = footer($ctx);
181         require Email::MIME;
182         my $mime = Email::MIME->new($x);
183         my $srch = searcher($ctx);
184         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
185           [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
186 }
187
188 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
189 sub get_full_html {
190         my ($ctx, $cgi) = @_;
191         my $x = mid2blob($ctx);
192         return r404() unless $x;
193         require PublicInbox::View;
194         my $foot = footer($ctx);
195         require Email::MIME;
196         my $mime = Email::MIME->new($x);
197         my $srch = searcher($ctx);
198         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
199           [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
200 }
201
202 # /$LISTNAME/t/$MESSAGE_ID.html
203 sub get_thread {
204         my ($ctx, $cgi) = @_;
205         my $srch = searcher($ctx) or return need_search($ctx);
206         require PublicInbox::View;
207         my $foot = footer($ctx);
208         my $body = PublicInbox::View->thread_html($ctx, $foot, $srch) or
209                 return r404();
210         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
211           [ $body ] ];
212 }
213
214 sub self_url {
215         my ($cgi) = @_;
216         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
217 }
218
219 sub redirect_list_index {
220         my ($ctx, $cgi) = @_;
221         do_redirect(self_url($cgi) . "/");
222 }
223
224 sub redirect_mid {
225         my ($ctx, $cgi, $pfx) = @_;
226         my $url = self_url($cgi);
227         my $anchor = '';
228         if (lc($pfx) eq 't') {
229                 $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
230         }
231         do_redirect($url . ".html$anchor");
232 }
233
234 sub do_redirect {
235         my ($url) = @_;
236         [ 301,
237           [ Location => $url, 'Content-Type' => 'text/plain' ],
238           [ "Redirecting to $url\n" ]
239         ]
240 }
241
242 sub ctx_get {
243         my ($ctx, $key) = @_;
244         my $val = $ctx->{$key};
245         (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
246         $val;
247 }
248
249 sub try_cat {
250         my ($path) = @_;
251         my $rv;
252         if (open(my $fh, '<', $path)) {
253                 local $/;
254                 $rv = <$fh>;
255                 close $fh;
256         }
257         $rv;
258 }
259
260 sub footer {
261         my ($ctx) = @_;
262         return '' unless $ctx;
263         my $git_dir = ctx_get($ctx, 'git_dir');
264
265         # favor user-supplied footer
266         my $footer = try_cat("$git_dir/public-inbox/footer.html");
267         if (defined $footer) {
268                 chomp $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         $desc =  $desc;
298         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                 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 1;