]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NewsWWW.pm
config: fix NewsWWW fallback for newsgroups in HTTP URLs
[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 URI::Escape qw(uri_escape_utf8);
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 $ng_map = $self->newsgroup_map;
23         my $path = $env->{PATH_INFO};
24         $path =~ s!\A/+!!;
25         $path =~ s!/+\z!!;
26
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});
32                 my $code = 301;
33                 if (defined $article && $article =~ /\A\d+\z/) {
34                         my $mid = eval { ng_mid_for($ng, $info, $article) };
35                         if (defined $mid) {
36                                 # article IDs are not stable across clones,
37                                 # do not encourage caching/bookmarking them
38                                 $code = 302;
39                                 $url .= uri_escape_utf8($mid) . '/';
40                         }
41                 }
42
43                 my $h = [ Location => $url, 'Content-Type' => 'text/plain' ];
44
45                 return [ $code, $h, [ "Redirecting to $url\n" ] ]
46         }
47         [ 404, [ 'Content-Type' => 'text/plain' ], [] ];
48 }
49
50 sub ng_mid_for {
51         my ($ng, $info, $article) = @_;
52         # may fail due to lack of Danga::Socket
53         # for defer_weaken:
54         require PublicInbox::NewsGroup;
55         $ng = $info->{ng} ||=
56                 PublicInbox::NewsGroup->new($ng, $info->{git_dir}, '');
57         $ng->mm->mid_for($article);
58 }
59
60 sub newsgroup_map {
61         my ($self) = @_;
62         my $rv;
63         $rv = $self->{ng_map} and return $rv;
64         my $pi_config = $self->{pi_config};
65         my %ng_map;
66         foreach my $k (keys %$pi_config) {
67                 $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
68                 my $inbox = $1;
69                 my $git_dir = $pi_config->{"publicinbox.$inbox.mainrepo"};
70                 my $url = $pi_config->{"publicinbox.$inbox.url"};
71                 defined $url or next;
72                 my $ng = $pi_config->{"publicinbox.$inbox.newsgroup"};
73                 next if (!defined $ng) || ($ng eq ''); # disabled
74
75                 $url =~ m!/\z! or $url .= '/';
76                 $ng_map{$ng} = { url => $url, git_dir => $git_dir };
77         }
78         $self->{ng_map} = \%ng_map;
79 }
80
81 1;