1 # Copyright (C) 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);
11 use List::Util qw(max);
14 # returns true if further checking is required
15 sub check_inodes ($) {
16 for (qw(over mm)) { $_[0]->{$_}->check_inodes if $_[0]->{$_} }
22 if (defined $ibx->{git}) {
23 $live = $ibx->isa(__PACKAGE__) ? $ibx->{git}->cleanup(1)
24 : $ibx->{git}->cleanup_if_unlinked;
25 delete($ibx->{git}) unless $live;
30 delete(@$ibx{qw(over mm description cloneurl
31 -imap_url -nntp_url -pop3_url)});
33 my $srch = $ibx->{search} // $ibx;
34 delete @$srch{qw(xdb qp)};
35 for my $git (@{$ibx->{-repo_objs} // []}) {
36 $live = 1 if $git->cleanup(1);
38 PublicInbox::DS::add_uniq_timer($ibx+0, 5, \&do_cleanup, $ibx) if $live;
41 sub _cleanup_later ($) {
42 # no need to require DS, here, if it were enabled another
43 # module would've require'd it, already
44 eval { PublicInbox::DS::in_loop() } and
45 PublicInbox::DS::add_uniq_timer($_[0]+0, 30, \&do_cleanup, @_)
48 sub _set_limiter ($$$) {
49 my ($self, $pi_cfg, $pfx) = @_;
50 my $lkey = "-${pfx}_limiter";
51 $self->{$lkey} //= do {
52 # full key is: publicinbox.$NAME.httpbackendmax
53 my $mkey = $pfx.'max';
54 my $val = $self->{$mkey} or return;
56 if ($val =~ /\A[0-9]+\z/) {
57 require PublicInbox::Qspawn;
58 $lim = PublicInbox::Qspawn::Limiter->new($val);
59 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
60 $lim = $pi_cfg->limiter($val);
61 warn "$mkey limiter=$val not found\n" if !$lim;
63 warn "$mkey limiter=$val not understood\n";
70 my ($class, $opts) = @_;
71 my $v = $opts->{address} ||= [ 'public-inbox@example.com' ];
72 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
73 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
74 my $pi_cfg = delete $opts->{-pi_cfg};
75 _set_limiter($opts, $pi_cfg, 'httpbackend');
76 my $fmax = $opts->{feedmax};
77 if (defined($fmax) && $fmax =~ /\A[0-9]+\z/) {
78 $opts->{feedmax} += 0;
80 delete $opts->{feedmax};
82 # allow any combination of multi-line or comma-delimited hide entries
84 if (defined(my $h = $opts->{hide})) {
86 $hide->{$_} = 1 foreach (split(/\s*,\s*/, $v));
88 $opts->{-hide} = $hide;
94 $_[0]->{version} //= -f "$_[0]->{inboxdir}/inbox.lock" ? 2 : 1
98 my ($self, $epoch) = @_; # v2-only, callers always supply $epoch
99 $self->{"$epoch.git"} //= do {
100 my $git_dir = "$self->{inboxdir}/git/$epoch.git";
101 return unless -d $git_dir;
102 my $g = PublicInbox::Git->new($git_dir);
103 my $lim = $self->{-httpbackend_limiter};
104 $g->{-httpbackend_limiter} = $lim if $lim;
105 # caller must manually cleanup when done
112 $self->{git} //= do {
113 my $git_dir = $self->{inboxdir};
114 $git_dir .= '/all.git' if $self->version == 2;
115 my $g = PublicInbox::Git->new($git_dir);
116 my $lim = $self->{-httpbackend_limiter};
117 $g->{-httpbackend_limiter} = $lim if $lim;
118 _cleanup_later($self);
125 return if $self->version < 2;
126 my $cur = $self->{-max_git_epoch};
128 if (!defined($cur) || ($changed = git($self)->alternates_changed)) {
129 $self->{git}->cleanup if $changed;
130 my $gits = "$self->{inboxdir}/git";
131 if (opendir my $dh, $gits) {
133 substr($_, 0, -4) + 0; # drop ".git" suffix
134 } grep(/\A[0-9]+\.git\z/, readdir($dh))) // return;
135 $cur = $self->{-max_git_epoch} = $max;
143 my $d = $self->{inboxdir};
144 ($self->version >= 2 ? $d : "$d/public-inbox").'/msgmap.sqlite3';
148 my ($self, $req) = @_;
149 $self->{mm} //= eval {
150 require PublicInbox::Msgmap;
151 _cleanup_later($self);
152 PublicInbox::Msgmap->new_file($self);
153 } // ($req ? croak("E: $@") : undef);
158 $self->{search} // eval {
159 _cleanup_later($self);
160 require PublicInbox::Search;
161 my $srch = PublicInbox::Search->new($self);
162 (eval { $srch->xdb }) ? ($self->{search} = $srch) : undef;
166 # isrch is preferred for read-only interfaces if available since it
167 # reduces kernel cache and FD overhead
168 sub isrch { $_[0]->{isrch} // search($_[0]) }
171 my ($self, $req) = @_;
172 $self->{over} // eval {
173 my $srch = $self->{search} // do {
174 require PublicInbox::Search;
175 PublicInbox::Search->new($self);
177 _cleanup_later($self);
178 my $over = PublicInbox::Over->new("$srch->{xpfx}/over.sqlite3");
179 $over->dbh; # may fail
180 $self->{over} = $over;
181 } // ($req ? croak("E: $@") : undef);
186 open(my $fh, '<', $path) or return '';
192 my $desc = try_cat($_[0]);
196 $desc =~ s/\s+/ /smg;
197 $desc eq '' ? undef : $desc;
202 ($self->{description} //= cat_desc("$self->{inboxdir}/description")) //
203 '($INBOX_DIR/description missing)';
208 $self->{cloneurl} // do {
209 my $s = try_cat("$self->{inboxdir}/cloneurl");
210 my @urls = split(/\s+/s, $s);
211 scalar(@urls) ? ($self->{cloneurl} = \@urls) : undef;
216 my ($self, $env) = @_; # env - PSGI env
217 if ($env && $env->{'psgi.url_scheme'}) {
218 my $url = PublicInbox::Git::host_prefix_url($env, '');
219 # for mount in Plack::Builder
220 $url .= '/' if $url !~ m!/\z!;
221 return $url .= $self->{name} . '/';
223 # called from a non-PSGI environment (e.g. NNTP/POP3):
224 my $url = $self->{url} // return undef;
225 $url = $url->[0] // return undef;
226 # expand protocol-relative URLs to HTTPS if we're
227 # not inside a web server
228 substr($url, 0, 0, 'https:') if substr($url, 0, 2) eq '//';
229 $url .= '/' if substr($url, -1, 1) ne '/';
233 # imapserver, nntpserver configs are used here:
235 my ($self, $x, $ctx) = @_; # $x is "imap" or "nntp"
236 # no checking for nntp_usable here, we can point entirely
237 # to non-local servers or users run by a different user
238 my $ns = $self->{"${x}server"} //
239 $ctx->{www}->{pi_cfg}->get_all("publicinbox.${x}server");
240 my $group = $self->{newsgroup};
244 my $u = m!\A${x}s?://! ? $_ : "$x://$_";
245 $u .= '/' if $u !~ m!/\z!;
249 if (my $mirrors = $self->{"${x}mirror"}) {
252 my $u = m!\A${x}s?://! ? $_ : "$x://$_";
253 if ($u =~ m!\A${x}s?://[^/]+/?\z!) {
255 $u .= '/' if $u !~ m!/\z!;
257 } else { # n.b. IMAP and POP3 use "newsgroup"
259 publicinbox.$self->{name}.${x}mirror=$_ missing newsgroup name
263 # else: allow full URLs like:
264 # nntp://news.example.com/alt.example
268 # List::Util::uniq requires Perl 5.26+, maybe we
269 # can use it by 2030 or so
271 @urls = grep { !$seen{$_}++ } (@urls, @m);
276 # my ($self, $ctx) = @_;
277 sub imap_url { $_[0]->{-imap_url} //= _x_url($_[0], 'imap', $_[1]) }
278 sub nntp_url { $_[0]->{-nntp_url} //= _x_url($_[0], 'nntp', $_[1]) }
281 my ($self, $ctx) = @_;
282 $self->{-pop3_url} //= do {
283 my $ps = $self->{'pop3server'} //
284 $ctx->{www}->{pi_cfg}->get_all('publicinbox.pop3server');
285 my $group = $self->{newsgroup};
288 @urls = map { m!\Apop3?s?://! ? $_ : "pop3://$_" } @$ps;
289 if (my $mi = $self->{'pop3mirror'}) {
290 my @m = map { m!\Apop3?s?://! ? $_ : "pop3://$_" } @$mi;
291 my %seen; # List::Util::uniq requires Perl 5.26+
292 @urls = grep { !$seen{$_}++ } (@urls, @m);
295 for (@urls) { $n += s!/+\z!! }
297 W: pop3server and/or pop3mirror URLs should not end with trailing slash `/'
305 my $ret = mm($self) && over($self);
306 delete @$self{qw(mm over search)};
310 # for v1 users w/o SQLite only
311 sub msg_by_path ($$) {
312 my ($self, $path) = @_;
313 git($self)->cat_file('HEAD:'.$path);
316 sub msg_by_smsg ($$) {
317 my ($self, $smsg) = @_;
319 # ghosts may have undef smsg (from SearchThread.node) or
322 $self->git->cat_file($smsg->{blob} // return);
326 my ($self, $smsg) = @_;
327 my $bref = msg_by_smsg($self, $smsg) or return;
328 my $eml = PublicInbox::Eml->new($bref);
329 $smsg->{num} // $smsg->populate($eml);
333 sub smsg_by_mid ($$) {
334 my ($self, $mid) = @_;
335 my $over = $self->over or return;
337 if (my $mm = $self->mm) {
338 # favor the Message-ID we used for the NNTP article number:
339 my $num = $mm->num_for($mid) // return;
340 $smsg = $over->get_art($num);
343 $smsg = $over->next_by_mid($mid, \$id, \$prev);
345 $smsg ? PublicInbox::Smsg::psgi_cull($smsg) : undef;
348 sub msg_by_mid ($$) {
349 my ($self, $mid) = @_;
350 my $smsg = smsg_by_mid($self, $mid);
351 $smsg ? msg_by_smsg($self, $smsg) : msg_by_path($self, mid2path($mid));
355 my ($self, $opts, $after, $before) = @_;
356 $self->over->recent($opts, $after, $before);
361 if (my $over = $self->over) {
362 my $msgs = $over->recent({limit => 1});
363 if (my $smsg = $msgs->[0]) {
368 git($self)->modified; # v1
371 # returns prefix => pathname mapping
372 # (pathname is NOT public, but prefix is used for Xapian queries)
376 require PublicInbox::AltId;
377 my $altid = $self->{altid} or return {};
379 my $x = PublicInbox::AltId->new($self, $_);
380 "$x->{prefix}" => $x->{filename}
386 # $obj must respond to ->on_inbox_unlock, which takes Inbox ($self) as an arg
387 sub subscribe_unlock {
388 my ($self, $ident, $obj) = @_;
389 $self->{unlock_subs}->{$ident} = $obj;
392 sub unsubscribe_unlock {
393 my ($self, $ident) = @_;
394 delete $self->{unlock_subs}->{$ident};
401 my $subs = $self->{unlock_subs} or return;
402 for my $obj (values %$subs) {
403 eval { $obj->on_inbox_unlock($self) };
404 warn "E: $@ ($self->{inboxdir})\n" if $@;
408 sub uidvalidity { $_[0]->{uidvalidity} //= eval { $_[0]->mm->created_at } }
410 sub eidx_key { $_[0]->{newsgroup} // $_[0]->{inboxdir} }
412 # only used by NNTP, so we need ->mm anyways
413 sub art_min { $_[0]->{-art_min} //= eval { $_[0]->mm(1)->min } }
415 # used by IMAP, too, which tries to avoid ->mm (but ->{mm} is likely
416 # faster since it's smaller iff available)
418 $_[0]->{-art_max} //= eval { $_[0]->{mm}->max } //
419 eval { $_[0]->over(1)->max };
422 sub mailboxid { # rfc 8474, 8620, 8621
423 my ($self, $imap_slice) = @_;
424 my $pfx = defined($imap_slice) ? $self->{newsgroup} : $self->{name};
425 utf8::encode($pfx); # to octets
426 # RFC 8620, 1.2 recommends not starting with dash or digits
427 # "A good solution to these issues is to prefix every id with a single
428 # alphabetical character."
429 'M'.join('', map { sprintf('%02x', ord) } split(//, $pfx)) .
430 (defined($imap_slice) ? sprintf('-%x', $imap_slice) : '') .
431 sprintf('-%x', uidvalidity($self) // 0)
434 sub thing_type { 'public inbox' }