]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
ada713c741657ef16522d4b118cffebcbd95c4be
[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 isweak);
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         foreach my $f (qw(git mm search)) {
23                 isweak($self->{$f}) or weaken($self->{$f});
24         }
25 }
26
27 sub git {
28         my ($self) = @_;
29         $self->{git} ||= eval { PublicInbox::Git->new($self->{mainrepo}) };
30 }
31
32 sub mm {
33         my ($self) = @_;
34         $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{mainrepo}) };
35 }
36
37 sub search {
38         my ($self) = @_;
39         $self->{search} ||= eval { PublicInbox::Search->new($self->{mainrepo}) };
40 }
41
42 sub try_cat {
43         my ($path) = @_;
44         my $rv = '';
45         if (open(my $fh, '<', $path)) {
46                 local $/;
47                 $rv = <$fh>;
48         }
49         $rv;
50 }
51
52 sub description {
53         my ($self) = @_;
54         my $desc = $self->{description};
55         return $desc if defined $desc;
56         $desc = try_cat("$self->{mainrepo}/description");
57         chomp $desc;
58         $desc =~ s/\s+/ /smg;
59         $desc = '($GIT_DIR/description missing)' if $desc eq '';
60         $self->{description} = $desc;
61 }
62
63 sub cloneurl {
64         my ($self) = @_;
65         my $url = $self->{cloneurl};
66         return $url if $url;
67         $url = try_cat("$self->{mainrepo}/cloneurl");
68         my @url = split(/\s+/s, $url);
69         chomp @url;
70         $self->{cloneurl} = \@url;
71 }
72
73 sub base_url {
74         my ($self, $prq) = @_; # Plack::Request
75         if (defined $prq) {
76                 my $url = $prq->base->as_string;
77                 $url .= '/' if $url !~ m!/\z!; # for mount in Plack::Builder
78                 $url .= $self->{name} . '/';
79         } else {
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!;
87                         $url;
88                 };
89         }
90 }
91
92 sub nntp_usable {
93         my ($self) = @_;
94         my $ret = $self->mm && $self->search;
95         $self->{mm} = $self->{search} = undef;
96         $ret;
97 }
98
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;
104         $str;
105 }
106
107 sub msg_by_mid ($$;$) {
108         my ($self, $mid, $ref) = @_;
109         msg_by_path($self, mid2path($mid), $ref);
110 }
111
112 1;