]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiConvert.pm
4c0bbd882c1c8bfedf0970a9e5e3c65198b2f8ec
[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::LeiStore;
12 use PublicInbox::LeiOverview;
13
14 sub mbox_cb {
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);
19 }
20
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);
24 }
25
26 sub mdir_cb {
27         my ($kw, $eml, $self) = @_;
28         $self->{wcb}->(undef, { kw => $kw }, $eml);
29 }
30
31 sub convert_fh ($$$$) {
32         my ($self, $ifmt, $fh, $name) = @_;
33         if ($ifmt eq 'eml') {
34                 my $buf = do { local $/; <$fh> } //
35                         return $self->{lei}->child_error(1 << 8, <<"");
36 error reading $name: $!
37
38                 my $eml = PublicInbox::Eml->new(\$buf);
39                 $self->{wcb}->(undef, { kw => [] }, $eml);
40         } else {
41                 PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self);
42         }
43 }
44
45 sub do_convert { # via wq_do
46         my ($self) = @_;
47         my $lei = $self->{lei};
48         my $in_fmt = $lei->{opt}->{'in-format'};
49         my $mics;
50         if (my $stdin = delete $self->{0}) {
51                 convert_fh($self, $in_fmt, $stdin, '<stdin>');
52         }
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);
57                         next;
58                 } elsif ($input =~ m!\A(?:nntps?|s?news)://!) {
59                         $lei->{net}->nntp_each($input, \&net_cb, $self);
60                         next;
61                 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
62                         $ifmt = lc $1;
63                 }
64                 if (-f $input) {
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);
70                 } elsif (-d _) {
71                         PublicInbox::MdirReader::maildir_each_eml($input,
72                                                         \&mdir_cb, $self);
73                 } else {
74                         die "BUG: $input unhandled"; # should've failed earlier
75                 }
76         }
77         delete $lei->{1};
78         delete $self->{wcb}; # commit
79 }
80
81 sub call { # the main "lei convert" method
82         my ($cls, $lei, @inputs) = @_;
83         my $opt = $lei->{opt};
84         $opt->{kw} //= 1;
85         my $self = $lei->{cnv} = bless {}, $cls;
86         my $in_fmt = $opt->{'in-format'};
87         my (@f, @d);
88         $opt->{dedupe} //= 'none';
89         my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
90         $lei->{l2m} or return
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';
94         if ($opt->{stdin}) {
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};
98         }
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) {
107                         my $ifmt = lc $1;
108                         if (($in_fmt // $ifmt) ne $ifmt) {
109                                 return $lei->fail(<<"");
110 --in-format=$in_fmt and `$ifmt:' conflict
111
112                         }
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");
118                         } elsif (-d _) {
119                                 require PublicInbox::MdirReader;
120                                 $ifmt eq 'maildir' or return
121                                         $lei->fail("$ifmt not supported");
122                         } else {
123                                 return $lei->fail("Unable to handle $input");
124                         }
125                 } elsif (-f $input) { push @f, $input }
126                 elsif (-d _) { push @d, $input }
127                 else { return $lei->fail("Unable to handle $input") }
128         }
129         if (@f) { $lei->check_input_format(\@f) or return }
130         if (@d) { # TODO: check for MH vs Maildir, here
131                 require PublicInbox::MdirReader;
132         }
133         $self->{inputs} = \@inputs;
134         if ($net) {
135                 if (my $err = $net->errors) {
136                         return $lei->fail($err);
137                 }
138                 $net->{quiet} = $opt->{quiet};
139                 $lei->{net} //= $net;
140         }
141         my $op = $lei->workers_start($self, 'lei_convert', 1, {
142                 '' => [ $lei->can('dclose'), $lei ]
143         });
144         $self->wq_io_do('do_convert', []);
145         $self->wq_close(1);
146         while ($op && $op->{sock}) { $op->event_step }
147 }
148
149 sub ipc_atfork_child {
150         my ($self) = @_;
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);
157         }
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;
164 }
165
166 1;