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 require PublicInbox::Msgmap;
136 _cleanup_later($self);
137 my $dir = $self->{mainrepo};
138 if (($self->{version} || 1) >= 2) {
139 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
141 PublicInbox::Msgmap->new($dir);
148 $self->{search} ||= eval {
149 _cleanup_later($self);
150 PublicInbox::Search->new($self, $self->{altid});
157 if (open(my $fh, '<', $path)) {
166 my $desc = $self->{description};
167 return $desc if defined $desc;
168 $desc = try_cat("$self->{mainrepo}/description");
171 $desc =~ s/\s+/ /smg;
172 $desc = '($REPO_DIR/description missing)' if $desc eq '';
173 $self->{description} = $desc;
178 my $url = $self->{cloneurl};
180 $url = try_cat("$self->{mainrepo}/cloneurl");
181 my @url = split(/\s+/s, $url);
184 $self->{cloneurl} = \@url;
188 my ($self, $env) = @_;
189 if ($env) { # PSGI env
190 my $scheme = $env->{'psgi.url_scheme'};
191 my $host_port = $env->{HTTP_HOST} ||
192 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
193 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
194 # for mount in Plack::Builder
195 $url .= '/' if $url !~ m!/\z!;
196 $url .= $self->{name} . '/';
198 # either called from a non-PSGI environment (e.g. NNTP/POP3)
199 $self->{-base_url} ||= do {
200 my $url = $self->{url} or return undef;
201 # expand protocol-relative URLs to HTTPS if we're
202 # not inside a web server
203 $url = "https:$url" if $url =~ m!\A//!;
204 $url .= '/' if $url !~ m!/\z!;
212 $self->{-nntp_url} ||= do {
213 # no checking for nntp_usable here, we can point entirely
214 # to non-local servers or users run by a different user
215 my $ns = $self->{nntpserver};
216 my $group = $self->{newsgroup};
219 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
221 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
222 $u .= '/' if $u !~ m!/\z!;
227 my $mirrors = $self->{nntpmirror};
230 foreach (@$mirrors) {
231 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
232 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
234 $u .= '/' if $u !~ m!/\z!;
238 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
241 # else: allow full URLs like:
242 # nntp://news.example.com/alt.example
245 my %seen = map { $_ => 1 } @urls;
258 my $ret = $self->mm && $self->search;
259 $self->{mm} = $self->{search} = undef;
263 sub msg_by_path ($$;$) {
264 my ($self, $path, $ref) = @_;
265 # TODO: allow other refs:
266 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
267 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
271 sub msg_by_smsg ($$;$) {
272 my ($self, $smsg, $ref) = @_;
274 # ghosts may have undef smsg (from SearchThread.node) or
275 # no {blob} field (from each_smsg_by_mid)
276 return unless defined $smsg;
277 defined(my $blob = $smsg->{blob}) or return;
279 my $str = git($self)->cat_file($blob, $ref);
280 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
285 my ($self, $smsg, $ref) = @_;
286 if (my $s = msg_by_smsg($self, $smsg, $ref)) {
287 $smsg->{mime} = PublicInbox::MIME->new($s);
293 my ($self, $path) = @_;
294 git($self)->check('HEAD:'.$path);
298 my ($self, $mid) = @_;
299 my $mm = mm($self) or return;
303 sub smsg_by_mid ($$) {
304 my ($self, $mid) = @_;
305 my $srch = search($self) or return;
306 # favor the Message-ID we used for the NNTP article number:
307 my $num = mid2num($self, $mid);
308 defined $num ? $srch->lookup_article($num) : undef;
311 sub msg_by_mid ($$;$) {
312 my ($self, $mid, $ref) = @_;
313 my $srch = search($self) or
314 return msg_by_path($self, mid2path($mid), $ref);
315 my $smsg = smsg_by_mid($self, $mid);
316 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;