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