]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NewsWWW.pm
cleanup: use '$ibx' consistently when referring to Inbox refs
[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, $mid) = @_;
29         # do not pass $env since HTTP_HOST may differ
30         my $url = $ibx->base_url or return;
31
32         eval { $ibx->mm->num_for($mid) } or return;
33
34         # 302 since the same message may show up on
35         # multiple inboxes and inboxes can be added/reordered
36         redirect(302, $url .= mid_escape($mid) . '/');
37 }
38
39 sub call {
40         my ($self, $env) = @_;
41
42         # some links may have the article number in them:
43         # /inbox.foo.bar/123456
44         my (undef, @parts) = split(m!/!, $env->{PATH_INFO});
45         my ($ng, $article) = @parts;
46         my $pi_config = $self->{pi_config};
47         if (my $ibx = $pi_config->lookup_newsgroup($ng)) {
48                 my $url = PublicInbox::Hval::prurl($env, $ibx->{url});
49                 my $code = 301;
50                 if (defined $article && $article =~ /\A\d+\z/) {
51                         my $mid = eval { $ibx->mm->mid_for($article) };
52                         if (defined $mid) {
53                                 # article IDs are not stable across clones,
54                                 # do not encourage caching/bookmarking them
55                                 $code = 302;
56                                 $url .= mid_escape($mid) . '/';
57                         }
58                 }
59                 return redirect($code, $url);
60         }
61
62         my $res;
63         my @try = (join('/', @parts));
64
65         # trailing slash is in the rest of our WWW, so maybe some users
66         # will assume it:
67         if ($parts[-1] eq '') {
68                 pop @parts;
69                 push @try, join('/', @parts);
70         }
71
72         foreach my $mid (@try) {
73                 $pi_config->each_inbox(sub {
74                         $res ||= try_inbox($_[0], $mid);
75                 });
76                 last if defined $res;
77         }
78         $res || [ 404, [qw(Content-Type text/plain)], ["404 Not Found\n"] ];
79 }
80
81 1;