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