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
21 my $CLEANUP = {}; # string(inbox) -> inbox
23 $cleanup_timer = undef;
24 for my $ibx (values %$CLEANUP) {
25 foreach my $f (qw(git mm search)) {
26 delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
32 sub _cleanup_later ($) {
34 return unless PublicInbox::EvCleanup::enabled();
35 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
36 $CLEANUP->{"$self"} = $self;
40 my ($opts, $field, $default) = @_;
41 my $val = $opts->{$field};
43 $val = $val->[-1] if ref($val) eq 'ARRAY';
44 $val = undef if $val !~ /\A\d+\z/;
46 $opts->{$field} = $val || $default;
49 sub _set_limiter ($$$) {
50 my ($self, $pi_config, $pfx) = @_;
51 my $lkey = "-${pfx}_limiter";
52 $self->{$lkey} ||= eval {
53 # full key is: publicinbox.$NAME.httpbackendmax
54 my $mkey = $pfx.'max';
55 my $val = $self->{$mkey} or return;
57 if ($val =~ /\A\d+\z/) {
58 require PublicInbox::Qspawn;
59 $lim = PublicInbox::Qspawn::Limiter->new($val);
60 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
61 $lim = $pi_config->limiter($val);
62 warn "$mkey limiter=$val not found\n" if !$lim;
64 warn "$mkey limiter=$val not understood\n";
71 my ($class, $opts) = @_;
72 my $v = $opts->{address} ||= 'public-inbox@example.com';
73 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
74 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
75 my $pi_config = delete $opts->{-pi_config};
76 _set_limiter($opts, $pi_config, 'httpbackend');
77 _set_uint($opts, 'feedmax', 25);
78 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
79 my $dir = $opts->{mainrepo};
80 if (defined $dir && -f "$dir/inbox.lock") {
87 my ($self, $part) = @_;
88 ($self->{version} || 1) == 2 or return;
89 $self->{"$part.git"} ||= eval {
90 my $git_dir = "$self->{mainrepo}/git/$part.git";
91 my $g = PublicInbox::Git->new($git_dir);
92 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
93 # no cleanup needed, we never cat-file off this, only clone
100 $self->{git} ||= eval {
101 my $git_dir = $self->{mainrepo};
102 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
103 my $g = PublicInbox::Git->new($git_dir);
104 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
105 _cleanup_later($self);
112 my $v = $self->{version};
113 return unless defined($v) && $v == 2;
114 my $part = $self->{-max_git_part};
115 my $changed = git($self)->alternates_changed;
116 if (!defined($part) || $changed) {
117 $self->git->cleanup if $changed;
118 my $gits = "$self->{mainrepo}/git";
119 if (opendir my $dh, $gits) {
121 while (defined(my $git_dir = readdir($dh))) {
122 $git_dir =~ m!\A(\d+)\.git\z! or next;
123 $max = $1 if $1 > $max;
125 $part = $self->{-max_git_part} = $max if $max >= 0;
127 warn "opendir $gits failed: $!\n";
135 $self->{mm} ||= eval {
136 require PublicInbox::Msgmap;
137 _cleanup_later($self);
138 my $dir = $self->{mainrepo};
139 if (($self->{version} || 1) >= 2) {
140 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
142 PublicInbox::Msgmap->new($dir);
149 $self->{search} ||= eval {
150 _cleanup_later($self);
151 PublicInbox::Search->new($self, $self->{altid});
158 if (open(my $fh, '<', $path)) {
167 my $desc = $self->{description};
168 return $desc if defined $desc;
169 $desc = try_cat("$self->{mainrepo}/description");
172 $desc =~ s/\s+/ /smg;
173 $desc = '($REPO_DIR/description missing)' if $desc eq '';
174 $self->{description} = $desc;
179 my $url = $self->{cloneurl};
181 $url = try_cat("$self->{mainrepo}/cloneurl");
182 my @url = split(/\s+/s, $url);
185 $self->{cloneurl} = \@url;
189 my ($self, $env) = @_;
190 if ($env) { # PSGI env
191 my $scheme = $env->{'psgi.url_scheme'};
192 my $host_port = $env->{HTTP_HOST} ||
193 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
194 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
195 # for mount in Plack::Builder
196 $url .= '/' if $url !~ m!/\z!;
197 $url .= $self->{name} . '/';
199 # either called from a non-PSGI environment (e.g. NNTP/POP3)
200 $self->{-base_url} ||= do {
201 my $url = $self->{url} or return undef;
202 # expand protocol-relative URLs to HTTPS if we're
203 # not inside a web server
204 $url = "https:$url" if $url =~ m!\A//!;
205 $url .= '/' if $url !~ m!/\z!;
213 $self->{-nntp_url} ||= do {
214 # no checking for nntp_usable here, we can point entirely
215 # to non-local servers or users run by a different user
216 my $ns = $self->{nntpserver};
217 my $group = $self->{newsgroup};
220 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
222 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
223 $u .= '/' if $u !~ m!/\z!;
228 my $mirrors = $self->{nntpmirror};
231 foreach (@$mirrors) {
232 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
233 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
235 $u .= '/' if $u !~ m!/\z!;
239 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
242 # else: allow full URLs like:
243 # nntp://news.example.com/alt.example
246 my %seen = map { $_ => 1 } @urls;
259 my $ret = $self->mm && $self->search;
260 $self->{mm} = $self->{search} = undef;
264 sub msg_by_path ($$;$) {
265 my ($self, $path, $ref) = @_;
266 # TODO: allow other refs:
267 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
268 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
272 sub msg_by_smsg ($$;$) {
273 my ($self, $smsg, $ref) = @_;
275 # ghosts may have undef smsg (from SearchThread.node) or
276 # no {blob} field (from each_smsg_by_mid)
277 return unless defined $smsg;
278 defined(my $blob = $smsg->{blob}) or return;
280 my $str = git($self)->cat_file($blob, $ref);
281 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
286 my ($self, $smsg, $ref) = @_;
287 if (my $s = msg_by_smsg($self, $smsg, $ref)) {
288 $smsg->{mime} = PublicInbox::MIME->new($s);
294 my ($self, $path) = @_;
295 git($self)->check('HEAD:'.$path);
299 my ($self, $mid) = @_;
300 my $mm = mm($self) or return;
304 sub smsg_by_mid ($$) {
305 my ($self, $mid) = @_;
306 my $srch = search($self) or return;
307 # favor the Message-ID we used for the NNTP article number:
308 my $num = mid2num($self, $mid);
309 defined $num ? $srch->lookup_article($num) : undef;
312 sub msg_by_mid ($$;$) {
313 my ($self, $mid, $ref) = @_;
314 my $srch = search($self) or
315 return msg_by_path($self, mid2path($mid), $ref);
316 my $smsg = smsg_by_mid($self, $mid);
317 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
321 my ($self, $opts, $after, $before) = @_;
322 search($self)->{over_ro}->recent($opts, $after, $before);