]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiConvert.pm
lei convert: mail format conversion sub-command
[public-inbox.git] / lib / PublicInbox / LeiConvert.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # front-end for the "lei convert" sub-command
5 package PublicInbox::LeiConvert;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC);
9 use PublicInbox::Eml;
10 use PublicInbox::InboxWritable qw(eml_from_path);
11 use PublicInbox::PktOp;
12 use PublicInbox::LeiStore;
13 use PublicInbox::LeiOverview;
14
15 sub mbox_cb {
16         my ($eml, $self) = @_;
17         my @kw = PublicInbox::LeiStore::mbox_keywords($eml);
18         $eml->header_set($_) for qw(Status X-Status);
19         $self->{wcb}->(undef, { kw => \@kw }, $eml);
20 }
21
22 sub imap_cb { # ->imap_each
23         my ($url, $uid, $kw, $eml, $self) = @_;
24         $self->{wcb}->(undef, { kw => $kw }, $eml);
25 }
26
27 sub mdir_cb {
28         my ($kw, $eml, $self) = @_;
29         $self->{wcb}->(undef, { kw => $kw }, $eml);
30 }
31
32 sub do_convert { # via wq_do
33         my ($self) = @_;
34         my $lei = $self->{lei};
35         my $in_fmt = $lei->{opt}->{'in-format'};
36         if (my $stdin = delete $self->{0}) {
37                 PublicInbox::MboxReader->$in_fmt($stdin, \&mbox_cb, $self);
38         }
39         for my $input (@{$self->{inputs}}) {
40                 my $ifmt = lc($in_fmt // '');
41                 if ($input =~ m!\A(?:imap|nntp)s?://!) { # TODO: nntp
42                         $lei->{nrd}->imap_each($input, \&imap_cb, $self);
43                         next;
44                 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
45                         $ifmt = lc $1;
46                 }
47                 if (-f $input) {
48                         open my $fh, '<', $input or
49                                         return $lei->fail("open $input: $!");
50                         PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self);
51                 } elsif (-d _) {
52                         PublicInbox::MdirReader::maildir_each_eml($input,
53                                                         \&mdir_cb, $self);
54                 } else {
55                         die "BUG: $input unhandled"; # should've failed earlier
56                 }
57         }
58         delete $lei->{1};
59         delete $self->{wcb}; # commit
60 }
61
62 sub convert_start {
63         my ($lei) = @_;
64         my $ops = {
65                 '!' => [ $lei->can('fail_handler'), $lei ],
66                 '|' => [ $lei->can('sigpipe_handler'), $lei ],
67                 'x_it' => [ $lei->can('x_it'), $lei ],
68                 'child_error' => [ $lei->can('child_error'), $lei ],
69                 '' => [ $lei->can('dclose'), $lei ],
70         };
71         ($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
72         my $self = $lei->{cnv};
73         $self->wq_workers_start('lei_convert', 1, $lei->oldset, {lei => $lei});
74         my $op = delete $lei->{pkt_op_c};
75         delete $lei->{pkt_op_p};
76         $self->wq_io_do('do_convert', []);
77         $self->wq_close(1);
78         $lei->event_step_init; # wait for shutdowns
79         if ($lei->{oneshot}) {
80                 while ($op->{sock}) { $op->event_step }
81         }
82 }
83
84 sub call { # the main "lei convert" method
85         my ($cls, $lei, @inputs) = @_;
86         my $opt = $lei->{opt};
87         $opt->{kw} //= 1;
88         my $self = $lei->{cnv} = bless {}, $cls;
89         my $in_fmt = $opt->{'in-format'};
90         my ($nrd, @f, @d);
91         $opt->{dedupe} //= 'none';
92         my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
93         $lei->{l2m} or return
94                 $lei->fail("output not specified or is not a mail destination");
95         $opt->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
96         if ($opt->{stdin}) {
97                 @inputs and return $lei->fail("--stdin and @inputs do not mix");
98                 $lei->check_input_format(undef, 'in-format') or return;
99                 $self->{0} = $lei->{0};
100         }
101         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
102         for my $input (@inputs) {
103                 my $input_path = $input;
104                 if ($input =~ m!\A(?:imap|nntp)s?://!i) {
105                         require PublicInbox::NetReader;
106                         $nrd //= PublicInbox::NetReader->new;
107                         $nrd->add_url($input);
108                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
109                         my $ifmt = lc $1;
110                         if (($in_fmt // $ifmt) ne $ifmt) {
111                                 return $lei->fail(<<"");
112 --in-format=$in_fmt and `$ifmt:' conflict
113
114                         }
115                         if (-f $input_path) {
116                                 require PublicInbox::MboxReader;
117                                 PublicInbox::MboxReader->can($ifmt) or return
118                                         $lei->fail("$ifmt not supported");
119                         } elsif (-d _) {
120                                 require PublicInbox::MdirReader;
121                                 $ifmt eq 'maildir' or return
122                                         $lei->fail("$ifmt not supported");
123                         } else {
124                                 return $lei->fail("Unable to handle $input");
125                         }
126                 } elsif (-f $input) { push @f, $input }
127                 elsif (-d _) { push @d, $input }
128                 else { return $lei->fail("Unable to handle $input") }
129         }
130         if (@f) { $lei->check_input_format(\@f, 'in-format') or return }
131         if (@d) { # TODO: check for MH vs Maildir, here
132                 require PublicInbox::MdirReader;
133         }
134         $self->{inputs} = \@inputs;
135         return convert_start($lei) if !$nrd;
136
137         if (my $err = $nrd->errors) {
138                 return $lei->fail($err);
139         }
140         $nrd->{quiet} = $opt->{quiet};
141         $lei->{nrd} = $nrd;
142         require PublicInbox::LeiAuth;
143         my $auth = $lei->{auth} = PublicInbox::LeiAuth->new($nrd);
144         $auth->auth_start($lei, \&convert_start, $lei);
145 }
146
147 sub ipc_atfork_child {
148         my ($self) = @_;
149         my $lei = $self->{lei};
150         $lei->lei_atfork_child;
151         my $l2m = delete $lei->{l2m};
152         $l2m->pre_augment($lei);
153         $l2m->do_augment($lei);
154         $l2m->post_augment($lei);
155         $self->{wcb} = $l2m->write_cb($lei);
156         $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
157         $self->SUPER::ipc_atfork_child;
158 }
159
160 1;