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);
13 $cleanup_timer = 'disabled';
14 require PublicInbox::EvCleanup;
15 $cleanup_timer = undef; # OK if we get here
18 my $CLEANUP = {}; # string(inbox) -> inbox
20 $cleanup_timer = undef;
21 delete $_->{git} for values %$CLEANUP;
26 my ($opts, $field, $default) = @_;
27 my $val = $opts->{$field};
29 $val = $val->[-1] if ref($val) eq 'ARRAY';
30 $val = undef if $val !~ /\A\d+\z/;
32 $opts->{$field} = $val || $default;
35 sub _set_limiter ($$$) {
36 my ($self, $pi_config, $pfx) = @_;
37 my $lkey = "-${pfx}_limiter";
38 $self->{$lkey} ||= eval {
39 # full key is: publicinbox.$NAME.httpbackendmax
40 my $mkey = $pfx.'max';
41 my $val = $self->{$mkey} or return;
43 if ($val =~ /\A\d+\z/) {
44 require PublicInbox::Qspawn;
45 $lim = PublicInbox::Qspawn::Limiter->new($val);
46 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
47 $lim = $pi_config->limiter($val);
48 warn "$mkey limiter=$val not found\n" if !$lim;
50 warn "$mkey limiter=$val not understood\n";
57 my ($class, $opts) = @_;
58 my $v = $opts->{address} ||= 'public-inbox@example.com';
59 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
60 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
61 my $pi_config = delete $opts->{-pi_config};
62 _set_limiter($opts, $pi_config, 'httpbackend');
63 _set_uint($opts, 'feedmax', 25);
64 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
70 $self->{git} ||= eval {
71 my $g = PublicInbox::Git->new($self->{mainrepo});
72 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
73 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
74 $CLEANUP->{"$self"} = $self;
81 $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{mainrepo}) };
86 $self->{search} ||= eval {
87 PublicInbox::Search->new($self->{mainrepo}, $self->{altid});
94 if (open(my $fh, '<', $path)) {
103 my $desc = $self->{description};
104 return $desc if defined $desc;
105 $desc = try_cat("$self->{mainrepo}/description");
108 $desc =~ s/\s+/ /smg;
109 $desc = '($GIT_DIR/description missing)' if $desc eq '';
110 $self->{description} = $desc;
115 my $url = $self->{cloneurl};
117 $url = try_cat("$self->{mainrepo}/cloneurl");
118 my @url = split(/\s+/s, $url);
121 $self->{cloneurl} = \@url;
125 my ($self, $env) = @_;
126 if ($env) { # PSGI env
127 my $scheme = $env->{'psgi.url_scheme'};
128 my $host_port = $env->{HTTP_HOST} ||
129 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
130 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
131 # for mount in Plack::Builder
132 $url .= '/' if $url !~ m!/\z!;
133 $url .= $self->{name} . '/';
135 # either called from a non-PSGI environment (e.g. NNTP/POP3)
136 $self->{-base_url} ||= do {
137 my $url = $self->{url} or return undef;
138 # expand protocol-relative URLs to HTTPS if we're
139 # not inside a web server
140 $url = "https:$url" if $url =~ m!\A//!;
141 $url .= '/' if $url !~ m!/\z!;
149 $self->{-nntp_url} ||= do {
150 # no checking for nntp_usable here, we can point entirely
151 # to non-local servers or users run by a different user
152 my $ns = $self->{nntpserver};
153 my $group = $self->{newsgroup};
156 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
158 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
159 $u .= '/' if $u !~ m!/\z!;
164 my $mirrors = $self->{nntpmirror};
167 foreach (@$mirrors) {
168 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
169 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
171 $u .= '/' if $u !~ m!/\z!;
175 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
178 # else: allow full URLs like:
179 # nntp://news.example.com/alt.example
182 my %seen = map { $_ => 1 } @urls;
195 my $ret = $self->mm && $self->search;
196 $self->{mm} = $self->{search} = undef;
200 sub msg_by_path ($$;$) {
201 my ($self, $path, $ref) = @_;
202 # TODO: allow other refs:
203 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
204 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
208 sub msg_by_smsg ($$;$) {
209 my ($self, $smsg, $ref) = @_;
211 return unless defined $smsg; # ghost
213 # backwards compat to fallback to msg_by_mid
214 # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
215 defined(my $blob = $smsg->{blob}) or
216 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);