1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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_first_doc_id('Q' . $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 if (my $mm = $cur->mm) {
89 my $res = $mm->mid_prefixes($tmp_mid, 100);
90 if ($res && scalar(@$res)) {
91 $n_partial += scalar(@$res);
92 push @partial, [ $cur, $res ];
93 # fixup common errors:
94 } elsif ($tmp_mid =~ s,/[tTf],,) {
95 $res = $mm->mid_prefixes($tmp_mid, 100);
96 if ($res && scalar(@$res)) {
97 $n_partial += scalar(@$res);
98 push @partial, [ $cur, $res ];
103 # can't find a partial match in current inbox, try the others:
104 if (!$n_partial && length($mid) >= 16) {
107 foreach my $ibx (@ibx) {
108 my $mm = $ibx->mm or next;
109 my $res = $mm->mid_prefixes($tmp_mid, 100);
110 if ($res && scalar(@$res)) {
111 $n_partial += scalar(@$res);
112 push @partial, [ $ibx, $res ];
113 last if $n_partial >= 100;
116 # fixup common errors:
117 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
123 my $h = PublicInbox::Hval->new_msgid($mid);
124 my $href = $h->{href};
125 my $html = $h->as_html;
126 my $title = "<$html> not found";
127 my $s = "<pre>Message-ID <$html>\nnot found\n";
130 my $es = $n_partial == 1 ? '' : 'es';
131 $s .= "\n$n_partial partial match$es found:\n\n";
132 my $cur_name = $cur->{name};
133 foreach my $pair (@partial) {
134 my ($ibx, $res) = @$pair;
135 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
136 my $u = $ibx->base_url($env) or next;
137 foreach my $m (@$res) {
138 my $p = PublicInbox::Hval->new_msgid($m);
141 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
145 my $ext = ext_urls($ctx, $mid, $href, $html);
150 $ctx->{-html_tip} = $s .= '</pre>';
151 $ctx->{-title_html} = $title;
152 $ctx->{-upfx} = '../';
153 PublicInbox::WwwStream->response($ctx, $code);
157 my ($ctx, $mid, $href, $html) = @_;
159 # Fall back to external repos if configured
160 if (@EXT_URL && index($mid, '@') >= 0) {
161 my $env = $ctx->{env};
162 my $e = "\nPerhaps try an external site:\n\n";
163 foreach my $url (@EXT_URL) {
164 my $u = PublicInbox::Hval::prurl($env, $url);
165 my $r = sprintf($u, $href);
166 my $t = sprintf($u, $html);
167 $e .= qq{<a\nhref="$r">$t</a>\n};
175 my ($ctx, $found, $mid) = @_;
176 my $h = PublicInbox::Hval->new_msgid($mid);
177 my $href = $h->{href};
178 my $html = $h->as_html;
179 my $title = "<$html> found in ";
180 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
181 $ctx->{-title_html} = $title . $end;
182 $ctx->{-upfx} = '../';
183 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
184 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
185 $ctx->{-html_tip} = join('',
186 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
188 my $u = $_->base_url;
189 qq(<a\nhref="$u$href/">$u$html/</a>\n)
191 $ext_urls, '</pre>');
192 PublicInbox::WwwStream->response($ctx, $code);