]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
extmsg: favor user-provided URL on partial matches
[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 PublicInbox::Hval;
12 use PublicInbox::MID qw/mid2path/;
13 use PublicInbox::WwwStream;
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 $cur = $ctx->{-inbox};
27         my $mid = $ctx->{mid};
28
29         eval { require PublicInbox::Search };
30         my $have_xap = $@ ? 0 : 1;
31         my (@nox, @ibx, @found);
32
33         $ctx->{www}->{pi_config}->each_inbox(sub {
34                 my ($other) = @_;
35                 return if $other->{name} eq $cur->{name} || !$other->base_url;
36
37                 my $s = $other->search;
38                 if (!$s) {
39                         push @nox, $other;
40                         return;
41                 }
42
43                 # try to find the URL with Xapian to avoid forking
44                 my $doc_id = eval { $s->find_unique_doc_id('mid', $mid) };
45                 if ($@) {
46                         # xapian not configured properly for this repo
47                         push @nox, $other;
48                         return;
49                 }
50
51                 # maybe we found it!
52                 if (defined $doc_id) {
53                         push @found, $other;
54                 } else {
55                         # no point in trying the fork fallback if we
56                         # know Xapian is up-to-date but missing the
57                         # message in the current repo
58                         push @ibx, $other;
59                 }
60         });
61
62         return exact($ctx, \@found, $mid) if @found;
63
64         # Xapian not installed or configured for some repos,
65         # do a full MID check (this is expensive...):
66         if (@nox) {
67                 my $path = mid2path($mid);
68                 foreach my $other (@nox) {
69                         my (undef, $type, undef) = $other->path_check($path);
70
71                         if ($type && $type eq 'blob') {
72                                 push @found, $other;
73                         }
74                 }
75         }
76         return exact($ctx, \@found, $mid) if @found;
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);
103         my $href = $h->as_href;
104         my $html = $h->as_html;
105         my $title = "&lt;$html&gt; not found";
106         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
107         if ($n_partial) {
108                 $code = 300;
109                 my $es = $n_partial == 1 ? '' : 'es';
110                 $s .= "\n$n_partial partial match$es found:\n\n";
111                 my $cur_name = $cur->{name};
112                 foreach my $pair (@partial) {
113                         my ($ibx, $res) = @$pair;
114                         my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
115                         my $u = $ibx->base_url($env) 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         my $ext = ext_urls($ctx, $mid, $href, $html);
125         if ($ext ne '') {
126                 $s .= $ext;
127                 $code = 300;
128         }
129         $ctx->{-html_tip} = $s .= '</pre>';
130         $ctx->{-title_html} = $title;
131         $ctx->{-upfx} = '../';
132         PublicInbox::WwwStream->response($ctx, $code);
133 }
134
135 sub ext_urls {
136         my ($ctx, $mid, $href, $html) = @_;
137
138         # Fall back to external repos if configured
139         if (@EXT_URL && index($mid, '@') >= 0) {
140                 my $env = $ctx->{env};
141                 my $e = "\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                         $e .= qq{<a\nhref="$r">$t</a>\n};
147                 }
148                 return $e;
149         }
150         ''
151 }
152
153 sub exact {
154         my ($ctx, $found, $mid) = @_;
155         my $h = PublicInbox::Hval->new_msgid($mid);
156         my $href = $h->as_href;
157         my $html = $h->as_html;
158         my $title = "&lt;$html&gt; found in ";
159         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
160         $ctx->{-title_html} = $title . $end;
161         $ctx->{-upfx} = '../';
162         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
163         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
164         $ctx->{-html_tip} = join('',
165                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
166                                 (map {
167                                         my $u = $_->base_url;
168                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
169                                 } @$found),
170                         $ext_urls, '</pre>');
171         PublicInbox::WwwStream->response($ctx, $code);
172 }
173
174 1;