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