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 return unless $PublicInbox::EvCleanup::ENABLED;
33 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
34 $CLEANUP->{"$self"} = $self;
38 my ($opts, $field, $default) = @_;
39 my $val = $opts->{$field};
41 $val = $val->[-1] if ref($val) eq 'ARRAY';
42 $val = undef if $val !~ /\A\d+\z/;
44 $opts->{$field} = $val || $default;
47 sub _set_limiter ($$$) {
48 my ($self, $pi_config, $pfx) = @_;
49 my $lkey = "-${pfx}_limiter";
50 $self->{$lkey} ||= eval {
51 # full key is: publicinbox.$NAME.httpbackendmax
52 my $mkey = $pfx.'max';
53 my $val = $self->{$mkey} or return;
55 if ($val =~ /\A\d+\z/) {
56 require PublicInbox::Qspawn;
57 $lim = PublicInbox::Qspawn::Limiter->new($val);
58 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
59 $lim = $pi_config->limiter($val);
60 warn "$mkey limiter=$val not found\n" if !$lim;
62 warn "$mkey limiter=$val not understood\n";
69 my ($class, $opts) = @_;
70 my $v = $opts->{address} ||= 'public-inbox@example.com';
71 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
72 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
73 my $pi_config = delete $opts->{-pi_config};
74 _set_limiter($opts, $pi_config, 'httpbackend');
75 _set_uint($opts, 'feedmax', 25);
76 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
77 my $dir = $opts->{mainrepo};
78 if (defined $dir && -f "$dir/msgmap.sqlite3") { # XXX DIRTY
86 $self->{git} ||= eval {
87 my $git_dir = $self->{mainrepo};
88 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
89 my $g = PublicInbox::Git->new($git_dir);
90 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
91 _cleanup_later($self);
98 $self->{mm} ||= eval {
99 _cleanup_later($self);
100 my $dir = $self->{mainrepo};
101 if (($self->{version} || 1) >= 2) {
102 PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
104 PublicInbox::Msgmap->new($dir);
111 $self->{search} ||= eval {
112 _cleanup_later($self);
113 PublicInbox::Search->new($self, $self->{altid});
120 if (open(my $fh, '<', $path)) {
129 my $desc = $self->{description};
130 return $desc if defined $desc;
131 $desc = try_cat("$self->{mainrepo}/description");
134 $desc =~ s/\s+/ /smg;
135 $desc = '($GIT_DIR/description missing)' if $desc eq '';
136 $self->{description} = $desc;
141 my $url = $self->{cloneurl};
143 $url = try_cat("$self->{mainrepo}/cloneurl");
144 my @url = split(/\s+/s, $url);
147 $self->{cloneurl} = \@url;
151 my ($self, $env) = @_;
152 if ($env) { # PSGI env
153 my $scheme = $env->{'psgi.url_scheme'};
154 my $host_port = $env->{HTTP_HOST} ||
155 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
156 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
157 # for mount in Plack::Builder
158 $url .= '/' if $url !~ m!/\z!;
159 $url .= $self->{name} . '/';
161 # either called from a non-PSGI environment (e.g. NNTP/POP3)
162 $self->{-base_url} ||= do {
163 my $url = $self->{url} or return undef;
164 # expand protocol-relative URLs to HTTPS if we're
165 # not inside a web server
166 $url = "https:$url" if $url =~ m!\A//!;
167 $url .= '/' if $url !~ m!/\z!;
175 $self->{-nntp_url} ||= do {
176 # no checking for nntp_usable here, we can point entirely
177 # to non-local servers or users run by a different user
178 my $ns = $self->{nntpserver};
179 my $group = $self->{newsgroup};
182 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
184 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
185 $u .= '/' if $u !~ m!/\z!;
190 my $mirrors = $self->{nntpmirror};
193 foreach (@$mirrors) {
194 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
195 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
197 $u .= '/' if $u !~ m!/\z!;
201 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
204 # else: allow full URLs like:
205 # nntp://news.example.com/alt.example
208 my %seen = map { $_ => 1 } @urls;
221 my $ret = $self->mm && $self->search;
222 $self->{mm} = $self->{search} = undef;
226 sub msg_by_path ($$;$) {
227 my ($self, $path, $ref) = @_;
228 # TODO: allow other refs:
229 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
230 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
234 sub msg_by_smsg ($$;$) {
235 my ($self, $smsg, $ref) = @_;
237 return unless defined $smsg; # ghost
239 # backwards compat to fallback to msg_by_mid
240 # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
241 defined(my $blob = $smsg->{blob}) or
242 return msg_by_path($self, mid2path($smsg->mid), $ref);
244 my $str = git($self)->cat_file($blob, $ref);
245 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
250 my ($self, $path) = @_;
251 git($self)->check('HEAD:'.$path);
254 sub msg_by_mid ($$;$) {
255 my ($self, $mid, $ref) = @_;
256 my $srch = search($self) or
257 return msg_by_path($self, mid2path($mid), $ref);
259 $srch->retry_reopen(sub {
260 $smsg = $srch->lookup_skeleton($mid) and $smsg->load_expand;
262 $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;