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