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