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