]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
extmsg: prevent cross-inbox matches from hogging event loop
[public-inbox.git] / lib / PublicInbox / ExtMsg.pm
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>
3 #
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
7 # sites.
8 package PublicInbox::ExtMsg;
9 use strict;
10 use warnings;
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;
15
16 # TODO: user-configurable
17 our @EXT_URL = map { ascii_html($_) } (
18         # leading "//" denotes protocol-relative (http:// or https://)
19         '//marc.info/?i=%s',
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'
28 );
29
30 sub PARTIAL_MAX () { 100 }
31
32 sub search_partial ($$) {
33         my ($ibx, $mid) = @_;
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*");
38         my $chop = $mid;
39         if ($chop =~ s/(\W+)(\w*)\z//) {
40                 my ($delim, $word) = ($1, $2);
41                 if (length($word)) {
42                         push @try, "m:$chop$delim";
43                         push @try, "m:$chop$delim*";
44                 }
45                 push @try, "m:$chop";
46                 push @try, "m:$chop*";
47         }
48
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));
54
55                 # is the last element long enough to not trigger excessive
56                 # wildcard matches?
57                 if (length($long[-1]) > 8) {
58                         $long[-1] .= '*';
59                         push(@try, join(' ', map { "m:$_" } @long));
60                 }
61         }
62
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;
69                 my @mids = map {
70                         $_->{mid}
71                 } @{$srch->mset_to_smsg($ibx, $mset)};
72                 return \@mids if scalar(@mids);
73         }
74 }
75
76 sub ext_msg_i {
77         my ($other, $ctx) = @_;
78
79         return if $other->{name} eq $ctx->{-inbox}->{name} || !$other->base_url;
80
81         my $mm = $other->mm or return;
82
83         # try to find the URL with Msgmap to avoid forking
84         my $num = $mm->num_for($ctx->{mid});
85         if (defined $num) {
86                 push @{$ctx->{found}}, $other;
87         } else {
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;
92         }
93 }
94
95 sub ext_msg_step {
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);
103         }
104 }
105
106 sub ext_msg {
107         my ($ctx) = @_;
108         sub {
109                 $ctx->{-wcb} = $_[0]; # HTTP server write callback
110
111                 if ($ctx->{env}->{'pi-httpd.async'}) {
112                         require PublicInbox::ConfigIter;
113                         my $iter = PublicInbox::ConfigIter->new(
114                                                 $ctx->{www}->{pi_config},
115                                                 \&ext_msg_step, $ctx);
116                         $iter->event_step;
117                 } else {
118                         $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i, $ctx);
119                         finalize_exact($ctx);
120                 }
121         };
122 }
123
124 # called via PublicInbox::DS->EventLoop
125 sub event_step {
126         my ($ctx, $sync) = @_;
127         # can't find a partial match in current inbox, try the others:
128         my $ibx = shift @{$ctx->{again}} or goto \&finalize_partial;
129         my $mids = search_partial($ibx, $ctx->{mid}) or
130                         return ($sync ? undef : PublicInbox::DS::requeue($ctx));
131         $ctx->{n_partial} += scalar(@$mids);
132         push @{$ctx->{partial}}, [ $ibx, $mids ];
133         $ctx->{n_partial} >= PARTIAL_MAX ? goto(\&finalize_partial)
134                         : ($sync ? undef : PublicInbox::DS::requeue($ctx));
135 }
136
137 sub finalize_exact {
138         my ($ctx) = @_;
139
140         return $ctx->{-wcb}->(exact($ctx)) if $ctx->{found};
141
142         # fall back to partial MID matching
143         my $mid = $ctx->{mid};
144         my $cur = $ctx->{-inbox};
145         my $mids = search_partial($cur, $mid);
146         if ($mids) {
147                 $ctx->{n_partial} = scalar(@$mids);
148                 push @{$ctx->{partial}}, [ $cur, $mids ];
149         } elsif ($ctx->{again} && length($mid) >= $MIN_PARTIAL_LEN) {
150                 bless $ctx, __PACKAGE__;
151                 if ($ctx->{env}->{'pi-httpd.async'}) {
152                         $ctx->event_step;
153                         return;
154                 }
155
156                 # synchronous fall-through
157                 $ctx->event_step while @{$ctx->{again}};
158         }
159         goto \&finalize_partial;
160 }
161
162 sub finalize_partial {
163         my ($ctx) = @_;
164         my $mid = $ctx->{mid};
165         my $code = 404;
166         my $href = mid_href($mid);
167         my $html = ascii_html($mid);
168         my $title = "&lt;$html&gt; not found";
169         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
170         if (my $n_partial = $ctx->{n_partial}) {
171                 $code = 300;
172                 my $es = $n_partial == 1 ? '' : 'es';
173                 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
174                 $s .= "\n$n_partial partial match$es found:\n\n";
175                 my $cur_name = $ctx->{-inbox}->{name};
176                 foreach my $pair (@{$ctx->{partial}}) {
177                         my ($ibx, $res) = @$pair;
178                         my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
179                         my $u = $ibx->base_url($env) or next;
180                         foreach my $m (@$res) {
181                                 my $href = mid_href($m);
182                                 my $html = ascii_html($m);
183                                 $s .= qq{<a\nhref="$u$href/">$u$html/</a>\n};
184                         }
185                 }
186         }
187         my $ext = ext_urls($ctx, $mid, $href, $html);
188         if ($ext ne '') {
189                 $s .= $ext;
190                 $code = 300;
191         }
192         $ctx->{-html_tip} = $s .= '</pre>';
193         $ctx->{-title_html} = $title;
194         $ctx->{-upfx} = '../';
195         $ctx->{-wcb}->(html_oneshot($ctx, $code));
196 }
197
198 sub ext_urls {
199         my ($ctx, $mid, $href, $html) = @_;
200
201         # Fall back to external repos if configured
202         if (@EXT_URL && index($mid, '@') >= 0) {
203                 my $env = $ctx->{env};
204                 my $e = "\nPerhaps try an external site:\n\n";
205                 foreach my $url (@EXT_URL) {
206                         my $u = prurl($env, $url);
207                         my $r = sprintf($u, $href);
208                         my $t = sprintf($u, $html);
209                         $e .= qq{<a\nhref="$r">$t</a>\n};
210                 }
211                 return $e;
212         }
213         ''
214 }
215
216 sub exact {
217         my ($ctx) = @_;
218         my $mid = $ctx->{mid};
219         my $found = $ctx->{found};
220         my $href = mid_href($mid);
221         my $html = ascii_html($mid);
222         my $title = "&lt;$html&gt; found in ";
223         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
224         $ctx->{-title_html} = $title . $end;
225         $ctx->{-upfx} = '../';
226         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
227         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
228         $ctx->{-html_tip} = join('',
229                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
230                                 (map {
231                                         my $u = $_->base_url;
232                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
233                                 } @$found),
234                         $ext_urls, '</pre>');
235         html_oneshot($ctx, $code);
236 }
237
238 1;