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);
14 $cleanup_timer = 'disabled';
15 require PublicInbox::EvCleanup;
16 $cleanup_timer = undef; # OK if we get here
19 my $CLEANUP = {}; # string(inbox) -> inbox
21 $cleanup_timer = undef;
22 for my $ibx (values %$CLEANUP) {
23 foreach my $f (qw(git mm search)) {
24 delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
30 sub _cleanup_later ($) {
32 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
33 $CLEANUP->{"$self"} = $self;
37 my ($opts, $field, $default) = @_;
38 my $val = $opts->{$field};
40 $val = $val->[-1] if ref($val) eq 'ARRAY';
41 $val = undef if $val !~ /\A\d+\z/;
43 $opts->{$field} = $val || $default;
46 sub _set_limiter ($$$) {
47 my ($self, $pi_config, $pfx) = @_;
48 my $lkey = "-${pfx}_limiter";
49 $self->{$lkey} ||= eval {
50 # full key is: publicinbox.$NAME.httpbackendmax
51 my $mkey = $pfx.'max';
52 my $val = $self->{$mkey} or return;
54 if ($val =~ /\A\d+\z/) {
55 require PublicInbox::Qspawn;
56 $lim = PublicInbox::Qspawn::Limiter->new($val);
57 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
58 $lim = $pi_config->limiter($val);
59 warn "$mkey limiter=$val not found\n" if !$lim;
61 warn "$mkey limiter=$val not understood\n";
68 my ($class, $opts) = @_;
69 my $v = $opts->{address} ||= 'public-inbox@example.com';
70 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
71 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
72 my $pi_config = delete $opts->{-pi_config};
73 _set_limiter($opts, $pi_config, 'httpbackend');
74 _set_uint($opts, 'feedmax', 25);
75 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
76 my $dir = $opts->{mainrepo};
77 if (defined $dir && -f "$dir/msgmap.sqlite3") { # XXX DIRTY
85 $self->{git} ||= eval {
86 my $git_dir = $self->{mainrepo};
87 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
88 my $g = PublicInbox::Git->new($git_dir);
89 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
90 _cleanup_later($self);
97 $self->{mm} ||= eval {
98 _cleanup_later($self);
99 my $dir = $self->{mainrepo};
100 if (($self->{version} || 1) >= 2) {
101 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
103 PublicInbox::Msgmap->new($dir);
110 $self->{search} ||= eval {
111 _cleanup_later($self);
112 PublicInbox::Search->new($self, $self->{altid});
119 if (open(my $fh, '<', $path)) {
128 my $desc = $self->{description};
129 return $desc if defined $desc;
130 $desc = try_cat("$self->{mainrepo}/description");
133 $desc =~ s/\s+/ /smg;
134 $desc = '($GIT_DIR/description missing)' if $desc eq '';
135 $self->{description} = $desc;
140 my $url = $self->{cloneurl};
142 $url = try_cat("$self->{mainrepo}/cloneurl");
143 my @url = split(/\s+/s, $url);
146 $self->{cloneurl} = \@url;
150 my ($self, $env) = @_;
151 if ($env) { # PSGI env
152 my $scheme = $env->{'psgi.url_scheme'};
153 my $host_port = $env->{HTTP_HOST} ||
154 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
155 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
156 # for mount in Plack::Builder
157 $url .= '/' if $url !~ m!/\z!;
158 $url .= $self->{name} . '/';
160 # either called from a non-PSGI environment (e.g. NNTP/POP3)
161 $self->{-base_url} ||= do {
162 my $url = $self->{url} or return undef;
163 # expand protocol-relative URLs to HTTPS if we're
164 # not inside a web server
165 $url = "https:$url" if $url =~ m!\A//!;
166 $url .= '/' if $url !~ m!/\z!;
174 $self->{-nntp_url} ||= do {
175 # no checking for nntp_usable here, we can point entirely
176 # to non-local servers or users run by a different user
177 my $ns = $self->{nntpserver};
178 my $group = $self->{newsgroup};
181 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
183 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
184 $u .= '/' if $u !~ m!/\z!;
189 my $mirrors = $self->{nntpmirror};
192 foreach (@$mirrors) {
193 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
194 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
196 $u .= '/' if $u !~ m!/\z!;
200 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
203 # else: allow full URLs like:
204 # nntp://news.example.com/alt.example
207 my %seen = map { $_ => 1 } @urls;
220 my $ret = $self->mm && $self->search;
221 $self->{mm} = $self->{search} = undef;
225 sub msg_by_path ($$;$) {
226 my ($self, $path, $ref) = @_;
227 # TODO: allow other refs:
228 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
229 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
233 sub msg_by_smsg ($$;$) {
234 my ($self, $smsg, $ref) = @_;
236 return unless defined $smsg; # ghost
238 # backwards compat to fallback to msg_by_mid
239 # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
240 defined(my $blob = $smsg->{blob}) or
241 return msg_by_path($self, mid2path($smsg->mid), $ref);
243 my $str = git($self)->cat_file($blob, $ref);
244 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
249 my ($self, $path) = @_;
250 git($self)->check('HEAD:'.$path);
253 sub msg_by_mid ($$;$) {
254 my ($self, $mid, $ref) = @_;
255 my $srch = search($self) or
256 return msg_by_path($self, mid2path($mid), $ref);
258 $srch->retry_reopen(sub {
259 $smsg = $srch->lookup_skeleton($mid) and $smsg->load_expand;
261 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;