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