]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
view: support SHA-1 of Message-IDs for message links
[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         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
179           [ PublicInbox::View->msg_html(Email::MIME->new($x), $pfx, $foot) ] ];
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         require Email::MIME;
189         my $foot = footer($ctx);
190         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
191           [ PublicInbox::View->msg_html(Email::MIME->new($x), undef, $foot)] ];
192 }
193
194 sub self_url {
195         my ($cgi) = @_;
196         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
197 }
198
199 sub redirect_list_index {
200         my ($ctx, $cgi) = @_;
201         do_redirect(self_url($cgi) . "/");
202 }
203
204 sub redirect_mid {
205         my ($ctx, $cgi) = @_;
206         my $url = self_url($cgi);
207         $url =~ s!/f/!/m/!;
208         do_redirect($url . '.html');
209 }
210
211 sub do_redirect {
212         my ($url) = @_;
213         [ 301,
214           [ Location => $url, 'Content-Type' => 'text/plain' ],
215           [ "Redirecting to $url\n" ]
216         ]
217 }
218
219 sub ctx_get {
220         my ($ctx, $key) = @_;
221         my $val = $ctx->{$key};
222         (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
223         $val;
224 }
225
226 sub try_cat {
227         my ($path) = @_;
228         my $rv;
229         if (open(my $fh, '<', $path)) {
230                 local $/;
231                 $rv = <$fh>;
232                 close $fh;
233         }
234         $rv;
235 }
236
237 sub footer {
238         my ($ctx) = @_;
239         return '' unless $ctx;
240         my $git_dir = ctx_get($ctx, 'git_dir');
241
242         # favor user-supplied footer
243         my $footer = try_cat("$git_dir/public-inbox/footer.html");
244         if (defined $footer) {
245                 chomp $footer;
246                 return $footer;
247         }
248
249         # auto-generate a footer
250         my $listname = ctx_get($ctx, 'listname');
251         my $desc = try_cat("$git_dir/description");
252         $desc = '$GIT_DIR/description missing' unless defined $desc;
253         chomp $desc;
254
255         my $urls = try_cat("$git_dir/cloneurl");
256         my @urls = split(/\r?\n/, $urls || '');
257         my $nurls = scalar @urls;
258         if ($nurls == 0) {
259                 $urls = '($GIT_DIR/cloneurl missing)';
260         } elsif ($nurls == 1) {
261                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
262                         '">ssoma</a>: ' . $urls[0];
263         } else {
264                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
265                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
266         }
267
268         my $addr = $pi_config->get($listname, 'address');
269         if (ref($addr) eq 'ARRAY') {
270                 $addr = $addr->[0]; # first address is primary
271         }
272
273         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
274         $desc =  $desc;
275         join("\n",
276                 '- ' . $desc,
277                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
278                         'anybody may post in plain-text (not HTML):',
279                 $addr,
280                 $urls
281         );
282 }
283
284 1;