]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxWritable.pm
b1d5caf53c73f092731b29994cbdd8ece953a5fc
[public-inbox.git] / lib / PublicInbox / InboxWritable.pm
1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Extends read-only Inbox for writing
5 package PublicInbox::InboxWritable;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::Inbox Exporter);
9 use PublicInbox::Import;
10 use PublicInbox::Filter::Base qw(REJECT);
11 use Errno qw(ENOENT);
12 our @EXPORT_OK = qw(eml_from_path warn_ignore_cb);
13
14 use constant {
15         PERM_UMASK => 0,
16         OLD_PERM_GROUP => 1,
17         OLD_PERM_EVERYBODY => 2,
18         PERM_GROUP => 0660,
19         PERM_EVERYBODY => 0664,
20 };
21
22 sub new {
23         my ($class, $ibx, $creat_opt) = @_;
24         return $ibx if ref($ibx) eq $class;
25         my $self = bless $ibx, $class;
26
27         # TODO: maybe stop supporting this
28         if ($creat_opt) { # for { nproc => $N }
29                 $self->{-creat_opt} = $creat_opt;
30                 init_inbox($self) if $self->version == 1;
31         }
32         $self;
33 }
34
35 sub assert_usable_dir {
36         my ($self) = @_;
37         my $dir = $self->{inboxdir};
38         return $dir if defined($dir) && $dir ne '';
39         die "no inboxdir defined for $self->{name}\n";
40 }
41
42 sub _init_v1 {
43         my ($self, $skip_artnum) = @_;
44         if (defined($self->{indexlevel}) || defined($skip_artnum)) {
45                 require PublicInbox::SearchIdx;
46                 require PublicInbox::Msgmap;
47                 my $sidx = PublicInbox::SearchIdx->new($self, 1); # just create
48                 $sidx->begin_txn_lazy;
49                 my $mm = PublicInbox::Msgmap->new($self->{inboxdir}, 1);
50                 if (defined $skip_artnum) {
51                         $mm->{dbh}->begin_work;
52                         $mm->skip_artnum($skip_artnum);
53                         $mm->{dbh}->commit;
54                 }
55                 undef $mm; # ->created_at set
56                 $sidx->commit_txn_lazy;
57         } else {
58                 open my $fh, '>>', "$self->{inboxdir}/ssoma.lock" or
59                         die "$self->{inboxdir}/ssoma.lock: $!\n";
60         }
61 }
62
63 sub init_inbox {
64         my ($self, $shards, $skip_epoch, $skip_artnum) = @_;
65         if ($self->version == 1) {
66                 my $dir = assert_usable_dir($self);
67                 PublicInbox::Import::init_bare($dir);
68                 $self->with_umask(\&_init_v1, $self, $skip_artnum);
69         } else {
70                 my $v2w = importer($self);
71                 $v2w->init_inbox($shards, $skip_epoch, $skip_artnum);
72         }
73 }
74
75 sub importer {
76         my ($self, $parallel) = @_;
77         my $v = $self->version;
78         if ($v == 2) {
79                 eval { require PublicInbox::V2Writable };
80                 die "v2 not supported: $@\n" if $@;
81                 my $opt = $self->{-creat_opt};
82                 my $v2w = PublicInbox::V2Writable->new($self, $opt);
83                 $v2w->{parallel} = $parallel if defined $parallel;
84                 $v2w;
85         } elsif ($v == 1) {
86                 my @arg = (undef, undef, undef, $self);
87                 PublicInbox::Import->new(@arg);
88         } else {
89                 $! = 78; # EX_CONFIG 5.3.5 local configuration error
90                 die "unsupported inbox version: $v\n";
91         }
92 }
93
94 sub filter {
95         my ($self, $im) = @_;
96         my $f = $self->{filter};
97         if ($f && $f =~ /::/) {
98                 # v2 keeps msgmap open, which causes conflicts for filters
99                 # such as PublicInbox::Filter::RubyLang which overload msgmap
100                 # for a predictable serial number.
101                 if ($im && $self->version >= 2 && $self->{altid}) {
102                         $im->done;
103                 }
104
105                 my @args = (ibx => $self);
106                 # basic line splitting, only
107                 # Perhaps we can have proper quote splitting one day...
108                 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
109
110                 eval "require $f";
111                 if ($@) {
112                         warn $@;
113                 } else {
114                         # e.g: PublicInbox::Filter::Vger->new(@args)
115                         return $f->new(@args);
116                 }
117         }
118         undef;
119 }
120
121 sub is_maildir_basename ($) {
122         my ($bn) = @_;
123         return 0 if $bn !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
124         if ($bn =~ /:2,([A-Z]+)\z/i) {
125                 my $flags = $1;
126                 return 0 if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
127         }
128         1;
129 }
130
131 sub is_maildir_path ($) {
132         my ($path) = @_;
133         my @p = split(m!/+!, $path);
134         (is_maildir_basename($p[-1]) && -f $path) ? 1 : 0;
135 }
136
137 sub eml_from_path ($) {
138         my ($path) = @_;
139         if (open my $fh, '<', $path) {
140                 my $str = do { local $/; <$fh> } or return;
141                 PublicInbox::Eml->new(\$str);
142         } else { # ENOENT is common with Maildir
143                 warn "failed to open $path: $!\n" if $! != ENOENT;
144                 undef;
145         }
146 }
147
148 sub import_maildir {
149         my ($self, $dir) = @_;
150         my $im = $self->importer(1);
151
152         foreach my $sub (qw(cur new tmp)) {
153                 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
154         }
155         foreach my $sub (qw(cur new)) {
156                 opendir my $dh, "$dir/$sub" or die "opendir $dir/$sub: $!\n";
157                 while (defined(my $fn = readdir($dh))) {
158                         next unless is_maildir_basename($fn);
159                         my $mime = eml_from_path("$dir/$fn") or next;
160
161                         if (my $filter = $self->filter($im)) {
162                                 my $ret = $filter->scrub($mime) or return;
163                                 return if $ret == REJECT();
164                                 $mime = $ret;
165                         }
166                         $im->add($mime);
167                 }
168         }
169         $im->done;
170 }
171
172 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
173 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
174
175 sub mb_add ($$$$) {
176         my ($im, $variant, $filter, $msg) = @_;
177         $$msg =~ s/(\r?\n)+\z/$1/s;
178         if ($variant eq 'mboxrd') {
179                 $$msg =~ s/^>(>*From )/$1/gms;
180         } elsif ($variant eq 'mboxo') {
181                 $$msg =~ s/^>From /From /gms;
182         }
183         my $mime = PublicInbox::Eml->new($msg);
184         if ($filter) {
185                 my $ret = $filter->scrub($mime) or return;
186                 return if $ret == REJECT();
187                 $mime = $ret;
188         }
189         $im->add($mime)
190 }
191
192 sub import_mbox {
193         my ($self, $fh, $variant) = @_;
194         if ($variant !~ /\A(?:mboxrd|mboxo)\z/) {
195                 die "variant must be 'mboxrd' or 'mboxo'\n";
196         }
197         my $im = $self->importer(1);
198         my $prev = undef;
199         my $msg = '';
200         my $filter = $self->filter;
201         while (defined(my $l = <$fh>)) {
202                 if ($l =~ /$from_strict/o) {
203                         if (!defined($prev) || $prev =~ /^\r?$/) {
204                                 mb_add($im, $variant, $filter, \$msg) if $msg;
205                                 $msg = '';
206                                 $prev = $l;
207                                 next;
208                         }
209                         warn "W[$.] $l\n";
210                 }
211                 $prev = $l;
212                 $msg .= $l;
213         }
214         mb_add($im, $variant, $filter, \$msg) if $msg;
215         $im->done;
216 }
217
218 sub _read_git_config_perm {
219         my ($self) = @_;
220         chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
221         $perm;
222 }
223
224 sub _git_config_perm {
225         my $self = shift;
226         my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
227         return PERM_UMASK if (!defined($perm) || $perm eq '');
228         return PERM_UMASK if ($perm eq 'umask');
229         return PERM_GROUP if ($perm eq 'group');
230         if ($perm =~ /\A(?:all|world|everybody)\z/) {
231                 return PERM_EVERYBODY;
232         }
233         return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
234         return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
235
236         my $i = oct($perm);
237         return PERM_UMASK if ($i == PERM_UMASK);
238         return PERM_GROUP if ($i == OLD_PERM_GROUP);
239         return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
240
241         if (($i & 0600) != 0600) {
242                 die "core.sharedRepository mode invalid: ".
243                     sprintf('%.3o', $i) . "\nOwner must have permissions\n";
244         }
245         ($i & 0666);
246 }
247
248 sub _umask_for {
249         my ($perm) = @_; # _git_config_perm return value
250         my $rv = $perm;
251         return umask if $rv == 0;
252
253         # set +x bit if +r or +w were set
254         $rv |= 0100 if ($rv & 0600);
255         $rv |= 0010 if ($rv & 0060);
256         $rv |= 0001 if ($rv & 0006);
257         (~$rv & 0777);
258 }
259
260 sub with_umask {
261         my ($self, $cb, @arg) = @_;
262         my $old = umask($self->{umask} //= umask_prepare($self));
263         my $rv = eval { $cb->(@arg) };
264         my $err = $@;
265         umask $old;
266         die $err if $err;
267         $rv;
268 }
269
270 sub umask_prepare {
271         my ($self) = @_;
272         my $perm = _git_config_perm($self);
273         _umask_for($perm);
274 }
275
276 sub cleanup ($) {
277         delete @{$_[0]}{qw(over mm git search)};
278 }
279
280 # warnings to ignore when handling spam mailboxes and maybe other places
281 sub warn_ignore {
282         my $s = "@_";
283         # Email::Address::XS warnings
284         $s =~ /^Argument contains empty address at /
285         || $s =~ /^Element at index [0-9]+ contains /
286         # PublicInbox::MsgTime
287         || $s =~ /^bogus TZ offset: .+?, ignoring and assuming \+0000/
288         || $s =~ /^bad Date: .+? in /
289         # Encode::Unicode::UTF7
290         || $s =~ /^Bad UTF7 data escape at /
291 }
292
293 # this expects to be RHS in this assignment: "local $SIG{__WARN__} = ..."
294 sub warn_ignore_cb {
295         my $cb = $SIG{__WARN__} // \&CORE::warn;
296         sub {
297                 return if warn_ignore(@_);
298                 $cb->(@_);
299         }
300 }
301
302 # v2+ only, XXX: maybe we can just rely on ->max_git_epoch and remove
303 sub git_dir_latest {
304         my ($self, $max) = @_;
305         defined($$max = $self->max_git_epoch) ?
306                 "$self->{inboxdir}/git/$$max.git" : undef;
307 }
308
309 1;