| 1 | package Mail::Karmasphere::Client; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | %QUEUE @QUEUE $QUEUE); | |||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Convert::Bencode qw(bencode bdecode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | IDT_IP4_ADDRESS => "ip4", | |||||||||||||||||||||||||||
| 16 | IDT_IP6_ADDRESS => "ip6", | ||||||||||||||||||||||||||||
| 17 | IDT_DOMAIN_NAME => "domain", | ||||||||||||||||||||||||||||
| 18 | IDT_EMAIL_ADDRESS => "email", | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | IDT_IP4 => "ip4", | ||||||||||||||||||||||||||||
| 21 | IDT_IP6 => "ip6", | ||||||||||||||||||||||||||||
| 22 | IDT_DOMAIN => "domain", | ||||||||||||||||||||||||||||
| 23 | IDT_EMAIL => "email", | ||||||||||||||||||||||||||||
| 24 | IDT_URL => "url", | ||||||||||||||||||||||||||||
| 25 | 5 | }; | |||||||||||||||||||||||||||
| 26 | 5 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 5 | AUTHENTIC => "a", | |||||||||||||||||||||||||||
| 28 | SMTP_CLIENT_IP => "smtp.client-ip", | ||||||||||||||||||||||||||||
| 29 | SMTP_ENV_HELO => "smtp.env.helo", | ||||||||||||||||||||||||||||
| 30 | SMTP_ENV_MAIL_FROM => "smtp.env.mail-from", | ||||||||||||||||||||||||||||
| 31 | SMTP_ENV_RCPT_TO => "smtp.env.rcpt-to", | ||||||||||||||||||||||||||||
| 32 | SMTP_HEADER_FROM_ADDRESS => "smtp.header.from.address", | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | FL_FACTS => 1, | ||||||||||||||||||||||||||||
| 35 | FL_DATA => 2, | ||||||||||||||||||||||||||||
| 36 | FL_TRACE => 4, | ||||||||||||||||||||||||||||
| 37 | FL_MODELTRACE => 8, | ||||||||||||||||||||||||||||
| 38 | 5 | }; | |||||||||||||||||||||||||||
| 39 | 5 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 5 | PROTO_TCP => 0+getprotobyname('tcp'), | |||||||||||||||||||||||||||
| 41 | PROTO_UDP => 0+getprotobyname('udp'), | ||||||||||||||||||||||||||||
| 42 | 5 | }; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 5 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 46 | 5 | $VERSION = "2.18"; | |||||||||||||||||||||||||||
| 47 | 5 | @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 48 | IDT_IP4_ADDRESS IDT_IP6_ADDRESS | ||||||||||||||||||||||||||||
| 49 | IDT_DOMAIN_NAME IDT_EMAIL_ADDRESS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | IDT_IP4 IDT_IP6 | ||||||||||||||||||||||||||||
| 52 | IDT_DOMAIN IDT_EMAIL | ||||||||||||||||||||||||||||
| 53 | IDT_URL | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | AUTHENTIC | ||||||||||||||||||||||||||||
| 56 | SMTP_CLIENT_IP | ||||||||||||||||||||||||||||
| 57 | SMTP_ENV_HELO SMTP_ENV_MAIL_FROM SMTP_ENV_RCPT_TO | ||||||||||||||||||||||||||||
| 58 | SMTP_HEADER_FROM_ADDRESS | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | FL_FACTS | ||||||||||||||||||||||||||||
| 61 | FL_DATA | ||||||||||||||||||||||||||||
| 62 | FL_TRACE | ||||||||||||||||||||||||||||
| 63 | FL_MODELTRACE | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | 5 | %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 66 | 'all' => \@EXPORT_OK, | ||||||||||||||||||||||||||||
| 67 | 'ALL' => \@EXPORT_OK, | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | 5 | %QUEUE = (); | |||||||||||||||||||||||||||
| 70 | 5 | @QUEUE = (); | |||||||||||||||||||||||||||
| 71 | 5 | $QUEUE = 100; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # We can't use these until we set up the above variables. | ||||||||||||||||||||||||||||
| 75 | 5 | ▶ | use Mail::Karmasphere::Query; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 5 | ▶ | use Mail::Karmasphere::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=28.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | if ($self->{Debug} and ref($self->{Debug}) ne 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"Debug"} and ref $self->{"Debug"} ne "CODE"
MC/DC0%0/2
$self->{"Debug"} and ref $self->{"Debug"} ne "CODE"
$self->{"Debug"}
ref $self->{"Debug"} ne "CODE"
Truth table33%1/3
A and B
A= $self->{"Debug"}B= ref $self->{"Debug"} ne "CODE"
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $self->{Debug} = sub { print STDERR Dumper(@_); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 2 | ▶ | $self->{Debug}->('new', $self) if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | unless ($self->{Socket}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | $self->{Proto} = 'udp' | ||||||||||||||||||||||||||||
| 89 | 2 | ▶ | unless defined $self->{Proto}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | $self->{PeerAddr} = $self->{PeerHost} | ||||||||||||||||||||||||||||
| 91 | 2 | ▶ | unless defined $self->{PeerAddr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | $self->{PeerAddr} = 'query.karmasphere.com' | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | unless defined $self->{PeerAddr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | $self->{PeerPort} = 8666 | ||||||||||||||||||||||||||||
| 95 | 2 | ▶ | unless $self->{Port}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 2 | _connect($self); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | return bless $self, $class; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | sub _connect { | ||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 103 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | $self->{Debug}->('connect') if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | $self->{Socket} = new IO::Socket::INET( | ||||||||||||||||||||||||||||
| 106 | Proto => $self->{Proto}, | ||||||||||||||||||||||||||||
| 107 | PeerAddr => $self->{PeerAddr}, | ||||||||||||||||||||||||||||
| 108 | PeerPort => $self->{PeerPort}, | ||||||||||||||||||||||||||||
| 109 | 2 | ReuseAddr => 1, | |||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | 2 | ▶ | unless (defined $self->{Socket}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | delete $self->{Socket}; | |||||||||||||||||||||||||||
| 113 | 2 | my @args = map { "$_=" . $self->{$_} } keys %$self; | |||||||||||||||||||||||||||
| 114 | 2 | die "Failed to create socket: $! (@args)"; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub query { | ||||||||||||||||||||||||||
|
Subroutine
query: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 0 | return $self->ask(new Mail::Karmasphere::Query(@_)); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub _previous_socket { | ||||||||||||||||||||||||||
|
Subroutine
_previous_socket: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 124 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | return undef unless exists $self->{PreviousTime}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ($self->{PreviousTime} + 10 > time()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | $self->{Debug}->('previous') if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | return $self->{PreviousSocket} | ||||||||||||||||||||||||||||
| 129 | 0 | } | |||||||||||||||||||||||||||
| 130 | 0 | delete $self->{PreviousSocket}; | |||||||||||||||||||||||||||
| 131 | 0 | delete $self->{PreviousTime}; | |||||||||||||||||||||||||||
| 132 | 0 | return undef; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub _is_tcp { | ||||||||||||||||||||||||||
|
Subroutine
_is_tcp: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 136 | 0 | my ($self, $socket) = @_; | |||||||||||||||||||||||||||
| 137 | 0 | return $socket->protocol == PROTO_TCP; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub _send_real { | ||||||||||||||||||||||||||
|
Subroutine
_send_real: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 141 | 0 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $socket = $self->{Socket}; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ($socket->protocol == PROTO_UDP) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (length $data > 1024) { # Server's UDP_MAX | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $self->{Debug}->('fallback') if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | $self->{PreviousSocket} = $self->{Socket}; | |||||||||||||||||||||||||||
| 149 | 0 | $self->{PreviousTime} = time(); | |||||||||||||||||||||||||||
| 150 | 0 | $self->{Proto} = 'tcp'; | |||||||||||||||||||||||||||
| 151 | # XXX This loses the old socket and any queries | ||||||||||||||||||||||||||||
| 152 | # sent thereon. | ||||||||||||||||||||||||||||
| 153 | 0 | $self->_connect(); | |||||||||||||||||||||||||||
| 154 | 0 | $socket = $self->{Socket}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | # This can NOT be an else as we clobber the variable above. | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($socket->protocol == PROTO_TCP) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | $self->{Debug}->('tcp prefix') if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | $data = pack("N", length($data)) . $data; | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | $self->{Debug}->('send_real', $data) if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | $socket->send($data) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | or die "Failed to send to socket: $!"; | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 0 | my ($self, $query) = @_; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | die "Not blessed reference: $query" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | unless ref($query) =~ /[a-z]/; | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | die "Not a query: $query" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | unless $query->isa('Mail::Karmasphere::Query'); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | $self->{Debug}->('send_query', $query) if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | my $id = $query->id; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | my $packet = { | |||||||||||||||||||||||||||
| 180 | _ => $id, | ||||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||
| 182 | 0 | ▶ | $packet->{i} = $query->identities if $query->has_identities; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | $packet->{s} = $query->composites if $query->has_composites; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | $packet->{f} = $query->feeds if $query->has_feeds; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | $packet->{c} = $query->combiners if $query->has_combiners; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | $packet->{fl} = $query->flags if $query->has_flags; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if (defined $self->{Principal}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | my $creds = defined $self->{Credentials} ?$self->{Credentials} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | $packet->{a} = [ $self->{Principal}, $creds ]; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | $self->{Debug}->('send_packet', $packet) if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | my $data = bencode($packet); | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | $self->{Debug}->('send_data', $data) if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | $self->_send_real($data); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | return $id; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | ▶ | sub _recv_real { | ||||||||||||||||||||||||||
|
Subroutine
_recv_real: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 202 | 0 | my ($self, $socket) = @_; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | my $data; | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | if ($socket->protocol == PROTO_TCP) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | $socket->read($data, 4) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | or die "Failed to receive length from socket: $!"; | ||||||||||||||||||||||||||||
| 208 | 0 | my $length = unpack("N", $data); | |||||||||||||||||||||||||||
| 209 | 0 | $data = ''; | |||||||||||||||||||||||||||
| 210 | 0 | ▶ | while ($length > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | my $block; | |||||||||||||||||||||||||||
| 212 | 0 | ▶ | my $bytes = $socket->read($block, $length) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | or die "Failed to receive data from socket: $!"; | ||||||||||||||||||||||||||||
| 214 | 0 | $data .= $block; | |||||||||||||||||||||||||||
| 215 | 0 | $length -= $bytes; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | $self->{Debug}->('recv_data', $data) if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||||||||||
| 220 | 0 | ▶ | $socket->recv($data, 8192) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | or die "Failed to receive from socket: $!"; | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | $self->{Debug}->('recv_data', $data) if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 0 | my $packet = bdecode($data); | |||||||||||||||||||||||||||
| 225 | 0 | ▶ | die $packet unless ref($packet) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | my $response = new Mail::Karmasphere::Response($packet); | |||||||||||||||||||||||||||
| 228 | 0 | ▶ | $self->{Debug}->('recv_response', $response) if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | return $response; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub recv { | ||||||||||||||||||||||||||
|
Subroutine
recv: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 0 | my ($self, $query, $timeout) = @_; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | ▶ | my $id = ref($query) ? $query->id : $query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | if (defined($id)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 0 | ▶ | if ($QUEUE{$id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | $self->{Debug}->('recv_find', $id, $QUEUE{$id}) | ||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if $self->{Debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 0 | @QUEUE = grep { $_ ne $id } @QUEUE; | |||||||||||||||||||||||||||
| 241 | 0 | return delete $QUEUE{$id}; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | else { | ||||||||||||||||||||||||||||
| 245 | 0 | ▶ | if (@QUEUE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 0 | $id = shift @QUEUE; | |||||||||||||||||||||||||||
| 247 | 0 | return delete $QUEUE{$id}; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | $timeout = 10 unless defined $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | my $finish = time() + $timeout; | |||||||||||||||||||||||||||
| 253 | 0 | my $select = new IO::Select(); | |||||||||||||||||||||||||||
| 254 | 0 | $select->add($self->{Socket}); | |||||||||||||||||||||||||||
| 255 | 0 | my $prev = $self->_previous_socket; | |||||||||||||||||||||||||||
| 256 | 0 | ▶ | $select->add($prev) if $prev; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | while ($timeout > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 0 | my @ready = $select->can_read($timeout); | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 0 | ▶ | if (@ready) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 0 | my $response = $self->_recv_real($ready[0]); | |||||||||||||||||||||||||||
| 262 | 0 | ▶ | $response->{query} = $query if ref $query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 0 | ▶ | return $response unless defined $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | return $response if $response->id eq $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | my $rid = $response->id; | |||||||||||||||||||||||||||
| 267 | 0 | push(@QUEUE, $rid); | |||||||||||||||||||||||||||
| 268 | 0 | $QUEUE{$rid} = $response; | |||||||||||||||||||||||||||
| 269 | 0 | ▶ | if (@QUEUE > $QUEUE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 0 | my $oid = shift @QUEUE; | |||||||||||||||||||||||||||
| 271 | 0 | delete $QUEUE{$oid}; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | $timeout = $finish - time(); | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | print STDERR "Failed to receive from socket: $!\n"; | |||||||||||||||||||||||||||
| 279 | 0 | return undef; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | ▶ | sub ask { | ||||||||||||||||||||||||||
|
Subroutine
ask: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 283 | 0 | my ($self, $query, $timeout) = @_; | |||||||||||||||||||||||||||
| 284 | 0 | ▶ | $timeout = 5 unless defined $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 0 | for (0..2) { | |||||||||||||||||||||||||||
| 286 | 0 | my $id = $self->send($query); | |||||||||||||||||||||||||||
| 287 | 0 | my $response = $self->recv($query, $timeout); | |||||||||||||||||||||||||||
| 288 | # $response->{query} = $query; | ||||||||||||||||||||||||||||
| 289 | 0 | ▶ | return $response if $response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 0 | $timeout += $timeout; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | 0 | return undef; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =head1 NAME | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Mail::Karmasphere::Client - Client for Karmasphere Reputation Server | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | use Mail::Karmasphere::Client qw(:all); | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | my $client = new Mail::Karmasphere::Client( | ||||||||||||||||||||||||||||
| 304 | PeerAddr => 'query.karmasphere.com', | ||||||||||||||||||||||||||||
| 305 | PeerPort => 8666, | ||||||||||||||||||||||||||||
| 306 | Principal => "my_assigned_query_username", | ||||||||||||||||||||||||||||
| 307 | Credentials => "my_assigned_query_password", | ||||||||||||||||||||||||||||
| 308 | # see http://my.karmasphere.com/devzone/client/configuration#credentials | ||||||||||||||||||||||||||||
| 309 | # quickstart: use temporary credentials for "generic perl". | ||||||||||||||||||||||||||||
| 310 | # recommended: use permanent credentials -- register for an account. | ||||||||||||||||||||||||||||
| 311 | ); | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | my $query = new Mail::Karmasphere::Query(); | ||||||||||||||||||||||||||||
| 314 | $query->identity('127.0.0.2', IDT_IP4); | ||||||||||||||||||||||||||||
| 315 | $query->composite('karmasphere.email-sender'); | ||||||||||||||||||||||||||||
| 316 | my $response = $client->ask($query, 6); | ||||||||||||||||||||||||||||
| 317 | print $response->as_string; | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | my $id = $client->send($query); | ||||||||||||||||||||||||||||
| 320 | my $response = $client->recv($query, 12); | ||||||||||||||||||||||||||||
| 321 | my $response = $client->recv($id, 12); | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | my $response = $client->query( | ||||||||||||||||||||||||||||
| 324 | Identities => [ ... ] | ||||||||||||||||||||||||||||
| 325 | Composite => 'karmasphere.email-sender', | ||||||||||||||||||||||||||||
| 326 | ); | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | The Perl Karma Client API consists of three objects: The Query, the | ||||||||||||||||||||||||||||
| 331 | Response, and the Client. The user constructs a Query and passes it | ||||||||||||||||||||||||||||
| 332 | to a Client, which returns a Response. | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | The class method new(...) constructs a new Client object. All arguments | ||||||||||||||||||||||||||||
| 337 | are optional. The following parameters are recognised as arguments | ||||||||||||||||||||||||||||
| 338 | to new(): | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =over 4 | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =item PeerAddr | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | The IP address or hostname to contact. See L<IO::Socket::INET>. The | ||||||||||||||||||||||||||||
| 345 | default is 'query.karmasphere.com'. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =item PeerPort | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | The TCP or UDP to contact. See L<IO::Socket::INET>. The default | ||||||||||||||||||||||||||||
| 350 | is 8666. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =item Proto | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Either 'udp' or 'tcp'. The default is 'udp' because it is faster. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =item Principal | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =item Credentials | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | A username and password are required to authenticate client | ||||||||||||||||||||||||||||
| 361 | connections. They are assigned by Karmasphere. See | ||||||||||||||||||||||||||||
| 362 | http://my.karmasphere.com/devzone/client/configuration#credentials | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | "Principal" corresponds to "username", and "Credentials" | ||||||||||||||||||||||||||||
| 365 | corresponds to "password". Note that these are not the same | ||||||||||||||||||||||||||||
| 366 | username and password you use to sign in to the website. | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =item Debug | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Either a true value for debugging to stderr, or a custom debug handler. | ||||||||||||||||||||||||||||
| 371 | The custom handler will be called with N arguments, the first of which | ||||||||||||||||||||||||||||
| 372 | is a string 'debug context'. The custom handler may choose to ignore | ||||||||||||||||||||||||||||
| 373 | messages from certain contexts. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =back | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =head1 METHODS | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =over 4 | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =item $response = $client->ask($query, $timeout) | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | Returns a L<Mail::Karmasphere::Response> to a | ||||||||||||||||||||||||||||
| 384 | L<Mail::Karmasphere::Query>. The core of this method is equivalent to | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | $client->recv($client->send($query), $timeout) | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | The method retries up to 3 times, doubling the timeout each time. If | ||||||||||||||||||||||||||||
| 389 | the application requires more control over retries or backoff, it | ||||||||||||||||||||||||||||
| 390 | should use send() and recv() individually. $timeout is optional. | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =item $id = $client->send($query) | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | Sends a L<Mail::Karmasphere::Query> to the server, and returns the | ||||||||||||||||||||||||||||
| 395 | id of the query, which may be passed to recv(). Note that any query | ||||||||||||||||||||||||||||
| 396 | longer than 64KB will be rejected by the server with a message advising | ||||||||||||||||||||||||||||
| 397 | that the maximum message length has been exceeded. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =item $response = $client->recv($id, $timeout) | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | Returns a L<Mail::Karmasphere::Response> to the query with id $id, | ||||||||||||||||||||||||||||
| 402 | assuming that the query has already been sent using send(). If no | ||||||||||||||||||||||||||||
| 403 | matching response is read before the timeout, undef is returned. | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =item $response = $client->query(...) | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | A convenience method, equivalent to | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | $client->ask(new Mail::Karmasphere::Query(...)); | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | See L<Mail::Karmasphere::Query> for more details. | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | =back | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =over 4 | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =item IDT_IP4 IDT_IP6 IDT_DOMAIN IDT_EMAIL IDT_URL | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | Identity type constants. | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =item AUTHENTIC SMTP_CLIENT_IP SMTP_ENV_HELO SMTP_ENV_MAIL_FROM SMTP_ENV_RCPT_TO SMTP_HEADER_FROM_ADDRESS | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Identity tags, indicating the context of an identity to the server. | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =item FL_FACTS | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | A flag indicating that all facts must be returned explicitly in the | ||||||||||||||||||||||||||||
| 430 | Response. | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =back | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =head1 NOTES ON THE IMPLEMENTATION | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | The server will discard any packet in TCP mode which exceeds | ||||||||||||||||||||||||||||
| 437 | 64K. Although the packet length field is 4 bytes, it is relatively | ||||||||||||||||||||||||||||
| 438 | common to get non-Karmasphere clients connecting to the port. | ||||||||||||||||||||||||||||
| 439 | Therefore the server checks that the top two bytes are \0 before | ||||||||||||||||||||||||||||
| 440 | accepting the packet. This saves everybody a headache. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | Some flags, notably those which generate large response packets, | ||||||||||||||||||||||||||||
| 443 | are totally ignored for UDP queries, even in the case that they would | ||||||||||||||||||||||||||||
| 444 | not generate a large response. This also saves many headaches. | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =head1 BUGS | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | UDP retries are not yet implemented. | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | L<Mail::Karmasphere::Query>, | ||||||||||||||||||||||||||||
| 453 | L<Mail::Karmasphere::Response>, | ||||||||||||||||||||||||||||
| 454 | http://www.karmasphere.com/, | ||||||||||||||||||||||||||||
| 455 | http://my.karmasphere.com/devzone/client/configuration, | ||||||||||||||||||||||||||||
| 456 | L<Mail::SpamAssassin::Plugin::Karmasphere> | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | Copyright (c) 2005-2006 Shevek, Karmasphere. All rights reserved. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 463 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =cut | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | 5 | 1; | |||||||||||||||||||||||||||