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';
42 foreach my $f (qw(git mm search)) {
43 isweak($self->{$f}) or weaken($self->{$f});
49 $self->{git} ||= eval {
51 PublicInbox::Git->new($self->{mainrepo});
57 $self->{mm} ||= eval {
59 PublicInbox::Msgmap->new($self->{mainrepo});
65 $self->{search} ||= eval {
67 PublicInbox::Search->new($self->{mainrepo});
74 if (open(my $fh, '<', $path)) {
83 my $desc = $self->{description};
84 return $desc if defined $desc;
85 $desc = try_cat("$self->{mainrepo}/description");
88 $desc = '($GIT_DIR/description missing)' if $desc eq '';
89 $self->{description} = $desc;
94 my $url = $self->{cloneurl};
96 $url = try_cat("$self->{mainrepo}/cloneurl");
97 my @url = split(/\s+/s, $url);
99 $self->{cloneurl} = \@url;
103 my ($self, $env) = @_;
104 if ($env) { # PSGI env
105 my $scheme = $env->{'psgi.url_scheme'};
106 my $host_port = $env->{HTTP_HOST} ||
107 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
108 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
109 # for mount in Plack::Builder
110 $url .= '/' if $url !~ m!/\z!;
111 $url .= $self->{name} . '/';
113 # either called from a non-PSGI environment (e.g. NNTP/POP3)
114 $self->{-base_url} ||= do {
115 my $url = $self->{url} or return undef;
116 # expand protocol-relative URLs to HTTPS if we're
117 # not inside a web server
118 $url = "https:$url" if $url =~ m!\A//!;
119 $url .= '/' if $url !~ m!/\z!;
127 my $ret = $self->mm && $self->search;
128 $self->{mm} = $self->{search} = undef;
132 sub msg_by_path ($$;$) {
133 my ($self, $path, $ref) = @_;
134 # TODO: allow other refs:
135 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
136 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
141 my ($self, $path) = @_;
142 git($self)->check('HEAD:'.$path);
145 sub msg_by_mid ($$;$) {
146 my ($self, $mid, $ref) = @_;
147 msg_by_path($self, mid2path($mid), $ref);