1 # Copyright (C) 2015-2019 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 qw/ascii_html/;
12 use PublicInbox::MID qw/mid2path/;
13 use PublicInbox::WwwStream;
14 our $MIN_PARTIAL_LEN = 16;
16 # TODO: user-configurable
17 our @EXT_URL = map { ascii_html($_) } (
18 # leading "//" denotes protocol-relative (http:// or https://)
20 '//www.mail-archive.com/search?l=mid&q=%s',
21 'nntp://news.gmane.org/%s',
22 'https://lists.debian.org/msgid-search/%s',
23 '//docs.FreeBSD.org/cgi/mid.cgi?db=mid&id=%s',
24 'https://www.w3.org/mid/%s',
25 'http://www.postgresql.org/message-id/%s',
26 'https://lists.debconf.org/cgi-lurker/keyword.cgi?'.
27 'doc-url=/lurker&format=en.html&query=id:%s'
30 sub PARTIAL_MAX () { 100 }
32 sub search_partial ($$) {
33 my ($srch, $mid) = @_;
34 return if length($mid) < $MIN_PARTIAL_LEN;
35 my $opt = { limit => PARTIAL_MAX, mset => 2 };
36 my @try = ("m:$mid*");
38 if ($chop =~ s/(\W+)(\w*)\z//) {
39 my ($delim, $word) = ($1, $2);
41 push @try, "m:$chop$delim";
42 push @try, "m:$chop$delim*";
45 push @try, "m:$chop*";
48 # break out long words individually to search for, because
49 # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
50 if ($mid =~ /\w{9,}/) {
51 my @long = ($mid =~ m!(\w{3,})!g);
52 push(@try, join(' ', map { "m:$_" } @long));
54 # is the last element long enough to not trigger excessive
56 if (length($long[-1]) > 8) {
58 push(@try, join(' ', map { "m:$_" } @long));
62 foreach my $m (@try) {
63 # If Xapian can't handle the wildcard since it
64 # has too many results. $@ can be
65 # Search::Xapian::QueryParserError or even:
66 # "something terrible happened at ../Search/Xapian/Enquire.pm"
67 my $mset = eval { $srch->query($m, $opt) } or next;
70 my $doc = $_->get_document;
71 PublicInbox::SearchMsg->load_doc($doc)->mid;
73 return \@mids if scalar(@mids);
79 my $cur = $ctx->{-inbox};
80 my $mid = $ctx->{mid};
82 eval { require PublicInbox::Msgmap };
85 $ctx->{www}->{pi_config}->each_inbox(sub {
87 return if $other->{name} eq $cur->{name} || !$other->base_url;
89 my $mm = $other->mm or return;
91 # try to find the URL with Msgmap to avoid forking
92 my $num = $mm->num_for($mid);
96 # no point in trying the fork fallback if we
97 # know Xapian is up-to-date but missing the
98 # message in the current repo
103 return exact($ctx, \@found, $mid) if @found;
105 # fall back to partial MID matching
108 my $srch = $cur->search;
109 my $mids = search_partial($srch, $mid) if $srch;
111 $n_partial = scalar(@$mids);
112 push @partial, [ $cur, $mids ];
115 # can't find a partial match in current inbox, try the others:
116 if (!$n_partial && length($mid) >= $MIN_PARTIAL_LEN) {
117 foreach my $ibx (@ibx) {
118 $srch = $ibx->search or next;
119 $mids = search_partial($srch, $mid) or next;
120 $n_partial += scalar(@$mids);
121 push @partial, [ $ibx, $mids];
122 last if $n_partial >= PARTIAL_MAX;
127 my $h = PublicInbox::Hval->new_msgid($mid);
128 my $href = $h->{href};
129 my $html = $h->as_html;
130 my $title = "<$html> not found";
131 my $s = "<pre>Message-ID <$html>\nnot found\n";
134 my $es = $n_partial == 1 ? '' : 'es';
135 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
136 $s .= "\n$n_partial partial match$es found:\n\n";
137 my $cur_name = $cur->{name};
138 foreach my $pair (@partial) {
139 my ($ibx, $res) = @$pair;
140 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
141 my $u = $ibx->base_url($env) or next;
142 foreach my $m (@$res) {
143 my $p = PublicInbox::Hval->new_msgid($m);
146 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
150 my $ext = ext_urls($ctx, $mid, $href, $html);
155 $ctx->{-html_tip} = $s .= '</pre>';
156 $ctx->{-title_html} = $title;
157 $ctx->{-upfx} = '../';
158 PublicInbox::WwwStream->response($ctx, $code);
162 my ($ctx, $mid, $href, $html) = @_;
164 # Fall back to external repos if configured
165 if (@EXT_URL && index($mid, '@') >= 0) {
166 my $env = $ctx->{env};
167 my $e = "\nPerhaps try an external site:\n\n";
168 foreach my $url (@EXT_URL) {
169 my $u = PublicInbox::Hval::prurl($env, $url);
170 my $r = sprintf($u, $href);
171 my $t = sprintf($u, $html);
172 $e .= qq{<a\nhref="$r">$t</a>\n};
180 my ($ctx, $found, $mid) = @_;
181 my $h = PublicInbox::Hval->new_msgid($mid);
182 my $href = $h->{href};
183 my $html = $h->as_html;
184 my $title = "<$html> found in ";
185 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
186 $ctx->{-title_html} = $title . $end;
187 $ctx->{-upfx} = '../';
188 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
189 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
190 $ctx->{-html_tip} = join('',
191 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
193 my $u = $_->base_url;
194 qq(<a\nhref="$u$href/">$u$html/</a>\n)
196 $ext_urls, '</pre>');
197 PublicInbox::WwwStream->response($ctx, $code);