]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
v2: support Xapian + SQLite indexing
[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
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 $git_dir = $self->{mainrepo};
83                 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
84                 my $g = PublicInbox::Git->new($git_dir);
85                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
86                 _cleanup_later($self);
87                 $g;
88         };
89 }
90
91 sub mm {
92         my ($self) = @_;
93         $self->{mm} ||= eval {
94                 _cleanup_later($self);
95                 PublicInbox::Msgmap->new($self->{mainrepo});
96         };
97 }
98
99 sub search {
100         my ($self) = @_;
101         $self->{search} ||= eval {
102                 _cleanup_later($self);
103                 PublicInbox::Search->new($self->{mainrepo}, $self->{altid});
104         };
105 }
106
107 sub try_cat {
108         my ($path) = @_;
109         my $rv = '';
110         if (open(my $fh, '<', $path)) {
111                 local $/;
112                 $rv = <$fh>;
113         }
114         $rv;
115 }
116
117 sub description {
118         my ($self) = @_;
119         my $desc = $self->{description};
120         return $desc if defined $desc;
121         $desc = try_cat("$self->{mainrepo}/description");
122         local $/ = "\n";
123         chomp $desc;
124         $desc =~ s/\s+/ /smg;
125         $desc = '($GIT_DIR/description missing)' if $desc eq '';
126         $self->{description} = $desc;
127 }
128
129 sub cloneurl {
130         my ($self) = @_;
131         my $url = $self->{cloneurl};
132         return $url if $url;
133         $url = try_cat("$self->{mainrepo}/cloneurl");
134         my @url = split(/\s+/s, $url);
135         local $/ = "\n";
136         chomp @url;
137         $self->{cloneurl} = \@url;
138 }
139
140 sub base_url {
141         my ($self, $env) = @_;
142         if ($env) { # PSGI env
143                 my $scheme = $env->{'psgi.url_scheme'};
144                 my $host_port = $env->{HTTP_HOST} ||
145                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
146                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
147                 # for mount in Plack::Builder
148                 $url .= '/' if $url !~ m!/\z!;
149                 $url .= $self->{name} . '/';
150         } else {
151                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
152                 $self->{-base_url} ||= do {
153                         my $url = $self->{url} or return undef;
154                         # expand protocol-relative URLs to HTTPS if we're
155                         # not inside a web server
156                         $url = "https:$url" if $url =~ m!\A//!;
157                         $url .= '/' if $url !~ m!/\z!;
158                         $url;
159                 };
160         }
161 }
162
163 sub nntp_url {
164         my ($self) = @_;
165         $self->{-nntp_url} ||= do {
166                 # no checking for nntp_usable here, we can point entirely
167                 # to non-local servers or users run by a different user
168                 my $ns = $self->{nntpserver};
169                 my $group = $self->{newsgroup};
170                 my @urls;
171                 if ($ns && $group) {
172                         $ns = [ $ns ] if ref($ns) ne 'ARRAY';
173                         @urls = map {
174                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
175                                 $u .= '/' if $u !~ m!/\z!;
176                                 $u.$group;
177                         } @$ns;
178                 }
179
180                 my $mirrors = $self->{nntpmirror};
181                 if ($mirrors) {
182                         my @m;
183                         foreach (@$mirrors) {
184                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
185                                 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
186                                         if ($group) {
187                                                 $u .= '/' if $u !~ m!/\z!;
188                                                 $u .= $group;
189                                         } else {
190                                                 warn
191 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
192                                         }
193                                 }
194                                 # else: allow full URLs like:
195                                 # nntp://news.example.com/alt.example
196                                 push @m, $u;
197                         }
198                         my %seen = map { $_ => 1 } @urls;
199                         foreach my $u (@m) {
200                                 next if $seen{$u};
201                                 $seen{$u} = 1;
202                                 push @urls, $u;
203                         }
204                 }
205                 \@urls;
206         };
207 }
208
209 sub nntp_usable {
210         my ($self) = @_;
211         my $ret = $self->mm && $self->search;
212         $self->{mm} = $self->{search} = undef;
213         $ret;
214 }
215
216 sub msg_by_path ($$;$) {
217         my ($self, $path, $ref) = @_;
218         # TODO: allow other refs:
219         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
220         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
221         $str;
222 }
223
224 sub msg_by_smsg ($$;$) {
225         my ($self, $smsg, $ref) = @_;
226
227         return unless defined $smsg; # ghost
228
229         # backwards compat to fallback to msg_by_mid
230         # TODO: remove if we bump SCHEMA_VERSION in Search.pm:
231         defined(my $blob = $smsg->{blob}) or
232                         return msg_by_mid($self, $smsg->mid);
233
234         my $str = git($self)->cat_file($blob, $ref);
235         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
236         $str;
237 }
238
239 sub path_check {
240         my ($self, $path) = @_;
241         git($self)->check('HEAD:'.$path);
242 }
243
244 sub msg_by_mid ($$;$) {
245         my ($self, $mid, $ref) = @_;
246         msg_by_path($self, mid2path($mid), $ref);
247 }
248
249 1;