1 # Copyright (C) 2015-2018 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;
12 use PublicInbox::MID qw/mid2path/;
13 use PublicInbox::WwwStream;
15 # TODO: user-configurable
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 search_partial ($$) {
32 my ($srch, $mid) = @_;
33 my $opt = { limit => PARTIAL_MAX, mset => 2 };
34 my @try = ("m:$mid*");
36 if ($chop =~ s/(\W+)(\w*)\z//) {
37 my ($delim, $word) = ($1, $2);
39 push @try, "m:$chop$delim";
40 push @try, "m:$chop$delim*";
43 push @try, "m:$chop*";
46 # break out long words individually to search for, because
47 # too many messages begin with "Pine.LNX." (or "alpine" or "nycvar")
48 if ($mid =~ /\w{9,}/) {
49 my @long = ($mid =~ m!(\w{3,})!g);
50 push(@try, join(' ', map { "m:$_" } @long));
52 # is the last element long enough to not trigger excessive
54 if (length($long[-1]) > 8) {
56 push(@try, join(' ', map { "m:$_" } @long));
60 foreach my $m (@try) {
61 my $mset = eval { $srch->query($m, $opt) };
62 if (ref($@) eq 'Search::Xapian::QueryParserError') {
63 # If Xapian can't handle the wildcard since it
64 # has too many results.
68 my $doc = $_->get_document;
69 PublicInbox::SearchMsg->load_doc($doc)->mid;
71 return \@mids if scalar(@mids);
77 my $cur = $ctx->{-inbox};
78 my $mid = $ctx->{mid};
80 eval { require PublicInbox::Msgmap };
81 my $have_mm = $@ ? 0 : 1;
84 $ctx->{www}->{pi_config}->each_inbox(sub {
86 return if $other->{name} eq $cur->{name} || !$other->base_url;
88 my $mm = $other->mm or return;
90 # try to find the URL with Msgmap to avoid forking
91 my $num = $mm->num_for($mid);
95 # no point in trying the fork fallback if we
96 # know Xapian is up-to-date but missing the
97 # message in the current repo
102 return exact($ctx, \@found, $mid) if @found;
104 # fall back to partial MID matching
107 my $srch = $cur->search;
108 my $mids = search_partial($srch, $mid) if $srch;
110 $n_partial = scalar(@$mids);
111 push @partial, [ $cur, $mids ];
114 # can't find a partial match in current inbox, try the others:
115 if (!$n_partial && length($mid) >= 16) {
116 foreach my $ibx (@ibx) {
117 $srch = $ibx->search or next;
118 $mids = search_partial($srch, $mid) or next;
119 $n_partial += scalar(@$mids);
120 push @partial, [ $ibx, $mids];
121 last if $n_partial >= PARTIAL_MAX;
126 my $h = PublicInbox::Hval->new_msgid($mid);
127 my $href = $h->{href};
128 my $html = $h->as_html;
129 my $title = "<$html> not found";
130 my $s = "<pre>Message-ID <$html>\nnot found\n";
133 my $es = $n_partial == 1 ? '' : 'es';
134 $n_partial .= '+' if ($n_partial == PARTIAL_MAX);
135 $s .= "\n$n_partial partial match$es found:\n\n";
136 my $cur_name = $cur->{name};
137 foreach my $pair (@partial) {
138 my ($ibx, $res) = @$pair;
139 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
140 my $u = $ibx->base_url($env) or next;
141 foreach my $m (@$res) {
142 my $p = PublicInbox::Hval->new_msgid($m);
145 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
149 my $ext = ext_urls($ctx, $mid, $href, $html);
154 $ctx->{-html_tip} = $s .= '</pre>';
155 $ctx->{-title_html} = $title;
156 $ctx->{-upfx} = '../';
157 PublicInbox::WwwStream->response($ctx, $code);
161 my ($ctx, $mid, $href, $html) = @_;
163 # Fall back to external repos if configured
164 if (@EXT_URL && index($mid, '@') >= 0) {
165 my $env = $ctx->{env};
166 my $e = "\nPerhaps try an external site:\n\n";
167 foreach my $url (@EXT_URL) {
168 my $u = PublicInbox::Hval::prurl($env, $url);
169 my $r = sprintf($u, $href);
170 my $t = sprintf($u, $html);
171 $e .= qq{<a\nhref="$r">$t</a>\n};
179 my ($ctx, $found, $mid) = @_;
180 my $h = PublicInbox::Hval->new_msgid($mid);
181 my $href = $h->{href};
182 my $html = $h->as_html;
183 my $title = "<$html> found in ";
184 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
185 $ctx->{-title_html} = $title . $end;
186 $ctx->{-upfx} = '../';
187 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
188 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
189 $ctx->{-html_tip} = join('',
190 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
192 my $u = $_->base_url;
193 qq(<a\nhref="$u$href/">$u$html/</a>\n)
195 $ext_urls, '</pre>');
196 PublicInbox::WwwStream->response($ctx, $code);