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, $ctx) = @_;
79 return if $other->{name} eq $ctx->{-inbox}->{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($ctx->{mid});
86 push @{$ctx->{found}}, $other;
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
91 push @{$ctx->{again}}, $other;
96 my ($pi_cfg, $section, $ctx) = @_;
97 if (defined($section)) {
98 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
99 my $ibx = $pi_cfg->lookup_name($1) or return;
100 ext_msg_i($ibx, $ctx);
101 } else { # undef == "EOF"
102 finalize_exact($ctx);
106 sub ext_msg_ALL ($) {
108 my $ALL = $ctx->{www}->{pi_config}->ALL or return;
109 my $by_eidx_key = $ctx->{www}->{pi_config}->{-by_eidx_key};
110 my $cur_key = $ctx->{-inbox}->eidx_key;
111 my %seen = ($cur_key => 1);
113 while (my $x = $ALL->over->next_by_mid($ctx->{mid}, \$id, \$prev)) {
114 my $xr3 = $ALL->over->get_xref3($x->{num});
116 $k =~ s/:[0-9]+:$x->{blob}\z// or next;
117 next if $k eq $cur_key;
118 my $ibx = $by_eidx_key->{$k} // next;
119 my $url = $ibx->base_url or next;
120 push(@{$ctx->{found}}, $ibx) unless $seen{$k}++;
123 return exact($ctx) if $ctx->{found};
125 # fall back to partial MID matching
126 for my $ibxish ($ctx->{-inbox}, $ALL) {
127 my $mids = search_partial($ibxish, $ctx->{mid}) or next;
128 push @{$ctx->{partial}}, [ $ibxish, $mids ];
129 last if ($ctx->{n_partial} += scalar(@$mids)) >= PARTIAL_MAX;
131 partial_response($ctx);
136 ext_msg_ALL($ctx) // sub {
137 $ctx->{-wcb} = $_[0]; # HTTP server write callback
139 if ($ctx->{env}->{'pi-httpd.async'}) {
140 require PublicInbox::ConfigIter;
141 my $iter = PublicInbox::ConfigIter->new(
142 $ctx->{www}->{pi_config},
143 \&ext_msg_step, $ctx);
146 $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i, $ctx);
147 finalize_exact($ctx);
152 # called via PublicInbox::DS->EventLoop
154 my ($ctx, $sync) = @_;
155 # can't find a partial match in current inbox, try the others:
156 my $ibx = shift @{$ctx->{again}} or return finalize_partial($ctx);
157 my $mids = search_partial($ibx, $ctx->{mid}) or
158 return ($sync ? undef : PublicInbox::DS::requeue($ctx));
159 $ctx->{n_partial} += scalar(@$mids);
160 push @{$ctx->{partial}}, [ $ibx, $mids ];
161 $ctx->{n_partial} >= PARTIAL_MAX ? finalize_partial($ctx)
162 : ($sync ? undef : PublicInbox::DS::requeue($ctx));
168 return $ctx->{-wcb}->(exact($ctx)) if $ctx->{found};
170 # fall back to partial MID matching
171 my $mid = $ctx->{mid};
172 my $cur = $ctx->{-inbox};
173 my $mids = search_partial($cur, $mid);
175 $ctx->{n_partial} = scalar(@$mids);
176 push @{$ctx->{partial}}, [ $cur, $mids ];
177 } elsif ($ctx->{again} && length($mid) >= $MIN_PARTIAL_LEN) {
178 bless $ctx, __PACKAGE__;
179 if ($ctx->{env}->{'pi-httpd.async'}) {
184 # synchronous fall-through
185 $ctx->event_step while @{$ctx->{again}};
187 finalize_partial($ctx);
190 sub partial_response ($) {
192 my $mid = $ctx->{mid};
194 my $href = mid_href($mid);
195 my $html = ascii_html($mid);
196 my $title = "<$html> not found";
197 my $s = "<pre>Message-ID <$html>\nnot found\n";
198 if (my $n_partial = $ctx->{n_partial}) {
200 my $es = $n_partial == 1 ? '' : 'es';
201 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
202 $s .= "\n$n_partial partial match$es found:\n\n";
203 my $cur_name = $ctx->{-inbox}->{name};
204 foreach my $pair (@{$ctx->{partial}}) {
205 my ($ibx, $res) = @$pair;
206 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
207 my $u = $ibx->base_url($env) or next;
208 foreach my $m (@$res) {
209 my $href = mid_href($m);
210 my $html = ascii_html($m);
211 $s .= qq{<a\nhref="$u$href/">$u$html/</a>\n};
215 my $ext = ext_urls($ctx, $mid, $href, $html);
220 $ctx->{-html_tip} = $s .= '</pre>';
221 $ctx->{-title_html} = $title;
222 $ctx->{-upfx} = '../';
223 html_oneshot($ctx, $code);
226 sub finalize_partial ($) { $_[0]->{-wcb}->(partial_response($_[0])) }
229 my ($ctx, $mid, $href, $html) = @_;
231 # Fall back to external repos if configured
232 if (@EXT_URL && index($mid, '@') >= 0) {
233 my $env = $ctx->{env};
234 my $e = "\nPerhaps try an external site:\n\n";
235 foreach my $url (@EXT_URL) {
236 my $u = prurl($env, $url);
237 my $r = sprintf($u, $href);
238 my $t = sprintf($u, $html);
239 $e .= qq{<a\nhref="$r">$t</a>\n};
248 my $mid = $ctx->{mid};
249 my $found = $ctx->{found};
250 my $href = mid_href($mid);
251 my $html = ascii_html($mid);
252 my $title = "<$html> found in ";
253 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
254 $ctx->{-title_html} = $title . $end;
255 $ctx->{-upfx} = '../';
256 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
257 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
258 $ctx->{-html_tip} = join('',
259 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
261 my $u = $_->base_url;
262 qq(<a\nhref="$u$href/">$u$html/</a>\n)
264 $ext_urls, '</pre>');
265 html_oneshot($ctx, $code);