]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NewsWWW.pm
b4d747633fc378146c9439c6c5b8af1a1bcb83c9
[public-inbox.git] / lib / PublicInbox / NewsWWW.pm
1 # Copyright (C) 2016 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 call {
21         my ($self, $env) = @_;
22         my $path = $env->{PATH_INFO};
23         $path =~ s!\A/+!!;
24         $path =~ s!/+\z!!;
25
26         # some links may have the article number in them:
27         # /inbox.foo.bar/123456
28         my ($ng, $article) = split(m!/+!, $path, 2);
29         if (my $inbox = $self->{pi_config}->lookup_newsgroup($ng)) {
30                 my $url = PublicInbox::Hval::prurl($env, $inbox->{url});
31                 my $code = 301;
32                 if (defined $article && $article =~ /\A\d+\z/) {
33                         my $mid = eval { $inbox->mm->mid_for($article) };
34                         if (defined $mid) {
35                                 # article IDs are not stable across clones,
36                                 # do not encourage caching/bookmarking them
37                                 $code = 302;
38                                 $url .= mid_escape($mid) . '/';
39                         }
40                 }
41
42                 my $h = [ Location => $url, 'Content-Type' => 'text/plain' ];
43
44                 return [ $code, $h, [ "Redirecting to $url\n" ] ]
45         }
46         [ 404, [ 'Content-Type' => 'text/plain' ], [ "404 Not Found\n" ] ];
47 }
48
49 1;