1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # front-end for the "lei export-kw" sub-command
5 package PublicInbox::LeiExportKw;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9 use Errno qw(EEXIST ENOENT);
11 sub export_kw_md { # LeiMailSync->each_src callback
12 my ($oidbin, $id, $self, $mdir) = @_;
13 my $sto_kw = $self->{lse}->oidbin_keywords($oidbin) or return;
15 my ($md_kw, $unknown, @try);
16 if ($bn =~ s/:2,([a-zA-Z]*)\z//) {
17 ($md_kw, $unknown) = PublicInbox::MdirReader::flags2kw($1);
23 if ($self->{-merge_kw} && $md_kw) { # merging keywords is the default
24 @$sto_kw{keys %$md_kw} = values(%$md_kw);
27 PublicInbox::LeiToMail::kw2suffix([keys %$sto_kw], @$unknown);
28 my $dst = "$mdir/cur/$bn";
30 my $lei = $self->{lei};
32 my $src = "$mdir/$d/$$id";
35 # we use link(2) + unlink(2) since rename(2) may
36 # inadvertently clobber if the "uniquefilename" part wasn't
38 if (link($src, $dst)) { # success
39 # unlink(2) may ENOENT from parallel invocation,
40 # ignore it, but not other serious errors
41 if (!unlink($src) and $! != ENOENT) {
42 $lei->child_error(1, "E: unlink($src): $!");
44 $lei->{sto}->ipc_do('lms_mv_src', "maildir:$mdir",
46 return; # success anyways if link(2) worked
48 if ($! == ENOENT && !-e $src) { # some other process moved it
49 $lei->{sto}->ipc_do('lms_clear_src',
50 "maildir:$mdir", $id);
53 push @fail, $src if $! != EEXIST;
58 my $orig = '['.join('|', @fail).']';
59 my $oidhex = unpack('H*', $oidbin);
60 $lei->child_error(1, "link($orig, $dst) ($oidhex): $e");
63 sub export_kw_imap { # LeiMailSync->each_src callback
64 my ($oidbin, $id, $self, $mic) = @_;
65 my $sto_kw = $self->{lse}->oidbin_keywords($oidbin) or return;
66 $self->{imap_mod_kw}->($self->{nwr}, $mic, $id, [ keys %$sto_kw ]);
69 # overrides PublicInbox::LeiInput::input_path_url
71 my ($self, $input, @args) = @_;
72 my $lms = $self->{-lms_ro} //= $self->{lse}->lms;
73 if ($input =~ /\Amaildir:(.+)/i) {
75 require PublicInbox::LeiToMail; # kw2suffix
76 $lms->each_src($input, \&export_kw_md, $self, $mdir);
77 } elsif ($input =~ m!\Aimaps?://!i) {
78 my $uri = PublicInbox::URIimap->new($input);
79 my $mic = $self->{nwr}->mic_for_folder($uri);
80 $lms->each_src($$uri, \&export_kw_imap, $self, $mic);
82 } else { die "BUG: $input not supported" }
83 my $wait = $self->{lei}->{sto}->ipc_do('done');
87 my ($lei, @folders) = @_;
88 my $sto = $lei->_lei_store or return $lei->fail(<<EOM);
89 lei/store uninitialized, see lei-import(1)
91 my $lse = $sto->search;
92 my $lms = $lse->lms or return $lei->fail(<<EOM);
93 lei mail_sync uninitialized, see lei-import(1)
95 my $opt = $lei->{opt};
96 if (defined(my $all = $opt->{all})) { # --all=<local|remote>
97 $lms->group2folders($lei, $all, \@folders) or return;
99 my $err = $lms->arg2folder($lei, \@folders);
100 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
101 return $lei->fail($err->{fail}) if $err->{fail};
103 my $self = bless { lse => $lse }, __PACKAGE__;
104 $lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
105 $self->prepare_inputs($lei, \@folders) or return;
106 my $j = $opt->{jobs} // scalar(@{$self->{inputs}}) || 1;
107 if (my @ro = grep(!/\A(?:maildir|imaps?):/i, @folders)) {
108 return $lei->fail("cannot export to read-only folders: @ro");
110 my $m = $opt->{mode} // 'merge';
111 if ($m eq 'merge') { # default
112 $self->{-merge_kw} = 1;
113 } elsif ($m eq 'set') {
115 return $lei->fail(<<EOM);
116 --mode=$m not supported (`set' or `merge')
119 if (my $net = $lei->{net}) {
120 require PublicInbox::NetWriter;
121 $self->{nwr} = bless $net, 'PublicInbox::NetWriter';
122 $self->{imap_mod_kw} = $net->can($self->{-merge_kw} ?
123 'imap_add_kw' : 'imap_set_kw');
125 undef $lms; # for fork
127 $sto->write_prepare($lei);
128 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
129 $self->{-wq_nr_workers} = $j // 1; # locked
130 (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
132 $lei->{-err_type} = 'non-fatal';
133 net_merge_all_done($self) unless $lei->{auth};
134 $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
137 sub _complete_export_kw {
138 my ($lei, @argv) = @_;
139 my $sto = $lei->_lei_store or return;
140 my $lms = $sto->search->lms or return;
141 my $match_cb = $lei->complete_url_prepare(\@argv);
142 map { $match_cb->($_) } $lms->folders;
147 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
148 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
150 # the following works even when LeiAuth is lazy-loaded
151 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;