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>
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
8 package PublicInbox::ExtMsg;
11 use PublicInbox::Hval qw(ascii_html prurl);
12 use PublicInbox::WwwStream;
13 our $MIN_PARTIAL_LEN = 16;
15 # TODO: user-configurable
16 our @EXT_URL = map { ascii_html($_) } (
17 # leading "//" denotes protocol-relative (http:// or https://)
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'
29 sub PARTIAL_MAX () { 100 }
31 sub mids_from_mset { # Search::retry_reopen callback
32 [ map { PublicInbox::SearchMsg::from_mitem($_)->mid } $_[0]->items ];
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*");
41 if ($chop =~ s/(\W+)(\w*)\z//) {
42 my ($delim, $word) = ($1, $2);
44 push @try, "m:$chop$delim";
45 push @try, "m:$chop$delim*";
48 push @try, "m:$chop*";
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));
57 # is the last element long enough to not trigger excessive
59 if (length($long[-1]) > 8) {
61 push(@try, join(' ', map { "m:$_" } @long));
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);
77 my ($other, $arg) = @_;
78 my ($cur, $mid, $ibxs, $found) = @$arg;
80 return if $other->{name} eq $cur->{name} || !$other->base_url;
82 my $mm = $other->mm or return;
84 # try to find the URL with Msgmap to avoid forking
85 my $num = $mm->num_for($mid);
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
98 my $cur = $ctx->{-inbox};
99 my $mid = $ctx->{mid};
101 eval { require PublicInbox::Msgmap };
104 my $arg = [ $cur, $mid, $ibxs, $found ];
106 $ctx->{www}->{pi_config}->each_inbox(\&ext_msg_i, $arg);
108 return exact($ctx, $found, $mid) if @$found;
110 # fall back to partial MID matching
113 my $srch = $cur->search;
114 my $mids = search_partial($srch, $mid) if $srch;
116 $n_partial = scalar(@$mids);
117 push @partial, [ $cur, $mids ];
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;
132 my $h = PublicInbox::Hval->new_msgid($mid);
133 my $href = $h->{href};
134 my $html = $h->as_html;
135 my $title = "<$html> not found";
136 my $s = "<pre>Message-ID <$html>\nnot found\n";
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);
151 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
155 my $ext = ext_urls($ctx, $mid, $href, $html);
160 $ctx->{-html_tip} = $s .= '</pre>';
161 $ctx->{-title_html} = $title;
162 $ctx->{-upfx} = '../';
163 PublicInbox::WwwStream->response($ctx, $code);
167 my ($ctx, $mid, $href, $html) = @_;
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};
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 = "<$html> 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: <$html>\nfound in $end:\n\n",
198 my $u = $_->base_url;
199 qq(<a\nhref="$u$href/">$u$html/</a>\n)
201 $ext_urls, '</pre>');
202 PublicInbox::WwwStream->response($ctx, $code);