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 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.io/%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 mids_from_mset { # Search::retry_reopen callback
33 [ map { PublicInbox::Smsg::from_mitem($_)->mid } $_[0]->items ];
36 sub search_partial ($$) {
37 my ($srch, $mid) = @_;
38 return if length($mid) < $MIN_PARTIAL_LEN;
39 my $opt = { limit => PARTIAL_MAX, mset => 2 };
40 my @try = ("m:$mid*");
42 if ($chop =~ s/(\W+)(\w*)\z//) {
43 my ($delim, $word) = ($1, $2);
45 push @try, "m:$chop$delim";
46 push @try, "m:$chop$delim*";
49 push @try, "m:$chop*";
52 # break out long words individually to search for, because
53 # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
54 if ($mid =~ /\w{9,}/) {
55 my @long = ($mid =~ m!(\w{3,})!g);
56 push(@try, join(' ', map { "m:$_" } @long));
58 # is the last element long enough to not trigger excessive
60 if (length($long[-1]) > 8) {
62 push(@try, join(' ', map { "m:$_" } @long));
66 foreach my $m (@try) {
67 # If Xapian can't handle the wildcard since it
68 # has too many results. $@ can be
69 # Search::Xapian::QueryParserError or even:
70 # "something terrible happened at ../Search/Xapian/Enquire.pm"
71 my $mset = eval { $srch->query($m, $opt) } or next;
72 my $mids = $srch->retry_reopen(\&mids_from_mset, $mset);
73 return $mids if scalar(@$mids);
78 my ($other, $arg) = @_;
79 my ($cur, $mid, $ibxs, $found) = @$arg;
81 return if $other->{name} eq $cur->{name} || !$other->base_url;
83 my $mm = $other->mm or return;
85 # try to find the URL with Msgmap to avoid forking
86 my $num = $mm->num_for($mid);
90 # no point in trying the fork fallback if we
91 # know Xapian is up-to-date but missing the
92 # message in the current repo
99 my $cur = $ctx->{-inbox};
100 my $mid = $ctx->{mid};
102 eval { require PublicInbox::Msgmap };
105 my $arg = [ $cur, $mid, $ibxs, $found ];
107 $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i, $arg);
109 return exact($ctx, $found, $mid) if @$found;
111 # fall back to partial MID matching
114 my $srch = $cur->search;
115 my $mids = search_partial($srch, $mid) if $srch;
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 $srch = $ibx->search or next;
125 $mids = search_partial($srch, $mid) or next;
126 $n_partial += scalar(@$mids);
127 push @partial, [ $ibx, $mids];
128 last if $n_partial >= PARTIAL_MAX;
133 my $href = mid_href($mid);
134 my $html = ascii_html($mid);
135 my $title = "<$html> not found";
136 my $s = "<pre>Message-ID <$html>\nnot found\n";
139 my $es = $n_partial == 1 ? '' : 'es';
140 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
141 $s .= "\n$n_partial partial match$es found:\n\n";
142 my $cur_name = $cur->{name};
143 foreach my $pair (@partial) {
144 my ($ibx, $res) = @$pair;
145 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
146 my $u = $ibx->base_url($env) or next;
147 foreach my $m (@$res) {
148 my $href = mid_href($m);
149 my $html = ascii_html($m);
150 $s .= qq{<a\nhref="$u$href/">$u$html/</a>\n};
154 my $ext = ext_urls($ctx, $mid, $href, $html);
159 $ctx->{-html_tip} = $s .= '</pre>';
160 $ctx->{-title_html} = $title;
161 $ctx->{-upfx} = '../';
162 html_oneshot($ctx, $code);
166 my ($ctx, $mid, $href, $html) = @_;
168 # Fall back to external repos if configured
169 if (@EXT_URL && index($mid, '@') >= 0) {
170 my $env = $ctx->{env};
171 my $e = "\nPerhaps try an external site:\n\n";
172 foreach my $url (@EXT_URL) {
173 my $u = prurl($env, $url);
174 my $r = sprintf($u, $href);
175 my $t = sprintf($u, $html);
176 $e .= qq{<a\nhref="$r">$t</a>\n};
184 my ($ctx, $found, $mid) = @_;
185 my $href = mid_href($mid);
186 my $html = ascii_html($mid);
187 my $title = "<$html> found in ";
188 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
189 $ctx->{-title_html} = $title . $end;
190 $ctx->{-upfx} = '../';
191 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
192 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
193 $ctx->{-html_tip} = join('',
194 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
196 my $u = $_->base_url;
197 qq(<a\nhref="$u$href/">$u$html/</a>\n)
199 $ext_urls, '</pre>');
200 html_oneshot($ctx, $code);