lib/Mail/Pyzor/Client.pm

filter: Statementstmt 88.8% 88 / 99 Branchbran 54.1% 13 / 24 Conditioncond 54.1% 13 / 24 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 94.7% 18 / 19 Podpod 100% 3 / 3 totaltotal 73.9% 136 / 184 CC 21 SCAR 15.9
CC
21
Cov
78%
_receive_packet
11.9
new
6.9
_send_packet
6.0
CRAP 4.9
« blib/lib/Mail/Pyzor/Digest/StripHtml.pm ↑ Summary lib/Mail/Pyzor/SHA.pm »
1 package Mail::Pyzor::Client;
2  
3 # Copyright 2018 cPanel, LLC.
4 # All rights reserved.
5 # http://cpanel.net
6 #
7 # This is free software; you can redistribute it and/or modify it under the
8 # same terms as Perl itself. See L<perlartistic>.
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =encoding utf-8
14  
15 =head1 NAME
16  
17 Mail::Pyzor::Client - Pyzor client logic
18  
19 =head1 SYNOPSIS
20  
21 use Mail::Pyzor::Client ();
22 use Mail::Pyzor::Digest ();
23  
24 my $client = Mail::Pyzor::Client->new();
25  
26 my $digest = Cpanel::Pyzor::Digest::get( $msg );
27  
28 my $check_ref = $client->check($digest);
29 die $check_ref->{'Diag'} if $check_ref->{'Code'} ne '200';
30  
31 my $report_ref = $client->report($digest);
32 die $report_ref->{'Diag'} if $report_ref->{'Code'} ne '200';
33  
34 =head1 DESCRIPTION
35  
36 A bare-bones L<Pyzor|http://pyzor.org> client that currently only
37 implements the functionality needed for L<Mail::SpamAssassin>.
38  
39 =head1 PROTOCOL DETAILS
40  
41 The Pyzor protocol is not a published standard, and there appears to be
42 no meaningful public documentation. What follows is enough information,
43 largely gleaned through forum posts and reverse engineering, to facilitate
44 effective use of this module:
45  
46 Pyzor is an RPC-oriented, message-based protocol. Each message
47 is a simple dictionary of 7-bit ASCII keys and values. Server responses
48 always include at least the following:
49  
50 =over
51  
52 =item * C<Code> - Similar to HTTP status codes; anything besides C<200>
53 is an error.
54  
55 =item * C<Diag> - Similar to HTTP status reasons: a text description
56 of the status.
57  
58 =back
59  
60 (NB: There are additional standard response headers that are useful only for
61 the protocol itself and thus are not part of this module’s returns.)
62  
63 =head2 Reliability
64  
65 Pyzor uses UDP rather than TCP, so no message is guaranteed to reach its
66 destination. A transmission failure can happen in either the request or
67 the response; in either case, a timeout error will result. Such errors
68 are represented as thrown instances of L<Mail::Pyzor::X::Timeout>.
69  
70 =cut
71  
72 #----------------------------------------------------------------------
73  
74 2 our $DEFAULT_SERVER_HOST = 'public.pyzor.org';
75 2 our $DEFAULT_SERVER_PORT = 24441;
76 2 our $DEFAULT_USERNAME = 'anonymous';
77 2 our $DEFAULT_PASSWORD = '';
78 2 our $DEFAULT_OP_SPEC = '20,3,60,3';
79 2 our $PYZOR_PROTOCOL_VERSION = 2.1;
80 2 our $DEFAULT_TIMEOUT = 3.5;
81 2 our $READ_SIZE = 8192;
82  
83 2 ▶use Mail::Pyzor::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use IO::Socket::INET ();
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use IO::SigGuard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use Mail::Pyzor::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 2 my @hash_order = ( 'Op', 'Op-Digest', 'Op-Spec', 'Thread', 'PV', 'User', 'Time', 'Sig' );
89  
90 #----------------------------------------------------------------------
91  
92 =head1 CONSTRUCTOR
93  
94 =head2 new(%OPTS)
95  
96 Create a new pyzor client.
97  
98 =over 2
99  
100 =item Input
101  
102 %OPTS are (all optional):
103  
104 =over 3
105  
106 =item * C<server_host> - The pyzor server host to connect to (default is
107 C<public.pyzor.org>)
108  
109 =item * C<server_port> - The pyzor server port to connect to (default is
110 24441)
111  
112 =item * C<username> - The username to present to the pyzor server (default
113 is C<anonymous>)
114  
115 =item * C<password> - The password to present to the pyzor server (default
116 is empty)
117  
118 =item * C<timeout> - The maximum time, in seconds, to wait for a response
119 from the pyzor server (defeault is 3.5)
120  
121 =back
122  
123 =item Output
124  
125 =over 3
126  
127 Returns a L<Cpanel::Pyzor::Client> object.
128  
129 =back
130  
131 =back
132  
133 =cut
134  
135 sub new {
136 5 ▶ my ( $class, %OPTS ) = @_;
Subroutine new: called CC=6 SCAR=19.3
Pod documented
137  
138 return bless {
139 '_server_host' => $OPTS{'server_host'} || $DEFAULT_SERVER_HOST,
140 '_server_port' => $OPTS{'server_port'} || $DEFAULT_SERVER_PORT,
141 '_username' => $OPTS{'username'} || $DEFAULT_USERNAME,
142 '_password' => $OPTS{'password'} || $DEFAULT_PASSWORD,
143 '_op_spec' => $DEFAULT_OP_SPEC,
144 5 ▶ '_timeout' => $OPTS{'timeout'} || $DEFAULT_TIMEOUT,
Condition66%10/15
$OPTS{'server_host'} || $Mail::Pyzor::Client::DEFAULT_SERVER_HOST
l!l&&r!l&&!r
140
$OPTS{'server_port'} || $Mail::Pyzor::Client::DEFAULT_SERVER_PORT
l!l&&r!l&&!r
140
$OPTS{'username'} || $Mail::Pyzor::Client::DEFAULT_USERNAME
l!l&&r!l&&!r
140
$OPTS{'password'} || $Mail::Pyzor::Client::DEFAULT_PASSWORD
l!l&&r!l&&!r
104
$OPTS{'timeout'} || $Mail::Pyzor::Client::DEFAULT_TIMEOUT
l!l&&r!l&&!r
140
MC/DC0%0/2
$OPTS{'server_host'} || $Mail::Pyzor::Client::DEFAULT_SERVER_HOST
$OPTS{'server_host'} $Mail::Pyzor::Client::DEFAULT_SERVER_HOST
MC/DC0%0/2
$OPTS{'server_port'} || $Mail::Pyzor::Client::DEFAULT_SERVER_PORT
$OPTS{'server_port'} $Mail::Pyzor::Client::DEFAULT_SERVER_PORT
MC/DC0%0/2
$OPTS{'username'} || $Mail::Pyzor::Client::DEFAULT_USERNAME
$OPTS{'username'} $Mail::Pyzor::Client::DEFAULT_USERNAME
MC/DC50%1/2
$OPTS{'password'} || $Mail::Pyzor::Client::DEFAULT_PASSWORD
$OPTS{'password'} $Mail::Pyzor::Client::DEFAULT_PASSWORD
MC/DC0%0/2
$OPTS{'timeout'} || $Mail::Pyzor::Client::DEFAULT_TIMEOUT
$OPTS{'timeout'} $Mail::Pyzor::Client::DEFAULT_TIMEOUT
Truth table66%2/3
A || B
A= $OPTS{'server_host'}B= $Mail::Pyzor::Client::DEFAULT_SERVER_HOST
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $OPTS{'server_port'}B= $Mail::Pyzor::Client::DEFAULT_SERVER_PORT
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $OPTS{'username'}B= $Mail::Pyzor::Client::DEFAULT_USERNAME
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $OPTS{'password'}B= $Mail::Pyzor::Client::DEFAULT_PASSWORD
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $OPTS{'timeout'}B= $Mail::Pyzor::Client::DEFAULT_TIMEOUT
ABresult
1X1
011
000
145 }, $class;
146 }
147  
148 #----------------------------------------------------------------------
149  
150 =head1 REQUEST METHODS
151  
152 =head2 report($digest)
153  
154 Report the digest of a spam message to the pyzor server. This function
155 will throw if a messaging failure or timeout happens.
156  
157 =over 2
158  
159 =item Input
160  
161 =over 3
162  
163 =item $digest C<SCALAR>
164  
165 The message digest to report, as given by
166 C<Mail::Pyzor::Digest::get()>.
167  
168 =back
169  
170 =item Output
171  
172 =over 3
173  
174 =item C<HASHREF>
175  
176 Returns a hashref of the standard attributes noted above.
177  
178 =back
179  
180 =back
181  
182 =cut
183  
184 sub report {
185 2 ▶ my ( $self, $digest ) = @_;
Subroutine report: called CC=1 SCAR=0.0
Pod documented
186  
187 2 my $msg_ref = $self->_get_base_msg( 'report', $digest );
188  
189 1 $msg_ref->{'Op-Spec'} = $self->{'_op_spec'};
190  
191 1 return $self->_send_receive_msg($msg_ref);
192 }
193  
194 =head2 check($digest)
195  
196 Check the digest of a message to see if
197 the pyzor server has a report for it. This function
198 will throw if a messaging failure or timeout happens.
199  
200 =over 2
201  
202 =item Input
203  
204 =over 3
205  
206 =item $digest C<SCALAR>
207  
208 The message digest to check, as given by
209 C<Mail::Pyzor::Digest::get()>.
210  
211 =back
212  
213 =item Output
214  
215 =over 3
216  
217 =item C<HASHREF>
218  
219 Returns a hashref of the standard attributes noted above
220 as well as the following:
221  
222 =over
223  
224 =item * C<Count> - The number of reports the server has received
225 for the given digest.
226  
227 =item * C<WL-Count> - The number of whitelist requests the server has received
228 for the given digest.
229  
230 =back
231  
232 =back
233  
234 =back
235  
236 =cut
237  
238 sub check {
239 2 ▶ my ( $self, $digest ) = @_;
Subroutine check: called CC=1 SCAR=0.0
Pod documented
240  
241 2 return $self->_send_receive_msg( $self->_get_base_msg( 'check', $digest ) );
242 }
243  
244 # ----------------------------------------
245  
246 sub _send_receive_msg {
247 2 ▶ my ( $self, $msg_ref ) = @_;
Subroutine _send_receive_msg: called CC=2 SCAR=7.0
248  
249 2 ▶ my $thread_id = $msg_ref->{'Thread'} or die 'No thread ID?';
Branch: 1 branch
BranchTrueFalseTotal
my $thread_id = $msg_ref->{"Thread"} or die "No thread ID?" 0 2 2
250  
251 2 $self->_sign_msg($msg_ref);
252  
253 2 return $self->_do_send_receive(
254 $self->_generate_packet_from_message($msg_ref) . "\n\n",
255 $thread_id,
256 );
257 }
258  
259 sub _get_base_msg {
260 5 ▶ my ( $self, $op, $digest ) = @_;
Subroutine _get_base_msg: called CC=3 SCAR=11.0
261  
262 5 ▶ die "Implementor error: op is required" if !$op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 1 4 5
263 4 ▶ die "error: digest is required" if !$digest;
Branch: 1 branch
BranchTrueFalseTotal
unless $digest 2 2 4
264  
265 return {
266 2 'User' => $self->{'_username'},
267 'PV' => $PYZOR_PROTOCOL_VERSION,
268 'Time' => time(),
269 'Op' => $op,
270 'Op-Digest' => $digest,
271 'Thread' => $self->_generate_thread_id()
272 };
273 }
274  
275 sub _do_send_receive {
276 2 ▶ my ( $self, $packet, $thread_id ) = @_;
Subroutine _do_send_receive: called CC=2 SCAR=7.0
277  
278 2 my $sock = $self->_get_connection_or_die();
279  
280 2 $self->_send_packet( $sock, $packet );
281 2 my $response = $self->_receive_packet( $sock, $thread_id );
282  
283 2 my $resp_hr = { map { ( split(m{: }) )[ 0, 1 ] } split( m{\n}, $response ) };
284  
285 2 delete $resp_hr->{'Thread'};
286  
287 2 my $response_pv = delete $resp_hr->{'PV'};
288  
289 2 ▶ if ( $PYZOR_PROTOCOL_VERSION ne $response_pv ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Pyzor::Client::PYZOR_PROTOCOL_VERSION ne $response_pv) 0 2 2
290 0 warn "Unexpected protocol version ($response_pv) in Pyzor response!";
291 }
292  
293 2 return $resp_hr;
294 }
295  
296 sub _receive_packet {
297 2 ▶ my ( $self, $sock, $thread_id ) = @_;
Subroutine _receive_packet: called CC=8 SCAR=24.8
298  
299 2 my $timeout = $self->{'_timeout'} * 1000;
300  
301 2 my $end_time = time + $self->{'_timeout'};
302  
303 2 $sock->blocking(0);
304 2 my $response = '';
305 2 my $rout = '';
306 2 my $rin = '';
307 2 vec( $rin, fileno($sock), 1 ) = 1;
308  
309 2 while (1) {
310 2 my $time_left = $end_time - time;
311  
312 2 ▶ if ( $time_left <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_left <= 0) 0 2 2
313 0 die Mail::Pyzor::X->create( 'Timeout', "Did not receive a response from the pyzor server $self->{'_server_host'}:$self->{'_server_port'} for $self->{'_timeout'} seconds!" );
314 }
315  
316 2 my $bytes = IO::SigGuard::sysread( $sock, $response, $READ_SIZE, length $response );
317 2 ▶ if ( !defined($bytes) && !$!{'EAGAIN'} && !$!{'EWOULDBLOCK'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bytes or $!{"EAGAIN"} or $!{"EWOULDBLOCK"}) 0 2 2
Condition33%2/6
defined $bytes or $!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
200
defined $bytes or $!{"EAGAIN"}
l!l&&r!l&&!r
200
MC/DC0%0/3
defined $bytes or $!{"EAGAIN"} or $!{"EWOULDBLOCK"}
defined $bytes $!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table25%1/4
A or B or C
A= defined $bytesB= $!{"EAGAIN"}C= $!{"EWOULDBLOCK"}
ABCresult
1XX1
01X1
0011
0000
318 0 warn "read from socket: $!";
319 }
320  
321 2 ▶ if ( index( $response, "\n\n" ) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($response, "\n\n") != -1) 2 0 2
322  
323 # Reject the response unless its thread ID matches what we sent.
324 # This prevents confusion among concurrent Pyzor reqeusts.
325 2 ▶ if ( index( $response, "\nThread: $thread_id\n" ) != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($response, "\nThread: $thread_id\n") != -1) { } 2 0 2
326 2 last;
327 }
328 else {
329 0 $response = '';
330 }
331 }
332  
333 0 my $found = IO::SigGuard::select( $rout = $rin, undef, undef, $time_left );
334 0 ▶ warn "select(): $!" if $found == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $found == -1 0 0 0
335 }
336  
337 2 return $response;
338 }
339  
340 sub _send_packet {
341 0 ▶ my ( $self, $sock, $packet ) = @_;
Subroutine _send_packet: not called CC=2 SCAR=17.9
342  
343 0 $sock->blocking(1);
344 0 ▶ IO::SigGuard::syswrite( $sock, $packet ) or warn "write to socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
IO::SigGuard::syswrite($sock, $packet) or warn "write to socket: $!" 0 0 0
345  
346 0 return;
347 }
348  
349 sub _get_connection_or_die {
350 1 ▶ my ($self) = @_;
Subroutine _get_connection_or_die: called CC=3 SCAR=12.7
351 $self->{'_sock'} ||= IO::Socket::INET->new(
352 'PeerHost' => $self->{'_server_host'},
353 1 ▶ 'PeerPort' => $self->{'_server_port'},
Condition33%1/3
$self->{"_sock"} ||= "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp")
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"_sock"} ||= "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp")
$self->{"_sock"} "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp")
Truth table33%1/3
A ||= B
A= $self->{"_sock"}B= "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp")
ABresult
1X1
011
000
354 'Proto' => 'udp'
355 );
356  
357 1 ▶ if ( !$self->{'_sock'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_sock"}) 1 0 1
358 1 die "Cannot connect to $self->{'_server_host'}:$self->{'_server_port'}: $@ $!";
359 }
360 0 return $self->{'_sock'};
361 }
362  
363 sub _sign_msg {
364 2 ▶ my ( $self, $msg_ref ) = @_;
Subroutine _sign_msg: called CC=1 SCAR=0.0
365  
366 $msg_ref->{'Sig'} = lc Mail::Pyzor::SHA::sha1_hex(
367 Mail::Pyzor::SHA::sha1( $self->_generate_packet_from_message($msg_ref) ) . #
368 ':' . #
369 2 $msg_ref->{'Time'} . #
370 ':' . #
371 $self->_get_user_pass_hash_key() #
372 );
373  
374 2 return 1;
375 }
376  
377 sub _generate_packet_from_message {
378 4 ▶ my ( $self, $msg_ref ) = @_;
Subroutine _generate_packet_from_message: called CC=1 SCAR=0.0
379  
380 4 return join( "\n", map { "$_: $msg_ref->{$_}" } grep { length $msg_ref->{$_} } @hash_order );
381 }
382  
383 sub _generate_thread_id {
384 2 ▶ my $RAND_MAX = 2**16;
Subroutine _generate_thread_id: called CC=2 SCAR=6.9
385 2 my $val = 0;
386 2 ▶ $val = int rand($RAND_MAX) while $val < 1024;
Branch: 1 branch
BranchTrueFalseTotal
$val < 1024 and do { $val = int rand $RAND_MAX } 2 2 4
387 2 return $val;
388 }
389  
390 sub _get_user_pass_hash_key {
391 2 ▶ my ($self) = @_;
Subroutine _get_user_pass_hash_key: called CC=1 SCAR=0.0
392  
393 2 return lc Mail::Pyzor::SHA::sha1_hex( $self->{'_username'} . ':' . $self->{'_password'} );
394 }
395  
396 2 1;
« blib/lib/Mail/Pyzor/Digest/StripHtml.pm ↑ Summary lib/Mail/Pyzor/SHA.pm »