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