]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
remove hard Devel::Peek dependency and lazy load for daemons
[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 PublicInbox::MIME;
11
12 # Long-running "git-cat-file --batch" processes won't notice
13 # unlinked packs, so we need to restart those processes occasionally.
14 # Xapian and SQLite file handles are mostly stable, but sometimes an
15 # admin will attempt to replace them atomically after compact/vacuum
16 # and we need to be prepared for that.
17 my $cleanup_timer;
18 my $cleanup_avail = -1; # 0, or 1
19 my $have_devel_peek;
20 my $CLEANUP = {}; # string(inbox) -> inbox
21 sub cleanup_task () {
22         $cleanup_timer = undef;
23         my $next = {};
24         for my $ibx (values %$CLEANUP) {
25                 my $again;
26                 if ($have_devel_peek) {
27                         foreach my $f (qw(mm search)) {
28                                 # we bump refcnt by assigning tmp, here:
29                                 my $tmp = $ibx->{$f} or next;
30                                 next if Devel::Peek::SvREFCNT($tmp) > 2;
31                                 delete $ibx->{$f};
32                                 # refcnt is zero when tmp is out-of-scope
33                         }
34                 }
35                 my $expire = time - 60;
36                 if (my $git = $ibx->{git}) {
37                         $again = $git->cleanup($expire);
38                 }
39                 if (my $gits = $ibx->{-repo_objs}) {
40                         foreach my $git (@$gits) {
41                                 $again = 1 if $git->cleanup($expire);
42                         }
43                 }
44                 if ($have_devel_peek) {
45                         $again ||= !!($ibx->{mm} || $ibx->{search});
46                 }
47                 $next->{"$ibx"} = $ibx if $again;
48         }
49         $CLEANUP = $next;
50 }
51
52 sub cleanup_possible () {
53         # no need to require EvCleanup, here, if it were enabled another
54         # module would've require'd it, already
55         eval { PublicInbox::EvCleanup::enabled() } or return 0;
56
57         eval {
58                 require Devel::Peek; # needs separate package in Fedora
59                 $have_devel_peek = 1;
60         };
61         1;
62 }
63
64 sub _cleanup_later ($) {
65         my ($self) = @_;
66         $cleanup_avail = cleanup_possible() if $cleanup_avail < 0;
67         return if $cleanup_avail != 1;
68         $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
69         $CLEANUP->{"$self"} = $self;
70 }
71
72 sub _set_uint ($$$) {
73         my ($opts, $field, $default) = @_;
74         my $val = $opts->{$field};
75         if (defined $val) {
76                 $val = $val->[-1] if ref($val) eq 'ARRAY';
77                 $val = undef if $val !~ /\A\d+\z/;
78         }
79         $opts->{$field} = $val || $default;
80 }
81
82 sub _set_limiter ($$$) {
83         my ($self, $pi_config, $pfx) = @_;
84         my $lkey = "-${pfx}_limiter";
85         $self->{$lkey} ||= eval {
86                 # full key is: publicinbox.$NAME.httpbackendmax
87                 my $mkey = $pfx.'max';
88                 my $val = $self->{$mkey} or return;
89                 my $lim;
90                 if ($val =~ /\A\d+\z/) {
91                         require PublicInbox::Qspawn;
92                         $lim = PublicInbox::Qspawn::Limiter->new($val);
93                 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
94                         $lim = $pi_config->limiter($val);
95                         warn "$mkey limiter=$val not found\n" if !$lim;
96                 } else {
97                         warn "$mkey limiter=$val not understood\n";
98                 }
99                 $lim;
100         }
101 }
102
103 sub new {
104         my ($class, $opts) = @_;
105         my $v = $opts->{address} ||= 'public-inbox@example.com';
106         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
107         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
108         my $pi_config = delete $opts->{-pi_config};
109         _set_limiter($opts, $pi_config, 'httpbackend');
110         _set_uint($opts, 'feedmax', 25);
111         $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
112         my $dir = $opts->{mainrepo};
113         if (defined $dir && -f "$dir/inbox.lock") {
114                 $opts->{version} = 2;
115         }
116
117         # allow any combination of multi-line or comma-delimited hide entries
118         my $hide = {};
119         if (defined(my $h = $opts->{hide})) {
120                 foreach my $v (@$h) {
121                         $hide->{$_} = 1 foreach (split(/\s*,\s*/, $v));
122                 }
123                 $opts->{-hide} = $hide;
124         }
125         bless $opts, $class;
126 }
127
128 sub git_part {
129         my ($self, $part) = @_;
130         ($self->{version} || 1) == 2 or return;
131         $self->{"$part.git"} ||= eval {
132                 my $git_dir = "$self->{mainrepo}/git/$part.git";
133                 my $g = PublicInbox::Git->new($git_dir);
134                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
135                 # no cleanup needed, we never cat-file off this, only clone
136                 $g;
137         };
138 }
139
140 sub git {
141         my ($self) = @_;
142         $self->{git} ||= eval {
143                 my $git_dir = $self->{mainrepo};
144                 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
145                 my $g = PublicInbox::Git->new($git_dir);
146                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
147                 _cleanup_later($self);
148                 $g;
149         };
150 }
151
152 sub max_git_part {
153         my ($self) = @_;
154         my $v = $self->{version};
155         return unless defined($v) && $v == 2;
156         my $part = $self->{-max_git_part};
157         my $changed = git($self)->alternates_changed;
158         if (!defined($part) || $changed) {
159                 $self->git->cleanup if $changed;
160                 my $gits = "$self->{mainrepo}/git";
161                 if (opendir my $dh, $gits) {
162                         my $max = -1;
163                         while (defined(my $git_dir = readdir($dh))) {
164                                 $git_dir =~ m!\A(\d+)\.git\z! or next;
165                                 $max = $1 if $1 > $max;
166                         }
167                         $part = $self->{-max_git_part} = $max if $max >= 0;
168                 } else {
169                         warn "opendir $gits failed: $!\n";
170                 }
171         }
172         $part;
173 }
174
175 sub mm {
176         my ($self) = @_;
177         $self->{mm} ||= eval {
178                 require PublicInbox::Msgmap;
179                 _cleanup_later($self);
180                 my $dir = $self->{mainrepo};
181                 if (($self->{version} || 1) >= 2) {
182                         PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
183                 } else {
184                         PublicInbox::Msgmap->new($dir);
185                 }
186         };
187 }
188
189 sub search {
190         my ($self) = @_;
191         $self->{search} ||= eval {
192                 _cleanup_later($self);
193                 PublicInbox::Search->new($self, $self->{altid});
194         };
195 }
196
197 sub try_cat {
198         my ($path) = @_;
199         my $rv = '';
200         if (open(my $fh, '<', $path)) {
201                 local $/;
202                 $rv = <$fh>;
203         }
204         $rv;
205 }
206
207 sub description {
208         my ($self) = @_;
209         my $desc = $self->{description};
210         return $desc if defined $desc;
211         $desc = try_cat("$self->{mainrepo}/description");
212         local $/ = "\n";
213         chomp $desc;
214         $desc =~ s/\s+/ /smg;
215         $desc = '($REPO_DIR/description missing)' if $desc eq '';
216         $self->{description} = $desc;
217 }
218
219 sub cloneurl {
220         my ($self) = @_;
221         my $url = $self->{cloneurl};
222         return $url if $url;
223         $url = try_cat("$self->{mainrepo}/cloneurl");
224         my @url = split(/\s+/s, $url);
225         local $/ = "\n";
226         chomp @url;
227         $self->{cloneurl} = \@url;
228 }
229
230 sub base_url {
231         my ($self, $env) = @_;
232         my $scheme;
233         if ($env && ($scheme = $env->{'psgi.url_scheme'})) { # PSGI env
234                 my $host_port = $env->{HTTP_HOST} ||
235                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
236                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
237                 # for mount in Plack::Builder
238                 $url .= '/' if $url !~ m!/\z!;
239                 $url .= $self->{name} . '/';
240         } else {
241                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
242                 $self->{-base_url} ||= do {
243                         my $url = $self->{url} or return undef;
244                         # expand protocol-relative URLs to HTTPS if we're
245                         # not inside a web server
246                         $url = "https:$url" if $url =~ m!\A//!;
247                         $url .= '/' if $url !~ m!/\z!;
248                         $url;
249                 };
250         }
251 }
252
253 sub nntp_url {
254         my ($self) = @_;
255         $self->{-nntp_url} ||= do {
256                 # no checking for nntp_usable here, we can point entirely
257                 # to non-local servers or users run by a different user
258                 my $ns = $self->{nntpserver};
259                 my $group = $self->{newsgroup};
260                 my @urls;
261                 if ($ns && $group) {
262                         $ns = [ $ns ] if ref($ns) ne 'ARRAY';
263                         @urls = map {
264                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
265                                 $u .= '/' if $u !~ m!/\z!;
266                                 $u.$group;
267                         } @$ns;
268                 }
269
270                 my $mirrors = $self->{nntpmirror};
271                 if ($mirrors) {
272                         my @m;
273                         foreach (@$mirrors) {
274                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
275                                 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
276                                         if ($group) {
277                                                 $u .= '/' if $u !~ m!/\z!;
278                                                 $u .= $group;
279                                         } else {
280                                                 warn
281 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
282                                         }
283                                 }
284                                 # else: allow full URLs like:
285                                 # nntp://news.example.com/alt.example
286                                 push @m, $u;
287                         }
288                         my %seen = map { $_ => 1 } @urls;
289                         foreach my $u (@m) {
290                                 next if $seen{$u};
291                                 $seen{$u} = 1;
292                                 push @urls, $u;
293                         }
294                 }
295                 \@urls;
296         };
297 }
298
299 sub nntp_usable {
300         my ($self) = @_;
301         my $ret = $self->mm && $self->search;
302         $self->{mm} = $self->{search} = undef;
303         $ret;
304 }
305
306 sub msg_by_path ($$;$) {
307         my ($self, $path, $ref) = @_;
308         # TODO: allow other refs:
309         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
310         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
311         $str;
312 }
313
314 sub msg_by_smsg ($$;$) {
315         my ($self, $smsg, $ref) = @_;
316
317         # ghosts may have undef smsg (from SearchThread.node) or
318         # no {blob} field
319         return unless defined $smsg;
320         defined(my $blob = $smsg->{blob}) or return;
321
322         my $str = git($self)->cat_file($blob, $ref);
323         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
324         $str;
325 }
326
327 sub smsg_mime {
328         my ($self, $smsg, $ref) = @_;
329         if (my $s = msg_by_smsg($self, $smsg, $ref)) {
330                 $smsg->{mime} = PublicInbox::MIME->new($s);
331                 return $smsg;
332         }
333 }
334
335 sub mid2num($$) {
336         my ($self, $mid) = @_;
337         my $mm = mm($self) or return;
338         $mm->num_for($mid);
339 }
340
341 sub smsg_by_mid ($$) {
342         my ($self, $mid) = @_;
343         my $srch = search($self) or return;
344         # favor the Message-ID we used for the NNTP article number:
345         defined(my $num = mid2num($self, $mid)) or return;
346         my $smsg = $srch->lookup_article($num) or return;
347         PublicInbox::SearchMsg::psgi_cull($smsg);
348 }
349
350 sub msg_by_mid ($$;$) {
351         my ($self, $mid, $ref) = @_;
352         my $srch = search($self) or
353                 return msg_by_path($self, mid2path($mid), $ref);
354         my $smsg = smsg_by_mid($self, $mid);
355         $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
356 }
357
358 sub recent {
359         my ($self, $opts, $after, $before) = @_;
360         search($self)->{over_ro}->recent($opts, $after, $before);
361 }
362
363 sub modified {
364         my ($self) = @_;
365         if (my $srch = search($self)) {
366                 my $msgs = $srch->{over_ro}->recent({limit => 1});
367                 if (my $smsg = $msgs->[0]) {
368                         return $smsg->{ts};
369                 }
370                 return time;
371         }
372         git($self)->modified; # v1
373 }
374
375 1;