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;
25 sub _cleanup_later ($) {
27 $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
28 $CLEANUP->{"$self"} = $self;
32 my ($opts, $field, $default) = @_;
33 my $val = $opts->{$field};
35 $val = $val->[-1] if ref($val) eq 'ARRAY';
36 $val = undef if $val !~ /\A\d+\z/;
38 $opts->{$field} = $val || $default;
41 sub _set_limiter ($$$) {
42 my ($self, $pi_config, $pfx) = @_;
43 my $lkey = "-${pfx}_limiter";
44 $self->{$lkey} ||= eval {
45 # full key is: publicinbox.$NAME.httpbackendmax
46 my $mkey = $pfx.'max';
47 my $val = $self->{$mkey} or return;
49 if ($val =~ /\A\d+\z/) {
50 require PublicInbox::Qspawn;
51 $lim = PublicInbox::Qspawn::Limiter->new($val);
52 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
53 $lim = $pi_config->limiter($val);
54 warn "$mkey limiter=$val not found\n" if !$lim;
56 warn "$mkey limiter=$val not understood\n";
63 my ($class, $opts) = @_;
64 my $v = $opts->{address} ||= 'public-inbox@example.com';
65 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
66 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
67 my $pi_config = delete $opts->{-pi_config};
68 _set_limiter($opts, $pi_config, 'httpbackend');
69 _set_uint($opts, 'feedmax', 25);
70 $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
76 $self->{git} ||= eval {
77 my $g = PublicInbox::Git->new($self->{mainrepo});
78 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
85 $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{mainrepo}) };
90 $self->{search} ||= eval {
91 PublicInbox::Search->new($self->{mainrepo}, $self->{altid});
98 if (open(my $fh, '<', $path)) {
107 my $desc = $self->{description};
108 return $desc if defined $desc;
109 $desc = try_cat("$self->{mainrepo}/description");
112 $desc =~ s/\s+/ /smg;
113 $desc = '($GIT_DIR/description missing)' if $desc eq '';
114 $self->{description} = $desc;
119 my $url = $self->{cloneurl};
121 $url = try_cat("$self->{mainrepo}/cloneurl");
122 my @url = split(/\s+/s, $url);
125 $self->{cloneurl} = \@url;
129 my ($self, $env) = @_;
130 if ($env) { # PSGI env
131 my $scheme = $env->{'psgi.url_scheme'};
132 my $host_port = $env->{HTTP_HOST} ||
133 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
134 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
135 # for mount in Plack::Builder
136 $url .= '/' if $url !~ m!/\z!;
137 $url .= $self->{name} . '/';
139 # either called from a non-PSGI environment (e.g. NNTP/POP3)
140 $self->{-base_url} ||= do {
141 my $url = $self->{url} or return undef;
142 # expand protocol-relative URLs to HTTPS if we're
143 # not inside a web server
144 $url = "https:$url" if $url =~ m!\A//!;
145 $url .= '/' if $url !~ m!/\z!;
153 $self->{-nntp_url} ||= do {
154 # no checking for nntp_usable here, we can point entirely
155 # to non-local servers or users run by a different user
156 my $ns = $self->{nntpserver};
157 my $group = $self->{newsgroup};
160 $ns = [ $ns ] if ref($ns) ne 'ARRAY';
162 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
163 $u .= '/' if $u !~ m!/\z!;
168 my $mirrors = $self->{nntpmirror};
171 foreach (@$mirrors) {
172 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
173 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
175 $u .= '/' if $u !~ m!/\z!;
179 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
182 # else: allow full URLs like:
183 # nntp://news.example.com/alt.example
186 my %seen = map { $_ => 1 } @urls;
199 my $ret = $self->mm && $self->search;
200 $self->{mm} = $self->{search} = undef;
204 sub msg_by_path ($$;$) {
205 my ($self, $path, $ref) = @_;
206 # TODO: allow other refs:
207 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
208 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
212 sub msg_by_smsg ($$;$) {
213 my ($self, $smsg, $ref) = @_;
215 return unless defined $smsg; # ghost
217 # backwards compat to fallback to msg_by_mid
218 # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
219 defined(my $blob = $smsg->blob) or return msg_by_mid($self, $smsg->mid);
221 my $str = git($self)->cat_file($blob, $ref);
222 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
227 my ($self, $path) = @_;
228 git($self)->check('HEAD:'.$path);
231 sub msg_by_mid ($$;$) {
232 my ($self, $mid, $ref) = @_;
233 msg_by_path($self, mid2path($mid), $ref);