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