]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiConvert.pm
lei: consolidate the bulk of the IPC code
[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 imap_cb { # ->imap_each
22         my ($url, $uid, $kw, $eml, $self) = @_;
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 do_convert { # via wq_do
32         my ($self) = @_;
33         my $lei = $self->{lei};
34         my $in_fmt = $lei->{opt}->{'in-format'};
35         if (my $stdin = delete $self->{0}) {
36                 PublicInbox::MboxReader->$in_fmt($stdin, \&mbox_cb, $self);
37         }
38         for my $input (@{$self->{inputs}}) {
39                 my $ifmt = lc($in_fmt // '');
40                 if ($input =~ m!\A(?:imap|nntp)s?://!) { # TODO: nntp
41                         $lei->{nrd}->imap_each($input, \&imap_cb, $self);
42                         next;
43                 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
44                         $ifmt = lc $1;
45                 }
46                 if (-f $input) {
47                         open my $fh, '<', $input or
48                                         return $lei->fail("open $input: $!");
49                         PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self);
50                 } elsif (-d _) {
51                         PublicInbox::MdirReader::maildir_each_eml($input,
52                                                         \&mdir_cb, $self);
53                 } else {
54                         die "BUG: $input unhandled"; # should've failed earlier
55                 }
56         }
57         delete $lei->{1};
58         delete $self->{wcb}; # commit
59 }
60
61 sub convert_start { # LeiAuth->auth_start callback
62         my ($lei) = @_;
63         my $self = $lei->{cnv};
64         my $op = $lei->workers_start($self, 'lei_convert', 1, {
65                 '' => [ $lei->can('dclose'), $lei ]
66         });
67         $self->wq_io_do('do_convert', []);
68         $self->wq_close(1);
69         while ($op && $op->{sock}) { $op->event_step }
70 }
71
72 sub call { # the main "lei convert" method
73         my ($cls, $lei, @inputs) = @_;
74         my $opt = $lei->{opt};
75         $opt->{kw} //= 1;
76         my $self = $lei->{cnv} = bless {}, $cls;
77         my $in_fmt = $opt->{'in-format'};
78         my ($nrd, @f, @d);
79         $opt->{dedupe} //= 'none';
80         my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
81         $lei->{l2m} or return
82                 $lei->fail("output not specified or is not a mail destination");
83         $opt->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
84         if ($opt->{stdin}) {
85                 @inputs and return $lei->fail("--stdin and @inputs do not mix");
86                 $lei->check_input_format(undef, 'in-format') or return;
87                 $self->{0} = $lei->{0};
88         }
89         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
90         for my $input (@inputs) {
91                 my $input_path = $input;
92                 if ($input =~ m!\A(?:imap|nntp)s?://!i) {
93                         require PublicInbox::NetReader;
94                         $nrd //= PublicInbox::NetReader->new;
95                         $nrd->add_url($input);
96                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
97                         my $ifmt = lc $1;
98                         if (($in_fmt // $ifmt) ne $ifmt) {
99                                 return $lei->fail(<<"");
100 --in-format=$in_fmt and `$ifmt:' conflict
101
102                         }
103                         if (-f $input_path) {
104                                 require PublicInbox::MboxReader;
105                                 PublicInbox::MboxReader->can($ifmt) or return
106                                         $lei->fail("$ifmt not supported");
107                         } elsif (-d _) {
108                                 require PublicInbox::MdirReader;
109                                 $ifmt eq 'maildir' or return
110                                         $lei->fail("$ifmt not supported");
111                         } else {
112                                 return $lei->fail("Unable to handle $input");
113                         }
114                 } elsif (-f $input) { push @f, $input }
115                 elsif (-d _) { push @d, $input }
116                 else { return $lei->fail("Unable to handle $input") }
117         }
118         if (@f) { $lei->check_input_format(\@f, 'in-format') or return }
119         if (@d) { # TODO: check for MH vs Maildir, here
120                 require PublicInbox::MdirReader;
121         }
122         $self->{inputs} = \@inputs;
123         return convert_start($lei) if !$nrd;
124
125         if (my $err = $nrd->errors) {
126                 return $lei->fail($err);
127         }
128         $nrd->{quiet} = $opt->{quiet};
129         $lei->{nrd} = $nrd;
130         require PublicInbox::LeiAuth;
131         my $auth = $lei->{auth} = PublicInbox::LeiAuth->new($nrd);
132         $auth->auth_start($lei, \&convert_start, $lei);
133 }
134
135 sub ipc_atfork_child {
136         my ($self) = @_;
137         my $lei = $self->{lei};
138         $lei->lei_atfork_child;
139         my $l2m = delete $lei->{l2m};
140         $l2m->pre_augment($lei);
141         $l2m->do_augment($lei);
142         $l2m->post_augment($lei);
143         $self->{wcb} = $l2m->write_cb($lei);
144         $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
145         $self->SUPER::ipc_atfork_child;
146 }
147
148 1;