1 # Copyright (C) 2018 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 base qw(PublicInbox::Inbox);
9 use PublicInbox::Import;
10 use PublicInbox::Filter::Base;
11 *REJECT = *PublicInbox::Filter::Base::REJECT;
16 OLD_PERM_EVERYBODY => 2,
18 PERM_EVERYBODY => 0664,
22 my ($class, $ibx) = @_;
27 my ($self, $parallel) = @_;
28 $self->{-importer} ||= eval {
29 my $v = $self->{version} || 1;
31 eval { require PublicInbox::V2Writable };
32 die "v2 not supported: $@\n" if $@;
33 my $v2w = PublicInbox::V2Writable->new($self);
34 $v2w->{parallel} = $parallel;
38 my $name = $self->{name};
39 my $addr = $self->{-primary_address};
40 PublicInbox::Import->new($git, $name, $addr, $self);
42 $! = 78; # EX_CONFIG 5.3.5 local configuration error
43 die "unsupported inbox version: $v\n";
50 my $f = $self->{filter};
51 if ($f && $f =~ /::/) {
52 # v2 keeps msgmap open, which causes conflicts for filters
53 # such as PublicInbox::Filter::RubyLang which overload msgmap
54 # for a predictable serial number.
55 if ($im && ($self->{version} || 1) >= 2 && $self->{altid}) {
59 my @args = (-inbox => $self);
60 # basic line splitting, only
61 # Perhaps we can have proper quote splitting one day...
62 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
68 # e.g: PublicInbox::Filter::Vger->new(@args)
69 return $f->new(@args);
75 sub is_maildir_basename ($) {
77 return 0 if $bn !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
78 if ($bn =~ /:2,([A-Z]+)\z/i) {
80 return 0 if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
85 sub is_maildir_path ($) {
87 my @p = split(m!/+!, $path);
88 (is_maildir_basename($p[-1]) && -f $path) ? 1 : 0;
91 sub maildir_path_load ($) {
93 if (open my $fh, '<', $path) {
97 return PublicInbox::MIME->new(\$str);
98 } elsif ($!{ENOENT}) {
102 warn "failed to open $path: $!\n";
108 my ($self, $dir) = @_;
109 my $im = $self->importer(1);
111 foreach my $sub (qw(cur new tmp)) {
112 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
114 foreach my $sub (qw(cur new)) {
115 opendir my $dh, "$dir/$sub" or die "opendir $dir/$sub: $!\n";
116 while (defined(my $fn = readdir($dh))) {
117 next unless is_maildir_basename($fn);
118 my $mime = maildir_file_load("$dir/$fn") or next;
120 if (my $filter = $self->filter($im)) {
121 my $ret = $filter->scrub($mime) or return;
122 return if $ret == REJECT();
131 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
132 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
135 my ($im, $variant, $filter, $msg) = @_;
136 $$msg =~ s/(\r?\n)+\z/$1/s;
137 my $mime = PublicInbox::MIME->new($msg);
138 if ($variant eq 'mboxrd') {
139 $$msg =~ s/^>(>*From )/$1/sm;
140 } elsif ($variant eq 'mboxo') {
141 $$msg =~ s/^>From /From /sm;
144 my $ret = $filter->scrub($mime) or return;
145 return if $ret == REJECT();
152 my ($self, $fh, $variant) = @_;
153 if ($variant !~ /\A(?:mboxrd|mboxo)\z/) {
154 die "variant must be 'mboxrd' or 'mboxo'\n";
156 my $im = $self->importer(1);
159 my $filter = $self->filter;
160 while (defined(my $l = <$fh>)) {
161 if ($l =~ /$from_strict/o) {
162 if (!defined($prev) || $prev =~ /^\r?$/) {
163 mb_add($im, $variant, $filter, \$msg) if $msg;
173 mb_add($im, $variant, $filter, \$msg) if $msg;
177 sub _read_git_config_perm {
179 chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
183 sub _git_config_perm {
185 my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
186 return PERM_UMASK if (!defined($perm) || $perm eq '');
187 return PERM_UMASK if ($perm eq 'umask');
188 return PERM_GROUP if ($perm eq 'group');
189 if ($perm =~ /\A(?:all|world|everybody)\z/) {
190 return PERM_EVERYBODY;
192 return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
193 return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
196 return PERM_UMASK if ($i == PERM_UMASK);
197 return PERM_GROUP if ($i == OLD_PERM_GROUP);
198 return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
200 if (($i & 0600) != 0600) {
201 die "core.sharedRepository mode invalid: ".
202 sprintf('%.3o', $i) . "\nOwner must have permissions\n";
208 my ($perm) = @_; # _git_config_perm return value
210 return umask if $rv == 0;
212 # set +x bit if +r or +w were set
213 $rv |= 0100 if ($rv & 0600);
214 $rv |= 0010 if ($rv & 0060);
215 $rv |= 0001 if ($rv & 0006);
220 my ($self, $cb) = @_;
221 my $old = umask $self->{umask};
222 my $rv = eval { $cb->() };
231 my $perm = _git_config_perm($self);
232 my $umask = _umask_for($perm);
233 $self->{umask} = $umask;