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 # leading "//" denotes protocol-relative (http:// or https://)
19 '//www.mail-archive.com/search?l=mid&q=%s',
20 'http://mid.gmane.org/%s',
21 'https://lists.debian.org/msgid-search/%s',
22 '//docs.FreeBSD.org/cgi/mid.cgi?db=mid&id=%s',
23 'https://www.w3.org/mid/%s',
24 'http://www.postgresql.org/message-id/%s',
25 'https://lists.debconf.org/cgi-lurker/keyword.cgi?'.
26 'doc-url=/lurker&format=en.html&query=id:%s'
31 my $cur = $ctx->{-inbox};
32 my $mid = $ctx->{mid};
34 eval { require PublicInbox::Search };
35 my $have_xap = $@ ? 0 : 1;
36 my (@nox, @ibx, @found);
38 $ctx->{www}->{pi_config}->each_inbox(sub {
40 return if $other->{name} eq $cur->{name} || !$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 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
67 return exact($ctx, \@found, $mid) if @found;
69 # Xapian not installed or configured for some repos,
70 # do a full MID check (this is expensive...):
72 my $path = mid2path($mid);
73 foreach my $other (@nox) {
74 my (undef, $type, undef) = $other->path_check($path);
76 if ($type && $type eq 'blob') {
81 return exact($ctx, \@found, $mid) if @found;
83 # fall back to partial MID matching
87 eval { require PublicInbox::Msgmap };
88 my $have_mm = $@ ? 0 : 1;
93 foreach my $ibx (@ibx) {
94 my $mm = $ibx->mm or next;
95 if (my $res = $mm->mid_prefixes($tmp_mid)) {
96 $n_partial += scalar(@$res);
97 push @partial, [ $ibx, $res ];
100 # fixup common errors:
101 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
107 my $h = PublicInbox::Hval->new_msgid($mid);
108 my $href = $h->{href};
109 my $html = $h->as_html;
110 my $title = "<$html> not found";
111 my $s = "<pre>Message-ID <$html>\nnot found\n";
114 my $es = $n_partial == 1 ? '' : 'es';
115 $s .= "\n$n_partial partial match$es found:\n\n";
116 my $cur_name = $cur->{name};
117 foreach my $pair (@partial) {
118 my ($ibx, $res) = @$pair;
119 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
120 my $u = $ibx->base_url($env) or next;
121 foreach my $m (@$res) {
122 my $p = PublicInbox::Hval->new_msgid($m);
125 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
129 my $ext = ext_urls($ctx, $mid, $href, $html);
134 $ctx->{-html_tip} = $s .= '</pre>';
135 $ctx->{-title_html} = $title;
136 $ctx->{-upfx} = '../';
137 PublicInbox::WwwStream->response($ctx, $code);
141 my ($ctx, $mid, $href, $html) = @_;
143 # Fall back to external repos if configured
144 if (@EXT_URL && index($mid, '@') >= 0) {
145 my $env = $ctx->{env};
146 my $e = "\nPerhaps try an external site:\n\n";
147 foreach my $url (@EXT_URL) {
148 my $u = PublicInbox::Hval::prurl($env, $url);
149 my $r = sprintf($u, $href);
150 my $t = sprintf($u, $html);
151 $e .= qq{<a\nhref="$r">$t</a>\n};
159 my ($ctx, $found, $mid) = @_;
160 my $h = PublicInbox::Hval->new_msgid($mid);
161 my $href = $h->{href};
162 my $html = $h->as_html;
163 my $title = "<$html> found in ";
164 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
165 $ctx->{-title_html} = $title . $end;
166 $ctx->{-upfx} = '../';
167 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
168 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
169 $ctx->{-html_tip} = join('',
170 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
172 my $u = $_->base_url;
173 qq(<a\nhref="$u$href/">$u$html/</a>\n)
175 $ext_urls, '</pre>');
176 PublicInbox::WwwStream->response($ctx, $code);