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);
173 $self->{search} ||= eval {
174 _cleanup_later($self);
175 PublicInbox::Search->new($self, $self->{altid});
182 if (open(my $fh, '<', $path)) {
191 my $desc = $self->{description};
192 return $desc if defined $desc;
193 $desc = try_cat("$self->{mainrepo}/description");
196 $desc =~ s/\s+/ /smg;
197 $desc = '($REPO_DIR/description missing)' if $desc eq '';
198 $self->{description} = $desc;
203 my $url = $self->{cloneurl};
205 $url = try_cat("$self->{mainrepo}/cloneurl");
206 my @url = split(/\s+/s, $url);
209 $self->{cloneurl} = \@url;
213 my ($self, $env) = @_;
215 if ($env && ($scheme = $env->{'psgi.url_scheme'})) { # PSGI env
216 my $host_port = $env->{HTTP_HOST} ||
217 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
218 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
219 # for mount in Plack::Builder
220 $url .= '/' if $url !~ m!/\z!;
221 $url .= $self->{name} . '/';
223 # either called from a non-PSGI environment (e.g. NNTP/POP3)
224 $self->{-base_url} ||= do {
225 my $url = $self->{url} or return undef;
226 # expand protocol-relative URLs to HTTPS if we're
227 # not inside a web server
228 $url = "https:$url" if $url =~ m!\A//!;
229 $url .= '/' if $url !~ m!/\z!;
237 $self->{-nntp_url} ||= do {
238 # no checking for nntp_usable here, we can point entirely
239 # to non-local servers or users run by a different user
240 my $ns = $self->{nntpserver};
241 my $group = $self->{newsgroup};
244 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
246 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
247 $u .= '/' if $u !~ m!/\z!;
252 my $mirrors = $self->{nntpmirror};
255 foreach (@$mirrors) {
256 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
257 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
259 $u .= '/' if $u !~ m!/\z!;
263 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
266 # else: allow full URLs like:
267 # nntp://news.example.com/alt.example
270 my %seen = map { $_ => 1 } @urls;
283 my $ret = $self->mm && $self->search;
284 $self->{mm} = $self->{search} = undef;
288 sub msg_by_path ($$;$) {
289 my ($self, $path, $ref) = @_;
290 # TODO: allow other refs:
291 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
292 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
296 sub msg_by_smsg ($$;$) {
297 my ($self, $smsg, $ref) = @_;
299 # ghosts may have undef smsg (from SearchThread.node) or
301 return unless defined $smsg;
302 defined(my $blob = $smsg->{blob}) or return;
304 my $str = git($self)->cat_file($blob, $ref);
305 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
310 my ($self, $smsg, $ref) = @_;
311 if (my $s = msg_by_smsg($self, $smsg, $ref)) {
312 $smsg->{mime} = PublicInbox::MIME->new($s);
318 my ($self, $mid) = @_;
319 my $mm = mm($self) or return;
323 sub smsg_by_mid ($$) {
324 my ($self, $mid) = @_;
325 my $srch = search($self) or return;
326 # favor the Message-ID we used for the NNTP article number:
327 defined(my $num = mid2num($self, $mid)) or return;
328 my $smsg = $srch->lookup_article($num) or return;
329 PublicInbox::SearchMsg::psgi_cull($smsg);
332 sub msg_by_mid ($$;$) {
333 my ($self, $mid, $ref) = @_;
334 my $srch = search($self) or
335 return msg_by_path($self, mid2path($mid), $ref);
336 my $smsg = smsg_by_mid($self, $mid);
337 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
341 my ($self, $opts, $after, $before) = @_;
342 search($self)->{over_ro}->recent($opts, $after, $before);
347 if (my $srch = search($self)) {
348 my $msgs = $srch->{over_ro}->recent({limit => 1});
349 if (my $smsg = $msgs->[0]) {
354 git($self)->modified; # v1