]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
extmsg: disable automatic inbox switching
[public-inbox.git] / lib / PublicInbox / ExtMsg.pm
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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;
12 use PublicInbox::MID qw/mid2path/;
13 use PublicInbox::WwwStream;
14
15 # TODO: user-configurable
16 our @EXT_URL = (
17         'http://mid.gmane.org/%s',
18         'https://lists.debian.org/msgid-search/%s',
19         # leading "//" denotes protocol-relative (http:// or https://)
20         '//mid.mail-archive.com/%s',
21         '//marc.info/?i=%s',
22 );
23
24 sub ext_msg {
25         my ($ctx) = @_;
26         my $pi_config = $ctx->{pi_config};
27         my $cur = $ctx->{-inbox};
28         my $mid = $ctx->{mid};
29         my $env = $ctx->{env};
30
31         eval { require PublicInbox::Search };
32         my $have_xap = $@ ? 0 : 1;
33         my (@nox, @ibx, @found);
34
35         $pi_config->each_inbox(sub {
36                 my ($other) = @_;
37                 return if $other->{name} eq $cur->{name} || !$other->base_url;
38
39                 my $s = $other->search;
40                 if (!$s) {
41                         push @nox, $other;
42                         return;
43                 }
44
45                 # try to find the URL with Xapian to avoid forking
46                 my $doc_id = eval { $s->find_unique_doc_id('mid', $mid) };
47                 if ($@) {
48                         # xapian not configured properly for this repo
49                         push @nox, $other;
50                         return;
51                 }
52
53                 # maybe we found it!
54                 if (defined $doc_id) {
55                         push @found, $other;
56                 } else {
57                         # no point in trying the fork fallback if we
58                         # know Xapian is up-to-date but missing the
59                         # message in the current repo
60                         push @ibx, $other;
61                 }
62         });
63
64         return exact($ctx, \@found, $mid) if @found;
65
66         # Xapian not installed or configured for some repos,
67         # do a full MID check (this is expensive...):
68         if (@nox) {
69                 my $path = mid2path($mid);
70                 foreach my $other (@nox) {
71                         my (undef, $type, undef) = $other->path_check($path);
72
73                         if ($type && $type eq 'blob') {
74                                 push @found, $other;
75                         }
76                 }
77         }
78         return exact($ctx, \@found, $mid) if @found;
79
80         # fall back to partial MID matching
81         my $n_partial = 0;
82         my @partial;
83
84         eval { require PublicInbox::Msgmap };
85         my $have_mm = $@ ? 0 : 1;
86         if ($have_mm) {
87                 my $tmp_mid = $mid;
88 again:
89                 unshift @ibx, $cur;
90                 foreach my $ibx (@ibx) {
91                         my $mm = $ibx->mm or next;
92                         if (my $res = $mm->mid_prefixes($tmp_mid)) {
93                                 $n_partial += scalar(@$res);
94                                 push @partial, [ $ibx, $res ];
95                         }
96                 }
97                 # fixup common errors:
98                 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
99                         goto again;
100                 }
101         }
102
103         my $code = 404;
104         my $h = PublicInbox::Hval->new_msgid($mid);
105         my $href = $h->as_href;
106         my $html = $h->as_html;
107         my $title = "Message-ID &lt;$html&gt; not found";
108         my $s = "<html><head><title>$title</title>" .
109                 "</head><body><pre><b>$title</b>\n";
110
111         if ($n_partial) {
112                 $code = 300;
113                 my $es = $n_partial == 1 ? '' : 'es';
114                 $s.= "\n$n_partial partial match$es found:\n\n";
115                 foreach my $pair (@partial) {
116                         my ($ibx, $res) = @$pair;
117                         my $u = $ibx->base_url or next;
118                         foreach my $m (@$res) {
119                                 my $p = PublicInbox::Hval->new_msgid($m);
120                                 my $r = $p->as_href;
121                                 my $t = $p->as_html;
122                                 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
123                         }
124                 }
125         }
126
127         # Fall back to external repos if configured
128         if (@EXT_URL && index($mid, '@') >= 0) {
129                 $code = 300;
130                 $s .= "\nPerhaps try an external site:\n\n";
131                 foreach my $url (@EXT_URL) {
132                         my $u = PublicInbox::Hval::prurl($env, $url);
133                         my $r = sprintf($u, $href);
134                         my $t = sprintf($u, $html);
135                         $s .= qq{<a\nhref="$r">$t</a>\n};
136                 }
137         }
138         $s .= '</pre></body></html>';
139
140         [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
141 }
142
143 sub ext_urls {
144         my ($ctx, $mid, $href, $html) = @_;
145
146         # Fall back to external repos if configured
147         if (@EXT_URL && index($mid, '@') >= 0) {
148                 my $env = $ctx->{env};
149                 my $e = "\nPerhaps try an external site:\n\n";
150                 foreach my $url (@EXT_URL) {
151                         my $u = PublicInbox::Hval::prurl($env, $url);
152                         my $r = sprintf($u, $href);
153                         my $t = sprintf($u, $html);
154                         $e .= qq{<a\nhref="$r">$t</a>\n};
155                 }
156                 return $e;
157         }
158         ''
159 }
160
161 sub exact {
162         my ($ctx, $found, $mid) = @_;
163         my $h = PublicInbox::Hval->new_msgid($mid);
164         my $href = $h->as_href;
165         my $html = $h->as_html;
166         my $title = "&lt;$html&gt; found in ";
167         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
168         $ctx->{-title_html} = $title . $end;
169         $ctx->{-upfx} = '../';
170         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
171         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
172         $ctx->{-html_tip} = join('',
173                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
174                                 (map {
175                                         my $u = $_->base_url;
176                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
177                                 } @$found),
178                         $ext_urls, '</pre>');
179         PublicInbox::WwwStream->response($ctx, $code);
180 }
181
182 1;