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 package PublicInbox::ExtMsg;
6 use URI::Escape qw(uri_escape_utf8);
8 use PublicInbox::MID qw/mid2path/;
10 # TODO: user-configurable
12 'http://mid.gmane.org/%s',
13 'https://lists.debian.org/msgid-search/%s',
14 'http://mid.mail-archive.com/%s',
15 'http://marc.info/?i=%s',
20 my $pi_config = $ctx->{pi_config};
21 my $listname = $ctx->{listname};
22 my $mid = $ctx->{mid};
24 eval { require PublicInbox::Search };
25 my $have_xap = $@ ? 0 : 1;
28 foreach my $k (keys %$pi_config) {
29 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
31 next if $list eq $listname;
33 my $git_dir = $pi_config->{"publicinbox.$list.mainrepo"};
34 defined $git_dir or next;
36 my $url = $pi_config->{"publicinbox.$list.url"};
41 # try to find the URL with Xapian to avoid forking
45 $s = PublicInbox::Search->new($git_dir);
46 $s->find_unique_doc_id('mid', $mid);
49 # xapian not configured for this repo
52 return r302($url, $mid) if (defined $doc_id);
54 # no point in trying the fork fallback if we
55 # know Xapian is up-to-date but missing the
56 # message in the current repo
57 push @pfx, { srch => $s, url => $url };
62 # queue up for forking after we've tried Xapian on all of them
63 push @nox, { git_dir => $git_dir, url => $url };
66 # Xapian not installed or configured for some repos
67 my $path = "HEAD:" . mid2path($mid);
69 foreach my $n (@nox) {
70 my @cmd = ('git', "--git-dir=$n->{git_dir}", 'cat-file',
72 my $pid = open my $fh, '-|';
73 defined $pid or die "fork failed: $!\n";
76 open STDERR, '>', '/dev/null'; # ignore errors
77 exec @cmd or die "exec failed: $!\n";
79 my $type = eval { local $/; <$fh> };
81 if ($? == 0 && $type eq "blob\n") {
82 return r302($n->{url}, $mid);
87 # fall back to partial MID matching
91 my $cgi = $ctx->{cgi};
92 my $url = ref($cgi) eq 'CGI' ? $cgi->url(-base) . '/'
93 : $cgi->base->as_string;
95 unshift @pfx, { srch => $ctx->{srch}, url => $url };
96 foreach my $pfx (@pfx) {
97 my $srch = delete $pfx->{srch} or next;
99 # FIXME we may need a proper prefix trie here...
100 if (my $res = $srch->mid_prefix($mid)) {
101 $n_partial += scalar(@$res);
108 my $h = PublicInbox::Hval->new_msgid($mid, 1);
109 my $href = $h->as_href;
110 my $html = $h->as_html;
111 my $title = "Message-ID <$html> not found";
112 my $s = "<html><head><title>$title</title>" .
113 "</head><body><pre><b>$title</b>\n";
117 $s.= "\nPartial matches found:\n\n";
118 foreach my $pfx (@partial) {
120 foreach my $m (@{$pfx->{res}}) {
121 $h = PublicInbox::Hval->new($m);
124 $s .= qq{<a\nhref="$u/$href/">$u/$html/</a>\n};
129 # Fall back to external repos if configured
132 $s .= "\nPerhaps try an external site:\n\n";
133 foreach my $u (@EXT_URL) {
134 my $r = sprintf($u, $href);
135 my $t = sprintf($u, $html);
136 $s .= qq{<a\nhref="$r">$t</a>\n};
139 $s .= '</pre></body></html>';
141 [300, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
144 # Redirect to another public-inbox which is mapped by $pi_config
146 my ($url, $mid) = @_;
147 $url .= '/' . uri_escape_utf8($mid) . '/';
149 [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
150 [ "Redirecting to\n$url\n" ] ]