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;
15 $cleanup_timer = 'disabled';
16 require PublicInbox::EvCleanup;
17 $cleanup_timer = undef; # OK if we get here
20 my $CLEANUP = {}; # string(inbox) -> inbox
22 $cleanup_timer = undef;
23 for my $ibx (values %$CLEANUP) {
24 foreach my $f (qw(git mm search)) {
25 delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
31 sub _cleanup_later ($) {
33 return unless PublicInbox::EvCleanup::enabled();
34 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
35 $CLEANUP->{"$self"} = $self;
39 my ($opts, $field, $default) = @_;
40 my $val = $opts->{$field};
42 $val = $val->[-1] if ref($val) eq 'ARRAY';
43 $val = undef if $val !~ /\A\d+\z/;
45 $opts->{$field} = $val || $default;
48 sub _set_limiter ($$$) {
49 my ($self, $pi_config, $pfx) = @_;
50 my $lkey = "-${pfx}_limiter";
51 $self->{$lkey} ||= eval {
52 # full key is: publicinbox.$NAME.httpbackendmax
53 my $mkey = $pfx.'max';
54 my $val = $self->{$mkey} or return;
56 if ($val =~ /\A\d+\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_config->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_config = delete $opts->{-pi_config};
75 _set_limiter($opts, $pi_config, 'httpbackend');
76 _set_uint($opts, 'feedmax', 25);
77 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
78 my $dir = $opts->{mainrepo};
79 if (defined $dir && -f "$dir/inbox.lock") {
86 my ($self, $part) = @_;
87 ($self->{version} || 1) == 2 or return;
88 $self->{"$part.git"} ||= eval {
89 my $git_dir = "$self->{mainrepo}/git/$part.git";
90 my $g = PublicInbox::Git->new($git_dir);
91 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
92 # no cleanup needed, we never cat-file off this, only clone
99 $self->{git} ||= eval {
100 my $git_dir = $self->{mainrepo};
101 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
102 my $g = PublicInbox::Git->new($git_dir);
103 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
104 _cleanup_later($self);
111 my $v = $self->{version};
112 return unless defined($v) && $v == 2;
113 my $part = $self->{-max_git_part};
114 my $changed = git($self)->alternates_changed;
115 if (!defined($part) || $changed) {
116 $self->git->cleanup if $changed;
117 my $gits = "$self->{mainrepo}/git";
118 if (opendir my $dh, $gits) {
120 while (defined(my $git_dir = readdir($dh))) {
121 $git_dir =~ m!\A(\d+)\.git\z! or next;
122 $max = $1 if $1 > $max;
124 $part = $self->{-max_git_part} = $max if $max >= 0;
126 warn "opendir $gits failed: $!\n";
134 $self->{mm} ||= eval {
135 _cleanup_later($self);
136 my $dir = $self->{mainrepo};
137 if (($self->{version} || 1) >= 2) {
138 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
140 PublicInbox::Msgmap->new($dir);
147 $self->{search} ||= eval {
148 _cleanup_later($self);
149 PublicInbox::Search->new($self, $self->{altid});
156 if (open(my $fh, '<', $path)) {
165 my $desc = $self->{description};
166 return $desc if defined $desc;
167 $desc = try_cat("$self->{mainrepo}/description");
170 $desc =~ s/\s+/ /smg;
171 $desc = '($REPO_DIR/description missing)' if $desc eq '';
172 $self->{description} = $desc;
177 my $url = $self->{cloneurl};
179 $url = try_cat("$self->{mainrepo}/cloneurl");
180 my @url = split(/\s+/s, $url);
183 $self->{cloneurl} = \@url;
187 my ($self, $env) = @_;
188 if ($env) { # PSGI env
189 my $scheme = $env->{'psgi.url_scheme'};
190 my $host_port = $env->{HTTP_HOST} ||
191 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
192 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
193 # for mount in Plack::Builder
194 $url .= '/' if $url !~ m!/\z!;
195 $url .= $self->{name} . '/';
197 # either called from a non-PSGI environment (e.g. NNTP/POP3)
198 $self->{-base_url} ||= do {
199 my $url = $self->{url} or return undef;
200 # expand protocol-relative URLs to HTTPS if we're
201 # not inside a web server
202 $url = "https:$url" if $url =~ m!\A//!;
203 $url .= '/' if $url !~ m!/\z!;
211 $self->{-nntp_url} ||= do {
212 # no checking for nntp_usable here, we can point entirely
213 # to non-local servers or users run by a different user
214 my $ns = $self->{nntpserver};
215 my $group = $self->{newsgroup};
218 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
220 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
221 $u .= '/' if $u !~ m!/\z!;
226 my $mirrors = $self->{nntpmirror};
229 foreach (@$mirrors) {
230 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
231 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
233 $u .= '/' if $u !~ m!/\z!;
237 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
240 # else: allow full URLs like:
241 # nntp://news.example.com/alt.example
244 my %seen = map { $_ => 1 } @urls;
257 my $ret = $self->mm && $self->search;
258 $self->{mm} = $self->{search} = undef;
262 sub msg_by_path ($$;$) {
263 my ($self, $path, $ref) = @_;
264 # TODO: allow other refs:
265 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
266 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
270 sub msg_by_smsg ($$;$) {
271 my ($self, $smsg, $ref) = @_;
273 # ghosts may have undef smsg (from SearchThread.node) or
274 # no {blob} field (from each_smsg_by_mid)
275 return unless defined $smsg;
276 defined(my $blob = $smsg->{blob}) or return;
278 my $str = git($self)->cat_file($blob, $ref);
279 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
284 my ($self, $smsg, $ref) = @_;
285 if (my $s = msg_by_smsg($self, $smsg, $ref)) {
286 $smsg->{mime} = PublicInbox::MIME->new($s);
292 my ($self, $path) = @_;
293 git($self)->check('HEAD:'.$path);
296 sub smsg_by_mid ($$) {
297 my ($self, $mid) = @_;
298 my $srch = search($self) or return;
299 # favor the Message-ID we used for the NNTP article number:
300 my $mm = mm($self) or return;
301 my $num = $mm->num_for($mid);
302 $srch->lookup_article($num);
305 sub msg_by_mid ($$;$) {
306 my ($self, $mid, $ref) = @_;
307 my $srch = search($self) or
308 return msg_by_path($self, mid2path($mid), $ref);
309 my $smsg = smsg_by_mid($self, $mid);
310 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;