]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: multi-line cloneurl maps to multiple lines
[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         require Digest::SHA;
134         my $hex = Digest::SHA::sha1_hex($ctx->{mid});
135         $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
136                         die "BUG: not a SHA-1 hex: $hex";
137
138         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
139                         qw(cat-file blob), "HEAD:$1/$2");
140         my $cmd = join(' ', @cmd);
141         my $pid = open my $fh, '-|';
142         defined $pid or die "fork failed: $!\n";
143         if ($pid == 0) {
144                 open STDERR, '>', '/dev/null'; # ignore errors
145                 exec @cmd or die "exec failed: $!\n";
146         } else {
147                 my $blob = eval { local $/; <$fh> };
148                 close $fh;
149                 $? == 0 ? \$blob : undef;
150         }
151 }
152
153 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
154 sub get_mid_txt {
155         my ($ctx, $cgi) = @_;
156         my $x = mid2blob($ctx);
157         $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
158 }
159
160 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
161 sub get_mid_html {
162         my ($ctx, $cgi) = @_;
163         my $x = mid2blob($ctx);
164         return r404() unless $x;
165
166         require PublicInbox::View;
167         my $mid_href = PublicInbox::Hval::ascii_html(
168                                                 uri_escape_utf8($ctx->{mid}));
169         my $pfx = "../f/$mid_href.html";
170         my $foot = footer($ctx);
171         require Email::MIME;
172         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
173           [ PublicInbox::View->msg_html(Email::MIME->new($x), $pfx, $foot) ] ];
174 }
175
176 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
177 sub get_full_html {
178         my ($ctx, $cgi) = @_;
179         my $x = mid2blob($ctx);
180         return r404() unless $x;
181         require PublicInbox::View;
182         require Email::MIME;
183         my $foot = footer($ctx);
184         [ 200, [ 'Content-Type' => 'text/html' ],
185           [ PublicInbox::View->msg_html(Email::MIME->new($x), undef, $foot)] ];
186 }
187
188 sub self_url {
189         my ($cgi) = @_;
190         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
191 }
192
193 sub redirect_list_index {
194         my ($ctx, $cgi) = @_;
195         do_redirect(self_url($cgi) . "/");
196 }
197
198 sub redirect_mid {
199         my ($ctx, $cgi) = @_;
200         my $url = self_url($cgi);
201         $url =~ s!/f/!/m/!;
202         do_redirect($url . '.html');
203 }
204
205 sub do_redirect {
206         my ($url) = @_;
207         [ 301,
208           [ Location => $url, 'Content-Type' => 'text/plain' ],
209           [ "Redirecting to $url\n" ]
210         ]
211 }
212
213 sub ctx_get {
214         my ($ctx, $key) = @_;
215         my $val = $ctx->{$key};
216         (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
217         $val;
218 }
219
220 sub try_cat {
221         my ($path) = @_;
222         my $rv;
223         if (open(my $fh, '<', $path)) {
224                 local $/;
225                 $rv = <$fh>;
226                 close $fh;
227         }
228         $rv;
229 }
230
231 sub footer {
232         my ($ctx) = @_;
233         return '' unless $ctx;
234         my $git_dir = ctx_get($ctx, 'git_dir');
235
236         # favor user-supplied footer
237         my $footer = try_cat("$git_dir/public-inbox/footer.html");
238         if (defined $footer) {
239                 chomp $footer;
240                 return $footer;
241         }
242
243         # auto-generate a footer
244         my $listname = ctx_get($ctx, 'listname');
245         my $desc = try_cat("$git_dir/description");
246         $desc = '$GIT_DIR/description missing' unless defined $desc;
247         chomp $desc;
248
249         my $urls = try_cat("$git_dir/cloneurl");
250         my @urls = split(/\r?\n/, $urls || '');
251         my $nurls = scalar @urls;
252         if ($nurls == 0) {
253                 $urls = '($GIT_DIR/cloneurl missing)';
254         } elsif ($nurls == 1) {
255                 $urls = 'git archive URL for <a href="' . SSOMA_URL .
256                         '">ssoma</a>: ' . $urls[0];
257         } else {
258                 $urls = 'git archive URLs for <a href="' . SSOMA_URL .
259                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
260         }
261
262         my $addr = $pi_config->get($listname, 'address');
263         if (ref($addr) eq 'ARRAY') {
264                 $addr = $addr->[0]; # first address is primary
265         }
266
267         $addr = "<a href=\"mailto:$addr\">$addr</a>";
268         $desc =  $desc;
269         join("\n",
270                 '- ' . $desc,
271                 'This is a <a href="' . PI_URL . '">public-inbox</a>, '.
272                 "anybody may post:",
273                 "\t$addr (text-only, no HTML please)",
274                 $urls
275         );
276 }
277
278 1;