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