]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
inbox: describe the full key name
[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 _set_uint ($$$) {
33         my ($opts, $field, $default) = @_;
34         my $val = $opts->{$field};
35         if (defined $val) {
36                 $val = $val->[-1] if ref($val) eq 'ARRAY';
37                 $val = undef if $val !~ /\A\d+\z/;
38         }
39         $opts->{$field} = $val || $default;
40 }
41
42 sub new {
43         my ($class, $opts) = @_;
44         my $v = $opts->{address} ||= 'public-inbox@example.com';
45         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
46         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
47         _set_uint($opts, 'feedmax', 25);
48         weaken($opts->{-pi_config});
49         bless $opts, $class;
50 }
51
52 sub _weaken_fields {
53         my ($self) = @_;
54         foreach my $f (qw(git mm search)) {
55                 isweak($self->{$f}) or weaken($self->{$f});
56         }
57 }
58
59 sub _set_limiter ($$$) {
60         my ($self, $git, $pfx) = @_;
61         my $lkey = "-${pfx}_limiter";
62         $git->{$lkey} = $self->{$lkey} ||= eval {
63                 # full key is: publicinbox.$NAME.httpbackendmax
64                 my $mkey = $pfx.'max';
65                 my $val = $self->{$mkey} or return;
66                 my $lim;
67                 if ($val =~ /\A\d+\z/) {
68                         require PublicInbox::Qspawn;
69                         $lim = PublicInbox::Qspawn::Limiter->new($val);
70                 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
71                         $lim = $self->{-pi_config}->limiter($val);
72                         warn "$mkey limiter=$val not found\n" if !$lim;
73                 } else {
74                         warn "$mkey limiter=$val not understood\n";
75                 }
76                 $lim;
77         }
78 }
79
80 sub git {
81         my ($self) = @_;
82         $self->{git} ||= eval {
83                 _weaken_later($self);
84                 my $g = PublicInbox::Git->new($self->{mainrepo});
85                 _set_limiter($self, $g, 'httpbackend');
86                 $g;
87         };
88 }
89
90 sub mm {
91         my ($self) = @_;
92         $self->{mm} ||= eval {
93                 _weaken_later($self);
94                 PublicInbox::Msgmap->new($self->{mainrepo});
95         };
96 }
97
98 sub search {
99         my ($self) = @_;
100         $self->{search} ||= eval {
101                 _weaken_later($self);
102                 PublicInbox::Search->new($self->{mainrepo}, $self->{altid});
103         };
104 }
105
106 sub try_cat {
107         my ($path) = @_;
108         my $rv = '';
109         if (open(my $fh, '<', $path)) {
110                 local $/;
111                 $rv = <$fh>;
112         }
113         $rv;
114 }
115
116 sub description {
117         my ($self) = @_;
118         my $desc = $self->{description};
119         return $desc if defined $desc;
120         $desc = try_cat("$self->{mainrepo}/description");
121         local $/ = "\n";
122         chomp $desc;
123         $desc =~ s/\s+/ /smg;
124         $desc = '($GIT_DIR/description missing)' if $desc eq '';
125         $self->{description} = $desc;
126 }
127
128 sub cloneurl {
129         my ($self) = @_;
130         my $url = $self->{cloneurl};
131         return $url if $url;
132         $url = try_cat("$self->{mainrepo}/cloneurl");
133         my @url = split(/\s+/s, $url);
134         local $/ = "\n";
135         chomp @url;
136         $self->{cloneurl} = \@url;
137 }
138
139 sub base_url {
140         my ($self, $env) = @_;
141         if ($env) { # PSGI env
142                 my $scheme = $env->{'psgi.url_scheme'};
143                 my $host_port = $env->{HTTP_HOST} ||
144                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
145                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
146                 # for mount in Plack::Builder
147                 $url .= '/' if $url !~ m!/\z!;
148                 $url .= $self->{name} . '/';
149         } else {
150                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
151                 $self->{-base_url} ||= do {
152                         my $url = $self->{url} or return undef;
153                         # expand protocol-relative URLs to HTTPS if we're
154                         # not inside a web server
155                         $url = "https:$url" if $url =~ m!\A//!;
156                         $url .= '/' if $url !~ m!/\z!;
157                         $url;
158                 };
159         }
160 }
161
162 sub nntp_url {
163         my ($self) = @_;
164         $self->{-nntp_url} ||= do {
165                 # no checking for nntp_usable here, we can point entirely
166                 # to non-local servers or users run by a different user
167                 my $ns = $self->{-pi_config}->{'publicinbox.nntpserver'};
168                 my $group = $self->{newsgroup};
169                 my @urls;
170                 if ($ns && $group) {
171                         $ns = [ $ns ] if ref($ns) ne 'ARRAY';
172                         @urls = map {
173                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
174                                 $u .= '/' if $u !~ m!/\z!;
175                                 $u.$group;
176                         } @$ns;
177                 }
178
179                 my $mirrors = $self->{nntpmirror};
180                 if ($mirrors) {
181                         my @m;
182                         foreach (@$mirrors) {
183                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
184                                 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
185                                         if ($group) {
186                                                 $u .= '/' if $u !~ m!/\z!;
187                                                 $u .= $group;
188                                         } else {
189                                                 warn
190 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
191                                         }
192                                 }
193                                 # else: allow full URLs like:
194                                 # nntp://news.example.com/alt.example
195                                 push @m, $u;
196                         }
197                         my %seen = map { $_ => 1 } @urls;
198                         foreach my $u (@m) {
199                                 next if $seen{$u};
200                                 $seen{$u} = 1;
201                                 push @urls, $u;
202                         }
203                 }
204                 \@urls;
205         };
206 }
207
208 sub nntp_usable {
209         my ($self) = @_;
210         my $ret = $self->mm && $self->search;
211         $self->{mm} = $self->{search} = undef;
212         $ret;
213 }
214
215 sub msg_by_path ($$;$) {
216         my ($self, $path, $ref) = @_;
217         # TODO: allow other refs:
218         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
219         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
220         $str;
221 }
222
223 sub msg_by_smsg ($$;$) {
224         my ($self, $smsg, $ref) = @_;
225
226         return unless defined $smsg; # ghost
227
228         # backwards compat to fallback to msg_by_mid
229         # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
230         defined(my $blob = $smsg->blob) or return msg_by_mid($self, $smsg->mid);
231
232         my $str = git($self)->cat_file($blob, $ref);
233         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
234         $str;
235 }
236
237 sub path_check {
238         my ($self, $path) = @_;
239         git($self)->check('HEAD:'.$path);
240 }
241
242 sub msg_by_mid ($$;$) {
243         my ($self, $mid, $ref) = @_;
244         msg_by_path($self, mid2path($mid), $ref);
245 }
246
247 1;