1 # Copyright (C) 2016-2018 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 Devel::Peek qw(SvREFCNT);
11 use PublicInbox::MIME;
12 use POSIX qw(strftime);
16 $cleanup_timer = 'disabled';
17 require PublicInbox::EvCleanup;
18 $cleanup_timer = undef; # OK if we get here
20 my $cleanup_broken = $@;
22 my $CLEANUP = {}; # string(inbox) -> inbox
24 $cleanup_timer = undef;
26 for my $ibx (values %$CLEANUP) {
28 foreach my $f (qw(mm search)) {
29 delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
31 my $expire = time - 60;
32 if (my $git = $ibx->{git}) {
33 $again = $git->cleanup($expire);
35 if (my $gits = $ibx->{-repo_objs}) {
36 foreach my $git (@$gits) {
37 $again = 1 if $git->cleanup($expire);
40 $again ||= !!($ibx->{mm} || $ibx->{search});
41 $next->{"$ibx"} = $ibx if $again;
46 sub _cleanup_later ($) {
48 return if $cleanup_broken;
49 return unless PublicInbox::EvCleanup::enabled();
50 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
51 $CLEANUP->{"$self"} = $self;
55 my ($opts, $field, $default) = @_;
56 my $val = $opts->{$field};
58 $val = $val->[-1] if ref($val) eq 'ARRAY';
59 $val = undef if $val !~ /\A\d+\z/;
61 $opts->{$field} = $val || $default;
64 sub _set_limiter ($$$) {
65 my ($self, $pi_config, $pfx) = @_;
66 my $lkey = "-${pfx}_limiter";
67 $self->{$lkey} ||= eval {
68 # full key is: publicinbox.$NAME.httpbackendmax
69 my $mkey = $pfx.'max';
70 my $val = $self->{$mkey} or return;
72 if ($val =~ /\A\d+\z/) {
73 require PublicInbox::Qspawn;
74 $lim = PublicInbox::Qspawn::Limiter->new($val);
75 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
76 $lim = $pi_config->limiter($val);
77 warn "$mkey limiter=$val not found\n" if !$lim;
79 warn "$mkey limiter=$val not understood\n";
86 my ($class, $opts) = @_;
87 my $v = $opts->{address} ||= 'public-inbox@example.com';
88 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
89 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
90 my $pi_config = delete $opts->{-pi_config};
91 _set_limiter($opts, $pi_config, 'httpbackend');
92 _set_uint($opts, 'feedmax', 25);
93 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
94 my $dir = $opts->{mainrepo};
95 if (defined $dir && -f "$dir/inbox.lock") {
99 # allow any combination of multi-line or comma-delimited hide entries
101 if (defined(my $h = $opts->{hide})) {
102 foreach my $v (@$h) {
103 $hide->{$_} = 1 foreach (split(/\s*,\s*/, $v));
105 $opts->{-hide} = $hide;
111 my ($self, $part) = @_;
112 ($self->{version} || 1) == 2 or return;
113 $self->{"$part.git"} ||= eval {
114 my $git_dir = "$self->{mainrepo}/git/$part.git";
115 my $g = PublicInbox::Git->new($git_dir);
116 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
117 # no cleanup needed, we never cat-file off this, only clone
124 $self->{git} ||= eval {
125 my $git_dir = $self->{mainrepo};
126 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
127 my $g = PublicInbox::Git->new($git_dir);
128 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
129 _cleanup_later($self);
136 my $v = $self->{version};
137 return unless defined($v) && $v == 2;
138 my $part = $self->{-max_git_part};
139 my $changed = git($self)->alternates_changed;
140 if (!defined($part) || $changed) {
141 $self->git->cleanup if $changed;
142 my $gits = "$self->{mainrepo}/git";
143 if (opendir my $dh, $gits) {
145 while (defined(my $git_dir = readdir($dh))) {
146 $git_dir =~ m!\A(\d+)\.git\z! or next;
147 $max = $1 if $1 > $max;
149 $part = $self->{-max_git_part} = $max if $max >= 0;
151 warn "opendir $gits failed: $!\n";
159 $self->{mm} ||= eval {
160 require PublicInbox::Msgmap;
161 _cleanup_later($self);
162 my $dir = $self->{mainrepo};
163 if (($self->{version} || 1) >= 2) {
164 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
166 PublicInbox::Msgmap->new($dir);
172 my ($self, $over_only) = @_;
173 my $srch = $self->{search} ||= eval {
174 _cleanup_later($self);
175 PublicInbox::Search->new($self, $self->{altid});
177 # TODO: lazily load Xapian
178 # return $srch if $over_only || eval { $srch->xdb };
183 my $srch = search($_[0], 1) or return;
190 if (open(my $fh, '<', $path)) {
199 my $desc = $self->{description};
200 return $desc if defined $desc;
201 $desc = try_cat("$self->{mainrepo}/description");
204 $desc =~ s/\s+/ /smg;
205 $desc = '($REPO_DIR/description missing)' if $desc eq '';
206 $self->{description} = $desc;
211 my $url = $self->{cloneurl};
213 $url = try_cat("$self->{mainrepo}/cloneurl");
214 my @url = split(/\s+/s, $url);
217 $self->{cloneurl} = \@url;
221 my ($self, $env) = @_;
223 if ($env && ($scheme = $env->{'psgi.url_scheme'})) { # PSGI env
224 my $host_port = $env->{HTTP_HOST} ||
225 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
226 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
227 # for mount in Plack::Builder
228 $url .= '/' if $url !~ m!/\z!;
229 $url .= $self->{name} . '/';
231 # either called from a non-PSGI environment (e.g. NNTP/POP3)
232 $self->{-base_url} ||= do {
233 my $url = $self->{url} or return undef;
234 # expand protocol-relative URLs to HTTPS if we're
235 # not inside a web server
236 $url = "https:$url" if $url =~ m!\A//!;
237 $url .= '/' if $url !~ m!/\z!;
245 $self->{-nntp_url} ||= do {
246 # no checking for nntp_usable here, we can point entirely
247 # to non-local servers or users run by a different user
248 my $ns = $self->{nntpserver};
249 my $group = $self->{newsgroup};
252 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
254 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
255 $u .= '/' if $u !~ m!/\z!;
260 my $mirrors = $self->{nntpmirror};
263 foreach (@$mirrors) {
264 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
265 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
267 $u .= '/' if $u !~ m!/\z!;
271 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
274 # else: allow full URLs like:
275 # nntp://news.example.com/alt.example
278 my %seen = map { $_ => 1 } @urls;
291 my $ret = mm($self) && over($self);
292 $self->{mm} = $self->{search} = undef;
296 sub msg_by_path ($$;$) {
297 my ($self, $path, $ref) = @_;
298 # TODO: allow other refs:
299 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
300 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
304 sub msg_by_smsg ($$;$) {
305 my ($self, $smsg, $ref) = @_;
307 # ghosts may have undef smsg (from SearchThread.node) or
309 return unless defined $smsg;
310 defined(my $blob = $smsg->{blob}) or return;
312 my $str = git($self)->cat_file($blob, $ref);
313 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
318 my ($self, $smsg, $ref) = @_;
319 if (my $s = msg_by_smsg($self, $smsg, $ref)) {
320 $smsg->{mime} = PublicInbox::MIME->new($s);
326 my ($self, $mid) = @_;
327 my $mm = mm($self) or return;
331 sub smsg_by_mid ($$) {
332 my ($self, $mid) = @_;
333 my $over = over($self) or return;
334 # favor the Message-ID we used for the NNTP article number:
335 defined(my $num = mid2num($self, $mid)) or return;
336 my $smsg = $over->get_art($num) or return;
337 PublicInbox::SearchMsg::psgi_cull($smsg);
340 sub msg_by_mid ($$;$) {
341 my ($self, $mid, $ref) = @_;
344 return msg_by_path($self, mid2path($mid), $ref);
346 my $smsg = smsg_by_mid($self, $mid);
347 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
351 my ($self, $opts, $after, $before) = @_;
352 over($self)->recent($opts, $after, $before);
357 if (my $over = over($self)) {
358 my $msgs = $over->recent({limit => 1});
359 if (my $smsg = $msgs->[0]) {
364 git($self)->modified; # v1