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