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>
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;
11 use PublicInbox::Config;
12 use PublicInbox::MID qw(mid_escape);
15 my ($class, $pi_config) = @_;
16 $pi_config ||= PublicInbox::Config->new;
17 bless { pi_config => $pi_config }, $class;
21 my ($code, $url) = @_;
23 [ Location => $url, 'Content-Type' => 'text/plain' ],
24 [ "Redirecting to $url\n" ] ]
29 # do not pass $env since HTTP_HOST may differ
30 my $url = $ibx->base_url or return;
32 eval { $ibx->mm->num_for($mid) } or return;
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) . '/');
40 my ($self, $env) = @_;
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});
50 if (defined $article && $article =~ /\A[0-9]+\z/) {
51 my $mid = eval { $ibx->mm->mid_for($article) };
53 # article IDs are not stable across clones,
54 # do not encourage caching/bookmarking them
56 $url .= mid_escape($mid) . '/';
59 return redirect($code, $url);
63 my @try = (join('/', @parts));
65 # trailing slash is in the rest of our WWW, so maybe some users
67 if ($parts[-1] eq '') {
69 push @try, join('/', @parts);
72 foreach my $mid (@try) {
73 $pi_config->each_inbox(sub {
74 $res ||= try_inbox($_[0], $mid);
78 $res || [ 404, [qw(Content-Type text/plain)], ["404 Not Found\n"] ];