]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiAuth.pm
lei <import|convert>: support NNTP sources
[public-inbox.git] / lib / PublicInbox / LeiAuth.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 # Authentication worker for anything that needs auth for read/write IMAP
5 # (eventually for read-only NNTP access)
6 package PublicInbox::LeiAuth;
7 use strict;
8 use v5.10.1;
9 use PublicInbox::PktOp qw(pkt_do);
10
11 sub do_auth_atfork { # used by IPC WQ workers
12         my ($self, $wq) = @_;
13         return if $wq->{-wq_worker_nr} != 0;
14         my $lei = $wq->{lei};
15         my $net = $lei->{net};
16         my $mics = $net->imap_common_init($lei);
17         my $nn = $net->nntp_common_init($lei);
18         pkt_do($lei->{pkt_op_p}, 'net_merge', $net) or
19                         die "pkt_do net_merge: $!";
20         $net->{mics_cached} = $mics if $mics;
21         $net->{nn_cached} = $nn if $nn;
22 }
23
24 sub net_merge_done1 { # bump merge-count in top-level lei-daemon
25         my ($wq) = @_;
26         return if ++$wq->{nr_net_merge_done} != $wq->{-wq_nr_workers};
27         $wq->net_merge_complete; # defined per wq-class (e.g. LeiImport)
28 }
29
30 sub net_merge_all { # called in wq worker via wq_broadcast
31         my ($wq, $net_new) = @_;
32         my $net = $wq->{lei}->{net};
33         %$net = (%$net, %$net_new);
34         pkt_do($wq->{lei}->{pkt_op_p}, 'net_merge_done1') or
35                 die "pkt_op_do net_merge_done1: $!";
36 }
37
38 # called by top-level lei-daemon when first worker is done with auth
39 sub net_merge_continue {
40         my ($wq, $net_new) = @_;
41         $wq->wq_broadcast('net_merge_all', $net_new);
42 }
43
44 sub op_merge { # prepares PktOp->pair ops
45         my ($self, $ops, $wq) = @_;
46         $ops->{net_merge} = [ \&net_merge_continue, $wq ];
47         $ops->{net_merge_done1} = [ \&net_merge_done1, $wq ];
48 }
49
50 sub new { bless \(my $x), __PACKAGE__ }
51
52 1;