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});
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 my $ret = $self->mm && $self->search;
153 $self->{mm} = $self->{search} = undef;
157 sub msg_by_path ($$;$) {
158 my ($self, $path, $ref) = @_;
159 # TODO: allow other refs:
160 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
161 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
166 my ($self, $path) = @_;
167 git($self)->check('HEAD:'.$path);
170 sub msg_by_mid ($$;$) {
171 my ($self, $mid, $ref) = @_;
172 msg_by_path($self, mid2path($mid), $ref);