1 # Copyright (C) 2015-2020 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 prurl mid_href);
12 use PublicInbox::WwwStream qw(html_oneshot);
13 use PublicInbox::Smsg;
14 use PublicInbox::Search qw(mdocid);
15 our $MIN_PARTIAL_LEN = 16;
17 # TODO: user-configurable
18 our @EXT_URL = map { ascii_html($_) } (
19 # leading "//" denotes protocol-relative (http:// or https://)
21 '//www.mail-archive.com/search?l=mid&q=%s',
22 'nntp://news.gmane.io/%s',
23 'https://lists.debian.org/msgid-search/%s',
24 '//docs.FreeBSD.org/cgi/mid.cgi?db=mid&id=%s',
25 'https://www.w3.org/mid/%s',
26 'http://www.postgresql.org/message-id/%s',
27 'https://lists.debconf.org/cgi-lurker/keyword.cgi?'.
28 'doc-url=/lurker&format=en.html&query=id:%s'
31 sub PARTIAL_MAX () { 100 }
33 sub search_partial ($$) {
35 return if length($mid) < $MIN_PARTIAL_LEN;
36 my $srch = $ibx->search or return;
37 my $opt = { limit => PARTIAL_MAX, mset => 2 };
38 my @try = ("m:$mid*");
40 if ($chop =~ s/(\W+)(\w*)\z//) {
41 my ($delim, $word) = ($1, $2);
43 push @try, "m:$chop$delim";
44 push @try, "m:$chop$delim*";
47 push @try, "m:$chop*";
50 # break out long words individually to search for, because
51 # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
52 if ($mid =~ /\w{9,}/) {
53 my @long = ($mid =~ m!(\w{3,})!g);
54 push(@try, join(' ', map { "m:$_" } @long));
56 # is the last element long enough to not trigger excessive
58 if (length($long[-1]) > 8) {
60 push(@try, join(' ', map { "m:$_" } @long));
64 my $n = $srch->{nshard} // 1;
65 foreach my $m (@try) {
66 # If Xapian can't handle the wildcard since it
67 # has too many results. $@ can be
68 # Search::Xapian::QueryParserError or even:
69 # "something terrible happened at ../Search/Xapian/Enquire.pm"
70 my $mset = eval { $srch->query($m, $opt) } or next;
73 } @{$ibx->over->get_all(map { mdocid($n, $_) } $mset->items)};
74 return \@mids if scalar(@mids);
79 my ($other, $arg) = @_;
80 my ($cur, $mid, $ibxs, $found) = @$arg;
82 return if $other->{name} eq $cur->{name} || !$other->base_url;
84 my $mm = $other->mm or return;
86 # try to find the URL with Msgmap to avoid forking
87 my $num = $mm->num_for($mid);
91 # no point in trying the fork fallback if we
92 # know Xapian is up-to-date but missing the
93 # message in the current repo
100 my $cur = $ctx->{-inbox};
101 my $mid = $ctx->{mid};
103 eval { require PublicInbox::Msgmap };
106 my $arg = [ $cur, $mid, $ibxs, $found ];
108 $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i, $arg);
110 return exact($ctx, $found, $mid) if @$found;
112 # fall back to partial MID matching
115 my $mids = search_partial($cur, $mid);
117 $n_partial = scalar(@$mids);
118 push @partial, [ $cur, $mids ];
121 # can't find a partial match in current inbox, try the others:
122 if (!$n_partial && length($mid) >= $MIN_PARTIAL_LEN) {
123 foreach my $ibx (@$ibxs) {
124 $mids = search_partial($ibx, $mid) or next;
125 $n_partial += scalar(@$mids);
126 push @partial, [ $ibx, $mids];
127 last if $n_partial >= PARTIAL_MAX;
132 my $href = mid_href($mid);
133 my $html = ascii_html($mid);
134 my $title = "<$html> not found";
135 my $s = "<pre>Message-ID <$html>\nnot found\n";
138 my $es = $n_partial == 1 ? '' : 'es';
139 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
140 $s .= "\n$n_partial partial match$es found:\n\n";
141 my $cur_name = $cur->{name};
142 foreach my $pair (@partial) {
143 my ($ibx, $res) = @$pair;
144 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
145 my $u = $ibx->base_url($env) or next;
146 foreach my $m (@$res) {
147 my $href = mid_href($m);
148 my $html = ascii_html($m);
149 $s .= qq{<a\nhref="$u$href/">$u$html/</a>\n};
153 my $ext = ext_urls($ctx, $mid, $href, $html);
158 $ctx->{-html_tip} = $s .= '</pre>';
159 $ctx->{-title_html} = $title;
160 $ctx->{-upfx} = '../';
161 html_oneshot($ctx, $code);
165 my ($ctx, $mid, $href, $html) = @_;
167 # Fall back to external repos if configured
168 if (@EXT_URL && index($mid, '@') >= 0) {
169 my $env = $ctx->{env};
170 my $e = "\nPerhaps try an external site:\n\n";
171 foreach my $url (@EXT_URL) {
172 my $u = prurl($env, $url);
173 my $r = sprintf($u, $href);
174 my $t = sprintf($u, $html);
175 $e .= qq{<a\nhref="$r">$t</a>\n};
183 my ($ctx, $found, $mid) = @_;
184 my $href = mid_href($mid);
185 my $html = ascii_html($mid);
186 my $title = "<$html> found in ";
187 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
188 $ctx->{-title_html} = $title . $end;
189 $ctx->{-upfx} = '../';
190 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
191 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
192 $ctx->{-html_tip} = join('',
193 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
195 my $u = $_->base_url;
196 qq(<a\nhref="$u$href/">$u$html/</a>\n)
198 $ext_urls, '</pre>');
199 html_oneshot($ctx, $code);