]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
167dc5e5d37fe0e2185d2eccbc47ff6f94263131
[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/;
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 search_partial ($$) {
33         my ($srch, $mid) = @_;
34         return if length($mid) < $MIN_PARTIAL_LEN;
35         my $opt = { limit => PARTIAL_MAX, mset => 2 };
36         my @try = ("m:$mid*");
37         my $chop = $mid;
38         if ($chop =~ s/(\W+)(\w*)\z//) {
39                 my ($delim, $word) = ($1, $2);
40                 if (length($word)) {
41                         push @try, "m:$chop$delim";
42                         push @try, "m:$chop$delim*";
43                 }
44                 push @try, "m:$chop";
45                 push @try, "m:$chop*";
46         }
47
48         # break out long words individually to search for, because
49         # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
50         if ($mid =~ /\w{9,}/) {
51                 my @long = ($mid =~ m!(\w{3,})!g);
52                 push(@try, join(' ', map { "m:$_" } @long));
53
54                 # is the last element long enough to not trigger excessive
55                 # wildcard matches?
56                 if (length($long[-1]) > 8) {
57                         $long[-1] .= '*';
58                         push(@try, join(' ', map { "m:$_" } @long));
59                 }
60         }
61
62         foreach my $m (@try) {
63                 # If Xapian can't handle the wildcard since it
64                 # has too many results.  $@ can be
65                 # Search::Xapian::QueryParserError or even:
66                 # "something terrible happened at ../Search/Xapian/Enquire.pm"
67                 my $mset = eval { $srch->query($m, $opt) } or next;
68
69                 my @mids = map {
70                         my $doc = $_->get_document;
71                         PublicInbox::SearchMsg->load_doc($doc)->mid;
72                 } $mset->items;
73                 return \@mids if scalar(@mids);
74         }
75 }
76
77 sub ext_msg {
78         my ($ctx) = @_;
79         my $cur = $ctx->{-inbox};
80         my $mid = $ctx->{mid};
81
82         eval { require PublicInbox::Msgmap };
83         my $have_mm = $@ ? 0 : 1;
84         my (@ibx, @found);
85
86         $ctx->{www}->{pi_config}->each_inbox(sub {
87                 my ($other) = @_;
88                 return if $other->{name} eq $cur->{name} || !$other->base_url;
89
90                 my $mm = $other->mm or return;
91
92                 # try to find the URL with Msgmap to avoid forking
93                 my $num = $mm->num_for($mid);
94                 if (defined $num) {
95                         push @found, $other;
96                 } else {
97                         # no point in trying the fork fallback if we
98                         # know Xapian is up-to-date but missing the
99                         # message in the current repo
100                         push @ibx, $other;
101                 }
102         });
103
104         return exact($ctx, \@found, $mid) if @found;
105
106         # fall back to partial MID matching
107         my @partial;
108         my $n_partial = 0;
109         my $srch = $cur->search;
110         my $mids = search_partial($srch, $mid) if $srch;
111         if ($mids) {
112                 $n_partial = scalar(@$mids);
113                 push @partial, [ $cur, $mids ];
114         }
115
116         # can't find a partial match in current inbox, try the others:
117         if (!$n_partial && length($mid) >= $MIN_PARTIAL_LEN) {
118                 foreach my $ibx (@ibx) {
119                         $srch = $ibx->search or next;
120                         $mids = search_partial($srch, $mid) or next;
121                         $n_partial += scalar(@$mids);
122                         push @partial, [ $ibx, $mids];
123                         last if $n_partial >= PARTIAL_MAX;
124                 }
125         }
126
127         my $code = 404;
128         my $h = PublicInbox::Hval->new_msgid($mid);
129         my $href = $h->{href};
130         my $html = $h->as_html;
131         my $title = "&lt;$html&gt; not found";
132         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
133         if ($n_partial) {
134                 $code = 300;
135                 my $es = $n_partial == 1 ? '' : 'es';
136                 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
137                 $s .= "\n$n_partial partial match$es found:\n\n";
138                 my $cur_name = $cur->{name};
139                 foreach my $pair (@partial) {
140                         my ($ibx, $res) = @$pair;
141                         my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
142                         my $u = $ibx->base_url($env) or next;
143                         foreach my $m (@$res) {
144                                 my $p = PublicInbox::Hval->new_msgid($m);
145                                 my $r = $p->{href};
146                                 my $t = $p->as_html;
147                                 $s .= qq{<a\nhref="$u$r/">$u$t/</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         PublicInbox::WwwStream->response($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 = PublicInbox::Hval::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 $h = PublicInbox::Hval->new_msgid($mid);
183         my $href = $h->{href};
184         my $html = $h->as_html;
185         my $title = "&lt;$html&gt; found in ";
186         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
187         $ctx->{-title_html} = $title . $end;
188         $ctx->{-upfx} = '../';
189         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
190         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
191         $ctx->{-html_tip} = join('',
192                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
193                                 (map {
194                                         my $u = $_->base_url;
195                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
196                                 } @$found),
197                         $ext_urls, '</pre>');
198         PublicInbox::WwwStream->response($ctx, $code);
199 }
200
201 1;