]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
feed: various object-orientation cleanups
[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         bless $opts, $class;
15 }
16
17 sub weaken_all {
18         my ($self) = @_;
19         weaken($self->{$_}) foreach qw(git mm search);
20 }
21
22 sub git {
23         my ($self) = @_;
24         $self->{git} ||= eval { PublicInbox::Git->new($self->{mainrepo}) };
25 }
26
27 sub mm {
28         my ($self) = @_;
29         $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{mainrepo}) };
30 }
31
32 sub search {
33         my ($self) = @_;
34         $self->{search} ||= eval { PublicInbox::Search->new($self->{mainrepo}) };
35 }
36
37 sub try_cat {
38         my ($path) = @_;
39         my $rv = '';
40         if (open(my $fh, '<', $path)) {
41                 local $/;
42                 $rv = <$fh>;
43         }
44         $rv;
45 }
46
47 sub description {
48         my ($self) = @_;
49         my $desc = $self->{description};
50         return $desc if defined $desc;
51         $desc = try_cat("$self->{mainrepo}/description");
52         chomp $desc;
53         $desc =~ s/\s+/ /smg;
54         $desc = '($GIT_DIR/description missing)' if $desc eq '';
55         $self->{description} = $desc;
56 }
57
58 sub cloneurl {
59         my ($self) = @_;
60         my $url = $self->{cloneurl};
61         return $url if $url;
62         $url = try_cat("$self->{mainrepo}/cloneurl");
63         my @url = split(/\s+/s, $url);
64         chomp @url;
65         $self->{cloneurl} = \@url;
66 }
67
68 sub base_url {
69         my ($self, $prq) = @_; # Plack::Request
70         if (defined $prq) {
71                 my $url = $prq->base->as_string;
72                 $url .= '/' if $url !~ m!/\z!; # for mount in Plack::Builder
73                 $url .= $self->{name} . '/';
74         } else {
75                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
76                 $self->{-base_url} ||= do {
77                         my $url = $self->{url} or return undef;
78                         # expand protocol-relative URLs to HTTPS if we're
79                         # not inside a web server
80                         $url = "https:$url" if $url =~ m!\A//!;
81                         $url .= '/' if $url !~ m!/\z!;
82                         $url;
83                 };
84         }
85 }
86
87 sub nntp_usable {
88         my ($self) = @_;
89         my $ret = $self->mm && $self->search;
90         $self->{mm} = $self->{search} = undef;
91         $ret;
92 }
93
94 sub msg_by_path ($$;$) {
95         my ($self, $path, $ref) = @_;
96         # TODO: allow other refs:
97         git($self)->cat_file('HEAD:'.$path, $ref);
98 }
99
100 sub msg_by_mid ($$;$) {
101         my ($self, $mid, $ref) = @_;
102         msg_by_path($self, mid2path($mid), $ref);
103 }
104
105 1;