]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
922ca9bbdc78c297e8c690fee95cd7c6b34243bb
[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 my $weakt;
13 eval {
14         $weakt = 'disabled';
15         require PublicInbox::EvCleanup;
16         $weakt = undef; # OK if we get here
17 };
18
19 my $WEAKEN = {}; # string(inbox) -> inbox
20 sub weaken_task () {
21         $weakt = undef;
22         _weaken_fields($_) for values %$WEAKEN;
23         $WEAKEN = {};
24 }
25
26 sub _weaken_later ($) {
27         my ($self) = @_;
28         $weakt ||= PublicInbox::EvCleanup::later(*weaken_task);
29         $WEAKEN->{"$self"} = $self;
30 }
31
32 sub new {
33         my ($class, $opts) = @_;
34         my $v = $opts->{address} ||= 'public-inbox@example.com';
35         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
36         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
37         weaken($opts->{-pi_config});
38         bless $opts, $class;
39 }
40
41 sub _weaken_fields {
42         my ($self) = @_;
43         foreach my $f (qw(git mm search)) {
44                 isweak($self->{$f}) or weaken($self->{$f});
45         }
46 }
47
48 sub _set_limiter ($$$) {
49         my ($self, $git, $pfx) = @_;
50         my $lkey = "-${pfx}_limiter";
51         $git->{$lkey} = $self->{$lkey} ||= eval {
52                 my $mkey = $pfx.'max';
53                 my $val = $self->{$mkey} or return;
54                 my $lim;
55                 if ($val =~ /\A\d+\z/) {
56                         require PublicInbox::Qspawn;
57                         $lim = PublicInbox::Qspawn::Limiter->new($val);
58                 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
59                         $lim = $self->{-pi_config}->limiter($val);
60                         warn "$mkey limiter=$val not found\n" if !$lim;
61                 } else {
62                         warn "$mkey limiter=$val not understood\n";
63                 }
64                 $lim;
65         }
66 }
67
68 sub git {
69         my ($self) = @_;
70         $self->{git} ||= eval {
71                 _weaken_later($self);
72                 my $g = PublicInbox::Git->new($self->{mainrepo});
73                 _set_limiter($self, $g, 'httpbackend');
74                 $g;
75         };
76 }
77
78 sub mm {
79         my ($self) = @_;
80         $self->{mm} ||= eval {
81                 _weaken_later($self);
82                 PublicInbox::Msgmap->new($self->{mainrepo});
83         };
84 }
85
86 sub search {
87         my ($self) = @_;
88         $self->{search} ||= eval {
89                 _weaken_later($self);
90                 PublicInbox::Search->new($self->{mainrepo}, $self->{altid});
91         };
92 }
93
94 sub try_cat {
95         my ($path) = @_;
96         my $rv = '';
97         if (open(my $fh, '<', $path)) {
98                 local $/;
99                 $rv = <$fh>;
100         }
101         $rv;
102 }
103
104 sub description {
105         my ($self) = @_;
106         my $desc = $self->{description};
107         return $desc if defined $desc;
108         $desc = try_cat("$self->{mainrepo}/description");
109         local $/ = "\n";
110         chomp $desc;
111         $desc =~ s/\s+/ /smg;
112         $desc = '($GIT_DIR/description missing)' if $desc eq '';
113         $self->{description} = $desc;
114 }
115
116 sub cloneurl {
117         my ($self) = @_;
118         my $url = $self->{cloneurl};
119         return $url if $url;
120         $url = try_cat("$self->{mainrepo}/cloneurl");
121         my @url = split(/\s+/s, $url);
122         local $/ = "\n";
123         chomp @url;
124         $self->{cloneurl} = \@url;
125 }
126
127 sub base_url {
128         my ($self, $env) = @_;
129         if ($env) { # PSGI env
130                 my $scheme = $env->{'psgi.url_scheme'};
131                 my $host_port = $env->{HTTP_HOST} ||
132                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
133                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
134                 # for mount in Plack::Builder
135                 $url .= '/' if $url !~ m!/\z!;
136                 $url .= $self->{name} . '/';
137         } else {
138                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
139                 $self->{-base_url} ||= do {
140                         my $url = $self->{url} or return undef;
141                         # expand protocol-relative URLs to HTTPS if we're
142                         # not inside a web server
143                         $url = "https:$url" if $url =~ m!\A//!;
144                         $url .= '/' if $url !~ m!/\z!;
145                         $url;
146                 };
147         }
148 }
149
150 sub nntp_usable {
151         my ($self) = @_;
152         my $ret = $self->mm && $self->search;
153         $self->{mm} = $self->{search} = undef;
154         $ret;
155 }
156
157 sub msg_by_path ($$;$) {
158         my ($self, $path, $ref) = @_;
159         # TODO: allow other refs:
160         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
161         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
162         $str;
163 }
164
165 sub msg_by_smsg ($$;$) {
166         my ($self, $smsg, $ref) = @_;
167
168         # backwards compat to fallback to msg_by_mid
169         # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
170         defined(my $blob = $smsg->blob) or return msg_by_mid($self, $smsg->mid);
171
172         my $str = git($self)->cat_file($blob, $ref);
173         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
174         $str;
175 }
176
177 sub path_check {
178         my ($self, $path) = @_;
179         git($self)->check('HEAD:'.$path);
180 }
181
182 sub msg_by_mid ($$;$) {
183         my ($self, $mid, $ref) = @_;
184         msg_by_path($self, mid2path($mid), $ref);
185 }
186
187 1;