]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: reduce unused arguments in internal API
[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 $cmd = join(' ', @cmd);
142         my $pid = open my $fh, '-|';
143         defined $pid or die "fork failed: $!\n";
144         if ($pid == 0) {
145                 open STDERR, '>', '/dev/null'; # ignore errors
146                 exec @cmd or die "exec failed: $!\n";
147         } else {
148                 my $blob = eval { local $/; <$fh> };
149                 close $fh;
150                 $? == 0 ? \$blob : undef;
151         }
152 }
153
154 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw mbox
155 sub get_mid_txt {
156         my ($ctx) = @_;
157         my $x = mid2blob($ctx) or return r404();
158         require PublicInbox::Mbox;
159         PublicInbox::Mbox::emit1($x);
160 }
161
162 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
163 sub get_mid_html {
164         my ($ctx) = @_;
165         my $x = mid2blob($ctx);
166         return r404() unless $x;
167
168         require PublicInbox::View;
169         my $pfx = msg_pfx($ctx);
170         my $foot = footer($ctx);
171         require Email::MIME;
172         my $mime = Email::MIME->new($x);
173         searcher($ctx);
174         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
175           [ PublicInbox::View::msg_html($ctx, $mime, $pfx, $foot) ] ];
176 }
177
178 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
179 sub get_full_html {
180         my ($ctx) = @_;
181         my $x = mid2blob($ctx);
182         return r404() unless $x;
183         require PublicInbox::View;
184         my $foot = footer($ctx);
185         require Email::MIME;
186         my $mime = Email::MIME->new($x);
187         searcher($ctx);
188         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
189           [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
190 }
191
192 # /$LISTNAME/t/$MESSAGE_ID.html
193 sub get_thread {
194         my ($ctx) = @_;
195         my $srch = searcher($ctx) or return need_search($ctx);
196         require PublicInbox::View;
197         my $foot = footer($ctx);
198         PublicInbox::View::thread_html($ctx, $foot, $srch);
199 }
200
201 sub self_url {
202         my ($cgi) = @_;
203         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
204 }
205
206 sub redirect_list_index {
207         my ($cgi) = @_;
208         do_redirect(self_url($cgi) . "/");
209 }
210
211 sub redirect_mid {
212         my ($ctx, $pfx) = @_;
213         my $url = self_url($ctx->{cgi});
214         my $anchor = '';
215         if (lc($pfx) eq 't') {
216                 $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
217         }
218         do_redirect($url . ".html$anchor");
219 }
220
221 # only hit when somebody tries to guess URLs manually:
222 sub redirect_mid_txt {
223         my ($ctx, $pfx) = @_;
224         my $listname = $ctx->{listname};
225         my $url = self_url($ctx->{cgi});
226         $url =~ s!/$listname/f/(\S+\.txt)\z!/$listname/m/$1!;
227         do_redirect($url);
228 }
229
230 sub do_redirect {
231         my ($url) = @_;
232         [ 301,
233           [ Location => $url, 'Content-Type' => 'text/plain' ],
234           [ "Redirecting to $url\n" ]
235         ]
236 }
237
238 sub ctx_get {
239         my ($ctx, $key) = @_;
240         my $val = $ctx->{$key};
241         (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
242         $val;
243 }
244
245 sub try_cat {
246         my ($path) = @_;
247         my $rv;
248         if (open(my $fh, '<', $path)) {
249                 local $/;
250                 $rv = <$fh>;
251                 close $fh;
252         }
253         $rv;
254 }
255
256 sub footer {
257         my ($ctx) = @_;
258         return '' unless $ctx;
259         my $git_dir = ctx_get($ctx, 'git_dir');
260
261         # favor user-supplied footer
262         my $footer = try_cat("$git_dir/public-inbox/footer.html");
263         if (defined $footer) {
264                 chomp $footer;
265                 $ctx->{footer} = $footer;
266                 return $footer;
267         }
268
269         # auto-generate a footer
270         my $listname = ctx_get($ctx, 'listname');
271         my $desc = try_cat("$git_dir/description");
272         $desc = '$GIT_DIR/description missing' unless defined $desc;
273         chomp $desc;
274
275         my $urls = try_cat("$git_dir/cloneurl");
276         my @urls = split(/\r?\n/, $urls || '');
277         my $nurls = scalar @urls;
278         if ($nurls == 0) {
279                 $urls = '($GIT_DIR/cloneurl missing)';
280         } elsif ($nurls == 1) {
281                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
282                         '">ssoma</a>: ' . $urls[0];
283         } else {
284                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
285                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
286         }
287
288         my $addr = $pi_config->get($listname, 'address');
289         if (ref($addr) eq 'ARRAY') {
290                 $addr = $addr->[0]; # first address is primary
291         }
292
293         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
294
295         $ctx->{footer} = join("\n",
296                 '- ' . $desc,
297                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
298                         'anybody may post in plain-text (not HTML):',
299                 $addr,
300                 $urls
301         );
302 }
303
304 # search support is optional, returns undef if Xapian is not installed
305 # or not configured for the given GIT_DIR
306 sub searcher {
307         my ($ctx) = @_;
308         eval {
309                 require PublicInbox::Search;
310                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
311         };
312 }
313
314 sub need_search {
315         my ($ctx) = @_;
316         my $msg = <<EOF;
317 <html><head><title>Search not available for this
318 public-inbox</title><body><pre>Search is not available for this public-inbox
319 <a href="../">Return to index</a></pre></body></html>
320 EOF
321         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
322 }
323
324 sub msg_pfx {
325         my ($ctx) = @_;
326         my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));
327         "../f/$href.html";
328 }
329
330 # /$LISTNAME/t/$MESSAGE_ID.mbox           -> thread as mbox
331 # /$LISTNAME/t/$MESSAGE_ID.mbox.gz        -> thread as gzipped mbox
332 # note: I'm not a big fan of other compression formats since they're
333 # significantly more expensive on CPU than gzip and less-widely available,
334 # especially on older systems.  Stick to zlib since that's what git uses.
335 sub get_thread_mbox {
336         my ($ctx, $sfx) = @_;
337         my $srch = searcher($ctx) or return need_search($ctx);
338         require PublicInbox::Mbox;
339         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
340 }
341
342 1;