blib/lib/Net/OSCAR/Utility.pm

filter: Statementstmt 20.9% 36 / 172 Branchbran 3.3% 3 / 90 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 41.6% 10 / 24 Podpod 0.0% 0 / 16 totaltotal 13.4% 49 / 365 CC 62 SCAR 47.3
CC
62
Cov
13%
hexdump
380.0
tlv_decode
90.0
log_print
56.0
CRAP 113.1
« blib/lib/Net/OSCAR.pm ↑ Summary blib/lib/Net/OSCAR/Connection/Server.pm »
1 =pod
2  
3 =head1 NAME
4  
5 Net::OSCAR::Utility -- internal utility functions for Net::OSCAR
6  
7 =head1 VERSION
8  
9 version 1.928
10  
11 =cut
12  
13 package Net::OSCAR::Utility;
14 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 $Net::OSCAR::Utility::VERSION = '1.928';
16 }
17  
18 5 $REVISION = '$Revision$';
19  
20 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use Net::OSCAR::TLV;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Net::OSCAR::Common qw(:loglevels);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Net::OSCAR::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 5 require Exporter;
30 5 @ISA = qw(Exporter);
31 5 @EXPORT = qw(
32 randchars log_print log_printf log_print_cond log_printf_cond hexdump normalize tlv_decode tlv_encode send_error bltie
33 signon_tlv encode_password send_versions hash_iter_reset millitime
34 );
35  
36 5 eval {
37 5 require Time::HiRes;
38 };
39 5 ▶our $finetime = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 5 5
40  
41  
42 0 ▶sub millitime() {
Subroutine millitime: not called CC=2 SCAR=17.9
Pod undocumented
43 0 ▶ my $time = $finetime ? Time::HiRes::time() : time();
Branch: 1 branch
BranchTrueFalseTotal
$Net::OSCAR::Utility::finetime ? : 0 0 0
44 0 return int($time * 1000);
45 }
46  
47 0 ▶sub randchars($) {
Subroutine randchars: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my $count = shift;
49 0 my $retval = "";
50 0 ▶ for(my $i = 0; $i < $count; $i++) { $retval .= chr(int(rand(256))); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 0 0 0
51 0 return $retval;
52 }
53  
54  
55 0 ▶sub log_print($$@) {
Subroutine log_print: not called CC=7 SCAR=40.3
Pod undocumented
56 0 my($obj, $level) = (shift, shift);
57 0 ▶ my $session = exists($obj->{session}) ? $obj->{session} : $obj;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'session'} ? : 0 0 0
58 0 ▶ return unless defined($session->{LOGLEVEL}) and $session->{LOGLEVEL} >= $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level 0 0 0
Condition0%0/3
defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level
!ll&&!rl&&r
000
MC/DC0%0/2
defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level
defined $session->{"LOGLEVEL"} $session->{"LOGLEVEL"} >= $level
Truth table0%0/3
A and B
A= defined $session->{"LOGLEVEL"}B= $session->{"LOGLEVEL"} >= $level
ABresult
0X0
100
111
59  
60 0 my $message = "";
61 0 ▶ $message .= $obj->{description}. ": " if $obj->{description};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"description"} 0 0 0
62 0 $message .= join("", @_). "\n";
63  
64 0 ▶ if($session->{callbacks}->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{'callbacks'}{'log'}) { } 0 0 0
65 0 $session->callback_log($level, $message);
66 } else {
67 0 ▶ $message = "(".$session->{screenname}.") $message" if $session->{SNDEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $session->{"SNDEBUG"} 0 0 0
68 0 print STDERR $message;
69 }
70 }
71  
72 0 ▶sub log_printf($$@) {
Subroutine log_printf: not called CC=1 SCAR=6.9
Pod undocumented
73 0 my($obj, $level, $fmtstr) = (shift, shift, shift);
74  
75 0 $obj->log_print($level, sprintf($fmtstr, @_));
76 }
77  
78 0 ▶sub log_printf_cond($$&) {
Subroutine log_printf_cond: not called CC=4 SCAR=30.0
Pod undocumented
79 0 my($obj, $level, $sub) = @_;
80 0 ▶ my $session = exists($obj->{session}) ? $obj->{session} : $obj;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'session'} ? : 0 0 0
81 0 ▶ return unless defined($session->{LOGLEVEL}) and $session->{LOGLEVEL} >= $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level 0 0 0
Condition0%0/3
defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level
!ll&&!rl&&r
000
MC/DC0%0/2
defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level
defined $session->{"LOGLEVEL"} $session->{"LOGLEVEL"} >= $level
Truth table0%0/3
A and B
A= defined $session->{"LOGLEVEL"}B= $session->{"LOGLEVEL"} >= $level
ABresult
0X0
100
111
82  
83 0 log_printf($obj, $level, &$sub);
84 }
85  
86 0 ▶sub log_print_cond($$&) {
Subroutine log_print_cond: not called CC=4 SCAR=30.0
Pod undocumented
87 0 my($obj, $level, $sub) = @_;
88 0 ▶ my $session = exists($obj->{session}) ? $obj->{session} : $obj;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'session'} ? : 0 0 0
89 0 ▶ return unless defined($session->{LOGLEVEL}) and $session->{LOGLEVEL} >= $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level 0 0 0
Condition0%0/3
defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level
!ll&&!rl&&r
000
MC/DC0%0/2
defined $session->{"LOGLEVEL"} and $session->{"LOGLEVEL"} >= $level
defined $session->{"LOGLEVEL"} $session->{"LOGLEVEL"} >= $level
Truth table0%0/3
A and B
A= defined $session->{"LOGLEVEL"}B= $session->{"LOGLEVEL"} >= $level
ABresult
0X0
100
111
90  
91 0 log_print($obj, $level, &$sub);
92 }
93  
94 0 ▶sub hexdump($;$) {
Subroutine hexdump: not called CC=19 SCAR=59.4
Pod undocumented
95 0 my $stuff = shift;
96 0 ▶ my $forcehex = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
97 0 my $retbuff = "";
98 0 my @stuff;
99  
100 0 ▶ return "" unless defined($stuff);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stuff 0 0 0
101 0 ▶ for(my $i = 0; $i < length($stuff); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $stuff) 0 0 0
102 0 push @stuff, substr($stuff, $i, 1);
103 }
104  
105 0 ▶ return $stuff unless $forcehex or grep { $_ lt chr(0x20) or $_ gt chr(0x7E) } @stuff;
Branch: 1 branch
BranchTrueFalseTotal
unless $forcehex or grep {$_ lt " " or $_ gt "~";} @stuff 0 0 0
Condition0%0/6
$forcehex or grep {$_ lt " " or $_ gt "~";} @stuff
l!l&&r!l&&!r
000
$_ lt ' ' || $_ gt '~'
l!l&&r!l&&!r
000
MC/DC0%0/2
$forcehex or grep {$_ lt " " or $_ gt "~";} @stuff
$forcehex grep {$_ lt " " or $_ gt "~";} @stuff
MC/DC0%0/2
$_ lt ' ' || $_ gt '~'
$_ lt ' ' $_ gt '~'
Truth table0%0/3
A or B
A= $forcehexB= grep {$_ lt " " or $_ gt "~";} @stuff
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $_ lt ' 'B= $_ gt '~'
ABresult
1X1
011
000
106 0 ▶ while(@stuff) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stuff) 0 0 0
107 0 my $i = 0;
108 0 $retbuff .= "\n\t";
109 0 my @currstuff = splice(@stuff, 0, 16);
110  
111 0 foreach my $currstuff(@currstuff) {
112 0 ▶ $retbuff .= " " unless $i % 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 4 0 0 0
113 0 ▶ $retbuff .= " " unless $i % 8;
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 8 0 0 0
114 0 $retbuff .= sprintf "%02X ", ord($currstuff);
115 0 $i++;
116 }
117 0 ▶ for(; $i < 16; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 16) 0 0 0
118 0 ▶ $retbuff .= " " unless $i % 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 4 0 0 0
119 0 ▶ $retbuff .= " " unless $i % 8;
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 8 0 0 0
120 0 $retbuff .= " ";
121 }
122  
123 0 $retbuff .= " ";
124 0 $i = 0;
125 0 foreach my $currstuff(@currstuff) {
126 0 ▶ $retbuff .= " " unless $i % 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 4 0 0 0
127 0 ▶ $retbuff .= " " unless $i % 8;
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 8 0 0 0
128 0 ▶ if($currstuff ge chr(0x20) and $currstuff le chr(0x7E)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currstuff ge ' ' and $currstuff le '~') { } 0 0 0
Condition0%0/3
$currstuff ge " " and $currstuff le "~"
!ll&&!rl&&r
000
MC/DC0%0/2
$currstuff ge " " and $currstuff le "~"
$currstuff ge " " $currstuff le "~"
Truth table0%0/3
A and B
A= $currstuff ge " "B= $currstuff le "~"
ABresult
0X0
100
111
129 0 $retbuff .= $currstuff;
130 } else {
131 0 $retbuff .= ".";
132 }
133 0 $i++;
134 }
135 }
136 0 return $retbuff;
137 }
138  
139 4 ▶sub normalize($) {
Subroutine normalize: called CC=3 SCAR=11.7
Pod undocumented
140 4 my $temp = shift;
141 4 ▶ $temp =~ tr/ //d if $temp;
Branch: 1 branch
BranchTrueFalseTotal
if $temp 4 0 4
142 4 ▶ return $temp ? lc($temp) : "";
Branch: 1 branch
BranchTrueFalseTotal
$temp ? : 4 0 4
143 }
144  
145 0 ▶sub tlv_decode($;$) {
Subroutine tlv_decode: not called CC=9 SCAR=45.0
Pod undocumented
146 0 my($tlv, $tlvcnt) = @_;
147 0 my($type, $len, $value, %retval);
148 0 my $currtlv = 0;
149 0 my $strpos = 0;
150  
151 0 my $retval = tlv;
152  
153 0 ▶ $tlvcnt = 0 unless $tlvcnt;
Branch: 1 branch
BranchTrueFalseTotal
unless $tlvcnt 0 0 0
154 0 ▶ while(length($tlv) >= 4 and (!$tlvcnt or $currtlv < $tlvcnt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tlv >= 4 and !$tlvcnt || $currtlv < $tlvcnt) 0 0 0
Condition0%0/6
length $tlv >= 4 and !$tlvcnt || $currtlv < $tlvcnt
!ll&&!rl&&r
000
not $tlvcnt or $currtlv < $tlvcnt
l!l&&r!l&&!r
000
MC/DC0%0/3
length $tlv >= 4 and !$tlvcnt || $currtlv < $tlvcnt
length $tlv >= 4 not $tlvcnt $currtlv < $tlvcnt
Truth table0%0/4
A and not(B) or C
A= length $tlv >= 4B= not $tlvcntC= $currtlv < $tlvcnt
ABCresult
0XX0
1000
11X1
1011
155 0 ($type, $len) = unpack("nn", $tlv);
156 0 ▶ $len = 0x2 if $type == 0x13;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 19 0 0 0
157 0 $strpos += 4;
158 0 substr($tlv, 0, 4) = "";
159 0 ▶ if($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) { } 0 0 0
160 0 ($value) = substr($tlv, 0, $len, "");
161 } else {
162 0 $value = "";
163 }
164 0 $strpos += $len;
165 0 ▶ $currtlv++ unless $type == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 0 0 0 0
166 0 $retval->{$type} = $value;
167 }
168  
169 0 ▶ return $tlvcnt ? ($retval, $strpos) : $retval;
Branch: 1 branch
BranchTrueFalseTotal
$tlvcnt ? : 0 0 0
170 }
171  
172 0 ▶sub tlv_encode($) {
Subroutine tlv_encode: not called CC=7 SCAR=40.3
Pod undocumented
173 0 my $tlv = shift;
174 0 my($buffer, $type, $value) = ("", 0, "");
175  
176 0 ▶ confess "You must use a tied Net::OSCAR::TLV hash!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tlv and ref $tlv eq "HASH" and defined tied %$tlv and tied(%$tlv)->isa("Net::OSCAR::TLV") 0 0 0
Condition0%0/9
defined $tlv and ref $tlv eq "HASH" and defined tied %$tlv and tied(%$tlv)->isa("Net::OSCAR::TLV")
!ll&&!rl&&r
000
defined $tlv and ref $tlv eq "HASH" and defined tied %$tlv
!ll&&!rl&&r
000
defined $tlv and ref $tlv eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/4
defined $tlv and ref $tlv eq "HASH" and defined tied %$tlv and tied(%$tlv)->isa("Net::OSCAR::TLV")
defined $tlv ref $tlv eq "HASH" defined tied %$tlv tied(%$tlv)->isa("Net::OSCAR::TLV")
Truth table0%0/5
A and B and C and D
A= defined $tlvB= ref $tlv eq "HASH"C= defined tied %$tlvD= tied(%$tlv)->isa("Net::OSCAR::TLV")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
177 unless defined($tlv) and ref($tlv) eq "HASH" and defined(tied(%$tlv)) and tied(%$tlv)->isa("Net::OSCAR::TLV");
178 0 ▶ while (($type, $value) = each %$tlv) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tlv) 0 0 0
179 0 ▶ $value ||= "";
Condition0%0/2
$value ||= ""
l!l
00
MC/DC0%0/1
$value ||= ""
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
180 0 $buffer .= pack("nna*", $type, length($value), $value);
181  
182 }
183 0 return $buffer;
184 }
185  
186 0 ▶sub send_error($$$$$;@) {
Subroutine send_error: not called CC=1 SCAR=6.9
Pod undocumented
187 0 my($oscar, $connection, $error, $desc, $fatal, @reqdata) = @_;
188 0 $desc = sprintf $desc, @reqdata;
189 0 $oscar->callback_error($connection, $error, $desc, $fatal);
190 }
191  
192 2 ▶sub bltie(;$) {
Subroutine bltie: called CC=1 SCAR=0.0
Pod undocumented
193 2 my $retval = {};
194 2 tie %$retval, "Net::OSCAR::Buddylist", @_;
195 2 return $retval;
196 }
197  
198 0 ▶sub signon_tlv($;$$) {
Subroutine signon_tlv: not called CC=5 SCAR=34.0
Pod undocumented
199 0 my($session, $password, $key) = @_;
200  
201 my %protodata = (
202 screenname => $session->{screenname},
203 clistr => $session->{svcdata}->{clistr},
204 supermajor => $session->{svcdata}->{supermajor},
205 major => $session->{svcdata}->{major},
206 minor => $session->{svcdata}->{minor},
207 subminor => $session->{svcdata}->{subminor},
208 build => $session->{svcdata}->{build},
209 subbuild => $session->{svcdata}->{subbuild},
210 0 );
211  
212 0 ▶ if($session->{svcdata}->{hashlogin}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{'svcdata'}{'hashlogin'}) { } 0 0 0
213 0 $protodata{password} = encode_password($session, $password);
214 } else {
215 0 ▶ if($session->{auth_response}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{'auth_response'}) { } 0 0 0
216 0 $protodata{auth_response} = delete $session->{auth_response};
217 0 ▶ $protodata{pass_is_hashed} = "" if delete $session->{pass_is_hashed};
Branch: 1 branch
BranchTrueFalseTotal
if delete $session->{"pass_is_hashed"} 0 0 0
218 } else {
219 # As of AIM 5.5, the password can be MD5'd before
220 # going into the things-to-cat-together-and-MD5.
221 # This lets applications that store AIM passwords
222 # store the MD5'd password. We do it by default
223 # because, well, AIM for Windows does. We support
224 # the old way to preserve compatibility with
225 # our auth_challenge/auth_response API.
226  
227 0 $protodata{pass_is_hashed} = "";
228 0 ▶ my $hashpass = $session->{pass_is_hashed} ? $password : md5($password);
Branch: 1 branch
BranchTrueFalseTotal
$session->{'pass_is_hashed'} ? : 0 0 0
229  
230 0 $protodata{auth_response} = encode_password($session, $hashpass, $key);
231 }
232 }
233  
234 0 return %protodata;
235 }
236  
237 0 ▶sub encode_password($$;$) {
Subroutine encode_password: not called CC=3 SCAR=24.8
Pod undocumented
238 0 my($session, $password, $key) = @_;
239  
240 0 ▶ if(!$session->{svcdata}->{hashlogin}) { # Use new SNAC-based method
Branch: 1 branch
BranchTrueFalseTotal
if (not $session->{'svcdata'}{'hashlogin'}) { } 0 0 0
241 0 my $md5 = Digest::MD5->new;
242  
243 0 $md5->add($key);
244 0 $md5->add($password);
245 0 $md5->add("AOL Instant Messenger (SM)");
246 0 return $md5->digest();
247 } else { # Use old roasting method. Courtesy of SDiZ Cheng.
248 0 my $ret = "";
249 0 my @pass = map {ord($_)} split(//, $password);
250  
251 0 my @encoding_table = map {hex($_)} qw(
252 F3 26 81 C4 39 86 DB 92 71 A3 B9 E6 53 7A 95 7C
253 );
254  
255 0 ▶ for(my $i = 0; $i < length($password); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $password) 0 0 0
256 0 $ret .= chr($pass[$i] ^ $encoding_table[$i]);
257 }
258  
259 0 return $ret;
260 }
261 }
262  
263 0 ▶sub send_versions($$;$) {
Subroutine send_versions: not called CC=7 SCAR=40.3
Pod undocumented
264 0 my($connection, $send_tools, $server) = @_;
265 0 my $conntype = $connection->{conntype};
266 0 my @services;
267  
268 0 ▶ if($conntype != CONNTYPE_BOS and !$server) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conntype != 2 and not $server) { } 0 0 0
Condition0%0/3
$conntype != "basic OSCAR services" and not $server
!ll&&!rl&&r
000
MC/DC0%0/2
$conntype != "basic OSCAR services" and not $server
$conntype != "basic OSCAR services" not $server
Truth table0%0/3
A and not(B)
A= $conntype != "basic OSCAR services"B= not $server
ABresult
0X0
100
111
269 0 @services = (1, $conntype);
270 } else {
271 0 @services = sort {$b <=> $a} grep {not OSCAR_TOOLDATA()->{$_}->{nobos}} keys %{OSCAR_TOOLDATA()};
272 }
273  
274 0 my %protodata = (service => []);
275 0 foreach my $service (@services) {
276 my %service = (
277 service_id => $service,
278 service_version => OSCAR_TOOLDATA->{$service}->{version}
279 0 );
280 0 ▶ if($send_tools) {
Branch: 1 branch
BranchTrueFalseTotal
if ($send_tools) 0 0 0
281 0 $service{tool_id} = OSCAR_TOOLDATA->{$service}->{toolid};
282 0 $service{tool_version} = OSCAR_TOOLDATA->{$service}->{toolversion};
283 }
284  
285 0 push @{$protodata{service}}, \%service;
286 }
287  
288 0 ▶ if($send_tools) {
Branch: 2 branches
BranchTrueFalseTotal
if ($send_tools) { } 0 0 0
elsif ($server) { } 0 0 0
289 0 $connection->proto_send(protobit => "set_tool_versions", protodata => \%protodata, nopause => 1);
290 } elsif($server) {
291 0 $connection->proto_send(protobit => "host_versions", protodata => \%protodata, nopause => 1);
292 } else {
293 0 $connection->proto_send(protobit => "set_service_versions", protodata => \%protodata, nopause => 1);
294 }
295 }
296  
297 # keys(%foo) in void context, the standard way of reseting
298 # a hash iterator, appears to leak memory.
299 #
300 0 ▶sub hash_iter_reset($) {
Subroutine hash_iter_reset: not called CC=2 SCAR=17.9
Pod undocumented
301 0 ▶ while((undef, undef) = each(%{$_[0]})) {}
Branch: 1 branch
BranchTrueFalseTotal
if ((undef, undef) = each %{$_[0];}) 0 0 0
302 }
303  
304 5 1;
« blib/lib/Net/OSCAR.pm ↑ Summary blib/lib/Net/OSCAR/Connection/Server.pm »