user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
db4f7441d3b753293b51e7a5604e7d98b64fbfb0 blob 5247 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
 
# Copyright (C) 2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# front-end for the "lei export-kw" sub-command
package PublicInbox::LeiExportKw;
use strict;
use v5.10.1;
use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
use Errno qw(EEXIST ENOENT);

sub export_kw_md { # LeiMailSync->each_src callback
	my ($oidbin, $id, $self, $mdir) = @_;
	my $oidhex = unpack('H*', $oidbin);
	my $sto_kw = $self->{lse}->oid_keywords($oidhex) or return;
	my $bn = $$id;
	my ($md_kw, $unknown, @try);
	if ($bn =~ s/:2,([a-zA-Z]*)\z//) {
		($md_kw, $unknown) = PublicInbox::MdirReader::flags2kw($1);
		@try = qw(cur new);
	} else {
		$unknown = [];
		@try = qw(new cur);
	}
	if ($self->{-merge_kw} && $md_kw) { # merging keywords is the default
		@$sto_kw{keys %$md_kw} = values(%$md_kw);
	}
	$bn .= ':2,'.
		PublicInbox::LeiToMail::kw2suffix([keys %$sto_kw], @$unknown);
	my $dst = "$mdir/cur/$bn";
	my @fail;
	for my $d (@try) {
		my $src = "$mdir/$d/$$id";
		next if $src eq $dst;

		# we use link(2) + unlink(2) since rename(2) may
		# inadvertently clobber if the "uniquefilename" part wasn't
		# actually unique.
		if (link($src, $dst)) { # success
			# unlink(2) may ENOENT from parallel invocation,
			# ignore it, but not other serious errors
			if (!unlink($src) and $! != ENOENT) {
				$self->{lei}->child_error(1,
							"E: unlink($src): $!");
			}
			$self->{lms}->mv_src("maildir:$mdir",
						$oidbin, $id, $bn) or die;
			return; # success anyways if link(2) worked
		}
		if ($! == ENOENT && !-e $src) { # some other process moved it
			$self->{lms}->clear_src("maildir:$mdir", $id);
			next;
		}
		push @fail, $src if $! != EEXIST;
	}
	return unless @fail;
	# both tries failed
	my $e = $!;
	my $orig = '['.join('|', @fail).']';
	$self->{lei}->child_error(1, "link($orig, $dst) ($oidhex): $e");
}

# overrides PublicInbox::LeiInput::input_path_url
sub input_path_url {
	my ($self, $input, @args) = @_;
	my $lms = $self->{lms} //= $self->{lse}->lms;
	$lms->lms_begin;
	if ($input =~ s/\Amaildir://i) {
		require PublicInbox::LeiToMail; # kw2suffix
		$lms->each_src("maildir:$input", \&export_kw_md, $self, $input);
	}
	$lms->lms_commit;
}

sub lei_export_kw {
	my ($lei, @folders) = @_;
	my $sto = $lei->_lei_store or return $lei->fail(<<EOM);
lei/store uninitialized, see lei-import(1)
EOM
	my $lse = $sto->search;
	my $lms = $lse->lms or return $lei->fail(<<EOM);
lei mail_sync uninitialized, see lei-import(1)
EOM
	my $opt = $lei->{opt};
	my $all = $opt->{all};
	my @all = $lms->folders;
	if (defined $all) { # --all=<local|remote>
		my %x = map { $_ => $_ } split(/,/, $all);
		my @ok = grep(defined, delete(@x{qw(local remote), ''}));
		my @no = keys %x;
		if (@no) {
			@no = (join(',', @no));
			return $lei->fail(<<EOM);
--all=@no not accepted (must be `local' and/or `remote')
EOM
		}
		my (%seen, @inc);
		for my $ok (@ok) {
			if ($ok eq 'local') {
				@inc = grep(!m!\A[a-z0-9\+]+://!i, @all);
			} elsif ($ok eq 'remote') {
				@inc = grep(m!\A[a-z0-9\+]+://!i, @all);
			} elsif ($ok ne '') {
				return $lei->fail("--all=$all not understood");
			} else {
				@inc = @all;
			}
			for (@inc) {
				push(@folders, $_) unless $seen{$_}++;
			}
		}
		return $lei->fail(<<EOM) if !@folders;
no --mail-sync folders known to lei
EOM
	} else {
		my %all = map { $_ => 1 } @all;
		my @no;
		for (@folders) {
			next if $all{$_}; # ok
			if (-d "$_/new" && -d "$_/cur") {
				my $d = 'maildir:'.$lei->rel2abs($_);
				push(@no, $_) unless $all{$d};
				$_ = $d;
			} else {
				push @no, $_;
			}
		}
		my $no = join("\n\t", @no);
		return $lei->fail(<<EOF) if @no;
No sync information for: $no
Run `lei ls-mail-sync' to display valid choices
EOF
	}
	my $self = bless { lse => $lse }, __PACKAGE__;
	$lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
	$self->prepare_inputs($lei, \@folders) or return;
	my $j = $opt->{jobs} // scalar(@{$self->{inputs}}) || 1;
	if (my @ro = grep(!/\A(?:maildir|imaps?):/, @folders)) {
		return $lei->fail("cannot export to read-only folders: @ro");
	}
	if (my $net = $lei->{net}) {
		require PublicInbox::NetWriter;
		bless $net, 'PublicInbox::NetWriter';
	}
	undef $lms;
	my $m = $opt->{mode} // 'merge';
	if ($m eq 'merge') { # default
		$self->{-merge_kw} = 1;
	} elsif ($m eq 'set') {
	} else {
		return $lei->fail(<<EOM);
--mode=$m not supported (`set' or `merge')
EOM
	}
	my $ops = {};
	$lei->{auth}->op_merge($ops, $self) if $lei->{auth};
	$self->{-wq_nr_workers} = $j // 1; # locked
	(my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
	$lei->{wq1} = $self;
	$lei->{-err_type} = 'non-fatal';
	net_merge_all_done($self) unless $lei->{auth};
	$op_c->op_wait_event($ops); # calls net_merge_all_done if $lei->{auth}
}

sub _complete_export_kw {
	my ($lei, @argv) = @_;
	my $sto = $lei->_lei_store or return;
	my $lms = $sto->search->lms or return;
	my $match_cb = $lei->complete_url_prepare(\@argv);
	map { $match_cb->($_) } $lms->folders;
}

no warnings 'once';

*ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
*net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;

# the following works even when LeiAuth is lazy-loaded
*net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;

1;
debug log:

solving db4f7441 ...
found db4f7441 in public-inbox.git

user/dev discussion of public-inbox itself

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 meta meta/ https://public-inbox.org/meta \
		meta@public-inbox.org
	public-inbox-index meta

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.mail.public-inbox.meta
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.mail.public-inbox.meta
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git