]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
extmsg: use updated mail-archive.com URL
[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         # leading "//" denotes protocol-relative (http:// or https://)
18         '//marc.info/?i=%s',
19         '//www.mail-archive.com/search?l=mid&q=%s',
20         'http://mid.gmane.org/%s',
21         'https://lists.debian.org/msgid-search/%s',
22         '//docs.FreeBSD.org/cgi/mid.cgi?db=mid&id=%s',
23         'https://www.w3.org/mid/%s',
24         'http://www.postgresql.org/message-id/%s',
25         'https://lists.debconf.org/cgi-lurker/keyword.cgi?'.
26                 'doc-url=/lurker&format=en.html&query=id:%s'
27 );
28
29 sub ext_msg {
30         my ($ctx) = @_;
31         my $cur = $ctx->{-inbox};
32         my $mid = $ctx->{mid};
33
34         eval { require PublicInbox::Search };
35         my $have_xap = $@ ? 0 : 1;
36         my (@nox, @ibx, @found);
37
38         $ctx->{www}->{pi_config}->each_inbox(sub {
39                 my ($other) = @_;
40                 return if $other->{name} eq $cur->{name} || !$other->base_url;
41
42                 my $s = $other->search;
43                 if (!$s) {
44                         push @nox, $other;
45                         return;
46                 }
47
48                 # try to find the URL with Xapian to avoid forking
49                 my $doc_id = eval { $s->find_unique_doc_id('mid', $mid) };
50                 if ($@) {
51                         # xapian not configured properly for this repo
52                         push @nox, $other;
53                         return;
54                 }
55
56                 # maybe we found it!
57                 if (defined $doc_id) {
58                         push @found, $other;
59                 } else {
60                         # no point in trying the fork fallback if we
61                         # know Xapian is up-to-date but missing the
62                         # message in the current repo
63                         push @ibx, $other;
64                 }
65         });
66
67         return exact($ctx, \@found, $mid) if @found;
68
69         # Xapian not installed or configured for some repos,
70         # do a full MID check (this is expensive...):
71         if (@nox) {
72                 my $path = mid2path($mid);
73                 foreach my $other (@nox) {
74                         my (undef, $type, undef) = $other->path_check($path);
75
76                         if ($type && $type eq 'blob') {
77                                 push @found, $other;
78                         }
79                 }
80         }
81         return exact($ctx, \@found, $mid) if @found;
82
83         # fall back to partial MID matching
84         my $n_partial = 0;
85         my @partial;
86
87         eval { require PublicInbox::Msgmap };
88         my $have_mm = $@ ? 0 : 1;
89         if ($have_mm) {
90                 my $tmp_mid = $mid;
91 again:
92                 unshift @ibx, $cur;
93                 foreach my $ibx (@ibx) {
94                         my $mm = $ibx->mm or next;
95                         if (my $res = $mm->mid_prefixes($tmp_mid)) {
96                                 $n_partial += scalar(@$res);
97                                 push @partial, [ $ibx, $res ];
98                         }
99                 }
100                 # fixup common errors:
101                 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
102                         goto again;
103                 }
104         }
105
106         my $code = 404;
107         my $h = PublicInbox::Hval->new_msgid($mid);
108         my $href = $h->{href};
109         my $html = $h->as_html;
110         my $title = "&lt;$html&gt; not found";
111         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
112         if ($n_partial) {
113                 $code = 300;
114                 my $es = $n_partial == 1 ? '' : 'es';
115                 $s .= "\n$n_partial partial match$es found:\n\n";
116                 my $cur_name = $cur->{name};
117                 foreach my $pair (@partial) {
118                         my ($ibx, $res) = @$pair;
119                         my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
120                         my $u = $ibx->base_url($env) or next;
121                         foreach my $m (@$res) {
122                                 my $p = PublicInbox::Hval->new_msgid($m);
123                                 my $r = $p->{href};
124                                 my $t = $p->as_html;
125                                 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
126                         }
127                 }
128         }
129         my $ext = ext_urls($ctx, $mid, $href, $html);
130         if ($ext ne '') {
131                 $s .= $ext;
132                 $code = 300;
133         }
134         $ctx->{-html_tip} = $s .= '</pre>';
135         $ctx->{-title_html} = $title;
136         $ctx->{-upfx} = '../';
137         PublicInbox::WwwStream->response($ctx, $code);
138 }
139
140 sub ext_urls {
141         my ($ctx, $mid, $href, $html) = @_;
142
143         # Fall back to external repos if configured
144         if (@EXT_URL && index($mid, '@') >= 0) {
145                 my $env = $ctx->{env};
146                 my $e = "\nPerhaps try an external site:\n\n";
147                 foreach my $url (@EXT_URL) {
148                         my $u = PublicInbox::Hval::prurl($env, $url);
149                         my $r = sprintf($u, $href);
150                         my $t = sprintf($u, $html);
151                         $e .= qq{<a\nhref="$r">$t</a>\n};
152                 }
153                 return $e;
154         }
155         ''
156 }
157
158 sub exact {
159         my ($ctx, $found, $mid) = @_;
160         my $h = PublicInbox::Hval->new_msgid($mid);
161         my $href = $h->{href};
162         my $html = $h->as_html;
163         my $title = "&lt;$html&gt; found in ";
164         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
165         $ctx->{-title_html} = $title . $end;
166         $ctx->{-upfx} = '../';
167         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
168         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
169         $ctx->{-html_tip} = join('',
170                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
171                                 (map {
172                                         my $u = $_->base_url;
173                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
174                                 } @$found),
175                         $ext_urls, '</pre>');
176         PublicInbox::WwwStream->response($ctx, $code);
177 }
178
179 1;