]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
config: introduce each_inbox for iteration
[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 URI::Escape qw(uri_escape_utf8);
12 use PublicInbox::Hval;
13 use PublicInbox::MID qw/mid2path/;
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         # TODO: multiple hits
65         return r302($found[0], $mid) if @found;
66
67         # Xapian not installed or configured for some repos,
68         # do a full MID check:
69         if (@nox) {
70                 my $path = mid2path($mid);
71                 foreach my $other (@nox) {
72                         my (undef, $type, undef) = $other->path_check($path);
73
74                         return r302($other, $mid) if $type && $type eq 'blob';
75                 }
76         }
77
78         # fall back to partial MID matching
79         my $n_partial = 0;
80         my @partial;
81
82         eval { require PublicInbox::Msgmap };
83         my $have_mm = $@ ? 0 : 1;
84         if ($have_mm) {
85                 my $tmp_mid = $mid;
86 again:
87                 unshift @ibx, $cur;
88                 foreach my $ibx (@ibx) {
89                         my $mm = $ibx->mm or next;
90                         if (my $res = $mm->mid_prefixes($tmp_mid)) {
91                                 $n_partial += scalar(@$res);
92                                 push @partial, [ $ibx, $res ];
93                         }
94                 }
95                 # fixup common errors:
96                 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
97                         goto again;
98                 }
99         }
100
101         my $code = 404;
102         my $h = PublicInbox::Hval->new_msgid($mid, 1);
103         my $href = $h->as_href;
104         my $html = $h->as_html;
105         my $title = "Message-ID &lt;$html&gt; not found";
106         my $s = "<html><head><title>$title</title>" .
107                 "</head><body><pre><b>$title</b>\n";
108
109         if ($n_partial) {
110                 $code = 300;
111                 my $es = $n_partial == 1 ? '' : 'es';
112                 $s.= "\n$n_partial partial match$es found:\n\n";
113                 foreach my $pair (@partial) {
114                         my ($ibx, $res) = @$pair;
115                         my $u = $ibx->base_url or next;
116                         foreach my $m (@$res) {
117                                 my $p = PublicInbox::Hval->new_msgid($m);
118                                 my $r = $p->as_href;
119                                 my $t = $p->as_html;
120                                 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
121                         }
122                 }
123         }
124
125         # Fall back to external repos if configured
126         if (@EXT_URL && index($mid, '@') >= 0) {
127                 $code = 300;
128                 $s .= "\nPerhaps try an external site:\n\n";
129                 foreach my $url (@EXT_URL) {
130                         my $u = PublicInbox::Hval::prurl($env, $url);
131                         my $r = sprintf($u, $href);
132                         my $t = sprintf($u, $html);
133                         $s .= qq{<a\nhref="$r">$t</a>\n};
134                 }
135         }
136         $s .= '</pre></body></html>';
137
138         [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
139 }
140
141 # Redirect to another public-inbox which is mapped by $pi_config
142 # TODO: prompt for inbox-switching
143 sub r302 {
144         my ($inbox, $mid) = @_;
145         my $url = $inbox->base_url . uri_escape_utf8($mid) . '/';
146         [ 302,
147           [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
148           [ "Redirecting to\n$url\n" ] ]
149 }
150
151 1;