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 $cur = $ctx->{-inbox};
28 my $mid = $ctx->{mid};
29 my $env = $ctx->{env};
31 eval { require PublicInbox::Search };
32 my $have_xap = $@ ? 0 : 1;
35 foreach my $k (keys %$pi_config) {
36 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
38 next if $name eq $cur->{name};
39 my $other = $pi_config->lookup_name($name) or next;
40 next unless $other->base_url;
42 my $s = $other->search;
48 # try to find the URL with Xapian to avoid forking
49 my $doc_id = eval { $s->find_unique_doc_id('mid', $mid) };
51 # xapian not configured properly for this repo
57 return r302($other, $mid) if defined $doc_id;
59 # no point in trying the fork fallback if we
60 # know Xapian is up-to-date but missing the
61 # message in the current repo
65 # Xapian not installed or configured for some repos,
66 # do a full MID check:
68 my $path = mid2path($mid);
69 foreach my $other (@nox) {
70 my (undef, $type, undef) = $other->path_check($path);
72 return r302($other, $mid) if $type && $type eq 'blob';
76 # fall back to partial MID matching
80 eval { require PublicInbox::Msgmap };
81 my $have_mm = $@ ? 0 : 1;
86 foreach my $ibx (@ibx) {
87 my $mm = $ibx->mm or next;
88 if (my $res = $mm->mid_prefixes($tmp_mid)) {
89 $n_partial += scalar(@$res);
90 push @partial, [ $ibx, $res ];
93 # fixup common errors:
94 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
100 my $h = PublicInbox::Hval->new_msgid($mid, 1);
101 my $href = $h->as_href;
102 my $html = $h->as_html;
103 my $title = "Message-ID <$html> not found";
104 my $s = "<html><head><title>$title</title>" .
105 "</head><body><pre><b>$title</b>\n";
109 my $es = $n_partial == 1 ? '' : 'es';
110 $s.= "\n$n_partial partial match$es found:\n\n";
111 foreach my $pair (@partial) {
112 my ($ibx, $res) = @$pair;
113 my $u = $ibx->base_url or next;
114 foreach my $m (@$res) {
115 my $p = PublicInbox::Hval->new_msgid($m);
118 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
123 # Fall back to external repos if configured
124 if (@EXT_URL && index($mid, '@') >= 0) {
126 $s .= "\nPerhaps try an external site:\n\n";
127 foreach my $url (@EXT_URL) {
128 my $u = PublicInbox::Hval::prurl($env, $url);
129 my $r = sprintf($u, $href);
130 my $t = sprintf($u, $html);
131 $s .= qq{<a\nhref="$r">$t</a>\n};
134 $s .= '</pre></body></html>';
136 [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
139 # Redirect to another public-inbox which is mapped by $pi_config
140 # TODO: prompt for inbox-switching
142 my ($inbox, $mid) = @_;
143 my $url = $inbox->base_url . uri_escape_utf8($mid) . '/';
145 [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
146 [ "Redirecting to\n$url\n" ] ]