]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
treewide: "require" + "use" cleanup and docs
[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::WwwStream;
13 our $MIN_PARTIAL_LEN = 16;
14
15 # TODO: user-configurable
16 our @EXT_URL = map { ascii_html($_) } (
17         # leading "//" denotes protocol-relative (http:// or https://)
18         '//marc.info/?i=%s',
19         '//www.mail-archive.com/search?l=mid&q=%s',
20         'nntp://news.gmane.org/%s',
21         'https://lists.debian.org/msgid-search/%s',
22         '//docs.FreeBSD.org/cgi/mid.cgi?db=mid&id=%s',
23         'https://www.w3.org/mid/%s',
24         'http://www.postgresql.org/message-id/%s',
25         'https://lists.debconf.org/cgi-lurker/keyword.cgi?'.
26                 'doc-url=/lurker&format=en.html&query=id:%s'
27 );
28
29 sub PARTIAL_MAX () { 100 }
30
31 sub mids_from_mset { # Search::retry_reopen callback
32         [ map { PublicInbox::SearchMsg::from_mitem($_)->mid } $_[0]->items ];
33 }
34
35 sub search_partial ($$) {
36         my ($srch, $mid) = @_;
37         return if length($mid) < $MIN_PARTIAL_LEN;
38         my $opt = { limit => PARTIAL_MAX, mset => 2 };
39         my @try = ("m:$mid*");
40         my $chop = $mid;
41         if ($chop =~ s/(\W+)(\w*)\z//) {
42                 my ($delim, $word) = ($1, $2);
43                 if (length($word)) {
44                         push @try, "m:$chop$delim";
45                         push @try, "m:$chop$delim*";
46                 }
47                 push @try, "m:$chop";
48                 push @try, "m:$chop*";
49         }
50
51         # break out long words individually to search for, because
52         # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
53         if ($mid =~ /\w{9,}/) {
54                 my @long = ($mid =~ m!(\w{3,})!g);
55                 push(@try, join(' ', map { "m:$_" } @long));
56
57                 # is the last element long enough to not trigger excessive
58                 # wildcard matches?
59                 if (length($long[-1]) > 8) {
60                         $long[-1] .= '*';
61                         push(@try, join(' ', map { "m:$_" } @long));
62                 }
63         }
64
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 = $srch->retry_reopen(\&mids_from_mset, $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 $srch = $cur->search;
114         my $mids = search_partial($srch, $mid) if $srch;
115         if ($mids) {
116                 $n_partial = scalar(@$mids);
117                 push @partial, [ $cur, $mids ];
118         }
119
120         # can't find a partial match in current inbox, try the others:
121         if (!$n_partial && length($mid) >= $MIN_PARTIAL_LEN) {
122                 foreach my $ibx (@$ibxs) {
123                         $srch = $ibx->search or next;
124                         $mids = search_partial($srch, $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 $h = PublicInbox::Hval->new_msgid($mid);
133         my $href = $h->{href};
134         my $html = $h->as_html;
135         my $title = "&lt;$html&gt; not found";
136         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
137         if ($n_partial) {
138                 $code = 300;
139                 my $es = $n_partial == 1 ? '' : 'es';
140                 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
141                 $s .= "\n$n_partial partial match$es found:\n\n";
142                 my $cur_name = $cur->{name};
143                 foreach my $pair (@partial) {
144                         my ($ibx, $res) = @$pair;
145                         my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
146                         my $u = $ibx->base_url($env) or next;
147                         foreach my $m (@$res) {
148                                 my $p = PublicInbox::Hval->new_msgid($m);
149                                 my $r = $p->{href};
150                                 my $t = $p->as_html;
151                                 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
152                         }
153                 }
154         }
155         my $ext = ext_urls($ctx, $mid, $href, $html);
156         if ($ext ne '') {
157                 $s .= $ext;
158                 $code = 300;
159         }
160         $ctx->{-html_tip} = $s .= '</pre>';
161         $ctx->{-title_html} = $title;
162         $ctx->{-upfx} = '../';
163         PublicInbox::WwwStream->response($ctx, $code);
164 }
165
166 sub ext_urls {
167         my ($ctx, $mid, $href, $html) = @_;
168
169         # Fall back to external repos if configured
170         if (@EXT_URL && index($mid, '@') >= 0) {
171                 my $env = $ctx->{env};
172                 my $e = "\nPerhaps try an external site:\n\n";
173                 foreach my $url (@EXT_URL) {
174                         my $u = prurl($env, $url);
175                         my $r = sprintf($u, $href);
176                         my $t = sprintf($u, $html);
177                         $e .= qq{<a\nhref="$r">$t</a>\n};
178                 }
179                 return $e;
180         }
181         ''
182 }
183
184 sub exact {
185         my ($ctx, $found, $mid) = @_;
186         my $h = PublicInbox::Hval->new_msgid($mid);
187         my $href = $h->{href};
188         my $html = $h->as_html;
189         my $title = "&lt;$html&gt; found in ";
190         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
191         $ctx->{-title_html} = $title . $end;
192         $ctx->{-upfx} = '../';
193         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
194         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
195         $ctx->{-html_tip} = join('',
196                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
197                                 (map {
198                                         my $u = $_->base_url;
199                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
200                                 } @$found),
201                         $ext_urls, '</pre>');
202         PublicInbox::WwwStream->response($ctx, $code);
203 }
204
205 1;