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