1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Authentication worker for anything that needs auth for read/write IMAP
5 # (eventually for read-only NNTP access)
8 # lei-daemon | LeiAuth worker #0 | other WQ workers
9 # ----------------------------------------------------------
10 # spawns all workers ---->[ workers all start and run ipc_atfork_child ]
11 # | do_auth_atfork | wq_worker_loop sleep
13 # | # queries git-credential|
14 # | send net_merge_continue |
17 # recv net_merge_continue <---------/ |
20 # broadcast net_merge_all [ all workers (including LeiAuth worker #0) ]
21 # [ LeiAuth worker #0 becomes just another WQ worker ]
23 # call net_merge_all_done ->-> do per-WQ-class defined actions
24 package PublicInbox::LeiAuth;
28 sub do_auth_atfork { # used by IPC WQ workers
30 return if $wq->{-wq_worker_nr} != 0; # only first worker calls this
32 my $net = $lei->{net};
33 eval { # fill auth info (may prompt user or read netrc)
34 my $mics = $net->imap_common_init($lei);
35 my $nn = $net->nntp_common_init($lei);
36 # broadcast successful auth info to lei-daemon:
37 $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
38 die "pkt_do net_merge_continue: $!";
39 $net->{mics_cached} = $mics if $mics;
40 $net->{nn_cached} = $nn if $nn;
45 sub net_merge_all { # called in wq worker via wq_broadcast
46 my ($wq, $net_new) = @_;
47 my $net = $wq->{lei}->{net};
48 %$net = (%$net, %$net_new);
51 # called by top-level lei-daemon when first worker is done with auth
52 sub net_merge_continue {
53 my ($wq, $net_new) = @_;
54 $wq->wq_broadcast('net_merge_all', $net_new); # pass to current workers
55 $wq->net_merge_all_done; # defined per-WQ
58 sub op_merge { # prepares PktOp->pair ops
59 my ($self, $ops, $wq) = @_;
60 $ops->{net_merge_continue} = [ \&net_merge_continue, $wq ];
63 sub new { bless \(my $x), __PACKAGE__ }