]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
rename most instances of "list" to "inbox"
[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 $inbox = $ctx->{inbox};
28         my $mid = $ctx->{mid};
29         my $cgi = $ctx->{cgi};
30         my $env = $cgi->{env};
31
32         eval { require PublicInbox::Search };
33         my $have_xap = $@ ? 0 : 1;
34         my (@nox, @pfx);
35
36         foreach my $k (keys %$pi_config) {
37                 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
38                 my $name = $1;
39                 next if $name eq $inbox;
40
41                 my $git_dir = $pi_config->{"publicinbox.$name.mainrepo"};
42                 defined $git_dir or next;
43
44                 my $url = $pi_config->{"publicinbox.$name.url"};
45                 defined $url or next;
46
47                 $url =~ s!/+\z!!;
48                 $url = PublicInbox::Hval::prurl($env, $url);
49
50                 # try to find the URL with Xapian to avoid forking
51                 if ($have_xap) {
52                         my $s;
53                         my $doc_id = eval {
54                                 $s = PublicInbox::Search->new($git_dir);
55                                 $s->find_unique_doc_id('mid', $mid);
56                         };
57                         if ($@) {
58                                 # xapian not configured for this repo
59                         } else {
60                                 # maybe we found it!
61                                 return r302($url, $mid) if (defined $doc_id);
62
63                                 # no point in trying the fork fallback if we
64                                 # know Xapian is up-to-date but missing the
65                                 # message in the current repo
66                                 push @pfx, { git_dir => $git_dir, url => $url };
67                                 next;
68                         }
69                 }
70
71                 # queue up for forking after we've tried Xapian on all of them
72                 push @nox, { git_dir => $git_dir, url => $url };
73         }
74
75         # Xapian not installed or configured for some repos
76         my $path = "HEAD:" . mid2path($mid);
77
78         foreach my $n (@nox) {
79                 # TODO: reuse existing PublicInbox::Git objects to save forks
80                 my $git = PublicInbox::Git->new($n->{git_dir});
81                 my (undef, $type, undef) = $git->check($path);
82                 return r302($n->{url}, $mid) if ($type && $type eq 'blob');
83         }
84
85         # fall back to partial MID matching
86         my $n_partial = 0;
87         my @partial;
88
89         eval { require PublicInbox::Msgmap };
90         my $have_mm = $@ ? 0 : 1;
91         my $base_url = $cgi->base->as_string;
92         if ($have_mm) {
93                 my $tmp_mid = $mid;
94                 my $url;
95 again:
96                 $url = $base_url . $inbox;
97                 unshift @pfx, { git_dir => $ctx->{git_dir}, url => $url };
98                 foreach my $pfx (@pfx) {
99                         my $git_dir = delete $pfx->{git_dir} or next;
100                         my $mm = eval { PublicInbox::Msgmap->new($git_dir) };
101
102                         $mm or next;
103                         if (my $res = $mm->mid_prefixes($tmp_mid)) {
104                                 $n_partial += scalar(@$res);
105                                 $pfx->{res} = $res;
106                                 push @partial, $pfx;
107                         }
108                 }
109                 # fixup common errors:
110                 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
111                         goto again;
112                 }
113         }
114
115         my $code = 404;
116         my $h = PublicInbox::Hval->new_msgid($mid, 1);
117         my $href = $h->as_href;
118         my $html = $h->as_html;
119         my $title = "Message-ID &lt;$html&gt; not found";
120         my $s = "<html><head><title>$title</title>" .
121                 "</head><body><pre><b>$title</b>\n";
122
123         if ($n_partial) {
124                 $code = 300;
125                 my $es = $n_partial == 1 ? '' : 'es';
126                 $s.= "\n$n_partial partial match$es found:\n\n";
127                 foreach my $pfx (@partial) {
128                         my $u = $pfx->{url};
129                         foreach my $m (@{$pfx->{res}}) {
130                                 my $p = PublicInbox::Hval->new_msgid($m);
131                                 my $r = $p->as_href;
132                                 my $t = $p->as_html;
133                                 $s .= qq{<a\nhref="$u/$r/">$u/$t/</a>\n};
134                         }
135                 }
136         }
137
138         # Fall back to external repos if configured
139         if (@EXT_URL && index($mid, '@') >= 0) {
140                 $code = 300;
141                 $s .= "\nPerhaps try an external site:\n\n";
142                 foreach my $url (@EXT_URL) {
143                         my $u = PublicInbox::Hval::prurl($env, $url);
144                         my $r = sprintf($u, $href);
145                         my $t = sprintf($u, $html);
146                         $s .= qq{<a\nhref="$r">$t</a>\n};
147                 }
148         }
149         $s .= '</pre></body></html>';
150
151         [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
152 }
153
154 # Redirect to another public-inbox which is mapped by $pi_config
155 sub r302 {
156         my ($url, $mid) = @_;
157         $url .= '/' . uri_escape_utf8($mid) . '/';
158         [ 302,
159           [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
160           [ "Redirecting to\n$url\n" ] ]
161 }
162
163 1;