1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Represents a public-inbox (which may have multiple mailing addresses)
5 package PublicInbox::Inbox;
9 use PublicInbox::MID qw(mid2path);
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.
18 my $cleanup_avail = -1; # 0, or 1
20 my $CLEANUP = {}; # string(inbox) -> inbox
24 my $git = $self->{git} or return;
29 $cleanup_timer = undef;
31 for my $ibx (values %$CLEANUP) {
33 if ($have_devel_peek) {
34 foreach my $f (qw(search)) {
35 # we bump refcnt by assigning tmp, here:
36 my $tmp = $ibx->{$f} or next;
37 next if Devel::Peek::SvREFCNT($tmp) > 2;
39 # refcnt is zero when tmp is out-of-scope
43 if (my $gits = $ibx->{-repo_objs}) {
44 foreach my $git (@$gits) {
45 $again = 1 if $git->cleanup;
49 if ($have_devel_peek) {
50 $again ||= !!$ibx->{search};
52 $next->{"$ibx"} = $ibx if $again;
57 sub cleanup_possible () {
58 # no need to require DS, here, if it were enabled another
59 # module would've require'd it, already
60 eval { PublicInbox::DS::in_loop() } or return 0;
63 require Devel::Peek; # needs separate package in Fedora
69 sub _cleanup_later ($) {
71 $cleanup_avail = cleanup_possible() if $cleanup_avail < 0;
72 return if $cleanup_avail != 1;
73 $cleanup_timer //= PublicInbox::DS::later(\&cleanup_task);
74 $CLEANUP->{"$self"} = $self;
78 my ($opts, $field, $default) = @_;
79 my $val = $opts->{$field};
81 $val = $val->[-1] if ref($val) eq 'ARRAY';
82 $val = undef if $val !~ /\A[0-9]+\z/;
84 $opts->{$field} = $val || $default;
87 sub _set_limiter ($$$) {
88 my ($self, $pi_cfg, $pfx) = @_;
89 my $lkey = "-${pfx}_limiter";
90 $self->{$lkey} ||= do {
91 # full key is: publicinbox.$NAME.httpbackendmax
92 my $mkey = $pfx.'max';
93 my $val = $self->{$mkey} or return;
95 if ($val =~ /\A[0-9]+\z/) {
96 require PublicInbox::Qspawn;
97 $lim = PublicInbox::Qspawn::Limiter->new($val);
98 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
99 $lim = $pi_cfg->limiter($val);
100 warn "$mkey limiter=$val not found\n" if !$lim;
102 warn "$mkey limiter=$val not understood\n";
109 my ($class, $opts) = @_;
110 my $v = $opts->{address} ||= [ 'public-inbox@example.com' ];
111 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
112 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
113 my $pi_cfg = delete $opts->{-pi_cfg};
114 _set_limiter($opts, $pi_cfg, 'httpbackend');
115 _set_uint($opts, 'feedmax', 25);
116 $opts->{nntpserver} ||= $pi_cfg->{'publicinbox.nntpserver'};
117 my $dir = $opts->{inboxdir};
118 if (defined $dir && -f "$dir/inbox.lock") {
119 $opts->{version} = 2;
122 # allow any combination of multi-line or comma-delimited hide entries
124 if (defined(my $h = $opts->{hide})) {
125 foreach my $v (@$h) {
126 $hide->{$_} = 1 foreach (split(/\s*,\s*/, $v));
128 $opts->{-hide} = $hide;
133 sub version { $_[0]->{version} // 1 }
136 my ($self, $epoch) = @_; # v2-only, callers always supply $epoch
137 $self->{"$epoch.git"} ||= do {
138 my $git_dir = "$self->{inboxdir}/git/$epoch.git";
139 return unless -d $git_dir;
140 my $g = PublicInbox::Git->new($git_dir);
141 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
142 # no cleanup needed, we never cat-file off this, only clone
149 $self->{git} ||= do {
150 my $git_dir = $self->{inboxdir};
151 $git_dir .= '/all.git' if $self->version == 2;
152 my $g = PublicInbox::Git->new($git_dir);
153 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
154 _cleanup_later($self);
161 return if $self->version < 2;
162 my $cur = $self->{-max_git_epoch};
163 my $changed = git($self)->alternates_changed;
164 if (!defined($cur) || $changed) {
165 git_cleanup($self) if $changed;
166 my $gits = "$self->{inboxdir}/git";
167 if (opendir my $dh, $gits) {
169 while (defined(my $git_dir = readdir($dh))) {
170 $git_dir =~ m!\A([0-9]+)\.git\z! or next;
171 $max = $1 if $1 > $max;
173 $cur = $self->{-max_git_epoch} = $max if $max >= 0;
175 warn "opendir $gits failed: $!\n";
183 $self->{mm} ||= eval {
184 require PublicInbox::Msgmap;
185 my $dir = $self->{inboxdir};
186 if ($self->version >= 2) {
187 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
189 PublicInbox::Msgmap->new($dir);
196 my $srch = $self->{search} //= eval {
197 _cleanup_later($self);
198 require PublicInbox::Search;
199 PublicInbox::Search->new($self);
201 (eval { $srch->xdb }) ? $srch : undef;
204 # isrch is preferred for read-only interfaces if available since it
205 # reduces kernel cache and FD overhead
206 sub isrch { $_[0]->{isrch} // search($_[0]) }
209 $_[0]->{over} //= eval {
210 my $srch = $_[0]->{search} //= eval {
211 _cleanup_later($_[0]);
212 require PublicInbox::Search;
213 PublicInbox::Search->new($_[0]);
215 my $over = PublicInbox::Over->new("$srch->{xpfx}/over.sqlite3");
216 $over->dbh; # may fail
225 if (open(my $fh, '<', $path)) {
233 my $desc = try_cat($_[0]);
237 $desc =~ s/\s+/ /smg;
238 $desc eq '' ? undef : $desc;
243 ($self->{description} //= cat_desc("$self->{inboxdir}/description")) //
244 '($INBOX_DIR/description missing)';
249 ($self->{cloneurl} //= do {
250 my $s = try_cat("$self->{inboxdir}/cloneurl");
251 my @urls = split(/\s+/s, $s);
252 scalar(@urls) ? \@urls : undef
257 my ($self, $env) = @_; # env - PSGI env
259 my $url = PublicInbox::Git::host_prefix_url($env, '');
260 # for mount in Plack::Builder
261 $url .= '/' if $url !~ m!/\z!;
262 return $url .= $self->{name} . '/';
264 # called from a non-PSGI environment (e.g. NNTP/POP3):
265 $self->{-base_url} ||= do {
266 my $url = $self->{url}->[0] or return undef;
267 # expand protocol-relative URLs to HTTPS if we're
268 # not inside a web server
269 $url = "https:$url" if $url =~ m!\A//!;
270 $url .= '/' if $url !~ m!/\z!;
277 $self->{-nntp_url} ||= do {
278 # no checking for nntp_usable here, we can point entirely
279 # to non-local servers or users run by a different user
280 my $ns = $self->{nntpserver};
281 my $group = $self->{newsgroup};
284 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
286 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
287 $u .= '/' if $u !~ m!/\z!;
292 my $mirrors = $self->{nntpmirror};
295 foreach (@$mirrors) {
296 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
297 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
299 $u .= '/' if $u !~ m!/\z!;
303 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
306 # else: allow full URLs like:
307 # nntp://news.example.com/alt.example
311 # List::Util::uniq requires Perl 5.26+, maybe we
312 # can use it by 2030 or so
314 @urls = grep { !$seen{$_}++ } (@urls, @m);
322 my $ret = mm($self) && over($self);
323 $self->{mm} = $self->{over} = $self->{search} = undef;
327 # for v1 users w/o SQLite only
328 sub msg_by_path ($$) {
329 my ($self, $path) = @_;
330 git($self)->cat_file('HEAD:'.$path);
333 sub msg_by_smsg ($$) {
334 my ($self, $smsg) = @_;
336 # ghosts may have undef smsg (from SearchThread.node) or
338 return unless defined $smsg;
339 defined(my $blob = $smsg->{blob}) or return;
341 $self->git->cat_file($blob);
345 my ($self, $smsg) = @_;
346 my $bref = msg_by_smsg($self, $smsg) or return;
347 my $eml = PublicInbox::Eml->new($bref);
348 $smsg->populate($eml) unless exists($smsg->{num}); # v1 w/o SQLite
352 sub smsg_by_mid ($$) {
353 my ($self, $mid) = @_;
354 my $over = $self->over or return;
356 if (my $mm = $self->mm) {
357 # favor the Message-ID we used for the NNTP article number:
358 defined(my $num = $mm->num_for($mid)) or return;
359 $smsg = $over->get_art($num);
362 $smsg = $over->next_by_mid($mid, \$id, \$prev);
364 $smsg ? PublicInbox::Smsg::psgi_cull($smsg) : undef;
367 sub msg_by_mid ($$) {
368 my ($self, $mid) = @_;
369 my $smsg = smsg_by_mid($self, $mid);
370 $smsg ? msg_by_smsg($self, $smsg) : msg_by_path($self, mid2path($mid));
374 my ($self, $opts, $after, $before) = @_;
375 $self->over->recent($opts, $after, $before);
380 if (my $over = $self->over) {
381 my $msgs = $over->recent({limit => 1});
382 if (my $smsg = $msgs->[0]) {
387 git($self)->modified; # v1
390 # returns prefix => pathname mapping
391 # (pathname is NOT public, but prefix is used for Xapian queries)
394 $self->{-altid_map} //= eval {
395 require PublicInbox::AltId;
396 my $altid = $self->{altid} or return {};
398 my $x = PublicInbox::AltId->new($self, $_);
399 "$x->{prefix}" => $x->{filename}
405 # $obj must respond to ->on_inbox_unlock, which takes Inbox ($self) as an arg
406 sub subscribe_unlock {
407 my ($self, $ident, $obj) = @_;
408 $self->{unlock_subs}->{$ident} = $obj;
411 sub unsubscribe_unlock {
412 my ($self, $ident) = @_;
413 delete $self->{unlock_subs}->{$ident};
416 sub check_inodes ($) {
418 for (qw(over mm)) { # TODO: search
419 $self->{$_}->check_inodes if $self->{$_};
427 my $subs = $self->{unlock_subs} or return;
428 for (values %$subs) {
429 eval { $_->on_inbox_unlock($self) };
430 warn "E: $@ ($self->{inboxdir})\n" if $@;
434 sub uidvalidity { $_[0]->{uidvalidity} //= $_[0]->mm->created_at }
436 sub eidx_key { $_[0]->{newsgroup} // $_[0]->{inboxdir} }