]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
inbox: move field population logic to initializer
[public-inbox.git] / lib / PublicInbox / Inbox.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Represents a public-inbox (which may have multiple mailing addresses)
5 package PublicInbox::Inbox;
6 use strict;
7 use warnings;
8 use Scalar::Util qw(weaken);
9 use PublicInbox::Git;
10 use PublicInbox::MID qw(mid2path);
11
12 sub new {
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';
17         bless $opts, $class;
18 }
19
20 sub weaken_all {
21         my ($self) = @_;
22         weaken($self->{$_}) foreach qw(git mm search);
23 }
24
25 sub git {
26         my ($self) = @_;
27         $self->{git} ||= eval { PublicInbox::Git->new($self->{mainrepo}) };
28 }
29
30 sub mm {
31         my ($self) = @_;
32         $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{mainrepo}) };
33 }
34
35 sub search {
36         my ($self) = @_;
37         $self->{search} ||= eval { PublicInbox::Search->new($self->{mainrepo}) };
38 }
39
40 sub try_cat {
41         my ($path) = @_;
42         my $rv = '';
43         if (open(my $fh, '<', $path)) {
44                 local $/;
45                 $rv = <$fh>;
46         }
47         $rv;
48 }
49
50 sub description {
51         my ($self) = @_;
52         my $desc = $self->{description};
53         return $desc if defined $desc;
54         $desc = try_cat("$self->{mainrepo}/description");
55         chomp $desc;
56         $desc =~ s/\s+/ /smg;
57         $desc = '($GIT_DIR/description missing)' if $desc eq '';
58         $self->{description} = $desc;
59 }
60
61 sub cloneurl {
62         my ($self) = @_;
63         my $url = $self->{cloneurl};
64         return $url if $url;
65         $url = try_cat("$self->{mainrepo}/cloneurl");
66         my @url = split(/\s+/s, $url);
67         chomp @url;
68         $self->{cloneurl} = \@url;
69 }
70
71 sub base_url {
72         my ($self, $prq) = @_; # Plack::Request
73         if (defined $prq) {
74                 my $url = $prq->base->as_string;
75                 $url .= '/' if $url !~ m!/\z!; # for mount in Plack::Builder
76                 $url .= $self->{name} . '/';
77         } else {
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!;
85                         $url;
86                 };
87         }
88 }
89
90 sub nntp_usable {
91         my ($self) = @_;
92         my $ret = $self->mm && $self->search;
93         $self->{mm} = $self->{search} = undef;
94         $ret;
95 }
96
97 sub msg_by_path ($$;$) {
98         my ($self, $path, $ref) = @_;
99         # TODO: allow other refs:
100         git($self)->cat_file('HEAD:'.$path, $ref);
101 }
102
103 sub msg_by_mid ($$;$) {
104         my ($self, $mid, $ref) = @_;
105         msg_by_path($self, mid2path($mid), $ref);
106 }
107
108 1;