1 # Copyright (C) 2016-2021 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);
13 # returns true if further checking is required
14 sub cleanup_shards { $_[0]->{search} ? $_[0]->{search}->cleanup_shards : undef }
16 sub check_inodes ($) {
17 for (qw(over mm)) { $_[0]->{$_}->check_inodes if $_[0]->{$_} }
23 if (defined $ibx->{git}) {
24 $live = $ibx->isa(__PACKAGE__) ? $ibx->{git}->cleanup(1)
25 : $ibx->{git}->cleanup_if_unlinked;
26 delete($ibx->{git}) unless $live;
31 delete(@$ibx{qw(over mm description cloneurl
32 -imap_url -nntp_url)});
34 $ibx->cleanup_shards and $live = 1;
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;
142 my ($self, $req) = @_;
143 $self->{mm} //= eval {
144 require PublicInbox::Msgmap;
145 _cleanup_later($self);
146 my $dir = $self->{inboxdir};
147 if ($self->version >= 2) {
148 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
150 PublicInbox::Msgmap->new($dir);
152 } // ($req ? croak("E: $@") : undef);
157 $self->{search} // eval {
158 _cleanup_later($self);
159 require PublicInbox::Search;
160 my $srch = PublicInbox::Search->new($self);
161 (eval { $srch->xdb }) ? ($self->{search} = $srch) : undef;
165 # isrch is preferred for read-only interfaces if available since it
166 # reduces kernel cache and FD overhead
167 sub isrch { $_[0]->{isrch} // search($_[0]) }
170 my ($self, $req) = @_;
171 $self->{over} // eval {
172 my $srch = $self->{search} // do {
173 require PublicInbox::Search;
174 PublicInbox::Search->new($self);
176 _cleanup_later($self);
177 my $over = PublicInbox::Over->new("$srch->{xpfx}/over.sqlite3");
178 $over->dbh; # may fail
179 $self->{over} = $over;
180 } // ($req ? croak("E: $@") : undef);
185 open(my $fh, '<', $path) or return '';
191 my $desc = try_cat($_[0]);
195 $desc =~ s/\s+/ /smg;
196 $desc eq '' ? undef : $desc;
201 ($self->{description} //= cat_desc("$self->{inboxdir}/description")) //
202 '($INBOX_DIR/description missing)';
207 $self->{cloneurl} // do {
208 my $s = try_cat("$self->{inboxdir}/cloneurl");
209 my @urls = split(/\s+/s, $s);
210 scalar(@urls) ? ($self->{cloneurl} = \@urls) : undef;
215 my ($self, $env) = @_; # env - PSGI env
216 if ($env && $env->{'psgi.url_scheme'}) {
217 my $url = PublicInbox::Git::host_prefix_url($env, '');
218 # for mount in Plack::Builder
219 $url .= '/' if $url !~ m!/\z!;
220 return $url .= $self->{name} . '/';
222 # called from a non-PSGI environment (e.g. NNTP/POP3):
223 my $url = $self->{url} // return undef;
224 $url = $url->[0] // return undef;
225 # expand protocol-relative URLs to HTTPS if we're
226 # not inside a web server
227 substr($url, 0, 0, 'https:') if substr($url, 0, 2) eq '//';
228 $url .= '/' if substr($url, -1, 1) ne '/';
233 my ($self, $x, $ctx) = @_; # $x is "nntp" or "imap"
234 # no checking for nntp_usable here, we can point entirely
235 # to non-local servers or users run by a different user
236 my $ns = $self->{"${x}server"} //
237 $ctx->{www}->{pi_cfg}->get_all("publicinbox.${x}server");
238 my $group = $self->{newsgroup};
242 my $u = m!\A${x}s?://! ? $_ : "$x://$_";
243 $u .= '/' if $u !~ m!/\z!;
247 if (my $mirrors = $self->{"${x}mirror"}) {
250 my $u = m!\A${x}s?://! ? $_ : "$x://$_";
251 if ($u =~ m!\A${x}s?://[^/]+/?\z!) {
253 $u .= '/' if $u !~ m!/\z!;
255 } else { # n.b. IMAP uses "newsgroup"
257 publicinbox.$self->{name}.${x}mirror=$_ missing newsgroup name
261 # else: allow full URLs like:
262 # nntp://news.example.com/alt.example
266 # List::Util::uniq requires Perl 5.26+, maybe we
267 # can use it by 2030 or so
269 @urls = grep { !$seen{$_}++ } (@urls, @m);
274 # my ($self, $ctx) = @_;
275 sub nntp_url { $_[0]->{-nntp_url} //= _x_url($_[0], 'nntp', $_[1]) }
276 sub imap_url { $_[0]->{-imap_url} //= _x_url($_[0], 'imap', $_[1]) }
280 my $ret = mm($self) && over($self);
281 delete @$self{qw(mm over search)};
285 # for v1 users w/o SQLite only
286 sub msg_by_path ($$) {
287 my ($self, $path) = @_;
288 git($self)->cat_file('HEAD:'.$path);
291 sub msg_by_smsg ($$) {
292 my ($self, $smsg) = @_;
294 # ghosts may have undef smsg (from SearchThread.node) or
296 return unless defined $smsg;
297 defined(my $blob = $smsg->{blob}) or return;
299 $self->git->cat_file($blob);
303 my ($self, $smsg) = @_;
304 my $bref = msg_by_smsg($self, $smsg) or return;
305 my $eml = PublicInbox::Eml->new($bref);
306 $smsg->populate($eml) unless exists($smsg->{num}); # v1 w/o SQLite
310 sub smsg_by_mid ($$) {
311 my ($self, $mid) = @_;
312 my $over = $self->over or return;
314 if (my $mm = $self->mm) {
315 # favor the Message-ID we used for the NNTP article number:
316 defined(my $num = $mm->num_for($mid)) or return;
317 $smsg = $over->get_art($num);
320 $smsg = $over->next_by_mid($mid, \$id, \$prev);
322 $smsg ? PublicInbox::Smsg::psgi_cull($smsg) : undef;
325 sub msg_by_mid ($$) {
326 my ($self, $mid) = @_;
327 my $smsg = smsg_by_mid($self, $mid);
328 $smsg ? msg_by_smsg($self, $smsg) : msg_by_path($self, mid2path($mid));
332 my ($self, $opts, $after, $before) = @_;
333 $self->over->recent($opts, $after, $before);
338 if (my $over = $self->over) {
339 my $msgs = $over->recent({limit => 1});
340 if (my $smsg = $msgs->[0]) {
345 git($self)->modified; # v1
348 # returns prefix => pathname mapping
349 # (pathname is NOT public, but prefix is used for Xapian queries)
353 require PublicInbox::AltId;
354 my $altid = $self->{altid} or return {};
356 my $x = PublicInbox::AltId->new($self, $_);
357 "$x->{prefix}" => $x->{filename}
363 # $obj must respond to ->on_inbox_unlock, which takes Inbox ($self) as an arg
364 sub subscribe_unlock {
365 my ($self, $ident, $obj) = @_;
366 $self->{unlock_subs}->{$ident} = $obj;
369 sub unsubscribe_unlock {
370 my ($self, $ident) = @_;
371 delete $self->{unlock_subs}->{$ident};
378 my $subs = $self->{unlock_subs} or return;
379 for my $obj (values %$subs) {
380 eval { $obj->on_inbox_unlock($self) };
381 warn "E: $@ ($self->{inboxdir})\n" if $@;
385 sub uidvalidity { $_[0]->{uidvalidity} //= eval { $_[0]->mm->created_at } }
387 sub eidx_key { $_[0]->{newsgroup} // $_[0]->{inboxdir} }
389 sub mailboxid { # rfc 8474, 8620, 8621
390 my ($self, $imap_slice) = @_;
391 my $pfx = defined($imap_slice) ? $self->{newsgroup} : $self->{name};
392 utf8::encode($pfx); # to octets
393 # RFC 8620, 1.2 recommends not starting with dash or digits
394 # "A good solution to these issues is to prefix every id with a single
395 # alphabetical character."
396 'M'.join('', map { sprintf('%02x', ord) } split(//, $pfx)) .
397 (defined($imap_slice) ? sprintf('-%x', $imap_slice) : '') .
398 sprintf('-%x', uidvalidity($self) // 0)