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