]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
admin: improve warnings and errors for missing modules
[public-inbox.git] / lib / PublicInbox / Inbox.pm
1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Represents a public-inbox (which may have multiple mailing addresses)
5 package PublicInbox::Inbox;
6 use strict;
7 use warnings;
8 use PublicInbox::Git;
9 use PublicInbox::MID qw(mid2path);
10 use Devel::Peek qw(SvREFCNT);
11 use PublicInbox::MIME;
12 use POSIX qw(strftime);
13
14 my $cleanup_timer;
15 eval {
16         $cleanup_timer = 'disabled';
17         require PublicInbox::EvCleanup;
18         $cleanup_timer = undef; # OK if we get here
19 };
20 my $cleanup_broken = $@;
21
22 my $CLEANUP = {}; # string(inbox) -> inbox
23 sub cleanup_task () {
24         $cleanup_timer = undef;
25         my $next = {};
26         for my $ibx (values %$CLEANUP) {
27                 my $again;
28                 foreach my $f (qw(mm search over)) {
29                         delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
30                 }
31                 my $expire = time - 60;
32                 if (my $git = $ibx->{git}) {
33                         $again = $git->cleanup($expire);
34                 }
35                 if (my $gits = $ibx->{-repo_objs}) {
36                         foreach my $git (@$gits) {
37                                 $again = 1 if $git->cleanup($expire);
38                         }
39                 }
40                 $again ||= !!($ibx->{over} || $ibx->{mm} || $ibx->{search});
41                 $next->{"$ibx"} = $ibx if $again;
42         }
43         $CLEANUP = $next;
44 }
45
46 sub _cleanup_later ($) {
47         my ($self) = @_;
48         return if $cleanup_broken;
49         return unless PublicInbox::EvCleanup::enabled();
50         $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
51         $CLEANUP->{"$self"} = $self;
52 }
53
54 sub _set_uint ($$$) {
55         my ($opts, $field, $default) = @_;
56         my $val = $opts->{$field};
57         if (defined $val) {
58                 $val = $val->[-1] if ref($val) eq 'ARRAY';
59                 $val = undef if $val !~ /\A\d+\z/;
60         }
61         $opts->{$field} = $val || $default;
62 }
63
64 sub _set_limiter ($$$) {
65         my ($self, $pi_config, $pfx) = @_;
66         my $lkey = "-${pfx}_limiter";
67         $self->{$lkey} ||= eval {
68                 # full key is: publicinbox.$NAME.httpbackendmax
69                 my $mkey = $pfx.'max';
70                 my $val = $self->{$mkey} or return;
71                 my $lim;
72                 if ($val =~ /\A\d+\z/) {
73                         require PublicInbox::Qspawn;
74                         $lim = PublicInbox::Qspawn::Limiter->new($val);
75                 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
76                         $lim = $pi_config->limiter($val);
77                         warn "$mkey limiter=$val not found\n" if !$lim;
78                 } else {
79                         warn "$mkey limiter=$val not understood\n";
80                 }
81                 $lim;
82         }
83 }
84
85 sub new {
86         my ($class, $opts) = @_;
87         my $v = $opts->{address} ||= 'public-inbox@example.com';
88         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
89         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
90         my $pi_config = delete $opts->{-pi_config};
91         _set_limiter($opts, $pi_config, 'httpbackend');
92         _set_uint($opts, 'feedmax', 25);
93         $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
94         my $dir = $opts->{mainrepo};
95         if (defined $dir && -f "$dir/inbox.lock") {
96                 $opts->{version} = 2;
97         }
98
99         # allow any combination of multi-line or comma-delimited hide entries
100         my $hide = {};
101         if (defined(my $h = $opts->{hide})) {
102                 foreach my $v (@$h) {
103                         $hide->{$_} = 1 foreach (split(/\s*,\s*/, $v));
104                 }
105                 $opts->{-hide} = $hide;
106         }
107         bless $opts, $class;
108 }
109
110 sub git_part {
111         my ($self, $part) = @_;
112         ($self->{version} || 1) == 2 or return;
113         $self->{"$part.git"} ||= eval {
114                 my $git_dir = "$self->{mainrepo}/git/$part.git";
115                 my $g = PublicInbox::Git->new($git_dir);
116                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
117                 # no cleanup needed, we never cat-file off this, only clone
118                 $g;
119         };
120 }
121
122 sub git {
123         my ($self) = @_;
124         $self->{git} ||= eval {
125                 my $git_dir = $self->{mainrepo};
126                 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
127                 my $g = PublicInbox::Git->new($git_dir);
128                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
129                 _cleanup_later($self);
130                 $g;
131         };
132 }
133
134 sub max_git_part {
135         my ($self) = @_;
136         my $v = $self->{version};
137         return unless defined($v) && $v == 2;
138         my $part = $self->{-max_git_part};
139         my $changed = git($self)->alternates_changed;
140         if (!defined($part) || $changed) {
141                 $self->git->cleanup if $changed;
142                 my $gits = "$self->{mainrepo}/git";
143                 if (opendir my $dh, $gits) {
144                         my $max = -1;
145                         while (defined(my $git_dir = readdir($dh))) {
146                                 $git_dir =~ m!\A(\d+)\.git\z! or next;
147                                 $max = $1 if $1 > $max;
148                         }
149                         $part = $self->{-max_git_part} = $max if $max >= 0;
150                 } else {
151                         warn "opendir $gits failed: $!\n";
152                 }
153         }
154         $part;
155 }
156
157 sub mm {
158         my ($self) = @_;
159         $self->{mm} ||= eval {
160                 require PublicInbox::Msgmap;
161                 _cleanup_later($self);
162                 my $dir = $self->{mainrepo};
163                 if (($self->{version} || 1) >= 2) {
164                         PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
165                 } else {
166                         PublicInbox::Msgmap->new($dir);
167                 }
168         };
169 }
170
171 sub search ($;$) {
172         my ($self, $over_only) = @_;
173         my $srch = $self->{search} ||= eval {
174                 _cleanup_later($self);
175                 require PublicInbox::Search;
176                 PublicInbox::Search->new($self, $self->{altid});
177         };
178         ($over_only || eval { $srch->xdb }) ? $srch : undef;
179 }
180
181 sub over ($) {
182         my ($self) = @_;
183         my $srch = search($self, 1) or return;
184         $self->{over} ||= eval {
185                 my $over = $srch->{over_ro};
186                 $over->dbh_new; # may fail
187                 $over;
188         }
189 }
190
191 sub try_cat {
192         my ($path) = @_;
193         my $rv = '';
194         if (open(my $fh, '<', $path)) {
195                 local $/;
196                 $rv = <$fh>;
197         }
198         $rv;
199 }
200
201 sub description {
202         my ($self) = @_;
203         my $desc = $self->{description};
204         return $desc if defined $desc;
205         $desc = try_cat("$self->{mainrepo}/description");
206         local $/ = "\n";
207         chomp $desc;
208         $desc =~ s/\s+/ /smg;
209         $desc = '($REPO_DIR/description missing)' if $desc eq '';
210         $self->{description} = $desc;
211 }
212
213 sub cloneurl {
214         my ($self) = @_;
215         my $url = $self->{cloneurl};
216         return $url if $url;
217         $url = try_cat("$self->{mainrepo}/cloneurl");
218         my @url = split(/\s+/s, $url);
219         local $/ = "\n";
220         chomp @url;
221         $self->{cloneurl} = \@url;
222 }
223
224 sub base_url {
225         my ($self, $env) = @_;
226         my $scheme;
227         if ($env && ($scheme = $env->{'psgi.url_scheme'})) { # PSGI env
228                 my $host_port = $env->{HTTP_HOST} ||
229                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
230                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
231                 # for mount in Plack::Builder
232                 $url .= '/' if $url !~ m!/\z!;
233                 $url .= $self->{name} . '/';
234         } else {
235                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
236                 $self->{-base_url} ||= do {
237                         my $url = $self->{url} or return undef;
238                         # expand protocol-relative URLs to HTTPS if we're
239                         # not inside a web server
240                         $url = "https:$url" if $url =~ m!\A//!;
241                         $url .= '/' if $url !~ m!/\z!;
242                         $url;
243                 };
244         }
245 }
246
247 sub nntp_url {
248         my ($self) = @_;
249         $self->{-nntp_url} ||= do {
250                 # no checking for nntp_usable here, we can point entirely
251                 # to non-local servers or users run by a different user
252                 my $ns = $self->{nntpserver};
253                 my $group = $self->{newsgroup};
254                 my @urls;
255                 if ($ns && $group) {
256                         $ns = [ $ns ] if ref($ns) ne 'ARRAY';
257                         @urls = map {
258                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
259                                 $u .= '/' if $u !~ m!/\z!;
260                                 $u.$group;
261                         } @$ns;
262                 }
263
264                 my $mirrors = $self->{nntpmirror};
265                 if ($mirrors) {
266                         my @m;
267                         foreach (@$mirrors) {
268                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
269                                 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
270                                         if ($group) {
271                                                 $u .= '/' if $u !~ m!/\z!;
272                                                 $u .= $group;
273                                         } else {
274                                                 warn
275 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
276                                         }
277                                 }
278                                 # else: allow full URLs like:
279                                 # nntp://news.example.com/alt.example
280                                 push @m, $u;
281                         }
282                         my %seen = map { $_ => 1 } @urls;
283                         foreach my $u (@m) {
284                                 next if $seen{$u};
285                                 $seen{$u} = 1;
286                                 push @urls, $u;
287                         }
288                 }
289                 \@urls;
290         };
291 }
292
293 sub nntp_usable {
294         my ($self) = @_;
295         my $ret = mm($self) && over($self);
296         $self->{mm} = $self->{over} = $self->{search} = undef;
297         $ret;
298 }
299
300 sub msg_by_path ($$;$) {
301         my ($self, $path, $ref) = @_;
302         # TODO: allow other refs:
303         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
304         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
305         $str;
306 }
307
308 sub msg_by_smsg ($$;$) {
309         my ($self, $smsg, $ref) = @_;
310
311         # ghosts may have undef smsg (from SearchThread.node) or
312         # no {blob} field
313         return unless defined $smsg;
314         defined(my $blob = $smsg->{blob}) or return;
315
316         my $str = git($self)->cat_file($blob, $ref);
317         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
318         $str;
319 }
320
321 sub smsg_mime {
322         my ($self, $smsg, $ref) = @_;
323         if (my $s = msg_by_smsg($self, $smsg, $ref)) {
324                 $smsg->{mime} = PublicInbox::MIME->new($s);
325                 return $smsg;
326         }
327 }
328
329 sub mid2num($$) {
330         my ($self, $mid) = @_;
331         my $mm = mm($self) or return;
332         $mm->num_for($mid);
333 }
334
335 sub smsg_by_mid ($$) {
336         my ($self, $mid) = @_;
337         my $over = over($self) or return;
338         # favor the Message-ID we used for the NNTP article number:
339         defined(my $num = mid2num($self, $mid)) or return;
340         my $smsg = $over->get_art($num) or return;
341         PublicInbox::SearchMsg::psgi_cull($smsg);
342 }
343
344 sub msg_by_mid ($$;$) {
345         my ($self, $mid, $ref) = @_;
346
347         over($self) or
348                 return msg_by_path($self, mid2path($mid), $ref);
349
350         my $smsg = smsg_by_mid($self, $mid);
351         $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
352 }
353
354 sub recent {
355         my ($self, $opts, $after, $before) = @_;
356         over($self)->recent($opts, $after, $before);
357 }
358
359 sub modified {
360         my ($self) = @_;
361         if (my $over = over($self)) {
362                 my $msgs = $over->recent({limit => 1});
363                 if (my $smsg = $msgs->[0]) {
364                         return $smsg->{ts};
365                 }
366                 return time;
367         }
368         git($self)->modified; # v1
369 }
370
371 1;