]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
inbox: reinstate periodic cleanup of Xapian and SQLite objects
[public-inbox.git] / lib / PublicInbox / Inbox.pm
1 # Copyright (C) 2016 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
12 my $cleanup_timer;
13 eval {
14         $cleanup_timer = 'disabled';
15         require PublicInbox::EvCleanup;
16         $cleanup_timer = undef; # OK if we get here
17 };
18
19 my $CLEANUP = {}; # string(inbox) -> inbox
20 sub cleanup_task () {
21         $cleanup_timer = undef;
22         for my $ibx (values %$CLEANUP) {
23                 foreach my $f (qw(git mm search)) {
24                         delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
25                 }
26         }
27         $CLEANUP = {};
28 }
29
30 sub _cleanup_later ($) {
31         my ($self) = @_;
32         $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
33         $CLEANUP->{"$self"} = $self;
34 }
35
36 sub _set_uint ($$$) {
37         my ($opts, $field, $default) = @_;
38         my $val = $opts->{$field};
39         if (defined $val) {
40                 $val = $val->[-1] if ref($val) eq 'ARRAY';
41                 $val = undef if $val !~ /\A\d+\z/;
42         }
43         $opts->{$field} = $val || $default;
44 }
45
46 sub _set_limiter ($$$) {
47         my ($self, $pi_config, $pfx) = @_;
48         my $lkey = "-${pfx}_limiter";
49         $self->{$lkey} ||= eval {
50                 # full key is: publicinbox.$NAME.httpbackendmax
51                 my $mkey = $pfx.'max';
52                 my $val = $self->{$mkey} or return;
53                 my $lim;
54                 if ($val =~ /\A\d+\z/) {
55                         require PublicInbox::Qspawn;
56                         $lim = PublicInbox::Qspawn::Limiter->new($val);
57                 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
58                         $lim = $pi_config->limiter($val);
59                         warn "$mkey limiter=$val not found\n" if !$lim;
60                 } else {
61                         warn "$mkey limiter=$val not understood\n";
62                 }
63                 $lim;
64         }
65 }
66
67 sub new {
68         my ($class, $opts) = @_;
69         my $v = $opts->{address} ||= 'public-inbox@example.com';
70         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
71         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
72         my $pi_config = delete $opts->{-pi_config};
73         _set_limiter($opts, $pi_config, 'httpbackend');
74         _set_uint($opts, 'feedmax', 25);
75         $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
76         bless $opts, $class;
77 }
78
79 sub git {
80         my ($self) = @_;
81         $self->{git} ||= eval {
82                 my $g = PublicInbox::Git->new($self->{mainrepo});
83                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
84                 _cleanup_later($self);
85                 $g;
86         };
87 }
88
89 sub mm {
90         my ($self) = @_;
91         $self->{mm} ||= eval {
92                 _cleanup_later($self);
93                 PublicInbox::Msgmap->new($self->{mainrepo});
94         };
95 }
96
97 sub search {
98         my ($self) = @_;
99         $self->{search} ||= eval {
100                 _cleanup_later($self);
101                 PublicInbox::Search->new($self->{mainrepo}, $self->{altid});
102         };
103 }
104
105 sub try_cat {
106         my ($path) = @_;
107         my $rv = '';
108         if (open(my $fh, '<', $path)) {
109                 local $/;
110                 $rv = <$fh>;
111         }
112         $rv;
113 }
114
115 sub description {
116         my ($self) = @_;
117         my $desc = $self->{description};
118         return $desc if defined $desc;
119         $desc = try_cat("$self->{mainrepo}/description");
120         local $/ = "\n";
121         chomp $desc;
122         $desc =~ s/\s+/ /smg;
123         $desc = '($GIT_DIR/description missing)' if $desc eq '';
124         $self->{description} = $desc;
125 }
126
127 sub cloneurl {
128         my ($self) = @_;
129         my $url = $self->{cloneurl};
130         return $url if $url;
131         $url = try_cat("$self->{mainrepo}/cloneurl");
132         my @url = split(/\s+/s, $url);
133         local $/ = "\n";
134         chomp @url;
135         $self->{cloneurl} = \@url;
136 }
137
138 sub base_url {
139         my ($self, $env) = @_;
140         if ($env) { # PSGI env
141                 my $scheme = $env->{'psgi.url_scheme'};
142                 my $host_port = $env->{HTTP_HOST} ||
143                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
144                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
145                 # for mount in Plack::Builder
146                 $url .= '/' if $url !~ m!/\z!;
147                 $url .= $self->{name} . '/';
148         } else {
149                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
150                 $self->{-base_url} ||= do {
151                         my $url = $self->{url} or return undef;
152                         # expand protocol-relative URLs to HTTPS if we're
153                         # not inside a web server
154                         $url = "https:$url" if $url =~ m!\A//!;
155                         $url .= '/' if $url !~ m!/\z!;
156                         $url;
157                 };
158         }
159 }
160
161 sub nntp_url {
162         my ($self) = @_;
163         $self->{-nntp_url} ||= do {
164                 # no checking for nntp_usable here, we can point entirely
165                 # to non-local servers or users run by a different user
166                 my $ns = $self->{nntpserver};
167                 my $group = $self->{newsgroup};
168                 my @urls;
169                 if ($ns && $group) {
170                         $ns = [ $ns ] if ref($ns) ne 'ARRAY';
171                         @urls = map {
172                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
173                                 $u .= '/' if $u !~ m!/\z!;
174                                 $u.$group;
175                         } @$ns;
176                 }
177
178                 my $mirrors = $self->{nntpmirror};
179                 if ($mirrors) {
180                         my @m;
181                         foreach (@$mirrors) {
182                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
183                                 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
184                                         if ($group) {
185                                                 $u .= '/' if $u !~ m!/\z!;
186                                                 $u .= $group;
187                                         } else {
188                                                 warn
189 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
190                                         }
191                                 }
192                                 # else: allow full URLs like:
193                                 # nntp://news.example.com/alt.example
194                                 push @m, $u;
195                         }
196                         my %seen = map { $_ => 1 } @urls;
197                         foreach my $u (@m) {
198                                 next if $seen{$u};
199                                 $seen{$u} = 1;
200                                 push @urls, $u;
201                         }
202                 }
203                 \@urls;
204         };
205 }
206
207 sub nntp_usable {
208         my ($self) = @_;
209         my $ret = $self->mm && $self->search;
210         $self->{mm} = $self->{search} = undef;
211         $ret;
212 }
213
214 sub msg_by_path ($$;$) {
215         my ($self, $path, $ref) = @_;
216         # TODO: allow other refs:
217         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
218         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
219         $str;
220 }
221
222 sub msg_by_smsg ($$;$) {
223         my ($self, $smsg, $ref) = @_;
224
225         return unless defined $smsg; # ghost
226
227         # backwards compat to fallback to msg_by_mid
228         # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
229         defined(my $blob = $smsg->{blob}) or
230                         return msg_by_mid($self, $smsg->mid);
231
232         my $str = git($self)->cat_file($blob, $ref);
233         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
234         $str;
235 }
236
237 sub path_check {
238         my ($self, $path) = @_;
239         git($self)->check('HEAD:'.$path);
240 }
241
242 sub msg_by_mid ($$;$) {
243         my ($self, $mid, $ref) = @_;
244         msg_by_path($self, mid2path($mid), $ref);
245 }
246
247 1;