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);
10 use PublicInbox::MID qw(mid2path);
13 my ($class, $opts) = @_;
14 my $v = $opts->{address} ||= 'public-inbox@example.com';
15 my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
16 $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
22 weaken($self->{$_}) foreach qw(git mm search);
27 $self->{git} ||= eval { PublicInbox::Git->new($self->{mainrepo}) };
32 $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{mainrepo}) };
37 $self->{search} ||= eval { PublicInbox::Search->new($self->{mainrepo}) };
43 if (open(my $fh, '<', $path)) {
52 my $desc = $self->{description};
53 return $desc if defined $desc;
54 $desc = try_cat("$self->{mainrepo}/description");
57 $desc = '($GIT_DIR/description missing)' if $desc eq '';
58 $self->{description} = $desc;
63 my $url = $self->{cloneurl};
65 $url = try_cat("$self->{mainrepo}/cloneurl");
66 my @url = split(/\s+/s, $url);
68 $self->{cloneurl} = \@url;
72 my ($self, $prq) = @_; # Plack::Request
74 my $url = $prq->base->as_string;
75 $url .= '/' if $url !~ m!/\z!; # for mount in Plack::Builder
76 $url .= $self->{name} . '/';
78 # either called from a non-PSGI environment (e.g. NNTP/POP3)
79 $self->{-base_url} ||= do {
80 my $url = $self->{url} or return undef;
81 # expand protocol-relative URLs to HTTPS if we're
82 # not inside a web server
83 $url = "https:$url" if $url =~ m!\A//!;
84 $url .= '/' if $url !~ m!/\z!;
92 my $ret = $self->mm && $self->search;
93 $self->{mm} = $self->{search} = undef;
97 sub msg_by_path ($$;$) {
98 my ($self, $path, $ref) = @_;
99 # TODO: allow other refs:
100 git($self)->cat_file('HEAD:'.$path, $ref);
103 sub msg_by_mid ($$;$) {
104 my ($self, $mid, $ref) = @_;
105 msg_by_path($self, mid2path($mid), $ref);