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;
8 use PublicInbox::MID qw(mid2path);
10 use List::Util qw(max);
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;
77 sub _set_limiter ($$$) {
78 my ($self, $pi_cfg, $pfx) = @_;
79 my $lkey = "-${pfx}_limiter";
80 $self->{$lkey} ||= do {
81 # full key is: publicinbox.$NAME.httpbackendmax
82 my $mkey = $pfx.'max';
83 my $val = $self->{$mkey} or return;
85 if ($val =~ /\A[0-9]+\z/) {
86 require PublicInbox::Qspawn;
87 $lim = PublicInbox::Qspawn::Limiter->new($val);
88 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
89 $lim = $pi_cfg->limiter($val);
90 warn "$mkey limiter=$val not found\n" if !$lim;
92 warn "$mkey limiter=$val not understood\n";
99 my ($class, $opts) = @_;
100 my $v = $opts->{address} ||= [ 'public-inbox@example.com' ];
101 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
102 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
103 my $pi_cfg = delete $opts->{-pi_cfg};
104 _set_limiter($opts, $pi_cfg, 'httpbackend');
105 my $fmax = $opts->{feedmax};
106 if (defined($fmax) && $fmax =~ /\A[0-9]+\z/) {
107 $opts->{feedmax} += 0;
109 delete $opts->{feedmax};
111 $opts->{nntpserver} ||= $pi_cfg->{'publicinbox.nntpserver'};
113 # allow any combination of multi-line or comma-delimited hide entries
115 if (defined(my $h = $opts->{hide})) {
116 foreach my $v (@$h) {
117 $hide->{$_} = 1 foreach (split(/\s*,\s*/, $v));
119 $opts->{-hide} = $hide;
125 $_[0]->{version} //= -f "$_[0]->{inboxdir}/inbox.lock" ? 2 : 1
129 my ($self, $epoch) = @_; # v2-only, callers always supply $epoch
130 $self->{"$epoch.git"} ||= do {
131 my $git_dir = "$self->{inboxdir}/git/$epoch.git";
132 return unless -d $git_dir;
133 my $g = PublicInbox::Git->new($git_dir);
134 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
135 # caller must manually cleanup when done
142 $self->{git} ||= do {
143 my $git_dir = $self->{inboxdir};
144 $git_dir .= '/all.git' if $self->version == 2;
145 my $g = PublicInbox::Git->new($git_dir);
146 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
147 _cleanup_later($self);
154 return if $self->version < 2;
155 my $cur = $self->{-max_git_epoch};
157 if (!defined($cur) || ($changed = git($self)->alternates_changed)) {
158 git_cleanup($self) if $changed;
159 my $gits = "$self->{inboxdir}/git";
160 if (opendir my $dh, $gits) {
162 substr($_, 0, -4) + 0; # drop ".git" suffix
163 } grep(/\A[0-9]+\.git\z/, readdir($dh))) // return;
164 $cur = $self->{-max_git_epoch} = $max;
172 $self->{mm} ||= eval {
173 require PublicInbox::Msgmap;
174 my $dir = $self->{inboxdir};
175 if ($self->version >= 2) {
176 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
178 PublicInbox::Msgmap->new($dir);
185 my $srch = $self->{search} //= eval {
186 _cleanup_later($self);
187 require PublicInbox::Search;
188 PublicInbox::Search->new($self);
190 (eval { $srch->xdb }) ? $srch : undef;
193 # isrch is preferred for read-only interfaces if available since it
194 # reduces kernel cache and FD overhead
195 sub isrch { $_[0]->{isrch} // search($_[0]) }
198 $_[0]->{over} //= eval {
199 my $srch = $_[0]->{search} //= eval {
200 _cleanup_later($_[0]);
201 require PublicInbox::Search;
202 PublicInbox::Search->new($_[0]);
204 my $over = PublicInbox::Over->new("$srch->{xpfx}/over.sqlite3");
205 $over->dbh; # may fail
214 if (open(my $fh, '<', $path)) {
222 my $desc = try_cat($_[0]);
226 $desc =~ s/\s+/ /smg;
227 $desc eq '' ? undef : $desc;
232 ($self->{description} //= cat_desc("$self->{inboxdir}/description")) //
233 '($INBOX_DIR/description missing)';
238 ($self->{cloneurl} //= do {
239 my $s = try_cat("$self->{inboxdir}/cloneurl");
240 my @urls = split(/\s+/s, $s);
241 scalar(@urls) ? \@urls : undef
246 my ($self, $env) = @_; # env - PSGI env
248 my $url = PublicInbox::Git::host_prefix_url($env, '');
249 # for mount in Plack::Builder
250 $url .= '/' if $url !~ m!/\z!;
251 return $url .= $self->{name} . '/';
253 # called from a non-PSGI environment (e.g. NNTP/POP3):
254 $self->{-base_url} ||= do {
255 my $url = $self->{url}->[0] or return undef;
256 # expand protocol-relative URLs to HTTPS if we're
257 # not inside a web server
258 $url = "https:$url" if $url =~ m!\A//!;
259 $url .= '/' if $url !~ m!/\z!;
266 $self->{-nntp_url} ||= do {
267 # no checking for nntp_usable here, we can point entirely
268 # to non-local servers or users run by a different user
269 my $ns = $self->{nntpserver};
270 my $group = $self->{newsgroup};
273 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
275 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
276 $u .= '/' if $u !~ m!/\z!;
281 my $mirrors = $self->{nntpmirror};
284 foreach (@$mirrors) {
285 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
286 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
288 $u .= '/' if $u !~ m!/\z!;
292 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
295 # else: allow full URLs like:
296 # nntp://news.example.com/alt.example
300 # List::Util::uniq requires Perl 5.26+, maybe we
301 # can use it by 2030 or so
303 @urls = grep { !$seen{$_}++ } (@urls, @m);
311 my $ret = mm($self) && over($self);
312 $self->{mm} = $self->{over} = $self->{search} = undef;
316 # for v1 users w/o SQLite only
317 sub msg_by_path ($$) {
318 my ($self, $path) = @_;
319 git($self)->cat_file('HEAD:'.$path);
322 sub msg_by_smsg ($$) {
323 my ($self, $smsg) = @_;
325 # ghosts may have undef smsg (from SearchThread.node) or
327 return unless defined $smsg;
328 defined(my $blob = $smsg->{blob}) or return;
330 $self->git->cat_file($blob);
334 my ($self, $smsg) = @_;
335 my $bref = msg_by_smsg($self, $smsg) or return;
336 my $eml = PublicInbox::Eml->new($bref);
337 $smsg->populate($eml) unless exists($smsg->{num}); # v1 w/o SQLite
341 sub smsg_by_mid ($$) {
342 my ($self, $mid) = @_;
343 my $over = $self->over or return;
345 if (my $mm = $self->mm) {
346 # favor the Message-ID we used for the NNTP article number:
347 defined(my $num = $mm->num_for($mid)) or return;
348 $smsg = $over->get_art($num);
351 $smsg = $over->next_by_mid($mid, \$id, \$prev);
353 $smsg ? PublicInbox::Smsg::psgi_cull($smsg) : undef;
356 sub msg_by_mid ($$) {
357 my ($self, $mid) = @_;
358 my $smsg = smsg_by_mid($self, $mid);
359 $smsg ? msg_by_smsg($self, $smsg) : msg_by_path($self, mid2path($mid));
363 my ($self, $opts, $after, $before) = @_;
364 $self->over->recent($opts, $after, $before);
369 if (my $over = $self->over) {
370 my $msgs = $over->recent({limit => 1});
371 if (my $smsg = $msgs->[0]) {
376 git($self)->modified; # v1
379 # returns prefix => pathname mapping
380 # (pathname is NOT public, but prefix is used for Xapian queries)
383 $self->{-altid_map} //= eval {
384 require PublicInbox::AltId;
385 my $altid = $self->{altid} or return {};
387 my $x = PublicInbox::AltId->new($self, $_);
388 "$x->{prefix}" => $x->{filename}
394 # $obj must respond to ->on_inbox_unlock, which takes Inbox ($self) as an arg
395 sub subscribe_unlock {
396 my ($self, $ident, $obj) = @_;
397 $self->{unlock_subs}->{$ident} = $obj;
400 sub unsubscribe_unlock {
401 my ($self, $ident) = @_;
402 delete $self->{unlock_subs}->{$ident};
405 sub check_inodes ($) {
407 for (qw(over mm)) { # TODO: search
408 $self->{$_}->check_inodes if $self->{$_};
416 my $subs = $self->{unlock_subs} or return;
417 for my $obj (values %$subs) {
418 eval { $obj->on_inbox_unlock($self) };
419 warn "E: $@ ($self->{inboxdir})\n" if $@;
423 sub uidvalidity { $_[0]->{uidvalidity} //= eval { $_[0]->mm->created_at } }
425 sub eidx_key { $_[0]->{newsgroup} // $_[0]->{inboxdir} }