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 $inbox = $ctx->{inbox};
28 my $mid = $ctx->{mid};
29 my $cgi = $ctx->{cgi};
30 my $env = $cgi->{env};
32 eval { require PublicInbox::Search };
33 my $have_xap = $@ ? 0 : 1;
36 foreach my $k (keys %$pi_config) {
37 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
39 next if $name eq $inbox;
41 my $git_dir = $pi_config->{"publicinbox.$name.mainrepo"};
42 defined $git_dir or next;
44 my $url = $pi_config->{"publicinbox.$name.url"};
48 $url = PublicInbox::Hval::prurl($env, $url);
50 # try to find the URL with Xapian to avoid forking
54 $s = PublicInbox::Search->new($git_dir);
55 $s->find_unique_doc_id('mid', $mid);
58 # xapian not configured for this repo
61 return r302($url, $mid) if (defined $doc_id);
63 # no point in trying the fork fallback if we
64 # know Xapian is up-to-date but missing the
65 # message in the current repo
66 push @pfx, { git_dir => $git_dir, url => $url };
71 # queue up for forking after we've tried Xapian on all of them
72 push @nox, { git_dir => $git_dir, url => $url };
75 # Xapian not installed or configured for some repos
76 my $path = "HEAD:" . mid2path($mid);
78 foreach my $n (@nox) {
79 # TODO: reuse existing PublicInbox::Git objects to save forks
80 my $git = PublicInbox::Git->new($n->{git_dir});
81 my (undef, $type, undef) = $git->check($path);
82 return r302($n->{url}, $mid) if ($type && $type eq 'blob');
85 # fall back to partial MID matching
89 eval { require PublicInbox::Msgmap };
90 my $have_mm = $@ ? 0 : 1;
91 my $base_url = $cgi->base->as_string;
96 $url = $base_url . $inbox;
97 unshift @pfx, { git_dir => $ctx->{git_dir}, url => $url };
98 foreach my $pfx (@pfx) {
99 my $git_dir = delete $pfx->{git_dir} or next;
100 my $mm = eval { PublicInbox::Msgmap->new($git_dir) };
103 if (my $res = $mm->mid_prefixes($tmp_mid)) {
104 $n_partial += scalar(@$res);
109 # fixup common errors:
110 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
116 my $h = PublicInbox::Hval->new_msgid($mid, 1);
117 my $href = $h->as_href;
118 my $html = $h->as_html;
119 my $title = "Message-ID <$html> not found";
120 my $s = "<html><head><title>$title</title>" .
121 "</head><body><pre><b>$title</b>\n";
125 my $es = $n_partial == 1 ? '' : 'es';
126 $s.= "\n$n_partial partial match$es found:\n\n";
127 foreach my $pfx (@partial) {
129 foreach my $m (@{$pfx->{res}}) {
130 my $p = PublicInbox::Hval->new_msgid($m);
133 $s .= qq{<a\nhref="$u/$r/">$u/$t/</a>\n};
138 # Fall back to external repos if configured
139 if (@EXT_URL && index($mid, '@') >= 0) {
141 $s .= "\nPerhaps try an external site:\n\n";
142 foreach my $url (@EXT_URL) {
143 my $u = PublicInbox::Hval::prurl($env, $url);
144 my $r = sprintf($u, $href);
145 my $t = sprintf($u, $html);
146 $s .= qq{<a\nhref="$r">$t</a>\n};
149 $s .= '</pre></body></html>';
151 [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
154 # Redirect to another public-inbox which is mapped by $pi_config
156 my ($url, $mid) = @_;
157 $url .= '/' . uri_escape_utf8($mid) . '/';
159 [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
160 [ "Redirecting to\n$url\n" ] ]