1 # Copyright (C) 2016 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'};
81 $self->{git} ||= eval {
82 my $g = PublicInbox::Git->new($self->{mainrepo});
83 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
84 _cleanup_later($self);
91 $self->{mm} ||= eval {
92 _cleanup_later($self);
93 PublicInbox::Msgmap->new($self->{mainrepo});
99 $self->{search} ||= eval {
100 _cleanup_later($self);
101 PublicInbox::Search->new($self->{mainrepo}, $self->{altid});
108 if (open(my $fh, '<', $path)) {
117 my $desc = $self->{description};
118 return $desc if defined $desc;
119 $desc = try_cat("$self->{mainrepo}/description");
122 $desc =~ s/\s+/ /smg;
123 $desc = '($GIT_DIR/description missing)' if $desc eq '';
124 $self->{description} = $desc;
129 my $url = $self->{cloneurl};
131 $url = try_cat("$self->{mainrepo}/cloneurl");
132 my @url = split(/\s+/s, $url);
135 $self->{cloneurl} = \@url;
139 my ($self, $env) = @_;
140 if ($env) { # PSGI env
141 my $scheme = $env->{'psgi.url_scheme'};
142 my $host_port = $env->{HTTP_HOST} ||
143 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
144 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
145 # for mount in Plack::Builder
146 $url .= '/' if $url !~ m!/\z!;
147 $url .= $self->{name} . '/';
149 # either called from a non-PSGI environment (e.g. NNTP/POP3)
150 $self->{-base_url} ||= do {
151 my $url = $self->{url} or return undef;
152 # expand protocol-relative URLs to HTTPS if we're
153 # not inside a web server
154 $url = "https:$url" if $url =~ m!\A//!;
155 $url .= '/' if $url !~ m!/\z!;
163 $self->{-nntp_url} ||= do {
164 # no checking for nntp_usable here, we can point entirely
165 # to non-local servers or users run by a different user
166 my $ns = $self->{nntpserver};
167 my $group = $self->{newsgroup};
170 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
172 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
173 $u .= '/' if $u !~ m!/\z!;
178 my $mirrors = $self->{nntpmirror};
181 foreach (@$mirrors) {
182 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
183 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
185 $u .= '/' if $u !~ m!/\z!;
189 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
192 # else: allow full URLs like:
193 # nntp://news.example.com/alt.example
196 my %seen = map { $_ => 1 } @urls;
209 my $ret = $self->mm && $self->search;
210 $self->{mm} = $self->{search} = undef;
214 sub msg_by_path ($$;$) {
215 my ($self, $path, $ref) = @_;
216 # TODO: allow other refs:
217 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
218 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
222 sub msg_by_smsg ($$;$) {
223 my ($self, $smsg, $ref) = @_;
225 return unless defined $smsg; # ghost
227 # backwards compat to fallback to msg_by_mid
228 # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
229 defined(my $blob = $smsg->{blob}) or
230 return msg_by_mid($self, $smsg->mid);
232 my $str = git($self)->cat_file($blob, $ref);
233 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
238 my ($self, $path) = @_;
239 git($self)->check('HEAD:'.$path);
242 sub msg_by_mid ($$;$) {
243 my ($self, $mid, $ref) = @_;
244 msg_by_path($self, mid2path($mid), $ref);