]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
32cc0b27be365c25b85f6b28393ec412c44ec556
[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 (lynx, w3m, 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, 0);
39         } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
40                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
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         # convenience redirects, order matters
53         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
54                 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
55
56         } else {
57                 r404();
58         }
59 }
60
61 # for CoW-friendliness, MOOOOO!
62 sub preload {
63         require PublicInbox::Feed;
64         require PublicInbox::View;
65         require PublicInbox::Thread;
66         require Email::MIME;
67         require Digest::SHA;
68         require POSIX;
69         require XML::Atom::SimpleFeed;
70 }
71
72 # private functions below
73
74 sub r404 { r(404, 'Not Found') }
75
76 # simple response for errors
77 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
78
79 # returns undef if valid, array ref response if invalid
80 sub invalid_list {
81         my ($ctx, $listname) = @_;
82         my $git_dir = $pi_config->get($listname, "mainrepo");
83         if (defined $git_dir) {
84                 $ctx->{git_dir} = $git_dir;
85                 $ctx->{listname} = $listname;
86                 return;
87         }
88         r404();
89 }
90
91 # returns undef if valid, array ref response if invalid
92 sub invalid_list_mid {
93         my ($ctx, $listname, $mid) = @_;
94         my $ret = invalid_list($ctx, $listname, $mid);
95         $ctx->{mid} = uri_unescape($mid) unless $ret;
96         $ret;
97 }
98
99 # /$LISTNAME/atom.xml                       -> Atom feed, includes replies
100 sub get_atom {
101         my ($ctx, $cgi, $top) = @_;
102         require PublicInbox::Feed;
103         [ 200, [ 'Content-Type' => 'application/xml' ],
104           [ PublicInbox::Feed->generate({
105                         git_dir => $ctx->{git_dir},
106                         listname => $ctx->{listname},
107                         pi_config => $pi_config,
108                         cgi => $cgi,
109                         top => $top,
110                 }) ]
111         ];
112 }
113
114 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
115 sub get_index {
116         my ($ctx, $cgi, $top) = @_;
117         require PublicInbox::Feed;
118         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
119           [ PublicInbox::Feed->generate_html_index({
120                         git_dir => $ctx->{git_dir},
121                         listname => $ctx->{listname},
122                         pi_config => $pi_config,
123                         cgi => $cgi,
124                         footer => footer($ctx),
125                         top => $top,
126                 }) ]
127         ];
128 }
129
130 # just returns a string ref for the blob in the current ctx
131 sub mid2blob {
132         my ($ctx) = @_;
133         my $hex = $ctx->{mid};
134         my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
135
136         unless (defined $x38) {
137                 # compatibility with old links
138                 require Digest::SHA;
139                 $hex = Digest::SHA::sha1_hex($hex);
140                 ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
141                 defined $x38 or die "BUG: not a SHA-1 hex: $hex";
142         }
143
144         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
145                         qw(cat-file blob), "HEAD:$x2/$x38");
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 $mid_href = PublicInbox::Hval::ascii_html(
174                                                 uri_escape_utf8($ctx->{mid}));
175         my $pfx = "../f/$mid_href.html";
176         my $foot = footer($ctx);
177         require Email::MIME;
178         my $mime = Email::MIME->new($x);
179         my $srch = searcher($ctx);
180         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
181           [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
182 }
183
184 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
185 sub get_full_html {
186         my ($ctx, $cgi) = @_;
187         my $x = mid2blob($ctx);
188         return r404() unless $x;
189         require PublicInbox::View;
190         my $foot = footer($ctx);
191         require Email::MIME;
192         my $mime = Email::MIME->new($x);
193         my $srch = searcher($ctx);
194         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
195           [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
196 }
197
198 sub self_url {
199         my ($cgi) = @_;
200         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
201 }
202
203 sub redirect_list_index {
204         my ($ctx, $cgi) = @_;
205         do_redirect(self_url($cgi) . "/");
206 }
207
208 sub redirect_mid {
209         my ($ctx, $cgi) = @_;
210         my $url = self_url($cgi);
211         $url =~ s!/f/!/m/!;
212         do_redirect($url . '.html');
213 }
214
215 sub do_redirect {
216         my ($url) = @_;
217         [ 301,
218           [ Location => $url, 'Content-Type' => 'text/plain' ],
219           [ "Redirecting to $url\n" ]
220         ]
221 }
222
223 sub ctx_get {
224         my ($ctx, $key) = @_;
225         my $val = $ctx->{$key};
226         (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
227         $val;
228 }
229
230 sub try_cat {
231         my ($path) = @_;
232         my $rv;
233         if (open(my $fh, '<', $path)) {
234                 local $/;
235                 $rv = <$fh>;
236                 close $fh;
237         }
238         $rv;
239 }
240
241 sub footer {
242         my ($ctx) = @_;
243         return '' unless $ctx;
244         my $git_dir = ctx_get($ctx, 'git_dir');
245
246         # favor user-supplied footer
247         my $footer = try_cat("$git_dir/public-inbox/footer.html");
248         if (defined $footer) {
249                 chomp $footer;
250                 return $footer;
251         }
252
253         # auto-generate a footer
254         my $listname = ctx_get($ctx, 'listname');
255         my $desc = try_cat("$git_dir/description");
256         $desc = '$GIT_DIR/description missing' unless defined $desc;
257         chomp $desc;
258
259         my $urls = try_cat("$git_dir/cloneurl");
260         my @urls = split(/\r?\n/, $urls || '');
261         my $nurls = scalar @urls;
262         if ($nurls == 0) {
263                 $urls = '($GIT_DIR/cloneurl missing)';
264         } elsif ($nurls == 1) {
265                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
266                         '">ssoma</a>: ' . $urls[0];
267         } else {
268                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
269                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
270         }
271
272         my $addr = $pi_config->get($listname, 'address');
273         if (ref($addr) eq 'ARRAY') {
274                 $addr = $addr->[0]; # first address is primary
275         }
276
277         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
278         $desc =  $desc;
279         join("\n",
280                 '- ' . $desc,
281                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
282                         'anybody may post in plain-text (not HTML):',
283                 $addr,
284                 $urls
285         );
286 }
287
288 # search support is optional, returns undef if Xapian is not installed
289 # or not configured for the given GIT_DIR
290 sub searcher {
291         my ($ctx) = @_;
292         eval {
293                 require PublicInbox::Search;
294                 PublicInbox::Search->new($ctx->{git_dir});
295         };
296 }
297
298 1;