blib/lib/Net/Doveadm.pm

filter: Statementstmt 88.2% 90 / 102 Branchbran 70.8% 34 / 48 Conditioncond 53.3% 8 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 78.9% 150 / 190 SCAR 38.0
CC
35
Cov
80%
_do_handshake
13.0
receive
12.0
send
6.9
CRAP 44.8
↑ Summary
1 package Net::Doveadm;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Net::Doveadm - Dovecot’s administrative interface protocol
11  
12 =head1 SYNOPSIS
13  
14 my $doveadm = Net::Doveadm->new(
15 io => $io_object,
16  
17 # Required for authentication,
18 # but we warn if the server doesn’t ask for them.
19 username => $username,
20 password => $password,
21 );
22  
23 $doveadm->send(
24 username => $cmd_username,
25 command => [ $cmd, @args ],
26 );
27  
28 my $result_ar;
29  
30 {
31 # If using non-blocking I/O …
32 # $io_object->flush_write_queue();
33  
34 last if $result_ar = $doveadm->receive();
35  
36 # If using non-blocking I/O, put a select(), poll(),
37 # or similar call here.
38  
39 redo;
40 }
41  
42 =head1 DESCRIPTION
43  
44 This module implements client logic for the
45 L<Doveadm protocol|https://wiki.dovecot.org/Design/DoveadmProtocol>,
46 which facilitates administrative communication with a
47 L<Dovecot|http://dovecot.org> server via TCP or a local socket.
48  
49 =head1 I/O
50  
51 This module is designed to use L<IO::Framed> as an abstraction over
52 the transmission medium. It is expected that most implementors will use
53 this module; however, if so desired, a compatible interface can be
54 substituted. In particular, the object must implement the C<write()>
55 and C<read_until()> methods of that class.
56  
57 If you use L<IO::Framed>, you should B<not> enable its C<allow_empty_read()>
58 mode. The Doveadm protocol is strictly RPC-oriented, which means the only
59 successful end to a session is one that the client terminates.
60  
61 Note that blocking and non-blocking I/O work nearly the same way;
62 the SYNOPSIS above demonstrates the difference. A particular feature of
63 this setup is that it’s possible to send multiple successive requests before
64 reading responses to those requests.
65  
66 =cut
67  
68 3 our $VERSION = '0.01-TRIAL1';
69  
70 use constant {
71 3 _flag_verbose => 'v',
72 _flag_debug => 'D',
73 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 3 our $DEBUG = 0;
76  
77 3 my $LF = "\x0a";
78  
79 =head1 METHODS
80  
81 =head2 I<CLASS->new( %OPTS )
82  
83 Instantiates this class. %OPTS are:
84  
85 =over
86  
87 =item * C<io> - An instance of L<IO::Framed> or a compatible interface.
88  
89 =item * C<username> - The username to use in authentication. Required if
90 the server asks for it; if given and the server does not ask for it, a
91 warning is given.
92  
93 =item * C<password> - As with C<username>.
94  
95 =back
96  
97 Note that no I/O happens in this method.
98  
99 =cut
100  
101 sub new {
102 3 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
103  
104 3 $opts{"_$_"} = delete $opts{$_} for keys %opts;
105 3 $opts{'_requests'} = [];
106  
107 3 return bless \%opts, $class;
108 }
109  
110 #----------------------------------------------------------------------
111  
112 =head2 I<OBJ>->send( %OPTS )
113  
114 Send (or enqueue the sending of) a command. %OPTS are:
115  
116 =over
117  
118 =item * C<username> - The username to send in the command.
119  
120 =item * C<command> - An array reference whose contents are (in order)
121 the command name and all arguments to the command.
122  
123 =back
124  
125 Note that, if the server handshake is not yet complete, this will
126 attempt to finish that before actually sending a message.
127  
128 =cut
129  
130 sub send {
131 3 ▶ my ($self, %opts) = @_;
Subroutine send: called CC=6 SCAR=19.3
Pod documented
132  
133 3 my $flags = q<>;
134 3 ▶ if ($opts{'flags'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"flags"}) 3 0 3
135 3 for my $flag ( @{ $opts{'flags'} } ) {
136 6 ▶ if (my $chr = __PACKAGE__->can("_flag_$flag")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chr = __PACKAGE__->can("_flag_$flag")) { } 6 0 6
137 6 $flags .= $chr->();
138 }
139 else {
140 0 die "Unknown flag: “$flag”";
141 }
142 }
143 }
144  
145 3 _validate_command_pieces( $opts{'username'}, $opts{'command'} );
146  
147 3 push @{ $self->{'_requests'} }, [ $flags, $opts{'username'}, @{ $opts{'command'} } ];
148  
149 3 ▶ if (!$self->{'_handshake_done'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_handshake_done"}) 3 0 3
150 3 ▶ return $self if !$self->_do_handshake();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_do_handshake 3 0 3
151 }
152  
153 0 $self->_flush_request_queue();
154  
155 0 return $self;
156 }
157  
158 sub _validate_command_pieces {
159 3 ▶ my ($username, $command_ar) = @_;
Subroutine _validate_command_pieces: called CC=3 SCAR=11.7
160  
161 3 for my $piece ($username, @$command_ar) {
162 12 ▶ if ($piece =~ tr<\t\x0a><>) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece =~ tr/\t\n//) 0 12 12
163 0 die "Invalid string in command: “$piece”";
164 }
165 }
166  
167 3 return;
168 }
169  
170 #----------------------------------------------------------------------
171  
172 =head2 $RESULT = I<OBJ>->receive()
173  
174 Looks for a response to a previously-sent command. If such a response is
175 ready,
176 it will be returned as an array reference; otherwise, undef is returned.
177  
178 Note that, if the server handshake is not yet complete, this will
179 attempt to finish that before actually trying to retrieve a message.
180  
181 =cut
182  
183 sub receive {
184 39 ▶ my ($self) = @_;
Subroutine receive: called CC=10 SCAR=24.8
Pod documented
185  
186 39 ▶ if (!$self->{'_handshake_done'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_handshake_done"}) 34 5 39
187 34 ▶ return undef if !$self->_do_handshake();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_do_handshake 31 3 34
188  
189 # If we just finished the handshake, then send any pending requests
190 # before we see about responses to them.
191 3 $self->_flush_request_queue();
192 }
193  
194 8 ▶ if ( !@{ $self->{'_requests'} } && !$self->{'_sent_requests'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"_requests"};} or $self->{"_sent_requests"}) 0 8 8
Condition33%1/3
@{$self->{"_requests"};} or $self->{"_sent_requests"}
l!l&&r!l&&!r
800
MC/DC0%0/2
@{$self->{"_requests"};} or $self->{"_sent_requests"}
@{$self->{"_requests"};} $self->{"_sent_requests"}
Truth table33%1/3
A or B
A= @{$self->{"_requests"};}B= $self->{"_sent_requests"}
ABresult
1X1
011
000
195 0 die "No requests pending!";
196 }
197  
198 8 ▶ $self->{'_line1'} ||= $self->_read_line() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_line1"} ||= $self->_read_line or return undef 5 3 8
Condition66%2/3
$self->{"_line1"} ||= $self->_read_line
l!l&&r!l&&!r
035
MC/DC50%1/2
$self->{"_line1"} ||= $self->_read_line
$self->{"_line1"} $self->_read_line
Truth table66%2/3
A ||= B
A= $self->{"_line1"}B= $self->_read_line
ABresult
1X1
011
000
199  
200 3 ▶ $self->{'_line2'} ||= $self->_read_line() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_line2"} ||= $self->_read_line or return undef 0 3 3
Condition33%1/3
$self->{"_line2"} ||= $self->_read_line
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->{"_line2"} ||= $self->_read_line
$self->{"_line2"} $self->_read_line
Truth table33%1/3
A ||= B
A= $self->{"_line2"}B= $self->_read_line
ABresult
1X1
011
000
201  
202 3 $self->{'_sent_requests'}--;
203  
204 3 my ($line1, $line2) = delete @{$self}{'_line1', '_line2'};
205 3 chop( $line1, $line2 );
206  
207 3 ▶ if ($line2 ne '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line2 ne "+") 0 3 3
208 0 die "Error: $line2 ($line1)";
209 }
210  
211 3 return [ split m<\t>, $line1, -1 ];
212 }
213  
214 #----------------------------------------------------------------------
215  
216 sub _flush_request_queue {
217 3 ▶ my ($self) = @_;
Subroutine _flush_request_queue: called CC=2 SCAR=9.2
218  
219 3 ▶ while ($self->_write($self->{'_requests'}[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_write($self->{"_requests"}[0])) 0 3 3
220 0 shift @{ $self->{'_requests'} };
221 0 $self->{'_sent_requests'}++;
222 }
223  
224 3 return;
225 }
226  
227 sub _do_handshake {
228 37 ▶ my ($self) = @_;
Subroutine _do_handshake: called CC=12 SCAR=25.6
229  
230 37 ▶ if (!$self->{'_sent_hello'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_sent_hello"}) 3 34 37
231 3 $self->_write( [ 'VERSION', 'doveadm-server', 1, 0 ] );
232 3 $self->{'_sent_hello'} = 1;
233 3 return undef;
234 }
235  
236 34 ▶ $self->{'_received_hello'} ||= $self->_read_line() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_received_hello"} ||= $self->_read_line or return undef 30 4 34
Condition100%3/3
$self->{"_received_hello"} ||= $self->_read_line
l!l&&r!l&&!r
1330
MC/DC100%2/2
$self->{"_received_hello"} ||= $self->_read_line
$self->{"_received_hello"} $self->_read_line
Truth table100%3/3
A ||= B
A= $self->{"_received_hello"}B= $self->_read_line
ABresult
1X1
011
000
237  
238 4 ▶ if ($self->{'_received_hello'} eq "+$LF") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_received_hello'} eq "+$LF") { } 2 2 4
elsif ($self->{'_received_hello'} eq "-$LF") { } 2 0 2
239 2 $self->{'_handshake_done'} = 1;
240  
241 2 for my $key ( qw( username password ) ) {
242 4 ▶ if ($self->{"_$key"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"_$key"}) 0 4 4
243 0 warn "“$key” submitted, but server says unneeded.";
244 }
245 }
246 }
247 elsif ($self->{'_received_hello'} eq "-$LF") {
248  
249 2 ▶ if (!$self->{'_authn_sent'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_authn_sent"}) 1 1 2
250 1 $self->_send_authn();
251  
252 1 $self->{'_authn_sent'} = 1;
253  
254 1 return undef;
255 }
256  
257 1 ▶ $self->{'_received_authn'} ||= $self->_read_line() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_received_authn"} ||= $self->_read_line or return undef 0 1 1
Condition33%1/3
$self->{"_received_authn"} ||= $self->_read_line
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_received_authn"} ||= $self->_read_line
$self->{"_received_authn"} $self->_read_line
Truth table33%1/3
A ||= B
A= $self->{"_received_authn"}B= $self->_read_line
ABresult
1X1
011
000
258  
259 1 ▶ if ($self->{'_received_authn'} eq "+$LF") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_received_authn'} eq "+$LF") { } 1 0 1
260  
261 1 $self->{'_handshake_done'} = 1;
262 }
263 else {
264 0 die "Failed authn: “$self->{'_received_authn'}”";
265 }
266 }
267  
268 3 return 1;
269 }
270  
271 sub _send_authn {
272 1 ▶ my ($self) = @_;
Subroutine _send_authn: called CC=3 SCAR=11.2
273  
274 1 for my $key ( qw( username password ) ) {
275 2 ▶ if (!length $self->{"_$key"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $$self{"_$key"}) 0 2 2
276 0 die "“$key” not submitted, but server says needed!";
277 }
278 }
279  
280 1 require MIME::Base64;
281 1 my $authn_b64 = MIME::Base64::encode_base64("\0" . $self->{'_username'} . "\0" . $self->{'_password'});
282 1 chop $authn_b64;
283  
284 1 $self->_write( [ 'PLAIN', $authn_b64 ] );
285  
286 1 return;
287 }
288  
289 sub _write {
290 7 ▶ my ($self, $pieces_ar) = @_;
Subroutine _write: called CC=2 SCAR=6.9
291  
292 7 ▶ $DEBUG && print "$$ doveadm enqueue send: [@$pieces_ar]\n";
Branch: 1 branch
BranchTrueFalseTotal
$Net::Doveadm::DEBUG and print "${$} doveadm enqueue send: [@$pieces_ar]\n" 3 4 7
293  
294 7 return $self->{'_io'}->write( join("\t", @$pieces_ar ) . $LF );
295 }
296  
297 3 my $line_sr;
298  
299 sub _read_line {
300 45 ▶ my ($self) = @_;
Subroutine _read_line: called CC=3 SCAR=11.0
301  
302 45 $line_sr = \$self->{'_io'}->read_until($LF);
303  
304 45 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Doveadm::DEBUG) 5 40 45
305 5 ▶ if ($$line_sr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$line_sr) { } 4 1 5
306 4 chop $$line_sr;
307  
308 4 printf "$$ doveadm received: [$$line_sr]$LF";
309 4 return "$$line_sr$LF";
310 }
311 else {
312 1 printf "$$ no line yet fully received\n";
313 }
314 }
315  
316 41 return $$line_sr;
317 }
318  
319 #----------------------------------------------------------------------
320  
321 =head1 REPOSITORY
322  
323 L<https://github.com/FGasper/p5-Net-Doveadm>
324  
325 =head1 AUTHOR
326  
327 Felipe Gasper (FELIPE)
328  
329 =head1 COPYRIGHT
330  
331 Copyright 2018 by L<Gasper Software Consulting|http://gaspersoftware.com>
332  
333 =head1 LICENSE
334  
335 This distribution is released under the same license as Perl.
336  
337 =cut
338  
339 3 1;
↑ Summary