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