1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # Used by the web interface to link to messages outside of the our
5 # public-inboxes. Mail threads may cross projects/threads; so
6 # we should ensure users can find more easily find them on other
8 package PublicInbox::ExtMsg;
11 use URI::Escape qw(uri_escape_utf8);
12 use PublicInbox::Hval;
13 use PublicInbox::MID qw/mid2path/;
15 # TODO: user-configurable
17 'http://mid.gmane.org/%s',
18 'https://lists.debian.org/msgid-search/%s',
19 # leading "//" denotes protocol-relative (http:// or https://)
20 '//mid.mail-archive.com/%s',
26 my $pi_config = $ctx->{pi_config};
27 my $listname = $ctx->{listname};
28 my $mid = $ctx->{mid};
30 eval { require PublicInbox::Search };
31 my $have_xap = $@ ? 0 : 1;
34 foreach my $k (keys %$pi_config) {
35 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
37 next if $list eq $listname;
39 my $git_dir = $pi_config->{"publicinbox.$list.mainrepo"};
40 defined $git_dir or next;
42 my $url = $pi_config->{"publicinbox.$list.url"};
47 # try to find the URL with Xapian to avoid forking
51 $s = PublicInbox::Search->new($git_dir);
52 $s->find_unique_doc_id('mid', $mid);
55 # xapian not configured for this repo
58 return r302($url, $mid) if (defined $doc_id);
60 # no point in trying the fork fallback if we
61 # know Xapian is up-to-date but missing the
62 # message in the current repo
63 push @pfx, { git_dir => $git_dir, url => $url };
68 # queue up for forking after we've tried Xapian on all of them
69 push @nox, { git_dir => $git_dir, url => $url };
72 # Xapian not installed or configured for some repos
73 my $path = "HEAD:" . mid2path($mid);
75 foreach my $n (@nox) {
76 # TODO: reuse existing PublicInbox::Git objects to save forks
77 my $git = PublicInbox::Git->new($n->{git_dir});
78 my (undef, $type, undef) = $git->check($path);
79 return r302($n->{url}, $mid) if ($type && $type eq 'blob');
82 # fall back to partial MID matching
86 eval { require PublicInbox::Msgmap };
87 my $have_mm = $@ ? 0 : 1;
88 my $cgi = $ctx->{cgi};
91 if (ref($cgi) eq 'CGI') {
92 $base_url = $cgi->url(-base) . '/';
93 $scheme = $cgi->protocol;
94 } else { # Plack::Request
95 $base_url = $cgi->base->as_string;
96 $scheme = $cgi->env->{'psgi.url_scheme'};
102 $url = $base_url . $listname;
103 unshift @pfx, { git_dir => $ctx->{git_dir}, url => $url };
104 foreach my $pfx (@pfx) {
105 my $git_dir = delete $pfx->{git_dir} or next;
106 my $mm = eval { PublicInbox::Msgmap->new($git_dir) };
109 if (my $res = $mm->mid_prefixes($tmp_mid)) {
110 $n_partial += scalar(@$res);
115 # fixup common errors:
116 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
122 my $h = PublicInbox::Hval->new_msgid($mid, 1);
123 my $href = $h->as_href;
124 my $html = $h->as_html;
125 my $title = "Message-ID <$html> not found";
126 my $s = "<html><head><title>$title</title>" .
127 "</head><body><pre><b>$title</b>\n";
131 my $es = $n_partial == 1 ? '' : 'es';
132 $s.= "\n$n_partial partial match$es found:\n\n";
133 foreach my $pfx (@partial) {
135 foreach my $m (@{$pfx->{res}}) {
136 my $p = PublicInbox::Hval->new_msgid($m);
139 $s .= qq{<a\nhref="$u/$r/">$u/$t/</a>\n};
144 # Fall back to external repos if configured
145 if (@EXT_URL && index($mid, '@') >= 0) {
147 $s .= "\nPerhaps try an external site:\n\n";
148 foreach my $u (@EXT_URL) {
149 $u = "$scheme:$u" if $u =~ m!\A//!;
150 my $r = sprintf($u, $href);
151 my $t = sprintf($u, $html);
152 $s .= qq{<a\nhref="$r">$t</a>\n};
155 $s .= '</pre></body></html>';
157 [300, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
160 # Redirect to another public-inbox which is mapped by $pi_config
162 my ($url, $mid) = @_;
163 $url .= '/' . uri_escape_utf8($mid) . '/';
165 [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
166 [ "Redirecting to\n$url\n" ] ]