]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
5c23dc311220bcc3c056e53d538381918fc21755
[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 use PublicInbox::Search qw(mdocid);
15 our $MIN_PARTIAL_LEN = 16;
16
17 # TODO: user-configurable
18 our @EXT_URL = map { ascii_html($_) } (
19         # leading "//" denotes protocol-relative (http:// or https://)
20         '//marc.info/?i=%s',
21         '//www.mail-archive.com/search?l=mid&q=%s',
22         'nntp://news.gmane.io/%s',
23         'https://lists.debian.org/msgid-search/%s',
24         '//docs.FreeBSD.org/cgi/mid.cgi?db=mid&id=%s',
25         'https://www.w3.org/mid/%s',
26         'http://www.postgresql.org/message-id/%s',
27         'https://lists.debconf.org/cgi-lurker/keyword.cgi?'.
28                 'doc-url=/lurker&format=en.html&query=id:%s'
29 );
30
31 sub PARTIAL_MAX () { 100 }
32
33 sub search_partial ($$) {
34         my ($ibx, $mid) = @_;
35         return if length($mid) < $MIN_PARTIAL_LEN;
36         my $srch = $ibx->search or return;
37         my $opt = { limit => PARTIAL_MAX, mset => 2 };
38         my @try = ("m:$mid*");
39         my $chop = $mid;
40         if ($chop =~ s/(\W+)(\w*)\z//) {
41                 my ($delim, $word) = ($1, $2);
42                 if (length($word)) {
43                         push @try, "m:$chop$delim";
44                         push @try, "m:$chop$delim*";
45                 }
46                 push @try, "m:$chop";
47                 push @try, "m:$chop*";
48         }
49
50         # break out long words individually to search for, because
51         # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
52         if ($mid =~ /\w{9,}/) {
53                 my @long = ($mid =~ m!(\w{3,})!g);
54                 push(@try, join(' ', map { "m:$_" } @long));
55
56                 # is the last element long enough to not trigger excessive
57                 # wildcard matches?
58                 if (length($long[-1]) > 8) {
59                         $long[-1] .= '*';
60                         push(@try, join(' ', map { "m:$_" } @long));
61                 }
62         }
63
64         my $n = $srch->{nshard} // 1;
65         foreach my $m (@try) {
66                 # If Xapian can't handle the wildcard since it
67                 # has too many results.  $@ can be
68                 # Search::Xapian::QueryParserError or even:
69                 # "something terrible happened at ../Search/Xapian/Enquire.pm"
70                 my $mset = eval { $srch->query($m, $opt) } or next;
71                 my @mids = map {
72                         $_->{mid}
73                 } @{$ibx->over->get_all(map { mdocid($n, $_) } $mset->items)};
74                 return \@mids if scalar(@mids);
75         }
76 }
77
78 sub ext_msg_i {
79         my ($other, $arg) = @_;
80         my ($cur, $mid, $ibxs, $found) = @$arg;
81
82         return if $other->{name} eq $cur->{name} || !$other->base_url;
83
84         my $mm = $other->mm or return;
85
86         # try to find the URL with Msgmap to avoid forking
87         my $num = $mm->num_for($mid);
88         if (defined $num) {
89                 push @$found, $other;
90         } else {
91                 # no point in trying the fork fallback if we
92                 # know Xapian is up-to-date but missing the
93                 # message in the current repo
94                 push @$ibxs, $other;
95         }
96 }
97
98 sub ext_msg {
99         my ($ctx) = @_;
100         my $cur = $ctx->{-inbox};
101         my $mid = $ctx->{mid};
102
103         eval { require PublicInbox::Msgmap };
104         my $ibxs = [];
105         my $found = [];
106         my $arg = [ $cur, $mid, $ibxs, $found ];
107
108         $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i, $arg);
109
110         return exact($ctx, $found, $mid) if @$found;
111
112         # fall back to partial MID matching
113         my @partial;
114         my $n_partial = 0;
115         my $mids = search_partial($cur, $mid);
116         if ($mids) {
117                 $n_partial = scalar(@$mids);
118                 push @partial, [ $cur, $mids ];
119         }
120
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                         $mids = search_partial($ibx, $mid) or next;
125                         $n_partial += scalar(@$mids);
126                         push @partial, [ $ibx, $mids];
127                         last if $n_partial >= PARTIAL_MAX;
128                 }
129         }
130
131         my $code = 404;
132         my $href = mid_href($mid);
133         my $html = ascii_html($mid);
134         my $title = "&lt;$html&gt; not found";
135         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
136         if ($n_partial) {
137                 $code = 300;
138                 my $es = $n_partial == 1 ? '' : 'es';
139                 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
140                 $s .= "\n$n_partial partial match$es found:\n\n";
141                 my $cur_name = $cur->{name};
142                 foreach my $pair (@partial) {
143                         my ($ibx, $res) = @$pair;
144                         my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
145                         my $u = $ibx->base_url($env) or next;
146                         foreach my $m (@$res) {
147                                 my $href = mid_href($m);
148                                 my $html = ascii_html($m);
149                                 $s .= qq{<a\nhref="$u$href/">$u$html/</a>\n};
150                         }
151                 }
152         }
153         my $ext = ext_urls($ctx, $mid, $href, $html);
154         if ($ext ne '') {
155                 $s .= $ext;
156                 $code = 300;
157         }
158         $ctx->{-html_tip} = $s .= '</pre>';
159         $ctx->{-title_html} = $title;
160         $ctx->{-upfx} = '../';
161         html_oneshot($ctx, $code);
162 }
163
164 sub ext_urls {
165         my ($ctx, $mid, $href, $html) = @_;
166
167         # Fall back to external repos if configured
168         if (@EXT_URL && index($mid, '@') >= 0) {
169                 my $env = $ctx->{env};
170                 my $e = "\nPerhaps try an external site:\n\n";
171                 foreach my $url (@EXT_URL) {
172                         my $u = prurl($env, $url);
173                         my $r = sprintf($u, $href);
174                         my $t = sprintf($u, $html);
175                         $e .= qq{<a\nhref="$r">$t</a>\n};
176                 }
177                 return $e;
178         }
179         ''
180 }
181
182 sub exact {
183         my ($ctx, $found, $mid) = @_;
184         my $href = mid_href($mid);
185         my $html = ascii_html($mid);
186         my $title = "&lt;$html&gt; found in ";
187         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
188         $ctx->{-title_html} = $title . $end;
189         $ctx->{-upfx} = '../';
190         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
191         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
192         $ctx->{-html_tip} = join('',
193                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
194                                 (map {
195                                         my $u = $_->base_url;
196                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
197                                 } @$found),
198                         $ext_urls, '</pre>');
199         html_oneshot($ctx, $code);
200 }
201
202 1;