]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
inbox: base_url method takes PSGI env hashref instead
[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, $env) = @_;
75         if ($env) { # PSGI env
76                 my $scheme = $env->{'psgi.url_scheme'};
77                 my $host_port = $env->{HTTP_HOST} ||
78                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
79                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
80                 # for mount in Plack::Builder
81                 $url .= '/' if $url !~ m!/\z!;
82                 $url .= $self->{name} . '/';
83         } else {
84                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
85                 $self->{-base_url} ||= do {
86                         my $url = $self->{url} or return undef;
87                         # expand protocol-relative URLs to HTTPS if we're
88                         # not inside a web server
89                         $url = "https:$url" if $url =~ m!\A//!;
90                         $url .= '/' if $url !~ m!/\z!;
91                         $url;
92                 };
93         }
94 }
95
96 sub nntp_usable {
97         my ($self) = @_;
98         my $ret = $self->mm && $self->search;
99         $self->{mm} = $self->{search} = undef;
100         $ret;
101 }
102
103 sub msg_by_path ($$;$) {
104         my ($self, $path, $ref) = @_;
105         # TODO: allow other refs:
106         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
107         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
108         $str;
109 }
110
111 sub msg_by_mid ($$;$) {
112         my ($self, $mid, $ref) = @_;
113         msg_by_path($self, mid2path($mid), $ref);
114 }
115
116 1;