]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
95feb88539b6459b7fdcbbb48b3a9989371c3d19
[public-inbox.git] / lib / PublicInbox / ExtMsg.pm
1 # Copyright (C) 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 = 14; # for 'XXXXXXXXXX.fsf' msgids gnus generates
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->isrch or return;
36         my $opt = { limit => PARTIAL_MAX, relevance => -1 };
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->{ibx}->{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_ALL ($) {
107         my ($ctx) = @_;
108         my $ALL = $ctx->{www}->{pi_cfg}->ALL or return;
109         my $by_eidx_key = $ctx->{www}->{pi_cfg}->{-by_eidx_key};
110         my $cur_key = eval { $ctx->{ibx}->eidx_key } //
111                         return partial_response($ctx); # $cur->{ibx} == $ALL
112         my %seen = ($cur_key => 1);
113         my ($id, $prev);
114         while (my $x = $ALL->over->next_by_mid($ctx->{mid}, \$id, \$prev)) {
115                 my $xr3 = $ALL->over->get_xref3($x->{num});
116                 for my $k (@$xr3) {
117                         $k =~ s/:[0-9]+:$x->{blob}\z// or next;
118                         next if $k eq $cur_key;
119                         my $ibx = $by_eidx_key->{$k} // next;
120                         $ibx->base_url or next;
121                         push(@{$ctx->{found}}, $ibx) unless $seen{$k}++;
122                 }
123         }
124         return exact($ctx) if $ctx->{found};
125
126         # fall back to partial MID matching
127         for my $ibxish ($ctx->{ibx}, $ALL) {
128                 my $mids = search_partial($ibxish, $ctx->{mid}) or next;
129                 push @{$ctx->{partial}}, [ $ibxish, $mids ];
130                 last if ($ctx->{n_partial} += scalar(@$mids)) >= PARTIAL_MAX;
131         }
132         partial_response($ctx);
133 }
134
135 sub ext_msg {
136         my ($ctx) = @_;
137         ext_msg_ALL($ctx) // sub {
138                 $ctx->{-wcb} = $_[0]; # HTTP server write callback
139
140                 if ($ctx->{env}->{'pi-httpd.async'}) {
141                         require PublicInbox::ConfigIter;
142                         my $iter = PublicInbox::ConfigIter->new(
143                                                 $ctx->{www}->{pi_cfg},
144                                                 \&ext_msg_step, $ctx);
145                         $iter->event_step;
146                 } else {
147                         $ctx->{www}->{pi_cfg}->each_inbox(\&ext_msg_i, $ctx);
148                         finalize_exact($ctx);
149                 }
150         };
151 }
152
153 # called via PublicInbox::DS::event_loop
154 sub event_step {
155         my ($ctx, $sync) = @_;
156         # can't find a partial match in current inbox, try the others:
157         my $ibx = shift @{$ctx->{again}} or return finalize_partial($ctx);
158         my $mids = search_partial($ibx, $ctx->{mid}) or
159                         return ($sync ? undef : PublicInbox::DS::requeue($ctx));
160         $ctx->{n_partial} += scalar(@$mids);
161         push @{$ctx->{partial}}, [ $ibx, $mids ];
162         $ctx->{n_partial} >= PARTIAL_MAX ? finalize_partial($ctx)
163                         : ($sync ? undef : PublicInbox::DS::requeue($ctx));
164 }
165
166 sub finalize_exact {
167         my ($ctx) = @_;
168
169         return $ctx->{-wcb}->(exact($ctx)) if $ctx->{found};
170
171         # fall back to partial MID matching
172         my $mid = $ctx->{mid};
173         my $cur = $ctx->{ibx};
174         my $mids = search_partial($cur, $mid);
175         if ($mids) {
176                 $ctx->{n_partial} = scalar(@$mids);
177                 push @{$ctx->{partial}}, [ $cur, $mids ];
178         } elsif ($ctx->{again} && length($mid) >= $MIN_PARTIAL_LEN) {
179                 bless $ctx, __PACKAGE__;
180                 if ($ctx->{env}->{'pi-httpd.async'}) {
181                         $ctx->event_step;
182                         return;
183                 }
184
185                 # synchronous fall-through
186                 $ctx->event_step while @{$ctx->{again}};
187         }
188         finalize_partial($ctx);
189 }
190
191 sub _url_pfx ($$) {
192         my ($ctx, $u) = @_;
193         (index($u, '://') < 0 && index($u, '/') != 0) ?
194                 "$ctx->{-upfx}../$u" : $u;
195 }
196
197 sub partial_response ($) {
198         my ($ctx) = @_;
199         my $mid = $ctx->{mid};
200         my $code = 404;
201         my $href = mid_href($mid);
202         my $html = ascii_html($mid);
203         my $title = "&lt;$html&gt; not found";
204         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
205         $ctx->{-upfx} //= '../';
206         if (my $n_partial = $ctx->{n_partial}) {
207                 $code = 300;
208                 my $es = $n_partial == 1 ? '' : 'es';
209                 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
210                 $s .= "\n$n_partial partial match$es found:\n\n";
211                 my $cur_name = $ctx->{ibx}->{name};
212                 foreach my $pair (@{$ctx->{partial}}) {
213                         my ($ibx, $res) = @$pair;
214                         my $e = $ibx->{name} eq $cur_name ? $ctx->{env} : undef;
215                         my $u = _url_pfx($ctx, $ibx->base_url($e) // next);
216                         foreach my $m (@$res) {
217                                 my $href = mid_href($m);
218                                 my $html = ascii_html($m);
219                                 $s .= qq{<a\nhref="$u$href/">$u$html/</a>\n};
220                         }
221                 }
222         }
223         my $ext = ext_urls($ctx, $mid, $href, $html);
224         if ($ext ne '') {
225                 $s .= $ext;
226                 $code = 300;
227         }
228         $ctx->{-html_tip} = $s .= '</pre>';
229         $ctx->{-title_html} = $title;
230         html_oneshot($ctx, $code);
231 }
232
233 sub finalize_partial ($) { $_[0]->{-wcb}->(partial_response($_[0])) }
234
235 sub ext_urls {
236         my ($ctx, $mid, $href, $html) = @_;
237
238         # Fall back to external repos if configured
239         if (@EXT_URL && index($mid, '@') >= 0) {
240                 my $env = $ctx->{env};
241                 my $e = "\nPerhaps try an external site:\n\n";
242                 foreach my $url (@EXT_URL) {
243                         my $u = prurl($env, $url);
244                         my $r = sprintf($u, $href);
245                         my $t = sprintf($u, $html);
246                         $e .= qq{<a\nhref="$r">$t</a>\n};
247                 }
248                 return $e;
249         }
250         ''
251 }
252
253 sub exact {
254         my ($ctx) = @_;
255         my $mid = $ctx->{mid};
256         my $found = $ctx->{found};
257         my $href = mid_href($mid);
258         my $html = ascii_html($mid);
259         my $title = "&lt;$html&gt; found in ";
260         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
261         $ctx->{-title_html} = $title . $end;
262         $ctx->{-upfx} //= '../';
263         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
264         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
265         $ctx->{-html_tip} = join('',
266                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
267                                 (map {
268                                         my $u = _url_pfx($ctx, $_->base_url);
269                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
270                                 } @$found),
271                         $ext_urls, '</pre>');
272         html_oneshot($ctx, $code);
273 }
274
275 1;