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 PublicInbox::Hval;
12 use PublicInbox::MID qw/mid2path/;
13 use PublicInbox::WwwStream;
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;
33 my (@nox, @ibx, @found);
35 $pi_config->each_inbox(sub {
37 return if $other->{name} eq $cur->{name} || !$other->base_url;
39 my $s = $other->search;
45 # try to find the URL with Xapian to avoid forking
46 my $doc_id = eval { $s->find_unique_doc_id('mid', $mid) };
48 # xapian not configured properly for this repo
54 if (defined $doc_id) {
57 # no point in trying the fork fallback if we
58 # know Xapian is up-to-date but missing the
59 # message in the current repo
64 return exact($ctx, \@found, $mid) if @found;
66 # Xapian not installed or configured for some repos,
67 # do a full MID check (this is expensive...):
69 my $path = mid2path($mid);
70 foreach my $other (@nox) {
71 my (undef, $type, undef) = $other->path_check($path);
73 if ($type && $type eq 'blob') {
78 return exact($ctx, \@found, $mid) if @found;
80 # fall back to partial MID matching
84 eval { require PublicInbox::Msgmap };
85 my $have_mm = $@ ? 0 : 1;
90 foreach my $ibx (@ibx) {
91 my $mm = $ibx->mm or next;
92 if (my $res = $mm->mid_prefixes($tmp_mid)) {
93 $n_partial += scalar(@$res);
94 push @partial, [ $ibx, $res ];
97 # fixup common errors:
98 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
104 my $h = PublicInbox::Hval->new_msgid($mid);
105 my $href = $h->as_href;
106 my $html = $h->as_html;
107 my $title = "<$html> not found";
108 my $s = "<pre>Message-ID <$html>\nnot found\n";
111 my $es = $n_partial == 1 ? '' : 'es';
112 $s .= "\n$n_partial partial match$es found:\n\n";
113 foreach my $pair (@partial) {
114 my ($ibx, $res) = @$pair;
115 my $u = $ibx->base_url or next;
116 foreach my $m (@$res) {
117 my $p = PublicInbox::Hval->new_msgid($m);
120 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
124 my $ext = ext_urls($ctx, $mid, $href, $html);
129 $ctx->{-html_tip} = $s .= '</pre>';
130 $ctx->{-title_html} = $title;
131 $ctx->{-upfx} = '../';
132 PublicInbox::WwwStream->response($ctx, $code);
136 my ($ctx, $mid, $href, $html) = @_;
138 # Fall back to external repos if configured
139 if (@EXT_URL && index($mid, '@') >= 0) {
140 my $env = $ctx->{env};
141 my $e = "\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 $e .= qq{<a\nhref="$r">$t</a>\n};
154 my ($ctx, $found, $mid) = @_;
155 my $h = PublicInbox::Hval->new_msgid($mid);
156 my $href = $h->as_href;
157 my $html = $h->as_html;
158 my $title = "<$html> found in ";
159 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
160 $ctx->{-title_html} = $title . $end;
161 $ctx->{-upfx} = '../';
162 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
163 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
164 $ctx->{-html_tip} = join('',
165 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
167 my $u = $_->base_url;
168 qq(<a\nhref="$u$href/">$u$html/</a>\n)
170 $ext_urls, '</pre>');
171 PublicInbox::WwwStream->response($ctx, $code);