1 # Copyright (C) 2016 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 URI::Escape qw(uri_escape_utf8);
15 my ($class, $pi_config) = @_;
16 $pi_config ||= PublicInbox::Config->new;
17 bless { pi_config => $pi_config }, $class;
21 my ($self, $env) = @_;
22 my $path = $env->{PATH_INFO};
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});
32 if (defined $article && $article =~ /\A\d+\z/) {
33 my $mid = eval { $inbox->mm->mid_for($article) };
35 # article IDs are not stable across clones,
36 # do not encourage caching/bookmarking them
38 $url .= uri_escape_utf8($mid) . '/';
42 my $h = [ Location => $url, 'Content-Type' => 'text/plain' ];
44 return [ $code, $h, [ "Redirecting to $url\n" ] ]
46 [ 404, [ 'Content-Type' => 'text/plain' ], [] ];