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 /$LISTNAME 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 my $h = [ Location => $url, 'Content-Type' => 'text/plain' ];
34 if (defined $article && $article =~ /\A\d+\z/) {
35 my $mid = eval { ng_mid_for($ng, $info, $article) };
37 # article IDs are not stable across clones,
38 # do not encourage caching/bookmarking them
40 $url .= uri_escape_utf8($mid) . '/';
44 return [ $code, $h, [ "Redirecting to $url\n" ] ]
46 [ 404, [ 'Content-Type' => 'text/plain' ], [] ];
50 my ($ng, $info, $article) = @_;
51 # may fail due to lack of Danga::Socket
53 require PublicInbox::NewsGroup;
55 PublicInbox::NewsGroup->new($ng, $info->{git_dir}, '');
56 $ng->mm->mid_for($article);
62 $rv = $self->{ng_map} and return $rv;
63 my $pi_config = $self->{pi_config};
65 foreach my $k (keys %$pi_config) {
66 $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
68 my $git_dir = $pi_config->{"publicinbox.$listname.mainrepo"};
69 my $url = $pi_config->{"publicinbox.$listname.url"};
71 my $ng = $pi_config->{"publicinbox.$listname.newsgroup"};
72 next if (!defined $ng) || ($ng eq ''); # disabled
74 $url =~ m!/\z! or $url .= '/';
75 $ng_map{$ng} = { url => $url, git_dir => $git_dir };
77 $self->{ng_map} = \%ng_map;