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