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