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 convert" sub-command
5 package PublicInbox::LeiConvert;
8 use parent qw(PublicInbox::IPC);
10 use PublicInbox::InboxWritable qw(eml_from_path);
11 use PublicInbox::LeiStore;
12 use PublicInbox::LeiOverview;
15 my ($eml, $self) = @_;
16 my @kw = PublicInbox::LeiStore::mbox_keywords($eml);
17 $eml->header_set($_) for qw(Status X-Status);
18 $self->{wcb}->(undef, { kw => \@kw }, $eml);
21 sub net_cb { # callback for ->imap_each, ->nntp_each
22 my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored
23 $self->{wcb}->(undef, { kw => $kw }, $eml);
27 my ($kw, $eml, $self) = @_;
28 $self->{wcb}->(undef, { kw => $kw }, $eml);
31 sub convert_fh ($$$$) {
32 my ($self, $ifmt, $fh, $name) = @_;
34 my $buf = do { local $/; <$fh> } //
35 return $self->{lei}->child_error(1 << 8, <<"");
36 error reading $name: $!
38 my $eml = PublicInbox::Eml->new(\$buf);
39 $self->{wcb}->(undef, { kw => [] }, $eml);
41 PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self);
45 sub do_convert { # via wq_do
47 my $lei = $self->{lei};
48 my $in_fmt = $lei->{opt}->{'in-format'};
50 if (my $stdin = delete $self->{0}) {
51 convert_fh($self, $in_fmt, $stdin, '<stdin>');
53 for my $input (@{$self->{inputs}}) {
54 my $ifmt = lc($in_fmt // '');
55 if ($input =~ m!\Aimaps?://!) {
56 $lei->{net}->imap_each($input, \&net_cb, $self);
58 } elsif ($input =~ m!\A(?:nntps?|s?news)://!) {
59 $lei->{net}->nntp_each($input, \&net_cb, $self);
61 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
65 my $m = $lei->{opt}->{'lock'} //
66 ($ifmt eq 'eml' ? ['none'] :
67 PublicInbox::MboxLock->defaults);
68 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
69 convert_fh($self, $ifmt, $mbl->{fh}, $input);
71 PublicInbox::MdirReader::maildir_each_eml($input,
74 die "BUG: $input unhandled"; # should've failed earlier
78 delete $self->{wcb}; # commit
81 sub call { # the main "lei convert" method
82 my ($cls, $lei, @inputs) = @_;
83 my $opt = $lei->{opt};
85 my $self = $lei->{cnv} = bless {}, $cls;
86 my $in_fmt = $opt->{'in-format'};
88 $opt->{dedupe} //= 'none';
89 my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
91 $lei->fail("output not specified or is not a mail destination");
92 my $net = $lei->{net}; # NetWriter may be created by l2m
93 $opt->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
95 @inputs and return $lei->fail("--stdin and @inputs do not mix");
96 $lei->check_input_format(undef) or return;
97 $self->{0} = $lei->{0};
99 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
100 for my $input (@inputs) {
101 my $input_path = $input;
102 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
103 require PublicInbox::NetReader;
104 $net //= PublicInbox::NetReader->new;
105 $net->add_url($input);
106 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
108 if (($in_fmt // $ifmt) ne $ifmt) {
109 return $lei->fail(<<"");
110 --in-format=$in_fmt and `$ifmt:' conflict
113 if (-f $input_path) {
114 require PublicInbox::MboxLock;
115 require PublicInbox::MboxReader;
116 PublicInbox::MboxReader->can($ifmt) or return
117 $lei->fail("$ifmt not supported");
119 require PublicInbox::MdirReader;
120 $ifmt eq 'maildir' or return
121 $lei->fail("$ifmt not supported");
123 return $lei->fail("Unable to handle $input");
125 } elsif (-f $input) { push @f, $input }
126 elsif (-d _) { push @d, $input }
127 else { return $lei->fail("Unable to handle $input") }
129 if (@f) { $lei->check_input_format(\@f) or return }
130 if (@d) { # TODO: check for MH vs Maildir, here
131 require PublicInbox::MdirReader;
133 $self->{inputs} = \@inputs;
135 if (my $err = $net->errors) {
136 return $lei->fail($err);
138 $net->{quiet} = $opt->{quiet};
139 $lei->{net} //= $net;
141 my $op = $lei->workers_start($self, 'lei_convert', 1, {
142 '' => [ $lei->can('dclose'), $lei ]
144 $self->wq_io_do('do_convert', []);
146 while ($op && $op->{sock}) { $op->event_step }
149 sub ipc_atfork_child {
151 my $lei = $self->{lei};
152 $lei->lei_atfork_child;
153 my $l2m = delete $lei->{l2m};
154 if (my $net = $lei->{net}) { # may prompt user once
155 $net->{mics_cached} = $net->imap_common_init($lei);
156 $net->{nn_cached} = $net->nntp_common_init($lei);
158 $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
159 $l2m->pre_augment($lei);
160 $l2m->do_augment($lei);
161 $l2m->post_augment($lei);
162 $self->{wcb} = $l2m->write_cb($lei);
163 $self->SUPER::ipc_atfork_child;