1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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 URI::Escape qw(uri_escape_utf8);
12 use PublicInbox::Hval;
13 use PublicInbox::MID qw/mid2path/;
15 # TODO: user-configurable
17 'http://mid.gmane.org/%s',
18 'https://lists.debian.org/msgid-search/%s',
19 'http://mid.mail-archive.com/%s',
20 'http://marc.info/?i=%s',
25 my $pi_config = $ctx->{pi_config};
26 my $listname = $ctx->{listname};
27 my $mid = $ctx->{mid};
29 eval { require PublicInbox::Search };
30 my $have_xap = $@ ? 0 : 1;
33 foreach my $k (keys %$pi_config) {
34 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
36 next if $list eq $listname;
38 my $git_dir = $pi_config->{"publicinbox.$list.mainrepo"};
39 defined $git_dir or next;
41 my $url = $pi_config->{"publicinbox.$list.url"};
46 # try to find the URL with Xapian to avoid forking
50 $s = PublicInbox::Search->new($git_dir);
51 $s->find_unique_doc_id('mid', $mid);
54 # xapian not configured for this repo
57 return r302($url, $mid) if (defined $doc_id);
59 # no point in trying the fork fallback if we
60 # know Xapian is up-to-date but missing the
61 # message in the current repo
62 push @pfx, { git_dir => $git_dir, url => $url };
67 # queue up for forking after we've tried Xapian on all of them
68 push @nox, { git_dir => $git_dir, url => $url };
71 # Xapian not installed or configured for some repos
72 my $path = "HEAD:" . mid2path($mid);
74 foreach my $n (@nox) {
75 # TODO: reuse existing PublicInbox::Git objects to save forks
76 my $git = PublicInbox::Git->new($n->{git_dir});
77 my (undef, $type, undef) = $git->check($path);
78 return r302($n->{url}, $mid) if ($type && $type eq 'blob');
81 # fall back to partial MID matching
85 eval { require PublicInbox::Msgmap };
86 my $have_mm = $@ ? 0 : 1;
90 my $cgi = $ctx->{cgi};
91 my $url = ref($cgi) eq 'CGI' ? $cgi->url(-base) . '/'
92 : $cgi->base->as_string;
94 unshift @pfx, { git_dir => $ctx->{git_dir}, url => $url };
95 foreach my $pfx (@pfx) {
96 my $git_dir = delete $pfx->{git_dir} or next;
97 my $mm = eval { PublicInbox::Msgmap->new($git_dir) };
100 if (my $res = $mm->mid_prefixes($tmp_mid)) {
101 $n_partial += scalar(@$res);
106 # fixup common errors:
107 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
113 my $h = PublicInbox::Hval->new_msgid($mid, 1);
114 my $href = $h->as_href;
115 my $html = $h->as_html;
116 my $title = "Message-ID <$html> not found";
117 my $s = "<html><head><title>$title</title>" .
118 "</head><body><pre><b>$title</b>\n";
122 my $es = $n_partial == 1 ? '' : 'es';
123 $s.= "\n$n_partial partial match$es found:\n\n";
124 foreach my $pfx (@partial) {
126 foreach my $m (@{$pfx->{res}}) {
127 my $p = PublicInbox::Hval->new($m);
130 $s .= qq{<a\nhref="$u/$r/">$u/$t/</a>\n};
135 # Fall back to external repos if configured
136 if (@EXT_URL && index($mid, '@') >= 0) {
138 $s .= "\nPerhaps try an external site:\n\n";
139 foreach my $u (@EXT_URL) {
140 my $r = sprintf($u, $href);
141 my $t = sprintf($u, $html);
142 $s .= qq{<a\nhref="$r">$t</a>\n};
145 $s .= '</pre></body></html>';
147 [300, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
150 # Redirect to another public-inbox which is mapped by $pi_config
152 my ($url, $mid) = @_;
153 $url .= '/' . uri_escape_utf8($mid) . '/';
155 [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
156 [ "Redirecting to\n$url\n" ] ]