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 if (defined $skip_artnum) {
50 my $mm = PublicInbox::Msgmap->new($self->{inboxdir}, 1);
51 $mm->{dbh}->begin_work;
52 $mm->skip_artnum($skip_artnum);
55 $sidx->commit_txn_lazy;
57 open my $fh, '>>', "$self->{inboxdir}/ssoma.lock" or
58 die "$self->{inboxdir}/ssoma.lock: $!\n";
63 my ($self, $shards, $skip_epoch, $skip_artnum) = @_;
64 if ($self->version == 1) {
65 my $dir = assert_usable_dir($self);
66 PublicInbox::Import::init_bare($dir);
68 $self->with_umask(\&_init_v1, $self, $skip_artnum);
70 my $v2w = importer($self);
71 $v2w->init_inbox($shards, $skip_epoch, $skip_artnum);
76 my ($self, $parallel) = @_;
77 my $v = $self->version;
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;
86 my @arg = (undef, undef, undef, $self);
87 PublicInbox::Import->new(@arg);
89 $! = 78; # EX_CONFIG 5.3.5 local configuration error
90 die "unsupported inbox version: $v\n";
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}) {
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+/;
114 # e.g: PublicInbox::Filter::Vger->new(@args)
115 return $f->new(@args);
121 sub is_maildir_basename ($) {
123 return 0 if $bn !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
124 if ($bn =~ /:2,([A-Z]+)\z/i) {
126 return 0 if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
131 sub is_maildir_path ($) {
133 my @p = split(m!/+!, $path);
134 (is_maildir_basename($p[-1]) && -f $path) ? 1 : 0;
137 sub eml_from_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;
149 my ($self, $dir) = @_;
150 my $im = $self->importer(1);
152 foreach my $sub (qw(cur new tmp)) {
153 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
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;
161 if (my $filter = $self->filter($im)) {
162 my $ret = $filter->scrub($mime) or return;
163 return if $ret == REJECT();
172 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
173 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
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;
183 my $mime = PublicInbox::Eml->new($msg);
185 my $ret = $filter->scrub($mime) or return;
186 return if $ret == REJECT();
193 my ($self, $fh, $variant) = @_;
194 if ($variant !~ /\A(?:mboxrd|mboxo)\z/) {
195 die "variant must be 'mboxrd' or 'mboxo'\n";
197 my $im = $self->importer(1);
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;
214 mb_add($im, $variant, $filter, \$msg) if $msg;
218 sub _read_git_config_perm {
220 chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
224 sub _git_config_perm {
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;
233 return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
234 return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
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);
241 if (($i & 0600) != 0600) {
242 die "core.sharedRepository mode invalid: ".
243 sprintf('%.3o', $i) . "\nOwner must have permissions\n";
249 my ($perm) = @_; # _git_config_perm return value
251 return umask if $rv == 0;
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);
261 my ($self, $cb, @arg) = @_;
262 my $old = umask $self->{umask};
263 my $rv = eval { $cb->(@arg) };
272 my $perm = _git_config_perm($self);
273 my $umask = _umask_for($perm);
274 $self->{umask} = $umask;
278 delete @{$_[0]}{qw(over mm git search)};
281 # warnings to ignore when handling spam mailboxes and maybe other places
284 # Email::Address::XS warnings
285 $s =~ /^Argument contains empty address at /
286 || $s =~ /^Element at index [0-9]+ contains /
287 # PublicInbox::MsgTime
288 || $s =~ /^bogus TZ offset: .+?, ignoring and assuming \+0000/
289 || $s =~ /^bad Date: .+? in /
290 # Encode::Unicode::UTF7
291 || $s =~ /^Bad UTF7 data escape at /
294 # this expects to be RHS in this assignment: "local $SIG{__WARN__} = ..."
296 my $cb = $SIG{__WARN__} // sub { print STDERR @_ };
298 return if warn_ignore(@_);
304 sub git_dir_n { "$_[0]->{inboxdir}/git/$_[1].git" }
308 my ($self, $max) = @_;
310 my $pfx = "$self->{inboxdir}/git";
311 return unless -d $pfx;
313 opendir my $dh, $pfx or die "opendir $pfx: $!\n";
314 while (defined(my $git_dir = readdir($dh))) {
315 $git_dir =~ m!\A([0-9]+)\.git\z! or next;
318 $latest = "$pfx/$git_dir";