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 $ng_map = $self->newsgroup_map;
23 my $path = $env->{PATH_INFO};
27 # some links may have the article number in them:
28 # /inbox.foo.bar/123456
29 my ($ng, $article) = split(m!/+!, $path, 2);
30 if (my $info = $ng_map->{$ng}) {
31 my $url = PublicInbox::Hval::prurl($env, $info->{url});
33 if (defined $article && $article =~ /\A\d+\z/) {
34 my $mid = eval { ng_mid_for($ng, $info, $article) };
36 # article IDs are not stable across clones,
37 # do not encourage caching/bookmarking them
39 $url .= uri_escape_utf8($mid) . '/';
43 my $h = [ Location => $url, 'Content-Type' => 'text/plain' ];
45 return [ $code, $h, [ "Redirecting to $url\n" ] ]
47 [ 404, [ 'Content-Type' => 'text/plain' ], [] ];
51 my ($ng, $info, $article) = @_;
52 # may fail due to lack of Danga::Socket
54 require PublicInbox::NewsGroup;
56 PublicInbox::NewsGroup->new($ng, $info->{git_dir}, '');
57 $ng->mm->mid_for($article);
63 $rv = $self->{ng_map} and return $rv;
64 my $pi_config = $self->{pi_config};
66 foreach my $k (keys %$pi_config) {
67 $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
69 my $git_dir = $pi_config->{"publicinbox.$inbox.mainrepo"};
70 my $url = $pi_config->{"publicinbox.$inbox.url"};
72 my $ng = $pi_config->{"publicinbox.$inbox.newsgroup"};
73 next if (!defined $ng) || ($ng eq ''); # disabled
75 $url =~ m!/\z! or $url .= '/';
76 $ng_map{$ng} = { url => $url, git_dir => $git_dir };
78 $self->{ng_map} = \%ng_map;