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;
13 our $MIN_PARTIAL_LEN = 16;
15 # TODO: user-configurable
16 our @EXT_URL = map { ascii_html($_) } (
17 # leading "//" denotes protocol-relative (http:// or https://)
19 '//www.mail-archive.com/search?l=mid&q=%s',
20 'nntp://news.gmane.io/%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'
29 sub PARTIAL_MAX () { 100 }
31 sub mids_from_mset { # Search::retry_reopen callback
32 [ map { PublicInbox::SearchMsg::from_mitem($_)->mid } $_[0]->items ];
35 sub search_partial ($$) {
36 my ($srch, $mid) = @_;
37 return if length($mid) < $MIN_PARTIAL_LEN;
38 my $opt = { limit => PARTIAL_MAX, mset => 2 };
39 my @try = ("m:$mid*");
41 if ($chop =~ s/(\W+)(\w*)\z//) {
42 my ($delim, $word) = ($1, $2);
44 push @try, "m:$chop$delim";
45 push @try, "m:$chop$delim*";
48 push @try, "m:$chop*";
51 # break out long words individually to search for, because
52 # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
53 if ($mid =~ /\w{9,}/) {
54 my @long = ($mid =~ m!(\w{3,})!g);
55 push(@try, join(' ', map { "m:$_" } @long));
57 # is the last element long enough to not trigger excessive
59 if (length($long[-1]) > 8) {
61 push(@try, join(' ', map { "m:$_" } @long));
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;
71 my $mids = $srch->retry_reopen(\&mids_from_mset, $mset);
72 return $mids if scalar(@$mids);
77 my ($other, $arg) = @_;
78 my ($cur, $mid, $ibxs, $found) = @$arg;
80 return if $other->{name} eq $cur->{name} || !$other->base_url;
82 my $mm = $other->mm or return;
84 # try to find the URL with Msgmap to avoid forking
85 my $num = $mm->num_for($mid);
89 # no point in trying the fork fallback if we
90 # know Xapian is up-to-date but missing the
91 # message in the current repo
98 my $cur = $ctx->{-inbox};
99 my $mid = $ctx->{mid};
101 eval { require PublicInbox::Msgmap };
104 my $arg = [ $cur, $mid, $ibxs, $found ];
106 $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i, $arg);
108 return exact($ctx, $found, $mid) if @$found;
110 # fall back to partial MID matching
113 my $srch = $cur->search;
114 my $mids = search_partial($srch, $mid) if $srch;
116 $n_partial = scalar(@$mids);
117 push @partial, [ $cur, $mids ];
120 # can't find a partial match in current inbox, try the others:
121 if (!$n_partial && length($mid) >= $MIN_PARTIAL_LEN) {
122 foreach my $ibx (@$ibxs) {
123 $srch = $ibx->search or next;
124 $mids = search_partial($srch, $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 PublicInbox::WwwStream->response($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 PublicInbox::WwwStream->response($ctx, $code);