]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NewsWWW.pm
ee11a0890b1f598dda2d89b27202658da0a9e7e6
[public-inbox.git] / lib / PublicInbox / NewsWWW.pm
1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Plack app redirector for mapping /$NEWSGROUP requests to
5 # the appropriate /$INBOX in PublicInbox::WWW because some
6 # auto-linkifiers cannot handle nntp:// redirects properly.
7 # This is also used directly by PublicInbox::WWW
8 package PublicInbox::NewsWWW;
9 use strict;
10 use warnings;
11 use PublicInbox::Config;
12 use PublicInbox::MID qw(mid_escape);
13
14 sub new {
15         my ($class, $pi_config) = @_;
16         $pi_config ||= PublicInbox::Config->new;
17         bless { pi_config => $pi_config }, $class;
18 }
19
20 sub redirect ($$) {
21         my ($code, $url) = @_;
22         [ $code,
23           [ Location => $url, 'Content-Type' => 'text/plain' ],
24           [ "Redirecting to $url\n" ] ]
25 }
26
27 sub try_inbox {
28         my ($ibx, $arg) = @_;
29         return if scalar(@$arg) > 1;
30
31         # do not pass $env since HTTP_HOST may differ
32         my $url = $ibx->base_url or return;
33
34         my ($mid) = @$arg;
35         eval { $ibx->mm->num_for($mid) } or return;
36
37         # 302 since the same message may show up on
38         # multiple inboxes and inboxes can be added/reordered
39         $arg->[1] = redirect(302, $url .= mid_escape($mid) . '/');
40 }
41
42 sub call {
43         my ($self, $env) = @_;
44
45         # some links may have the article number in them:
46         # /inbox.foo.bar/123456
47         my (undef, @parts) = split(m!/!, $env->{PATH_INFO});
48         my ($ng, $article) = @parts;
49         my $pi_config = $self->{pi_config};
50         if (my $ibx = $pi_config->lookup_newsgroup($ng)) {
51                 my $url = PublicInbox::Hval::prurl($env, $ibx->{url});
52                 my $code = 301;
53                 if (defined $article && $article =~ /\A[0-9]+\z/) {
54                         my $mid = eval { $ibx->mm->mid_for($article) };
55                         if (defined $mid) {
56                                 # article IDs are not stable across clones,
57                                 # do not encourage caching/bookmarking them
58                                 $code = 302;
59                                 $url .= mid_escape($mid) . '/';
60                         }
61                 }
62                 return redirect($code, $url);
63         }
64
65         my $res;
66         my @try = (join('/', @parts));
67
68         # trailing slash is in the rest of our WWW, so maybe some users
69         # will assume it:
70         if ($parts[-1] eq '') {
71                 pop @parts;
72                 push @try, join('/', @parts);
73         }
74
75         foreach my $mid (@try) {
76                 my $arg = [ $mid ];
77                 $pi_config->each_inbox(\&try_inbox, $arg);
78                 defined($res = $arg->[1]) and last;
79         }
80         $res || [ 404, [qw(Content-Type text/plain)], ["404 Not Found\n"] ];
81 }
82
83 1;