]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
implement external Message-ID finder
[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 package PublicInbox::ExtMsg;
4 use strict;
5 use warnings;
6 use URI::Escape qw(uri_escape_utf8);
7 use PublicInbox::Hval;
8 use PublicInbox::MID qw/mid_compress mid2path/;
9
10 sub ext_msg {
11         my ($ctx) = @_;
12         my $pi_config = $ctx->{pi_config};
13         my $listname = $ctx->{listname};
14         my $mid = $ctx->{mid};
15         my $cmid = mid_compress($mid);
16
17         eval { require PublicInbox::Search };
18         my $have_xap = $@ ? 0 : 1;
19         my @nox;
20
21         foreach my $k (keys %$pi_config) {
22                 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
23                 my $list = $1;
24                 next if $list eq $listname;
25
26                 my $git_dir = $pi_config->{"publicinbox.$list.mainrepo"};
27                 defined $git_dir or next;
28
29                 my $url = $pi_config->{"publicinbox.$list.url"};
30                 defined $url or next;
31
32                 $url =~ s!/+\z!!;
33
34                 # try to find the URL with Xapian to avoid forking
35                 if ($have_xap) {
36                         my $doc_id = eval {
37                                 my $s = PublicInbox::Search->new($git_dir);
38                                 $s->find_unique_doc_id('mid', $cmid);
39                         };
40                         if ($@) {
41                                 # xapian not configured for this repo
42                         } else {
43                                 # maybe we found it!
44                                 return r302($url, $cmid) if (defined $doc_id);
45
46                                 # no point in trying the fork fallback if we
47                                 # know Xapian is up-to-date but missing the
48                                 # message in the current repo
49                                 next;
50                         }
51                 }
52
53                 # queue up for forking after we've tried Xapian on all of them
54                 push @nox, { git_dir => $git_dir, url => $url };
55         }
56
57         # Xapian not installed or configured for some repos
58         my $path = "HEAD:" . mid2path($cmid);
59
60         foreach my $n (@nox) {
61                 my @cmd = ('git', "--git-dir=$n->{git_dir}", 'cat-file',
62                            '-t', $path);
63                 my $pid = open my $fh, '-|';
64                 defined $pid or die "fork failed: $!\n";
65
66                 if ($pid == 0) {
67                         open STDERR, '>', '/dev/null'; # ignore errors
68                         exec @cmd or die "exec failed: $!\n";
69                 } else {
70                         my $type = eval { local $/; <$fh> };
71                         close $fh;
72                         if ($? == 0 && $type eq "blob\n") {
73                                 return r302($n->{url}, $cmid);
74                         }
75                 }
76         }
77
78         # Fall back to external repos
79
80         [404, ['Content-Type'=>'text/plain'], ['Not found']];
81 }
82
83 # Redirect to another public-inbox which is mapped by $pi_config
84 sub r302 {
85         my ($url, $mid) = @_;
86         $url .= '/' . uri_escape_utf8($mid) . '/';
87         [ 302,
88           [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
89           [ "Redirecting to\n$url\n" ] ]
90 }
91
92 1;