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 search_partial ($$) {
34 return if length($mid) < $MIN_PARTIAL_LEN;
35 my $srch = $ibx->search or return;
36 my $opt = { limit => PARTIAL_MAX, mset => 2 };
37 my @try = ("m:$mid*");
39 if ($chop =~ s/(\W+)(\w*)\z//) {
40 my ($delim, $word) = ($1, $2);
42 push @try, "m:$chop$delim";
43 push @try, "m:$chop$delim*";
46 push @try, "m:$chop*";
49 # break out long words individually to search for, because
50 # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
51 if ($mid =~ /\w{9,}/) {
52 my @long = ($mid =~ m!(\w{3,})!g);
53 push(@try, join(' ', map { "m:$_" } @long));
55 # is the last element long enough to not trigger excessive
57 if (length($long[-1]) > 8) {
59 push(@try, join(' ', map { "m:$_" } @long));
63 foreach my $m (@try) {
64 # If Xapian can't handle the wildcard since it
65 # has too many results. $@ can be
66 # Search::Xapian::QueryParserError or even:
67 # "something terrible happened at ../Search/Xapian/Enquire.pm"
68 my $mset = eval { $srch->mset($m, $opt) } or next;
71 } @{$srch->mset_to_smsg($ibx, $mset)};
72 return \@mids if scalar(@mids);
77 my ($other, $cur, $mid, $ibxs, $found) = @_;
79 return if $other->{name} eq $cur->{name} || !$other->base_url;
81 my $mm = $other->mm or return;
83 # try to find the URL with Msgmap to avoid forking
84 my $num = $mm->num_for($mid);
88 # no point in trying the fork fallback if we
89 # know Xapian is up-to-date but missing the
90 # message in the current repo
97 my $cur = $ctx->{-inbox};
98 my $mid = $ctx->{mid};
100 eval { require PublicInbox::Msgmap };
104 $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i,
105 $cur, $mid, $ibxs, $found);
107 return exact($ctx, $found, $mid) if @$found;
109 # fall back to partial MID matching
112 my $mids = search_partial($cur, $mid);
114 $n_partial = scalar(@$mids);
115 push @partial, [ $cur, $mids ];
118 # can't find a partial match in current inbox, try the others:
119 if (!$n_partial && length($mid) >= $MIN_PARTIAL_LEN) {
120 foreach my $ibx (@$ibxs) {
121 $mids = search_partial($ibx, $mid) or next;
122 $n_partial += scalar(@$mids);
123 push @partial, [ $ibx, $mids];
124 last if $n_partial >= PARTIAL_MAX;
129 my $href = mid_href($mid);
130 my $html = ascii_html($mid);
131 my $title = "<$html> not found";
132 my $s = "<pre>Message-ID <$html>\nnot found\n";
135 my $es = $n_partial == 1 ? '' : 'es';
136 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
137 $s .= "\n$n_partial partial match$es found:\n\n";
138 my $cur_name = $cur->{name};
139 foreach my $pair (@partial) {
140 my ($ibx, $res) = @$pair;
141 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
142 my $u = $ibx->base_url($env) or next;
143 foreach my $m (@$res) {
144 my $href = mid_href($m);
145 my $html = ascii_html($m);
146 $s .= qq{<a\nhref="$u$href/">$u$html/</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 html_oneshot($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 = 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 $href = mid_href($mid);
182 my $html = ascii_html($mid);
183 my $title = "<$html> found in ";
184 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
185 $ctx->{-title_html} = $title . $end;
186 $ctx->{-upfx} = '../';
187 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
188 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
189 $ctx->{-html_tip} = join('',
190 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
192 my $u = $_->base_url;
193 qq(<a\nhref="$u$href/">$u$html/</a>\n)
195 $ext_urls, '</pre>');
196 html_oneshot($ctx, $code);