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 ($class, $opts) = @_;
34 my $v = $opts->{address} ||= 'public-inbox@example.com';
35 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
36 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
37 weaken($opts->{-pi_config});
43 foreach my $f (qw(git mm search)) {
44 isweak($self->{$f}) or weaken($self->{$f});
48 sub _set_limiter ($$$) {
49 my ($self, $git, $pfx) = @_;
50 my $lkey = "-${pfx}_limiter";
51 $git->{$lkey} = $self->{$lkey} ||= eval {
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 = $self->{-pi_config}->limiter($val);
60 warn "$mkey limiter=$val not found\n" if !$lim;
62 warn "$mkey limiter=$val not understood\n";
70 $self->{git} ||= eval {
72 my $g = PublicInbox::Git->new($self->{mainrepo});
73 _set_limiter($self, $g, 'httpbackend');
80 $self->{mm} ||= eval {
82 PublicInbox::Msgmap->new($self->{mainrepo});
88 $self->{search} ||= eval {
90 PublicInbox::Search->new($self->{mainrepo}, $self->{altid});
97 if (open(my $fh, '<', $path)) {
106 my $desc = $self->{description};
107 return $desc if defined $desc;
108 $desc = try_cat("$self->{mainrepo}/description");
111 $desc =~ s/\s+/ /smg;
112 $desc = '($GIT_DIR/description missing)' if $desc eq '';
113 $self->{description} = $desc;
118 my $url = $self->{cloneurl};
120 $url = try_cat("$self->{mainrepo}/cloneurl");
121 my @url = split(/\s+/s, $url);
124 $self->{cloneurl} = \@url;
128 my ($self, $env) = @_;
129 if ($env) { # PSGI env
130 my $scheme = $env->{'psgi.url_scheme'};
131 my $host_port = $env->{HTTP_HOST} ||
132 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
133 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
134 # for mount in Plack::Builder
135 $url .= '/' if $url !~ m!/\z!;
136 $url .= $self->{name} . '/';
138 # either called from a non-PSGI environment (e.g. NNTP/POP3)
139 $self->{-base_url} ||= do {
140 my $url = $self->{url} or return undef;
141 # expand protocol-relative URLs to HTTPS if we're
142 # not inside a web server
143 $url = "https:$url" if $url =~ m!\A//!;
144 $url .= '/' if $url !~ m!/\z!;
152 $self->{-nntp_url} ||= do {
153 # no checking for nntp_usable here, we can point entirely
154 # to non-local servers or users run by a different user
155 my $ns = $self->{-pi_config}->{'publicinbox.nntpserver'};
156 my $group = $self->{newsgroup};
159 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
161 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
162 $u .= '/' if $u !~ m!/\z!;
167 my $mirrors = $self->{nntpmirror};
170 foreach (@$mirrors) {
171 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
172 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
174 $u .= '/' if $u !~ m!/\z!;
178 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
181 # else: allow full URLs like:
182 # nntp://news.example.com/alt.example
185 my %seen = map { $_ => 1 } @urls;
198 my $ret = $self->mm && $self->search;
199 $self->{mm} = $self->{search} = undef;
203 sub msg_by_path ($$;$) {
204 my ($self, $path, $ref) = @_;
205 # TODO: allow other refs:
206 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
207 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
211 sub msg_by_smsg ($$;$) {
212 my ($self, $smsg, $ref) = @_;
214 # backwards compat to fallback to msg_by_mid
215 # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
216 defined(my $blob = $smsg->blob) or return msg_by_mid($self, $smsg->mid);
218 my $str = git($self)->cat_file($blob, $ref);
219 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
224 my ($self, $path) = @_;
225 git($self)->check('HEAD:'.$path);
228 sub msg_by_mid ($$;$) {
229 my ($self, $mid, $ref) = @_;
230 msg_by_path($self, mid2path($mid), $ref);