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>
4 # Represents a public-inbox (which may have multiple mailing addresses)
5 package PublicInbox::Inbox;
9 use PublicInbox::MID qw(mid2path);
10 use PublicInbox::MIME;
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
22 $cleanup_timer = undef;
24 for my $ibx (values %$CLEANUP) {
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;
32 # refcnt is zero when tmp is out-of-scope
35 if (my $git = $ibx->{git}) {
36 $again = $git->cleanup;
38 if (my $gits = $ibx->{-repo_objs}) {
39 foreach my $git (@$gits) {
40 $again = 1 if $git->cleanup;
43 if ($have_devel_peek) {
44 $again ||= !!($ibx->{over} || $ibx->{mm} ||
47 $next->{"$ibx"} = $ibx if $again;
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;
58 require Devel::Peek; # needs separate package in Fedora
64 sub _cleanup_later ($) {
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;
73 my ($opts, $field, $default) = @_;
74 my $val = $opts->{$field};
76 $val = $val->[-1] if ref($val) eq 'ARRAY';
77 $val = undef if $val !~ /\A[0-9]+\z/;
79 $opts->{$field} = $val || $default;
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;
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;
97 warn "$mkey limiter=$val not understood\n";
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;
117 # allow any combination of multi-line or comma-delimited hide entries
119 if (defined(my $h = $opts->{hide})) {
120 foreach my $v (@$h) {
121 $hide->{$_} = 1 foreach (split(/\s*,\s*/, $v));
123 $opts->{-hide} = $hide;
128 sub version { $_[0]->{version} // 1 }
131 my ($self, $epoch) = @_;
132 $self->version == 2 or return;
133 $self->{"$epoch.git"} ||= eval {
134 my $git_dir = "$self->{inboxdir}/git/$epoch.git";
135 my $g = PublicInbox::Git->new($git_dir);
136 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
137 # no cleanup needed, we never cat-file off this, only clone
144 $self->{git} ||= eval {
145 my $git_dir = $self->{inboxdir};
146 $git_dir .= '/all.git' if $self->version == 2;
147 my $g = PublicInbox::Git->new($git_dir);
148 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
149 _cleanup_later($self);
156 return if $self->version < 2;
157 my $cur = $self->{-max_git_epoch};
158 my $changed = git($self)->alternates_changed;
159 if (!defined($cur) || $changed) {
160 $self->git->cleanup if $changed;
161 my $gits = "$self->{inboxdir}/git";
162 if (opendir my $dh, $gits) {
164 while (defined(my $git_dir = readdir($dh))) {
165 $git_dir =~ m!\A([0-9]+)\.git\z! or next;
166 $max = $1 if $1 > $max;
168 $cur = $self->{-max_git_epoch} = $max if $max >= 0;
170 warn "opendir $gits failed: $!\n";
178 $self->{mm} ||= eval {
179 require PublicInbox::Msgmap;
180 _cleanup_later($self);
181 my $dir = $self->{inboxdir};
182 if ($self->version >= 2) {
183 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
185 PublicInbox::Msgmap->new($dir);
191 my ($self, $over_only) = @_;
192 my $srch = $self->{search} ||= eval {
193 _cleanup_later($self);
194 require PublicInbox::Search;
195 PublicInbox::Search->new($self);
197 ($over_only || eval { $srch->xdb }) ? $srch : undef;
202 my $srch = search($self, 1) or return;
203 $self->{over} ||= eval {
204 my $over = $srch->{over_ro};
205 $over->dbh_new; # may fail
213 if (open(my $fh, '<', $path)) {
222 my $desc = $self->{description};
223 return $desc if defined $desc;
224 $desc = try_cat("$self->{inboxdir}/description");
227 $desc =~ s/\s+/ /smg;
228 $desc = '($INBOX_DIR/description missing)' if $desc eq '';
229 $self->{description} = $desc;
234 my $url = $self->{cloneurl};
236 $url = try_cat("$self->{inboxdir}/cloneurl");
237 my @url = split(/\s+/s, $url);
240 $self->{cloneurl} = \@url;
244 my ($self, $env) = @_; # env - PSGI env
246 my $url = PublicInbox::Git::host_prefix_url($env, '');
247 # for mount in Plack::Builder
248 $url .= '/' if $url !~ m!/\z!;
249 return $url .= $self->{name} . '/';
251 # called from a non-PSGI environment (e.g. NNTP/POP3):
252 $self->{-base_url} ||= do {
253 my $url = $self->{url}->[0] or return undef;
254 # expand protocol-relative URLs to HTTPS if we're
255 # not inside a web server
256 $url = "https:$url" if $url =~ m!\A//!;
257 $url .= '/' if $url !~ m!/\z!;
264 $self->{-nntp_url} ||= do {
265 # no checking for nntp_usable here, we can point entirely
266 # to non-local servers or users run by a different user
267 my $ns = $self->{nntpserver};
268 my $group = $self->{newsgroup};
271 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
273 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
274 $u .= '/' if $u !~ m!/\z!;
279 my $mirrors = $self->{nntpmirror};
282 foreach (@$mirrors) {
283 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
284 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
286 $u .= '/' if $u !~ m!/\z!;
290 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
293 # else: allow full URLs like:
294 # nntp://news.example.com/alt.example
298 # List::Util::uniq requires Perl 5.26+, maybe we
299 # can use it by 2030 or so
301 @urls = grep { !$seen{$_}++ } (@urls, @m);
309 my $ret = mm($self) && over($self);
310 $self->{mm} = $self->{over} = $self->{search} = undef;
314 sub msg_by_path ($$;$) {
315 my ($self, $path, $ref) = @_;
316 # TODO: allow other refs:
317 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
318 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
322 sub msg_by_smsg ($$;$) {
323 my ($self, $smsg, $ref) = @_;
325 # ghosts may have undef smsg (from SearchThread.node) or
327 return unless defined $smsg;
328 defined(my $blob = $smsg->{blob}) or return;
330 my $str = git($self)->cat_file($blob, $ref);
331 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
336 my ($self, $smsg, $ref) = @_;
337 if (my $s = msg_by_smsg($self, $smsg, $ref)) {
338 $smsg->{mime} = PublicInbox::MIME->new($s);
344 my ($self, $mid) = @_;
345 my $mm = mm($self) or return;
349 sub smsg_by_mid ($$) {
350 my ($self, $mid) = @_;
351 my $over = over($self) or return;
352 # favor the Message-ID we used for the NNTP article number:
353 defined(my $num = mid2num($self, $mid)) or return;
354 my $smsg = $over->get_art($num) or return;
355 PublicInbox::SearchMsg::psgi_cull($smsg);
358 sub msg_by_mid ($$;$) {
359 my ($self, $mid, $ref) = @_;
362 return msg_by_path($self, mid2path($mid), $ref);
364 my $smsg = smsg_by_mid($self, $mid);
365 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
369 my ($self, $opts, $after, $before) = @_;
370 over($self)->recent($opts, $after, $before);
375 if (my $over = over($self)) {
376 my $msgs = $over->recent({limit => 1});
377 if (my $smsg = $msgs->[0]) {
382 git($self)->modified; # v1