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;
25 for my $ibx (values %$CLEANUP) {
26 foreach my $f (qw(git mm search)) {
27 delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
33 sub _cleanup_later ($) {
35 return if $cleanup_broken;
36 return unless PublicInbox::EvCleanup::enabled();
37 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
38 $CLEANUP->{"$self"} = $self;
42 my ($opts, $field, $default) = @_;
43 my $val = $opts->{$field};
45 $val = $val->[-1] if ref($val) eq 'ARRAY';
46 $val = undef if $val !~ /\A\d+\z/;
48 $opts->{$field} = $val || $default;
51 sub _set_limiter ($$$) {
52 my ($self, $pi_config, $pfx) = @_;
53 my $lkey = "-${pfx}_limiter";
54 $self->{$lkey} ||= eval {
55 # full key is: publicinbox.$NAME.httpbackendmax
56 my $mkey = $pfx.'max';
57 my $val = $self->{$mkey} or return;
59 if ($val =~ /\A\d+\z/) {
60 require PublicInbox::Qspawn;
61 $lim = PublicInbox::Qspawn::Limiter->new($val);
62 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
63 $lim = $pi_config->limiter($val);
64 warn "$mkey limiter=$val not found\n" if !$lim;
66 warn "$mkey limiter=$val not understood\n";
73 my ($class, $opts) = @_;
74 my $v = $opts->{address} ||= 'public-inbox@example.com';
75 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
76 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
77 my $pi_config = delete $opts->{-pi_config};
78 _set_limiter($opts, $pi_config, 'httpbackend');
79 _set_uint($opts, 'feedmax', 25);
80 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
81 my $dir = $opts->{mainrepo};
82 if (defined $dir && -f "$dir/inbox.lock") {
89 my ($self, $part) = @_;
90 ($self->{version} || 1) == 2 or return;
91 $self->{"$part.git"} ||= eval {
92 my $git_dir = "$self->{mainrepo}/git/$part.git";
93 my $g = PublicInbox::Git->new($git_dir);
94 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
95 # no cleanup needed, we never cat-file off this, only clone
102 $self->{git} ||= eval {
103 my $git_dir = $self->{mainrepo};
104 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
105 my $g = PublicInbox::Git->new($git_dir);
106 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
107 _cleanup_later($self);
114 my $v = $self->{version};
115 return unless defined($v) && $v == 2;
116 my $part = $self->{-max_git_part};
117 my $changed = git($self)->alternates_changed;
118 if (!defined($part) || $changed) {
119 $self->git->cleanup if $changed;
120 my $gits = "$self->{mainrepo}/git";
121 if (opendir my $dh, $gits) {
123 while (defined(my $git_dir = readdir($dh))) {
124 $git_dir =~ m!\A(\d+)\.git\z! or next;
125 $max = $1 if $1 > $max;
127 $part = $self->{-max_git_part} = $max if $max >= 0;
129 warn "opendir $gits failed: $!\n";
137 $self->{mm} ||= eval {
138 require PublicInbox::Msgmap;
139 _cleanup_later($self);
140 my $dir = $self->{mainrepo};
141 if (($self->{version} || 1) >= 2) {
142 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
144 PublicInbox::Msgmap->new($dir);
151 $self->{search} ||= eval {
152 _cleanup_later($self);
153 PublicInbox::Search->new($self, $self->{altid});
160 if (open(my $fh, '<', $path)) {
169 my $desc = $self->{description};
170 return $desc if defined $desc;
171 $desc = try_cat("$self->{mainrepo}/description");
174 $desc =~ s/\s+/ /smg;
175 $desc = '($REPO_DIR/description missing)' if $desc eq '';
176 $self->{description} = $desc;
181 my $url = $self->{cloneurl};
183 $url = try_cat("$self->{mainrepo}/cloneurl");
184 my @url = split(/\s+/s, $url);
187 $self->{cloneurl} = \@url;
191 my ($self, $env) = @_;
192 if ($env) { # PSGI env
193 my $scheme = $env->{'psgi.url_scheme'};
194 my $host_port = $env->{HTTP_HOST} ||
195 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
196 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
197 # for mount in Plack::Builder
198 $url .= '/' if $url !~ m!/\z!;
199 $url .= $self->{name} . '/';
201 # either called from a non-PSGI environment (e.g. NNTP/POP3)
202 $self->{-base_url} ||= do {
203 my $url = $self->{url} or return undef;
204 # expand protocol-relative URLs to HTTPS if we're
205 # not inside a web server
206 $url = "https:$url" if $url =~ m!\A//!;
207 $url .= '/' if $url !~ m!/\z!;
215 $self->{-nntp_url} ||= do {
216 # no checking for nntp_usable here, we can point entirely
217 # to non-local servers or users run by a different user
218 my $ns = $self->{nntpserver};
219 my $group = $self->{newsgroup};
222 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
224 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
225 $u .= '/' if $u !~ m!/\z!;
230 my $mirrors = $self->{nntpmirror};
233 foreach (@$mirrors) {
234 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
235 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
237 $u .= '/' if $u !~ m!/\z!;
241 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
244 # else: allow full URLs like:
245 # nntp://news.example.com/alt.example
248 my %seen = map { $_ => 1 } @urls;
261 my $ret = $self->mm && $self->search;
262 $self->{mm} = $self->{search} = undef;
266 sub msg_by_path ($$;$) {
267 my ($self, $path, $ref) = @_;
268 # TODO: allow other refs:
269 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
270 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
274 sub msg_by_smsg ($$;$) {
275 my ($self, $smsg, $ref) = @_;
277 # ghosts may have undef smsg (from SearchThread.node) or
279 return unless defined $smsg;
280 defined(my $blob = $smsg->{blob}) or return;
282 my $str = git($self)->cat_file($blob, $ref);
283 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
288 my ($self, $smsg, $ref) = @_;
289 if (my $s = msg_by_smsg($self, $smsg, $ref)) {
290 $smsg->{mime} = PublicInbox::MIME->new($s);
296 my ($self, $mid) = @_;
297 my $mm = mm($self) or return;
301 sub smsg_by_mid ($$) {
302 my ($self, $mid) = @_;
303 my $srch = search($self) or return;
304 # favor the Message-ID we used for the NNTP article number:
305 defined(my $num = mid2num($self, $mid)) or return;
306 my $smsg = $srch->lookup_article($num) or return;
307 PublicInbox::SearchMsg::psgi_cull($smsg);
310 sub msg_by_mid ($$;$) {
311 my ($self, $mid, $ref) = @_;
312 my $srch = search($self) or
313 return msg_by_path($self, mid2path($mid), $ref);
314 my $smsg = smsg_by_mid($self, $mid);
315 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
319 my ($self, $opts, $after, $before) = @_;
320 search($self)->{over_ro}->recent($opts, $after, $before);