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>
4 # Extends read-only Inbox for writing
5 package PublicInbox::InboxWritable;
8 use parent qw(PublicInbox::Inbox Exporter);
9 use PublicInbox::Import;
10 use PublicInbox::Filter::Base qw(REJECT);
12 our @EXPORT_OK = qw(eml_from_path warn_ignore_cb);
17 OLD_PERM_EVERYBODY => 2,
19 PERM_EVERYBODY => 0664,
23 my ($class, $ibx, $creat_opt) = @_;
24 return $ibx if ref($ibx) eq $class;
25 my $self = bless $ibx, $class;
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;
35 sub assert_usable_dir {
37 my $dir = $self->{inboxdir};
38 return $dir if defined($dir) && $dir ne '';
39 die "no inboxdir defined for $self->{name}\n";
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);
55 undef $mm; # ->created_at set
56 $sidx->commit_txn_lazy;
58 open my $fh, '>>', "$self->{inboxdir}/ssoma.lock" or
59 die "$self->{inboxdir}/ssoma.lock: $!\n";
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);
69 $self->with_umask(\&_init_v1, $self, $skip_artnum);
71 my $v2w = importer($self);
72 $v2w->init_inbox($shards, $skip_epoch, $skip_artnum);
77 my ($self, $parallel) = @_;
78 my $v = $self->version;
80 eval { require PublicInbox::V2Writable };
81 die "v2 not supported: $@\n" if $@;
82 my $opt = $self->{-creat_opt};
83 my $v2w = PublicInbox::V2Writable->new($self, $opt);
84 $v2w->{parallel} = $parallel if defined $parallel;
87 my @arg = (undef, undef, undef, $self);
88 PublicInbox::Import->new(@arg);
90 $! = 78; # EX_CONFIG 5.3.5 local configuration error
91 die "unsupported inbox version: $v\n";
97 my $f = $self->{filter};
98 if ($f && $f =~ /::/) {
99 # v2 keeps msgmap open, which causes conflicts for filters
100 # such as PublicInbox::Filter::RubyLang which overload msgmap
101 # for a predictable serial number.
102 if ($im && $self->version >= 2 && $self->{altid}) {
106 my @args = (ibx => $self);
107 # basic line splitting, only
108 # Perhaps we can have proper quote splitting one day...
109 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
115 # e.g: PublicInbox::Filter::Vger->new(@args)
116 return $f->new(@args);
122 sub is_maildir_basename ($) {
124 return 0 if $bn !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
125 if ($bn =~ /:2,([A-Z]+)\z/i) {
127 return 0 if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
132 sub is_maildir_path ($) {
134 my @p = split(m!/+!, $path);
135 (is_maildir_basename($p[-1]) && -f $path) ? 1 : 0;
138 sub eml_from_path ($) {
140 if (open my $fh, '<', $path) {
141 my $str = do { local $/; <$fh> } or return;
142 PublicInbox::Eml->new(\$str);
143 } else { # ENOENT is common with Maildir
144 warn "failed to open $path: $!\n" if $! != ENOENT;
150 my ($self, $dir) = @_;
151 my $im = $self->importer(1);
153 foreach my $sub (qw(cur new tmp)) {
154 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
156 foreach my $sub (qw(cur new)) {
157 opendir my $dh, "$dir/$sub" or die "opendir $dir/$sub: $!\n";
158 while (defined(my $fn = readdir($dh))) {
159 next unless is_maildir_basename($fn);
160 my $mime = eml_from_path("$dir/$fn") or next;
162 if (my $filter = $self->filter($im)) {
163 my $ret = $filter->scrub($mime) or return;
164 return if $ret == REJECT();
173 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
174 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
177 my ($im, $variant, $filter, $msg) = @_;
178 $$msg =~ s/(\r?\n)+\z/$1/s;
179 if ($variant eq 'mboxrd') {
180 $$msg =~ s/^>(>*From )/$1/gms;
181 } elsif ($variant eq 'mboxo') {
182 $$msg =~ s/^>From /From /gms;
184 my $mime = PublicInbox::Eml->new($msg);
186 my $ret = $filter->scrub($mime) or return;
187 return if $ret == REJECT();
194 my ($self, $fh, $variant) = @_;
195 if ($variant !~ /\A(?:mboxrd|mboxo)\z/) {
196 die "variant must be 'mboxrd' or 'mboxo'\n";
198 my $im = $self->importer(1);
201 my $filter = $self->filter;
202 while (defined(my $l = <$fh>)) {
203 if ($l =~ /$from_strict/o) {
204 if (!defined($prev) || $prev =~ /^\r?$/) {
205 mb_add($im, $variant, $filter, \$msg) if $msg;
215 mb_add($im, $variant, $filter, \$msg) if $msg;
219 sub _read_git_config_perm {
221 chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
225 sub _git_config_perm {
227 my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
228 return PERM_UMASK if (!defined($perm) || $perm eq '');
229 return PERM_UMASK if ($perm eq 'umask');
230 return PERM_GROUP if ($perm eq 'group');
231 if ($perm =~ /\A(?:all|world|everybody)\z/) {
232 return PERM_EVERYBODY;
234 return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
235 return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
238 return PERM_UMASK if ($i == PERM_UMASK);
239 return PERM_GROUP if ($i == OLD_PERM_GROUP);
240 return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
242 if (($i & 0600) != 0600) {
243 die "core.sharedRepository mode invalid: ".
244 sprintf('%.3o', $i) . "\nOwner must have permissions\n";
250 my ($perm) = @_; # _git_config_perm return value
252 return umask if $rv == 0;
254 # set +x bit if +r or +w were set
255 $rv |= 0100 if ($rv & 0600);
256 $rv |= 0010 if ($rv & 0060);
257 $rv |= 0001 if ($rv & 0006);
262 my ($self, $cb, @arg) = @_;
263 my $old = umask $self->{umask};
264 my $rv = eval { $cb->(@arg) };
273 my $perm = _git_config_perm($self);
274 my $umask = _umask_for($perm);
275 $self->{umask} = $umask;
279 delete @{$_[0]}{qw(over mm git search)};
282 # warnings to ignore when handling spam mailboxes and maybe other places
285 # Email::Address::XS warnings
286 $s =~ /^Argument contains empty address at /
287 || $s =~ /^Element at index [0-9]+ contains /
288 # PublicInbox::MsgTime
289 || $s =~ /^bogus TZ offset: .+?, ignoring and assuming \+0000/
290 || $s =~ /^bad Date: .+? in /
291 # Encode::Unicode::UTF7
292 || $s =~ /^Bad UTF7 data escape at /
295 # this expects to be RHS in this assignment: "local $SIG{__WARN__} = ..."
297 my $cb = $SIG{__WARN__} // sub { print STDERR @_ };
299 return if warn_ignore(@_);
304 # v2+ only, XXX: maybe we can just rely on ->max_git_epoch and remove
306 my ($self, $max) = @_;
307 defined($$max = $self->max_git_epoch) ?
308 "$self->{inboxdir}/git/$$max.git" : undef;