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 'http://mid.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'
31 my $cur = $ctx->{-inbox};
32 my $mid = $ctx->{mid};
34 eval { require PublicInbox::Msgmap };
35 my $have_mm = $@ ? 0 : 1;
38 $ctx->{www}->{pi_config}->each_inbox(sub {
40 return if $other->{name} eq $cur->{name} || !$other->base_url;
42 my $mm = $other->mm or return;
44 # try to find the URL with Msgmap to avoid forking
45 my $num = $mm->num_for($mid);
49 # no point in trying the fork fallback if we
50 # know Xapian is up-to-date but missing the
51 # message in the current repo
56 return exact($ctx, \@found, $mid) if @found;
58 # fall back to partial MID matching
62 if (my $mm = $cur->mm) {
64 my $res = $mm->mid_prefixes($tmp_mid, 100);
65 if ($res && scalar(@$res)) {
66 $n_partial += scalar(@$res);
67 push @partial, [ $cur, $res ];
68 # fixup common errors:
69 } elsif ($tmp_mid =~ s,/[tTf],,) {
70 $res = $mm->mid_prefixes($tmp_mid, 100);
71 if ($res && scalar(@$res)) {
72 $n_partial += scalar(@$res);
73 push @partial, [ $cur, $res ];
78 # can't find a partial match in current inbox, try the others:
79 if (!$n_partial && length($mid) >= 16) {
82 foreach my $ibx (@ibx) {
83 my $mm = $ibx->mm or next;
84 my $res = $mm->mid_prefixes($tmp_mid, 100);
85 if ($res && scalar(@$res)) {
86 $n_partial += scalar(@$res);
87 push @partial, [ $ibx, $res ];
88 last if $n_partial >= 100;
91 # fixup common errors:
92 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
98 my $h = PublicInbox::Hval->new_msgid($mid);
99 my $href = $h->{href};
100 my $html = $h->as_html;
101 my $title = "<$html> not found";
102 my $s = "<pre>Message-ID <$html>\nnot found\n";
105 my $es = $n_partial == 1 ? '' : 'es';
106 $s .= "\n$n_partial partial match$es found:\n\n";
107 my $cur_name = $cur->{name};
108 foreach my $pair (@partial) {
109 my ($ibx, $res) = @$pair;
110 my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
111 my $u = $ibx->base_url($env) or next;
112 foreach my $m (@$res) {
113 my $p = PublicInbox::Hval->new_msgid($m);
116 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
120 my $ext = ext_urls($ctx, $mid, $href, $html);
125 $ctx->{-html_tip} = $s .= '</pre>';
126 $ctx->{-title_html} = $title;
127 $ctx->{-upfx} = '../';
128 PublicInbox::WwwStream->response($ctx, $code);
132 my ($ctx, $mid, $href, $html) = @_;
134 # Fall back to external repos if configured
135 if (@EXT_URL && index($mid, '@') >= 0) {
136 my $env = $ctx->{env};
137 my $e = "\nPerhaps try an external site:\n\n";
138 foreach my $url (@EXT_URL) {
139 my $u = PublicInbox::Hval::prurl($env, $url);
140 my $r = sprintf($u, $href);
141 my $t = sprintf($u, $html);
142 $e .= qq{<a\nhref="$r">$t</a>\n};
150 my ($ctx, $found, $mid) = @_;
151 my $h = PublicInbox::Hval->new_msgid($mid);
152 my $href = $h->{href};
153 my $html = $h->as_html;
154 my $title = "<$html> found in ";
155 my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
156 $ctx->{-title_html} = $title . $end;
157 $ctx->{-upfx} = '../';
158 my $ext_urls = ext_urls($ctx, $mid, $href, $html);
159 my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
160 $ctx->{-html_tip} = join('',
161 "<pre>Message-ID: <$html>\nfound in $end:\n\n",
163 my $u = $_->base_url;
164 qq(<a\nhref="$u$href/">$u$html/</a>\n)
166 $ext_urls, '</pre>');
167 PublicInbox::WwwStream->response($ctx, $code);