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