]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
65892161dec73e3c50126ba14bc88cc1d52408d3
[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->query($m, $opt) } or next;
69                 my @mids = map {
70                         $_->{mid}
71                 } @{$ibx->over->get_all(@{$srch->mset_to_artnums($mset)})};
72                 return \@mids if scalar(@mids);
73         }
74 }
75
76 sub ext_msg_i {
77         my ($other, $arg) = @_;
78         my ($cur, $mid, $ibxs, $found) = @$arg;
79
80         return if $other->{name} eq $cur->{name} || !$other->base_url;
81
82         my $mm = $other->mm or return;
83
84         # try to find the URL with Msgmap to avoid forking
85         my $num = $mm->num_for($mid);
86         if (defined $num) {
87                 push @$found, $other;
88         } else {
89                 # no point in trying the fork fallback if we
90                 # know Xapian is up-to-date but missing the
91                 # message in the current repo
92                 push @$ibxs, $other;
93         }
94 }
95
96 sub ext_msg {
97         my ($ctx) = @_;
98         my $cur = $ctx->{-inbox};
99         my $mid = $ctx->{mid};
100
101         eval { require PublicInbox::Msgmap };
102         my $ibxs = [];
103         my $found = [];
104         my $arg = [ $cur, $mid, $ibxs, $found ];
105
106         $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i, $arg);
107
108         return exact($ctx, $found, $mid) if @$found;
109
110         # fall back to partial MID matching
111         my @partial;
112         my $n_partial = 0;
113         my $mids = search_partial($cur, $mid);
114         if ($mids) {
115                 $n_partial = scalar(@$mids);
116                 push @partial, [ $cur, $mids ];
117         }
118
119         # can't find a partial match in current inbox, try the others:
120         if (!$n_partial && length($mid) >= $MIN_PARTIAL_LEN) {
121                 foreach my $ibx (@$ibxs) {
122                         $mids = search_partial($ibx, $mid) or next;
123                         $n_partial += scalar(@$mids);
124                         push @partial, [ $ibx, $mids];
125                         last if $n_partial >= PARTIAL_MAX;
126                 }
127         }
128
129         my $code = 404;
130         my $href = mid_href($mid);
131         my $html = ascii_html($mid);
132         my $title = "&lt;$html&gt; not found";
133         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
134         if ($n_partial) {
135                 $code = 300;
136                 my $es = $n_partial == 1 ? '' : 'es';
137                 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
138                 $s .= "\n$n_partial partial match$es found:\n\n";
139                 my $cur_name = $cur->{name};
140                 foreach my $pair (@partial) {
141                         my ($ibx, $res) = @$pair;
142                         my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
143                         my $u = $ibx->base_url($env) or next;
144                         foreach my $m (@$res) {
145                                 my $href = mid_href($m);
146                                 my $html = ascii_html($m);
147                                 $s .= qq{<a\nhref="$u$href/">$u$html/</a>\n};
148                         }
149                 }
150         }
151         my $ext = ext_urls($ctx, $mid, $href, $html);
152         if ($ext ne '') {
153                 $s .= $ext;
154                 $code = 300;
155         }
156         $ctx->{-html_tip} = $s .= '</pre>';
157         $ctx->{-title_html} = $title;
158         $ctx->{-upfx} = '../';
159         html_oneshot($ctx, $code);
160 }
161
162 sub ext_urls {
163         my ($ctx, $mid, $href, $html) = @_;
164
165         # Fall back to external repos if configured
166         if (@EXT_URL && index($mid, '@') >= 0) {
167                 my $env = $ctx->{env};
168                 my $e = "\nPerhaps try an external site:\n\n";
169                 foreach my $url (@EXT_URL) {
170                         my $u = prurl($env, $url);
171                         my $r = sprintf($u, $href);
172                         my $t = sprintf($u, $html);
173                         $e .= qq{<a\nhref="$r">$t</a>\n};
174                 }
175                 return $e;
176         }
177         ''
178 }
179
180 sub exact {
181         my ($ctx, $found, $mid) = @_;
182         my $href = mid_href($mid);
183         my $html = ascii_html($mid);
184         my $title = "&lt;$html&gt; found in ";
185         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
186         $ctx->{-title_html} = $title . $end;
187         $ctx->{-upfx} = '../';
188         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
189         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
190         $ctx->{-html_tip} = join('',
191                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
192                                 (map {
193                                         my $u = $_->base_url;
194                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
195                                 } @$found),
196                         $ext_urls, '</pre>');
197         html_oneshot($ctx, $code);
198 }
199
200 1;