]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
extmsg: remove expensive git path checks
[public-inbox.git] / lib / PublicInbox / ExtMsg.pm
1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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::Msgmap };
35         my $have_mm = $@ ? 0 : 1;
36         my (@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 $mm = $other->mm or return;
43
44                 # try to find the URL with Msgmap to avoid forking
45                 my $num = $mm->num_for($mid);
46                 if (defined $num) {
47                         push @found, $other;
48                 } else {
49                         # no point in trying the fork fallback if we
50                         # know Xapian is up-to-date but missing the
51                         # message in the current repo
52                         push @ibx, $other;
53                 }
54         });
55
56         return exact($ctx, \@found, $mid) if @found;
57
58         # fall back to partial MID matching
59         my $n_partial = 0;
60         my @partial;
61
62         if (my $mm = $cur->mm) {
63                 my $tmp_mid = $mid;
64                 my $res = $mm->mid_prefixes($tmp_mid, 100);
65                 if ($res && scalar(@$res)) {
66                         $n_partial += scalar(@$res);
67                         push @partial, [ $cur, $res ];
68                 # fixup common errors:
69                 } elsif ($tmp_mid =~ s,/[tTf],,) {
70                         $res = $mm->mid_prefixes($tmp_mid, 100);
71                         if ($res && scalar(@$res)) {
72                                 $n_partial += scalar(@$res);
73                                 push @partial, [ $cur, $res ];
74                         }
75                 }
76         }
77
78         # can't find a partial match in current inbox, try the others:
79         if (!$n_partial && length($mid) >= 16) {
80                 my $tmp_mid = $mid;
81 again:
82                 foreach my $ibx (@ibx) {
83                         my $mm = $ibx->mm or next;
84                         my $res = $mm->mid_prefixes($tmp_mid, 100);
85                         if ($res && scalar(@$res)) {
86                                 $n_partial += scalar(@$res);
87                                 push @partial, [ $ibx, $res ];
88                                 last if $n_partial >= 100;
89                         }
90                 }
91                 # fixup common errors:
92                 if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
93                         goto again;
94                 }
95         }
96
97         my $code = 404;
98         my $h = PublicInbox::Hval->new_msgid($mid);
99         my $href = $h->{href};
100         my $html = $h->as_html;
101         my $title = "&lt;$html&gt; not found";
102         my $s = "<pre>Message-ID &lt;$html&gt;\nnot found\n";
103         if ($n_partial) {
104                 $code = 300;
105                 my $es = $n_partial == 1 ? '' : 'es';
106                 $s .= "\n$n_partial partial match$es found:\n\n";
107                 my $cur_name = $cur->{name};
108                 foreach my $pair (@partial) {
109                         my ($ibx, $res) = @$pair;
110                         my $env = $ctx->{env} if $ibx->{name} eq $cur_name;
111                         my $u = $ibx->base_url($env) or next;
112                         foreach my $m (@$res) {
113                                 my $p = PublicInbox::Hval->new_msgid($m);
114                                 my $r = $p->{href};
115                                 my $t = $p->as_html;
116                                 $s .= qq{<a\nhref="$u$r/">$u$t/</a>\n};
117                         }
118                 }
119         }
120         my $ext = ext_urls($ctx, $mid, $href, $html);
121         if ($ext ne '') {
122                 $s .= $ext;
123                 $code = 300;
124         }
125         $ctx->{-html_tip} = $s .= '</pre>';
126         $ctx->{-title_html} = $title;
127         $ctx->{-upfx} = '../';
128         PublicInbox::WwwStream->response($ctx, $code);
129 }
130
131 sub ext_urls {
132         my ($ctx, $mid, $href, $html) = @_;
133
134         # Fall back to external repos if configured
135         if (@EXT_URL && index($mid, '@') >= 0) {
136                 my $env = $ctx->{env};
137                 my $e = "\nPerhaps try an external site:\n\n";
138                 foreach my $url (@EXT_URL) {
139                         my $u = PublicInbox::Hval::prurl($env, $url);
140                         my $r = sprintf($u, $href);
141                         my $t = sprintf($u, $html);
142                         $e .= qq{<a\nhref="$r">$t</a>\n};
143                 }
144                 return $e;
145         }
146         ''
147 }
148
149 sub exact {
150         my ($ctx, $found, $mid) = @_;
151         my $h = PublicInbox::Hval->new_msgid($mid);
152         my $href = $h->{href};
153         my $html = $h->as_html;
154         my $title = "&lt;$html&gt; found in ";
155         my $end = @$found == 1 ? 'another inbox' : 'other inboxes';
156         $ctx->{-title_html} = $title . $end;
157         $ctx->{-upfx} = '../';
158         my $ext_urls = ext_urls($ctx, $mid, $href, $html);
159         my $code = (@$found == 1 && $ext_urls eq '') ? 200 : 300;
160         $ctx->{-html_tip} = join('',
161                         "<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
162                                 (map {
163                                         my $u = $_->base_url;
164                                         qq(<a\nhref="$u$href/">$u$html/</a>\n)
165                                 } @$found),
166                         $ext_urls, '</pre>');
167         PublicInbox::WwwStream->response($ctx, $code);
168 }
169
170 1;