]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
evcleanup: disable outside of daemon
[public-inbox.git] / lib / PublicInbox / Inbox.pm
1 # Copyright (C) 2016-2018 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 PublicInbox::Git;
9 use PublicInbox::MID qw(mid2path);
10 use Devel::Peek qw(SvREFCNT);
11
12 my $cleanup_timer;
13 eval {
14         $cleanup_timer = 'disabled';
15         require PublicInbox::EvCleanup;
16         $cleanup_timer = undef; # OK if we get here
17 };
18
19 my $CLEANUP = {}; # string(inbox) -> inbox
20 sub cleanup_task () {
21         $cleanup_timer = undef;
22         for my $ibx (values %$CLEANUP) {
23                 foreach my $f (qw(git mm search)) {
24                         delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
25                 }
26         }
27         $CLEANUP = {};
28 }
29
30 sub _cleanup_later ($) {
31         my ($self) = @_;
32         return unless $PublicInbox::EvCleanup::ENABLED;
33         $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
34         $CLEANUP->{"$self"} = $self;
35 }
36
37 sub _set_uint ($$$) {
38         my ($opts, $field, $default) = @_;
39         my $val = $opts->{$field};
40         if (defined $val) {
41                 $val = $val->[-1] if ref($val) eq 'ARRAY';
42                 $val = undef if $val !~ /\A\d+\z/;
43         }
44         $opts->{$field} = $val || $default;
45 }
46
47 sub _set_limiter ($$$) {
48         my ($self, $pi_config, $pfx) = @_;
49         my $lkey = "-${pfx}_limiter";
50         $self->{$lkey} ||= eval {
51                 # full key is: publicinbox.$NAME.httpbackendmax
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 = $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 new {
69         my ($class, $opts) = @_;
70         my $v = $opts->{address} ||= 'public-inbox@example.com';
71         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
72         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
73         my $pi_config = delete $opts->{-pi_config};
74         _set_limiter($opts, $pi_config, 'httpbackend');
75         _set_uint($opts, 'feedmax', 25);
76         $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
77         my $dir = $opts->{mainrepo};
78         if (defined $dir && -f "$dir/msgmap.sqlite3") { # XXX DIRTY
79                 $opts->{version} = 2;
80         }
81         bless $opts, $class;
82 }
83
84 sub git {
85         my ($self) = @_;
86         $self->{git} ||= eval {
87                 my $git_dir = $self->{mainrepo};
88                 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
89                 my $g = PublicInbox::Git->new($git_dir);
90                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
91                 _cleanup_later($self);
92                 $g;
93         };
94 }
95
96 sub mm {
97         my ($self) = @_;
98         $self->{mm} ||= eval {
99                 _cleanup_later($self);
100                 my $dir = $self->{mainrepo};
101                 if (($self->{version} || 1) >= 2) {
102                         PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
103                 } else {
104                         PublicInbox::Msgmap->new($dir);
105                 }
106         };
107 }
108
109 sub search {
110         my ($self) = @_;
111         $self->{search} ||= eval {
112                 _cleanup_later($self);
113                 PublicInbox::Search->new($self, $self->{altid});
114         };
115 }
116
117 sub try_cat {
118         my ($path) = @_;
119         my $rv = '';
120         if (open(my $fh, '<', $path)) {
121                 local $/;
122                 $rv = <$fh>;
123         }
124         $rv;
125 }
126
127 sub description {
128         my ($self) = @_;
129         my $desc = $self->{description};
130         return $desc if defined $desc;
131         $desc = try_cat("$self->{mainrepo}/description");
132         local $/ = "\n";
133         chomp $desc;
134         $desc =~ s/\s+/ /smg;
135         $desc = '($GIT_DIR/description missing)' if $desc eq '';
136         $self->{description} = $desc;
137 }
138
139 sub cloneurl {
140         my ($self) = @_;
141         my $url = $self->{cloneurl};
142         return $url if $url;
143         $url = try_cat("$self->{mainrepo}/cloneurl");
144         my @url = split(/\s+/s, $url);
145         local $/ = "\n";
146         chomp @url;
147         $self->{cloneurl} = \@url;
148 }
149
150 sub base_url {
151         my ($self, $env) = @_;
152         if ($env) { # PSGI env
153                 my $scheme = $env->{'psgi.url_scheme'};
154                 my $host_port = $env->{HTTP_HOST} ||
155                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
156                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
157                 # for mount in Plack::Builder
158                 $url .= '/' if $url !~ m!/\z!;
159                 $url .= $self->{name} . '/';
160         } else {
161                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
162                 $self->{-base_url} ||= do {
163                         my $url = $self->{url} or return undef;
164                         # expand protocol-relative URLs to HTTPS if we're
165                         # not inside a web server
166                         $url = "https:$url" if $url =~ m!\A//!;
167                         $url .= '/' if $url !~ m!/\z!;
168                         $url;
169                 };
170         }
171 }
172
173 sub nntp_url {
174         my ($self) = @_;
175         $self->{-nntp_url} ||= do {
176                 # no checking for nntp_usable here, we can point entirely
177                 # to non-local servers or users run by a different user
178                 my $ns = $self->{nntpserver};
179                 my $group = $self->{newsgroup};
180                 my @urls;
181                 if ($ns && $group) {
182                         $ns = [ $ns ] if ref($ns) ne 'ARRAY';
183                         @urls = map {
184                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
185                                 $u .= '/' if $u !~ m!/\z!;
186                                 $u.$group;
187                         } @$ns;
188                 }
189
190                 my $mirrors = $self->{nntpmirror};
191                 if ($mirrors) {
192                         my @m;
193                         foreach (@$mirrors) {
194                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
195                                 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
196                                         if ($group) {
197                                                 $u .= '/' if $u !~ m!/\z!;
198                                                 $u .= $group;
199                                         } else {
200                                                 warn
201 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
202                                         }
203                                 }
204                                 # else: allow full URLs like:
205                                 # nntp://news.example.com/alt.example
206                                 push @m, $u;
207                         }
208                         my %seen = map { $_ => 1 } @urls;
209                         foreach my $u (@m) {
210                                 next if $seen{$u};
211                                 $seen{$u} = 1;
212                                 push @urls, $u;
213                         }
214                 }
215                 \@urls;
216         };
217 }
218
219 sub nntp_usable {
220         my ($self) = @_;
221         my $ret = $self->mm && $self->search;
222         $self->{mm} = $self->{search} = undef;
223         $ret;
224 }
225
226 sub msg_by_path ($$;$) {
227         my ($self, $path, $ref) = @_;
228         # TODO: allow other refs:
229         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
230         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
231         $str;
232 }
233
234 sub msg_by_smsg ($$;$) {
235         my ($self, $smsg, $ref) = @_;
236
237         return unless defined $smsg; # ghost
238
239         # backwards compat to fallback to msg_by_mid
240         # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
241         defined(my $blob = $smsg->{blob}) or
242                         return msg_by_path($self, mid2path($smsg->mid), $ref);
243
244         my $str = git($self)->cat_file($blob, $ref);
245         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
246         $str;
247 }
248
249 sub path_check {
250         my ($self, $path) = @_;
251         git($self)->check('HEAD:'.$path);
252 }
253
254 sub msg_by_mid ($$;$) {
255         my ($self, $mid, $ref) = @_;
256         my $srch = search($self) or
257                         return msg_by_path($self, mid2path($mid), $ref);
258         my $smsg;
259         $srch->retry_reopen(sub {
260                 $smsg = $srch->lookup_skeleton($mid) and $smsg->load_expand;
261         });
262         $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
263 }
264
265 1;