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