1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # Main web interface for mailing list archives
6 # We focus on the lowest common denominators here:
7 # - targeted at text-only console browsers (w3m, links, etc..)
8 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
9 # - No JavaScript, graphics or icons allowed.
10 # - Must not rely on static content
11 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
12 package PublicInbox::WWW;
16 use PublicInbox::Config qw(try_cat);
17 use URI::Escape qw(uri_escape_utf8 uri_unescape);
18 use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
19 use constant PI_URL => 'http://public-inbox.org/';
20 require PublicInbox::Git;
21 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
22 our $MID_RE = qr!([^/]+)!;
23 our $END_RE = qr!(f/|T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
27 my ($cgi, $method) = @_;
28 $pi_config ||= PublicInbox::Config->new;
29 my $ctx = { cgi => $cgi, pi_config => $pi_config };
30 if ($method !~ /\AGET|HEAD\z/) {
31 return r(405, 'Method Not Allowed');
33 my $path_info = $cgi->path_info;
35 # top-level indices and feeds
36 if ($path_info eq '/') {
38 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
39 invalid_list($ctx, $1) || r301($ctx, $1);
40 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
41 invalid_list($ctx, $1) || get_index($ctx);
42 } elsif ($path_info =~ m!$LISTNAME_RE/(?:atom\.xml|new\.atom)\z!o) {
43 invalid_list($ctx, $1) || get_atom($ctx);
45 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/$END_RE\z!o) {
46 msg_page($ctx, $1, $2, $3);
48 # in case people leave off the trailing slash:
49 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t)\z!o) {
50 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
52 # convenience redirects order matters
53 } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
57 legacy_redirects($ctx, $path_info);
61 # for CoW-friendliness, MOOOOO!
63 require PublicInbox::Feed;
64 require PublicInbox::View;
65 require PublicInbox::Thread;
71 require PublicInbox::Search;
72 require PublicInbox::SearchView;
73 require PublicInbox::Mbox;
74 require IO::Compress::Gzip;
78 # private functions below
82 if ($ctx && $ctx->{mid}) {
83 require PublicInbox::ExtMsg;
85 return PublicInbox::ExtMsg::ext_msg($ctx);
90 # simple response for errors
91 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
93 # returns undef if valid, array ref response if invalid
95 my ($ctx, $listname) = @_;
96 my $git_dir = $pi_config->get($listname, "mainrepo");
97 if (defined $git_dir) {
98 $ctx->{git_dir} = $git_dir;
99 $ctx->{git} = PublicInbox::Git->new($git_dir);
100 $ctx->{listname} = $listname;
106 # returns undef if valid, array ref response if invalid
107 sub invalid_list_mid {
108 my ($ctx, $listname, $mid) = @_;
109 my $ret = invalid_list($ctx, $listname, $mid);
112 $ctx->{mid} = $mid = uri_unescape($mid);
113 if ($mid =~ /\A[a-f0-9]{40}\z/) {
114 if ($mid = mid2blob($ctx)) {
115 require Email::Simple;
116 use PublicInbox::MID qw/mid_clean/;
117 $mid = Email::Simple->new($mid);
118 $ctx->{mid} = mid_clean($mid->header('Message-ID'));
124 # /$LISTNAME/new.atom -> Atom feed, includes replies
127 require PublicInbox::Feed;
128 PublicInbox::Feed::generate($ctx);
131 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
134 require PublicInbox::Feed;
135 my $srch = searcher($ctx);
137 if (defined $ctx->{cgi}->param('q')) {
138 require PublicInbox::SearchView;
139 PublicInbox::SearchView::sres_top_html($ctx);
141 PublicInbox::Feed::generate_html_index($ctx);
145 # just returns a string ref for the blob in the current ctx
148 require PublicInbox::MID;
149 my $path = PublicInbox::MID::mid2path($ctx->{mid});
150 $ctx->{git}->cat_file("HEAD:$path");
153 # /$LISTNAME/$MESSAGE_ID/raw -> raw mbox
156 my $x = mid2blob($ctx) or return r404($ctx);
157 require PublicInbox::Mbox;
158 PublicInbox::Mbox::emit1($ctx, $x);
161 # /$LISTNAME/$MESSAGE_ID/ -> HTML content (short quotes)
164 my $x = mid2blob($ctx) or return r404($ctx);
166 require PublicInbox::View;
167 my $foot = footer($ctx);
169 my $mime = Email::MIME->new($x);
171 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
172 [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
175 # /$LISTNAME/$MESSAGE_ID/f/ -> HTML content (fullquotes)
178 my $x = mid2blob($ctx) or return r404($ctx);
180 require PublicInbox::View;
181 my $foot = footer($ctx);
183 my $mime = Email::MIME->new($x);
185 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
186 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
189 # /$LISTNAME/$MESSAGE_ID/t/
191 my ($ctx, $flat) = @_;
192 my $srch = searcher($ctx) or return need_search($ctx);
193 require PublicInbox::View;
194 my $foot = footer($ctx);
195 $ctx->{flat} = $flat;
196 PublicInbox::View::thread_html($ctx, $foot, $srch);
201 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
205 my ($ctx, $key) = @_;
206 my $val = $ctx->{$key};
207 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
213 return '' unless $ctx;
214 my $git_dir = ctx_get($ctx, 'git_dir');
216 # favor user-supplied footer
217 my $footer = try_cat("$git_dir/public-inbox/footer.html");
218 if (defined $footer) {
220 $ctx->{footer} = $footer;
224 # auto-generate a footer
225 my $listname = ctx_get($ctx, 'listname');
226 my $desc = try_cat("$git_dir/description");
227 $desc = '$GIT_DIR/description missing' unless defined $desc;
230 my $urls = try_cat("$git_dir/cloneurl");
231 my @urls = split(/\r?\n/, $urls || '');
232 my $nurls = scalar @urls;
234 $urls = '($GIT_DIR/cloneurl missing)';
235 } elsif ($nurls == 1) {
236 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
237 '">ssoma</a>: ' . $urls[0];
239 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
240 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
243 my $addr = $pi_config->get($listname, 'address');
244 if (ref($addr) eq 'ARRAY') {
245 $addr = $addr->[0]; # first address is primary
248 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
250 $ctx->{footer} = join("\n",
252 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
253 'anybody may post in plain-text (not HTML):',
259 # search support is optional, returns undef if Xapian is not installed
260 # or not configured for the given GIT_DIR
264 require PublicInbox::Search;
265 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
272 <html><head><title>Search not available for this
273 public-inbox</title><body><pre>Search is not available for this public-inbox
274 <a href="../">Return to index</a></pre></body></html>
276 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
279 # /$LISTNAME/$MESSAGE_ID/t.mbox -> thread as mbox
280 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
281 # note: I'm not a big fan of other compression formats since they're
282 # significantly more expensive on CPU than gzip and less-widely available,
283 # especially on older systems. Stick to zlib since that's what git uses.
284 sub get_thread_mbox {
285 my ($ctx, $sfx) = @_;
286 my $srch = searcher($ctx) or return need_search($ctx);
287 require PublicInbox::Mbox;
288 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
292 # /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
293 sub get_thread_atom {
295 searcher($ctx) or return need_search($ctx);
296 $ctx->{self_url} = self_url($ctx->{cgi});
297 require PublicInbox::Feed;
298 PublicInbox::Feed::generate_thread_atom($ctx);
301 sub legacy_redirects {
302 my ($ctx, $path_info) = @_;
304 # single-message pages
305 if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
307 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
308 r301($ctx, $1, $2, 'raw');
310 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
311 r301($ctx, $1, $2, 'f/');
314 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
315 r301($ctx, $1, $2, 't/#u');
317 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
318 r301($ctx, $1, $2, "t.mbox$3");
320 # even older legacy redirects
321 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
324 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
325 r301($ctx, $1, $2, 't/#u');
327 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
328 r301($ctx, $1, $2, 'f/');
330 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
331 r301($ctx, $1, $2, 'raw');
333 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
334 r301($ctx, $1, $2, "t$3");
336 # legacy convenience redirects, order still matters
337 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
339 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
340 r301($ctx, $1, $2, 't/#u');
341 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
342 r301($ctx, $1, $2, 'f/');
344 # some Message-IDs have slashes in them and the HTTP server
345 # may try to be clever and unescape them :<
346 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
347 msg_page($ctx, $1, $2, $3);
349 # in case people leave off the trailing slash:
350 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
351 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
358 my ($ctx, $listname, $mid, $suffix) = @_;
359 my $cgi = $ctx->{cgi};
361 if (ref($cgi) eq 'CGI') {
362 $url = $cgi->url(-base) . '/';
364 $url = $cgi->base->as_string;
367 $url .= $listname . '/';
368 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
369 $url .= $suffix if (defined $suffix);
372 [ Location => $url, 'Content-Type' => 'text/plain' ],
373 [ "Redirecting to $url\n" ] ]
377 my ($ctx, $list, $mid, $e) = @_;
378 unless (invalid_list_mid($ctx, $list, $mid)) {
379 '' eq $e and return get_mid_html($ctx);
380 't/' eq $e and return get_thread($ctx);
381 't.atom' eq $e and return get_thread_atom($ctx);
382 't.mbox' eq $e and return get_thread_mbox($ctx);
383 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
384 'T/' eq $e and return get_thread($ctx, 1);
385 'raw' eq $e and return get_mid_txt($ctx);
386 'f/' eq $e and return get_full_html($ctx);