YahooMessenger/EventFactory.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 236 CC 20 SCAR 45.1
CC
20
Cov
0%
create_by_raw_data
272.0
_recv_by_length
12.0
create_by_name
6.0
CRAP 91.1
1 package Net::YahooMessenger::EventFactory;
2 use strict;
3  
4 use Net::YahooMessenger;
5  
6 use constant YMSG_LOGIN => 1;
7 use constant YMSG_LOGOUT => 2;
8 use constant YMSG_CHANGE_STATE => 3;
9 use constant YMSG_CHANGE_AVAILABLE => 4;
10 use constant YMSG_MESSAGE => 6;
11 use constant YMSG_NEW_FRIEND_ALERT => 15;
12 use constant YMSG_SERVER_IS_ALIVE => 76;
13 use constant YMSG_AUTH => 84;
14 use constant YMSG_BUDDY_LIST => 241;
15 use constant YMSG_BEGIN_AUTH => 87;
16  
17 use constant ID => '1';
18 use constant BADDY_NAME => '7';
19 use constant NUMBER_OF_ONLINE_BADDY => '8';
20 use constant STATE => '10';
21 use constant SESSION_ID => '11';
22 use constant ONLINE_OR_OFFLINE => '13';
23 use constant STATE_OF_USER_DEFINITION => '19';
24 use constant BUSYNESS => '47';
25  
26 use constant EVENT_CLASS => {
27 1 => 'Net::YahooMessenger::GoesOnline',
28 2 => 'Net::YahooMessenger::GoesOffline',
29 3 => 'Net::YahooMessenger::ChangeState',
30 4 => 'Net::YahooMessenger::ChangeState',
31 6 => 'Net::YahooMessenger::ReceiveMessage',
32 15 => 'Net::YahooMessenger::NewFriendAlert',
33 76 => 'Net::YahooMessenger::ServerIsAlive',
34 85 => 'Net::YahooMessenger::UserInfo',
35 87 => 'Net::YahooMessenger::ChallengeStart',
36 241 => 'Net::YahooMessenger::ReceiveBuddyList',
37 };
38  
39 sub new {
40 my $class = shift;
41 my $connection = shift;
42 bless { connection => $connection }, $class;
43 }
44  
45 sub create_by_raw_data {
46 my $self = shift;
47 my $connection = $self->{connection};
48 my ( $event_code, $identifier, $body ) = eval { $self->_get_message_body; };
49 if ($@) {
50  
51 # print STDERR $@;
52 require Net::YahooMessenger::InvalidLogin;
53 return Net::YahooMessenger::InvalidLogin->new($connection);
54 }
55  
56 my ( $code, $recipient, $private, $sender, @baddy ) = split /\xC0\x80/,
57 $body;
58 if ( $event_code == YMSG_LOGIN && lc $recipient eq lc $sender ) {
59 require Net::YahooMessenger::Login;
60 my $event = Net::YahooMessenger::Login->new($connection);
61 $event->source($body);
62 $event->from($sender);
63 $connection->identifier($identifier);
64 return $event if scalar $connection->buddy_list <= 0;
65  
66 shift @baddy;
67 my $id = shift @baddy;
68 my $buddy;
69 for ( my $i = 0 ; $i < scalar @baddy ; $i += 2 ) {
70 if ( $baddy[$i] eq BADDY_NAME ) {
71 $buddy = $connection->get_buddy_by_name( $baddy[ $i + 1 ] );
72 }
73 elsif ( $baddy[$i] eq STATE ) {
74 $buddy->status( $baddy[ $i + 1 ] ) if $buddy;
75 }
76 elsif ( $baddy[$i] eq STATE_OF_USER_DEFINITION ) {
77 $buddy->custom_status( $baddy[ $i + 1 ] ) if $buddy;
78 }
79 elsif ( $baddy[$i] eq BUSYNESS ) {
80 $buddy->busy( $baddy[ $i + 1 ] ) if $buddy;
81 }
82 elsif ( $baddy[$i] eq SESSION_ID ) {
83 $buddy->session_id( $baddy[ $i + 1 ] ) if $buddy;
84 }
85 elsif ( $baddy[$i] eq ONLINE_OR_OFFLINE ) {
86 $buddy->online( $baddy[ $i + 1 ] ) if $buddy;
87 }
88 else { }
89 }
90 return $event;
91  
92 }
93 else {
94 my $class = EVENT_CLASS->{$event_code}
95 || 'Net::YahooMessenger::DummyEvent';
96 eval "require $class";
97 if ($@) {
98  
99 # print STDERR $@;
100 require Net::YahooMessenger::UnImplementEvent;
101 my $event = Net::YahooMessenger::UnImplementEvent->new($connection);
102 $event->code($event_code);
103 $event->source($body);
104 return $event;
105 }
106 my $event = $class->new($connection);
107 $event->source($body);
108 return $event;
109 }
110 }
111  
112 sub create_by_name {
113 my $self = shift;
114 my $class = 'Net::YahooMessenger::' . shift;
115 eval "require $class";
116 if ($@) {
117 warn "No event matched require failed : $@";
118 require Net::YahooMessenger::NullEvent;
119 return Net::YahooMessenger::NullEvent->new;
120 }
121 return $class->new( $self->{connection} );
122 }
123  
124 sub _get_message_body {
125 my $self = shift;
126 my $handle = $self->{connection}->handle;
127 my %command;
128  
129 my $header = $self->_recv_by_length(20);
130 my ( $signature, $version, $length, $event_code, $return, $identifier ) =
131 unpack "a4Cx3nnNN", $header;
132  
133 die 'Wring protocol' if $signature ne Net::YahooMessenger->YMSG_STD_HEADER;
134 my $message = $self->_recv_by_length($length);
135 return ( $event_code, $identifier, $message );
136  
137 }
138  
139 sub _recv_by_length {
140 my $self = shift;
141 my $length = shift || 0;
142 my $handle = $self->{connection}->handle;
143 my $message = '';
144  
145 while ( length $message < $length ) {
146 my $buff = '';
147 $handle->sysread( $buff, 1, 0 )
148 or die "Disconnect socket";
149 $message .= $buff;
150 }
151 if (0) {
152 print "\n";
153 print map { sprintf "%s ", $_ =~ /[\w\d]/ ? $_ : '.'; } split //,
154 $message;
155 print "\n";
156 print map { sprintf "%02x ", ord $_; } split //, $message;
157 }
158 return $message;
159 }
160 1;
161 __END__