about summary refs log tree commit homepage
path: root/lib/PublicInbox/NewsWWW.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-02-26 01:57:57 +0000
committerEric Wong <e@80x24.org>2016-02-26 02:45:24 +0000
commit3111c278a1ca996a69398896945cd29a3277cdb7 (patch)
tree07f4f7fed0eb2cc65335d3faca5684d8e36b6d97 /lib/PublicInbox/NewsWWW.pm
parentd42cdde813719ed96c2ece567543a66f4884e946 (diff)
downloadpublic-inbox-3111c278a1ca996a69398896945cd29a3277cdb7.tar.gz
Some linkifiers to create invalid HTTP links when it sees a
link intended for NNTP services.  This means we may see links
to news.public-inbox.org/inbox.comp.mail.public-inbox.meta
point to "http://" on port 80 instead of 119.  Try to
redirect users to http://public-inbox.org/meta/ in this case.
Diffstat (limited to 'lib/PublicInbox/NewsWWW.pm')
-rw-r--r--lib/PublicInbox/NewsWWW.pm80
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/PublicInbox/NewsWWW.pm b/lib/PublicInbox/NewsWWW.pm
new file mode 100644
index 00000000..e19765c9
--- /dev/null
+++ b/lib/PublicInbox/NewsWWW.pm
@@ -0,0 +1,80 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+#
+# Plack app redirector for mapping /$NEWSGROUP requests to
+# the appropriate /$LISTNAME in PublicInbox::WWW because some
+# auto-linkifiers cannot handle nntp:// redirects properly.
+# This is also used directly by PublicInbox::WWW
+package PublicInbox::NewsWWW;
+use strict;
+use warnings;
+use PublicInbox::Config;
+use URI::Escape qw(uri_escape_utf8);
+
+sub new {
+        my ($class, $pi_config) = @_;
+        $pi_config ||= PublicInbox::Config->new;
+        bless { pi_config => $pi_config }, $class;
+}
+
+sub call {
+        my ($self, $env) = @_;
+        my $ng_map = $self->newsgroup_map;
+        my $path = $env->{PATH_INFO};
+        $path =~ s!\A/+!!;
+        $path =~ s!/+\z!!;
+
+        # some links may have the article number in them:
+        # /inbox.foo.bar/123456
+        my ($ng, $article) = split(m!/+!, $path, 2);
+        if (my $info = $ng_map->{$ng}) {
+                my $url = PublicInbox::Hval::prurl($env, $info->{url});
+                my $code = 301;
+                my $h = [ Location => $url, 'Content-Type' => 'text/plain' ];
+                if (defined $article && $article =~ /\A\d+\z/) {
+                        my $mid = eval { ng_mid_for($ng, $info, $article) };
+                        if (defined $mid) {
+                                # article IDs are not stable across clones,
+                                # do not encourage caching/bookmarking them
+                                $code = 302;
+                                $url .= uri_escape_utf8($mid) . '/';
+                        }
+                }
+
+                return [ $code, $h, [ "Redirecting to $url\n" ] ]
+        }
+        [ 404, [ 'Content-Length' => 'text/plain' ], [] ];
+}
+
+sub ng_mid_for {
+        my ($ng, $info, $article) = @_;
+        # may fail due to lack of Danga::Socket
+        # for defer_weaken:
+        require PublicInbox::NewsGroup;
+        $ng = $info->{ng} ||=
+                PublicInbox::NewsGroup->new($ng, $info->{git_dir}, '');
+        $ng->mm->mid_for($article);
+}
+
+sub newsgroup_map {
+        my ($self) = @_;
+        my $rv;
+        $rv = $self->{ng_map} and return $rv;
+        my $pi_config = $self->{pi_config};
+        my %ng_map;
+        foreach my $k (keys %$pi_config) {
+                $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
+                my $listname = $1;
+                my $git_dir = $pi_config->{"publicinbox.$listname.mainrepo"};
+                my $url = $pi_config->{"publicinbox.$listname.url"};
+                defined $url or next;
+                my $ng = $pi_config->{"publicinbox.$listname.newsgroup"};
+                next if (!defined $ng) || ($ng eq ''); # disabled
+
+                $url =~ m!/\z! or $url .= '/';
+                $ng_map{$ng} = { url => $url, git_dir => $git_dir };
+        }
+        $self->{ng_map} = \%ng_map;
+}
+
+1;