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