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
19 my $cleanup_broken = $@;
21 my $CLEANUP = {}; # string(inbox) -> inbox
23 $cleanup_timer = undef;
25 for my $ibx (values %$CLEANUP) {
27 foreach my $f (qw(mm search)) {
28 delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
30 my $expire = time - 60;
31 if (my $git = $ibx->{git}) {
32 $again = $git->cleanup($expire);
34 if (my $gits = $ibx->{-repo_objs}) {
35 foreach my $git (@$gits) {
36 $again = 1 if $git->cleanup($expire);
39 $again ||= !!($ibx->{mm} || $ibx->{search});
40 $next->{"$ibx"} = $ibx if $again;
45 sub _cleanup_later ($) {
47 return if $cleanup_broken;
48 return unless PublicInbox::EvCleanup::enabled();
49 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
50 $CLEANUP->{"$self"} = $self;
54 my ($opts, $field, $default) = @_;
55 my $val = $opts->{$field};
57 $val = $val->[-1] if ref($val) eq 'ARRAY';
58 $val = undef if $val !~ /\A\d+\z/;
60 $opts->{$field} = $val || $default;
63 sub _set_limiter ($$$) {
64 my ($self, $pi_config, $pfx) = @_;
65 my $lkey = "-${pfx}_limiter";
66 $self->{$lkey} ||= eval {
67 # full key is: publicinbox.$NAME.httpbackendmax
68 my $mkey = $pfx.'max';
69 my $val = $self->{$mkey} or return;
71 if ($val =~ /\A\d+\z/) {
72 require PublicInbox::Qspawn;
73 $lim = PublicInbox::Qspawn::Limiter->new($val);
74 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
75 $lim = $pi_config->limiter($val);
76 warn "$mkey limiter=$val not found\n" if !$lim;
78 warn "$mkey limiter=$val not understood\n";
85 my ($class, $opts) = @_;
86 my $v = $opts->{address} ||= 'public-inbox@example.com';
87 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
88 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
89 my $pi_config = delete $opts->{-pi_config};
90 _set_limiter($opts, $pi_config, 'httpbackend');
91 _set_uint($opts, 'feedmax', 25);
92 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
93 my $dir = $opts->{mainrepo};
94 if (defined $dir && -f "$dir/inbox.lock") {
98 # allow any combination of multi-line or comma-delimited hide entries
100 if (defined(my $h = $opts->{hide})) {
101 foreach my $v (@$h) {
102 $hide->{$_} = 1 foreach (split(/\s*,\s*/, $v));
104 $opts->{-hide} = $hide;
110 my ($self, $part) = @_;
111 ($self->{version} || 1) == 2 or return;
112 $self->{"$part.git"} ||= eval {
113 my $git_dir = "$self->{mainrepo}/git/$part.git";
114 my $g = PublicInbox::Git->new($git_dir);
115 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
116 # no cleanup needed, we never cat-file off this, only clone
123 $self->{git} ||= eval {
124 my $git_dir = $self->{mainrepo};
125 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
126 my $g = PublicInbox::Git->new($git_dir);
127 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
128 _cleanup_later($self);
135 my $v = $self->{version};
136 return unless defined($v) && $v == 2;
137 my $part = $self->{-max_git_part};
138 my $changed = git($self)->alternates_changed;
139 if (!defined($part) || $changed) {
140 $self->git->cleanup if $changed;
141 my $gits = "$self->{mainrepo}/git";
142 if (opendir my $dh, $gits) {
144 while (defined(my $git_dir = readdir($dh))) {
145 $git_dir =~ m!\A(\d+)\.git\z! or next;
146 $max = $1 if $1 > $max;
148 $part = $self->{-max_git_part} = $max if $max >= 0;
150 warn "opendir $gits failed: $!\n";
158 $self->{mm} ||= eval {
159 require PublicInbox::Msgmap;
160 _cleanup_later($self);
161 my $dir = $self->{mainrepo};
162 if (($self->{version} || 1) >= 2) {
163 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
165 PublicInbox::Msgmap->new($dir);
172 $self->{search} ||= eval {
173 _cleanup_later($self);
174 PublicInbox::Search->new($self, $self->{altid});
181 if (open(my $fh, '<', $path)) {
190 my $desc = $self->{description};
191 return $desc if defined $desc;
192 $desc = try_cat("$self->{mainrepo}/description");
195 $desc =~ s/\s+/ /smg;
196 $desc = '($REPO_DIR/description missing)' if $desc eq '';
197 $self->{description} = $desc;
202 my $url = $self->{cloneurl};
204 $url = try_cat("$self->{mainrepo}/cloneurl");
205 my @url = split(/\s+/s, $url);
208 $self->{cloneurl} = \@url;
212 my ($self, $env) = @_;
214 if ($env && ($scheme = $env->{'psgi.url_scheme'})) { # PSGI env
215 my $host_port = $env->{HTTP_HOST} ||
216 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
217 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
218 # for mount in Plack::Builder
219 $url .= '/' if $url !~ m!/\z!;
220 $url .= $self->{name} . '/';
222 # either called from a non-PSGI environment (e.g. NNTP/POP3)
223 $self->{-base_url} ||= do {
224 my $url = $self->{url} or return undef;
225 # expand protocol-relative URLs to HTTPS if we're
226 # not inside a web server
227 $url = "https:$url" if $url =~ m!\A//!;
228 $url .= '/' if $url !~ m!/\z!;
236 $self->{-nntp_url} ||= do {
237 # no checking for nntp_usable here, we can point entirely
238 # to non-local servers or users run by a different user
239 my $ns = $self->{nntpserver};
240 my $group = $self->{newsgroup};
243 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
245 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
246 $u .= '/' if $u !~ m!/\z!;
251 my $mirrors = $self->{nntpmirror};
254 foreach (@$mirrors) {
255 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
256 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
258 $u .= '/' if $u !~ m!/\z!;
262 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
265 # else: allow full URLs like:
266 # nntp://news.example.com/alt.example
269 my %seen = map { $_ => 1 } @urls;
282 my $ret = $self->mm && $self->search;
283 $self->{mm} = $self->{search} = undef;
287 sub msg_by_path ($$;$) {
288 my ($self, $path, $ref) = @_;
289 # TODO: allow other refs:
290 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
291 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
295 sub msg_by_smsg ($$;$) {
296 my ($self, $smsg, $ref) = @_;
298 # ghosts may have undef smsg (from SearchThread.node) or
300 return unless defined $smsg;
301 defined(my $blob = $smsg->{blob}) or return;
303 my $str = git($self)->cat_file($blob, $ref);
304 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
309 my ($self, $smsg, $ref) = @_;
310 if (my $s = msg_by_smsg($self, $smsg, $ref)) {
311 $smsg->{mime} = PublicInbox::MIME->new($s);
317 my ($self, $mid) = @_;
318 my $mm = mm($self) or return;
322 sub smsg_by_mid ($$) {
323 my ($self, $mid) = @_;
324 my $srch = search($self) or return;
325 # favor the Message-ID we used for the NNTP article number:
326 defined(my $num = mid2num($self, $mid)) or return;
327 my $smsg = $srch->lookup_article($num) or return;
328 PublicInbox::SearchMsg::psgi_cull($smsg);
331 sub msg_by_mid ($$;$) {
332 my ($self, $mid, $ref) = @_;
333 my $srch = search($self) or
334 return msg_by_path($self, mid2path($mid), $ref);
335 my $smsg = smsg_by_mid($self, $mid);
336 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
340 my ($self, $opts, $after, $before) = @_;
341 search($self)->{over_ro}->recent($opts, $after, $before);
346 if (my $srch = search($self)) {
347 my $msgs = $srch->{over_ro}->recent({limit => 1});
348 if (my $smsg = $msgs->[0]) {
353 git($self)->modified; # v1