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;
8 use Scalar::Util qw(weaken isweak);
10 use PublicInbox::MID qw(mid2path);
15 require PublicInbox::EvCleanup;
16 $weakt = undef; # OK if we get here
19 my $WEAKEN = {}; # string(inbox) -> inbox
22 _weaken_fields($_) for values %$WEAKEN;
26 sub _weaken_later ($) {
28 $weakt ||= PublicInbox::EvCleanup::later(*weaken_task);
29 $WEAKEN->{"$self"} = $self;
33 my ($opts, $field, $default) = @_;
34 my $val = $opts->{$field};
36 $val = $val->[-1] if ref($val) eq 'ARRAY';
37 $val = undef if $val !~ /\A\d+\z/;
39 $opts->{$field} = $val || $default;
43 my ($class, $opts) = @_;
44 my $v = $opts->{address} ||= 'public-inbox@example.com';
45 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
46 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
47 _set_uint($opts, 'feedmax', 25);
48 weaken($opts->{-pi_config});
54 foreach my $f (qw(git mm search)) {
55 isweak($self->{$f}) or weaken($self->{$f});
59 sub _set_limiter ($$$) {
60 my ($self, $git, $pfx) = @_;
61 my $lkey = "-${pfx}_limiter";
62 $git->{$lkey} = $self->{$lkey} ||= eval {
63 my $mkey = $pfx.'max';
64 my $val = $self->{$mkey} or return;
66 if ($val =~ /\A\d+\z/) {
67 require PublicInbox::Qspawn;
68 $lim = PublicInbox::Qspawn::Limiter->new($val);
69 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
70 $lim = $self->{-pi_config}->limiter($val);
71 warn "$mkey limiter=$val not found\n" if !$lim;
73 warn "$mkey limiter=$val not understood\n";
81 $self->{git} ||= eval {
83 my $g = PublicInbox::Git->new($self->{mainrepo});
84 _set_limiter($self, $g, 'httpbackend');
91 $self->{mm} ||= eval {
93 PublicInbox::Msgmap->new($self->{mainrepo});
99 $self->{search} ||= eval {
100 _weaken_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->{-pi_config}->{'publicinbox.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 return msg_by_mid($self, $smsg->mid);
231 my $str = git($self)->cat_file($blob, $ref);
232 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
237 my ($self, $path) = @_;
238 git($self)->check('HEAD:'.$path);
241 sub msg_by_mid ($$;$) {
242 my ($self, $mid, $ref) = @_;
243 msg_by_path($self, mid2path($mid), $ref);