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);
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 foreach my $f (qw(git mm search)) {
23 isweak($self->{$f}) or weaken($self->{$f});
29 $self->{git} ||= eval { PublicInbox::Git->new($self->{mainrepo}) };
34 $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{mainrepo}) };
39 $self->{search} ||= eval { PublicInbox::Search->new($self->{mainrepo}) };
45 if (open(my $fh, '<', $path)) {
54 my $desc = $self->{description};
55 return $desc if defined $desc;
56 $desc = try_cat("$self->{mainrepo}/description");
59 $desc = '($GIT_DIR/description missing)' if $desc eq '';
60 $self->{description} = $desc;
65 my $url = $self->{cloneurl};
67 $url = try_cat("$self->{mainrepo}/cloneurl");
68 my @url = split(/\s+/s, $url);
70 $self->{cloneurl} = \@url;
74 my ($self, $prq) = @_; # Plack::Request
76 my $url = $prq->base->as_string;
77 $url .= '/' if $url !~ m!/\z!; # for mount in Plack::Builder
78 $url .= $self->{name} . '/';
80 # either called from a non-PSGI environment (e.g. NNTP/POP3)
81 $self->{-base_url} ||= do {
82 my $url = $self->{url} or return undef;
83 # expand protocol-relative URLs to HTTPS if we're
84 # not inside a web server
85 $url = "https:$url" if $url =~ m!\A//!;
86 $url .= '/' if $url !~ m!/\z!;
94 my $ret = $self->mm && $self->search;
95 $self->{mm} = $self->{search} = undef;
99 sub msg_by_path ($$;$) {
100 my ($self, $path, $ref) = @_;
101 # TODO: allow other refs:
102 my $str = git($self)->cat_file('HEAD:'.$path, $ref);
103 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
107 sub msg_by_mid ($$;$) {
108 my ($self, $mid, $ref) = @_;
109 msg_by_path($self, mid2path($mid), $ref);