YahooMessenger.pm

filter: Statementstmt 20.4% 38 / 186 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 30.7% 12 / 39 Podpod 45.4% 10 / 22 totaltotal 20.7% 60 / 289 CC 24 SCAR 19.5
CC
24
Cov
17%
add_event_source
20.0
do_one_loop
20.0
new
12.0
CRAP 7.0
« YahooMessenger/ChangeState.pm ↑ Summary YahooMessenger/GoesOffline.pm »
1 package Net::YahooMessenger;
2  
3 =head1 NAME
4  
5 Net::YahooMessenger - Interface to the Yahoo!Messenger IM protocol
6  
7 =head1 SYNOPSIS
8  
9 use Net::YahooMessenger;
10  
11 my $yahoo = Net::YahooMessenger->new(
12 id => 'your_yahoo_id',
13 password => 'your_password',
14 );
15 $yahoo->login or die "Can't login Yahoo!Messenger";
16 $yahoo->send('recipient_yahoo_id', 'Hello World!');
17  
18 =head1 CAVEATS
19  
20 Module was changed to work with the latest version(16) of the Yahoo protocol. However, this might not work very well, your opinions and suggestions will be helpfull.
21  
22 =head1 DESCRIPTION
23  
24 Net::YahooMessenger is a client class for connecting with the Yahoo!Messenger server, and transmitting and receiving a message.
25  
26 Since implement of a protocol is the result of analyzing and investigating a packet, it has an inadequate place. However, it is working as expected usually.
27  
28 =cut
29  
30 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Net::YahooMessenger::Buddy;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Net::YahooMessenger::CRAM;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use Net::YahooMessenger::HTTPS;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 1 ▶use constant YMSG_STD_HEADER => 'YMSG';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant YMSG_SEPARATER => "\xC0\x80";
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant YMSG_SALT => '_2S43d5f';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant YMSG_PROTOCOL_VERSION => '16';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 $VERSION = '0.19';
46  
47 =head1 METHODS
48  
49 This section documents method of the Net::YahooMessenger class.
50  
51 =head2 Net::YahooMessenger->new()
52  
53 It should be called with following arguments (items with default value are optional):
54  
55 id => yahoo id
56 password => password
57 hostname => server hostname
58 (default 'cs.yahoo.com)
59  
60 Returns a blessed instantiation of Net::YahooMessenger.
61  
62 Note: If you plan to connect with Yahoo!Japan(yahoo.co.jp), it sets up as follows.
63  
64 my $yahoo_japan = Net::YahooMessenger->new(
65 hostname => 'cs.yahoo.co.jp',
66 );
67  
68 I<Since it connects with Yahoo!(yahoo.com), this procedure is unnecessary in almost all countries.>
69  
70 =cut
71  
72 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
73 0 my $class = shift;
74 0 my %args = @_;
75  
76 bless {
77 id => $args{id},
78 password => $args{password},
79 hostname => $args{hostname} || 'scsa.msg.yahoo.com',
80  
81 #this is probably not needed for the version 16 YM protocol
82 pre_login_url => $args{pre_login_url}
83 0 ▶ || 'http://msg.edit.yahoo.com/config/',
Condition0%0/4
$args{'hostname'} || 'scsa.msg.yahoo.com'
l!l
00
$args{'pre_login_url'} || 'http://msg.edit.yahoo.com/config/'
l!l
00
MC/DC0%0/1
$args{'hostname'} || 'scsa.msg.yahoo.com'
$args{'hostname'}
MC/DC0%0/1
$args{'pre_login_url'} || 'http://msg.edit.yahoo.com/config/'
$args{'pre_login_url'}
Truth table0%0/2
A
A= $args{'hostname'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'pre_login_url'}
Aresult
11
00
84 handle => undef,
85 _read => IO::Select->new,
86 _write => IO::Select->new,
87 _error => IO::Select->new,
88 event_handler => undef,
89 buddy_list => [],
90 }, $class;
91 }
92  
93 =head2 $yahoo->id([$yahoo_id])
94  
95 This method gets or sets the present B<Yahoo Id>.
96  
97 =cut
98  
99 0 ▶sub id {
Subroutine id: not called CC=2 SCAR=17.9
Pod documented
100 0 my $self = shift;
101 0 ▶ $self->{id} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
102 0 $self->{id};
103 }
104  
105 =head2 $yahoo->password([$password])
106  
107 This method gets or sets the present B<password>.
108  
109 =cut
110  
111 0 ▶sub password {
Subroutine password: not called CC=2 SCAR=17.9
Pod documented
112 0 my $self = shift;
113 0 ▶ $self->{password} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
114 0 $self->{password};
115 }
116  
117 =head2 $yahoo->login()
118  
119 Call this after C<new()> to logon the Yahoo!Messenger service.
120  
121 =cut
122  
123 0 ▶sub login {
Subroutine login: not called CC=2 SCAR=17.9
Pod documented
124 0 my $self = shift;
125  
126 0 my $server = $self->get_connection;
127  
128 0 my $msg = $self->_create_message( 87, 0, '1' => $self->id, );
129 0 $server->send( $msg, 0 );
130 0 my $event = $self->recv();
131 0 my $https = Net::YahooMessenger::HTTPS->new( $self->id, $self->password,
132 $event->body );
133 0 my $auth = $self->_create_message(
134 84, 0,
135 '1' => $self->id,
136 '0' => $self->id,
137 '277' => $https->y_string,
138 '278' => $https->t_string,
139 '307' => $https->md5_string,
140 '244' => '4194239',
141 '2' => $self->id,
142 '2' => $self->id,
143 '135' => '9.0.0.2152',
144 );
145 0 $server->send($auth);
146 0 my $user_info = $self->recv();
147 0 my $buddy_list = $self->recv();
148  
149 0 my $login = $self->recv();
150 0 my $handler = $self->get_event_handler();
151 0 ▶ $handler->accept($login) if $handler;
Branch: 1 branch
BranchTrueFalseTotal
if $handler 0 0 0
152  
153 $self->add_event_source(
154 $server,
155 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 0 my $event = $self->recv;
157 0 my $handler = $self->get_event_handler;
158 0 $handler->accept($event);
159 },
160 0 'r'
161 );
162  
163 0 return $login->is_enable();
164 }
165  
166 0 ▶sub _dump_packet {
Subroutine _dump_packet: not called CC=2 SCAR=17.9
167 0 my $source = shift;
168 print join ' ',
169 0 ▶ map { sprintf '%02x(%s)', ord $_, (/^[\w\-_]$/) ? $_ : '.'; } split //,
Branch: 1 branch
BranchTrueFalseTotal
/^[\w\-_]$/ ? : 0 0 0
170 $source;
171 0 print "\n";
172 }
173  
174 =head2 $yahoo->send($yahoo_id, $message)
175  
176 This method send an Instant-Message C<$message> to the user specified by C<$yahoo_id>. A kanji code is Shift_JIS when including Japanese in $message.
177  
178 =cut
179  
180 0 ▶sub send {
Subroutine send: not called CC=1 SCAR=6.9
Pod documented
181 0 my $self = shift;
182 0 my $recipient = shift;
183 0 my $message = join '', @_;
184 0 my $server = $self->handle;
185  
186 0 my $event = $self->create('SendMessage');
187 0 $event->from( $self->id );
188 0 $event->to($recipient);
189 0 $event->body($message);
190 0 $event->option(1515563606); # in Buddy list then 1515563606 else 1515563605
191 0 $server->send( $event->to_raw_string, 0 );
192 }
193  
194 =head2 $yahoo->change_state($busy, $status_message)
195  
196 This method sets the I<status messages> for the current user. 'Status message' is set by C<$status_message>. 'Busy icon' is set by the numerical value of C<$busy>.
197  
198 The C<$busy> should be called with following arguments:
199  
200 0 - I'm Available
201 1 - Busy
202 2 - Sleep
203  
204 =cut
205  
206 0 ▶sub change_state {
Subroutine change_state: not called CC=1 SCAR=6.9
Pod documented
207 0 my $self = shift;
208 0 my $busy = shift;
209 0 my $message = join '', @_;
210 0 my $server = $self->handle;
211  
212 0 my $event = $self->create('ChangeState');
213 0 $event->status_code(99); # 99 : Custom status
214 0 $event->busy($busy);
215 0 $event->body($message);
216  
217 0 $server->send( $event->to_raw_string, 0 );
218 }
219  
220 0 ▶sub change_status_by_code {
Subroutine change_status_by_code: not called CC=2 SCAR=17.9
Pod undocumented
221 0 my $self = shift;
222 0 ▶ my $status_code = 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
223 0 my $server = $self->handle;
224  
225 0 my $event = $self->create('ChangeState');
226 0 $event->status_code($status_code);
227 0 $event->busy(1);
228  
229 0 $server->send( $event->to_raw_string, 0 );
230 }
231  
232 0 ▶sub ping {
Subroutine ping: not called CC=1 SCAR=6.9
Pod undocumented
233 0 my $self = shift;
234 0 my $server = $self->get_connection;
235 0 my $command = $self->_create_message( 76, 0, 0, '' );
236 0 $server->send( $command, 0 );
237 0 my $pong = $self->recv();
238 0 return $pong->is_enable;
239 }
240  
241 =head2 $yahoo->recv()
242  
243 This method reads the message from a server socket and returns a corresponding B<Event object>.
244 The B<Event object> which will be returned is as follows:
245  
246 Net::YahooMessenger::InvalidLogin - Invalid Login
247 Net::YahooMessenger::Login - Succeeded in Login.
248 Net::YahooMessenger::GoesOnline - Buddy has logged in.
249 Net::YahooMessenger::ReceiveMessage - Message was received.
250 Net::YahooMessenger::ChangeState - Buddy has change status.
251 Net::YahooMessenger::GoesOffline - Buddy logged out.
252 Net::YahooMessenger::NewFriendAlert - New Friend Alert.
253 Net::YahooMessenger::UnImplementEvent - Un-implemented event was received.
254  
255 All event objects have the following attributes:
256  
257 =over 4
258  
259 =item $event->from
260  
261 B<Yahoo id> which invoked the event.
262  
263 =item $event->to
264  
265 B<Yahoo id> which should receive an event.
266  
267 =item $event->body
268  
269 The contents of an event. The message and state which were transmitted.
270  
271 =item $event->code
272  
273 The event number on Yahoo Messenger Protocol.
274  
275 =back
276  
277 =cut
278  
279 0 ▶sub recv {
Subroutine recv: not called CC=1 SCAR=6.9
Pod documented
280 0 my $self = shift;
281 0 require Net::YahooMessenger::EventFactory;
282 0 my $event_factory = Net::YahooMessenger::EventFactory->new($self);
283 0 return $event_factory->create_by_raw_data();
284 }
285  
286 =head2 $yahoo->get_connection()
287  
288 This method returns a raw server socket. When connection has already ended, the socket is returned, and when not connecting, it connects newly.
289  
290 =cut
291  
292 0 ▶sub get_connection {
Subroutine get_connection: not called CC=3 SCAR=24.8
Pod documented
293 0 my $self = shift;
294 0 ▶ return $self->handle if $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle 0 0 0
295  
296 my $server = IO::Socket::INET->new(
297 PeerAddr => $self->{hostname},
298 0 ▶ PeerPort => $self->get_port,
Branch: 1 branch
BranchTrueFalseTotal
my $server = "IO::Socket::INET"->new("PeerAddr", $self->{"hostname"}, "PeerPort", $self->get_port, "Proto", "tcp", "Timeout", 30) or die $! 0 0 0
299 Proto => 'tcp',
300 Timeout => 30,
301 ) or die $!;
302 0 $server->autoflush(1);
303 0 return $self->handle($server);
304 }
305  
306 0 ▶sub buddy_list {
Subroutine buddy_list: not called CC=2 SCAR=17.9
Pod undocumented
307 0 my $self = shift;
308 0 ▶ @{ $self->{buddy_list} } = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
309 0 return @{ $self->{buddy_list} };
310 }
311  
312 0 ▶sub get_buddy_by_name {
Subroutine get_buddy_by_name: not called CC=1 SCAR=6.9
Pod undocumented
313 0 my $self = shift;
314 0 my $name = shift;
315 0 my ($buddy) = grep { lc $_->name eq lc $name } $self->buddy_list;
316 0 return $buddy;
317 }
318  
319 =head2 $yahoo->set_event_hander($event_handler)
320  
321 This method sets the Event handler for a specific Yahoo!Messenger server event. C<$event_handler> is the sub class of Net::YahooMessenger::EventHandler.
322  
323 Note: The event which can be overwritten should look at the method signature of L<Net::YahooMessenger::EventHandler>.
324  
325 =cut
326  
327 0 ▶sub set_event_handler {
Subroutine set_event_handler: not called CC=1 SCAR=6.9
Pod undocumented
328 0 my $self = shift;
329 0 $self->{event_handler} = shift;
330 }
331  
332 0 ▶sub get_event_handler {
Subroutine get_event_handler: not called CC=1 SCAR=6.9
Pod undocumented
333 0 my $self = shift;
334 0 return $self->{event_handler};
335 }
336  
337 =head2 $yahoo->add_event_source($file_handle, $code_ref, $flag)
338  
339 This method adds the file handle (event sauce) to supervise. The file handle to add is specified by C<$file_handle>. The code reference to the processing to perform is specified by $code_ref.
340  
341 C<$flag> eq 'r' - set when the file handle to add is an object for read.
342 C<$flag> eq 'w' - set when the file handle to add is an object for write.
343  
344 By adding another handle (for example, STDIN), processing can be performed based on those inputs. Usually, the server socket of 'Yahoo!Messenger server' is set as a candidate for surveillance.
345  
346 ex:
347 # The input of STDIN is transmitted to 'EXAMPLE_YAHOO_ID'.
348 $yahoo->add_event_source(\*STDIN, sub {
349 my $message = scalar <STDIN>;
350 chomp $message;
351 $yahoo->send('EXAMPLE_YAHOO_ID', $message);
352 }, 'r');
353  
354 =cut
355  
356 0 ▶sub add_event_source {
Subroutine add_event_source: not called CC=4 SCAR=30.0
Pod documented
357 0 my $self = shift;
358 0 my ( $handle, $code, $flag, $obj ) = @_;
359  
360 0 foreach my $mode ( split //, lc $flag ) {
361 0 ▶ if ( $mode eq 'r' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'r') { } 0 0 0
elsif ($mode eq 'w') { } 0 0 0
362 0 $self->{_read}->add($handle);
363 }
364 elsif ( $mode eq 'w' ) {
365 0 $self->{_write}->add($handle);
366 }
367 }
368 0 $self->{_connhash}->{$handle} = [ $code, $obj ];
369 }
370  
371 =head2 $yahoo->start()
372  
373 If you're writing a fairly simple application that doesn't need to interface with other event-loop-based libraries, you can just call start() to begin communicating with the server.
374  
375 =cut
376  
377 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
378 0 my $self = shift;
379 0 while (1) {
380 0 $self->do_one_loop;
381 }
382 }
383  
384 0 ▶sub do_one_loop {
Subroutine do_one_loop: not called CC=4 SCAR=30.0
Pod undocumented
385 0 my $self = shift;
386  
387 0 for my $ready (
388 IO::Select->select(
389 $self->{_read}, $self->{_write}, $self->{_error}, 10
390 )
391 )
392 {
393 0 for my $handle (@$ready) {
394 0 my $event = $self->{_connhash}->{$handle};
395 0 ▶ $event->[0]->( $event->[1] ? ( $event->[1], $handle ) : $handle );
Branch: 1 branch
BranchTrueFalseTotal
$event->[1] ? : 0 0 0
396 }
397 }
398 }
399  
400 0 ▶sub get_port {
Subroutine get_port: not called CC=2 SCAR=17.9
Pod undocumented
401 0 my $self = shift;
402 0 ▶ return $self->{port} if $self->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"port"} 0 0 0
403 0 return 5050;
404 }
405  
406 0 ▶sub _create_message {
Subroutine _create_message: not called CC=3 SCAR=24.8
407 0 my $self = shift;
408 0 my $event_code = shift;
409 0 my $option = shift;
410 0 my @param = @_;
411 0 my $body = '';
412  
413 0 ▶ while (@param) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param) 0 0 0
414 0 my $key = shift @param;
415 0 my $value = shift @param;
416 0 $body .= $key . YMSG_SEPARATER . $value . YMSG_SEPARATER;
417 }
418  
419 0 ▶ my $header = pack "a4xCx2nnNN",
Condition0%0/2
$self->identifier || 0
l!l
00
MC/DC0%0/1
$self->identifier || 0
$self->identifier
Truth table0%0/2
A
A= $self->identifier
Aresult
11
00
420 YMSG_STD_HEADER,
421 YMSG_PROTOCOL_VERSION,
422 length $body,
423 $event_code,
424 $option,
425 $self->identifier || 0;
426 0 return $header . $body;
427 }
428  
429 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod undocumented
430 0 my $self = shift;
431 0 my $event_name = shift;
432  
433 0 require Net::YahooMessenger::EventFactory;
434 0 my $event_factory = Net::YahooMessenger::EventFactory->new($self);
435 0 return $event_factory->create_by_name($event_name);
436 }
437  
438 0 ▶sub _create_login_command {
Subroutine _create_login_command: not called CC=1 SCAR=6.9
439 0 my $self = shift;
440 0 my $event = $self->create('Login');
441 0 $event->id( $self->id );
442 0 $event->password( $self->password );
443 0 $event->from( $self->id );
444 0 $event->hide(0);
445 0 return $event->to_raw_string;
446 }
447  
448 0 ▶sub handle {
Subroutine handle: not called CC=2 SCAR=17.9
Pod undocumented
449 0 my $self = shift;
450 0 ▶ $self->{handle} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
451 0 $self->{handle};
452 }
453  
454 0 ▶sub identifier {
Subroutine identifier: not called CC=2 SCAR=17.9
Pod undocumented
455 0 my $self = shift;
456 0 ▶ $self->{identifier} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
457 0 $self->{identifier};
458 }
459  
460 #
461  
462 # my @buddy = $self->_get_buddy_list_by_array(
463 # $self->_get_list_by_name('BUDDYLIST', $response->content)
464 # );
465 # $self->buddy_list(@buddy);
466  
467 0 ▶sub _get_list_by_name {
Subroutine _get_list_by_name: not called CC=2 SCAR=17.9
468 0 my $self = shift;
469 0 my $name = shift;
470 0 my $string = shift;
471  
472 0 ▶ if ( $string =~ /BEGIN $name\r?\n(.*)\r?\nEND $name/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /BEGIN $name\r?\n(.*)\r?\nEND $name/s) 0 0 0
473 0 my @list = split /\r?\n/, $1;
474 0 return @list;
475 }
476 }
477  
478 0 ▶sub add_buddy_by_name {
Subroutine add_buddy_by_name: not called CC=2 SCAR=17.9
Pod undocumented
479 0 my $self = shift;
480 0 my $group = shift;
481 0 my @buddy_name = @_;
482 0 my @buddy_list = $self->buddy_list();
483 0 for my $name (@buddy_name) {
484 0 my $buddy = Net::YahooMessenger::Buddy->new;
485 0 $buddy->name($name);
486 0 push @buddy_list, $buddy;
487 }
488 0 $self->buddy_list(@buddy_list);
489 }
490  
491 1 1;
492 __END__
« YahooMessenger/ChangeState.pm ↑ Summary YahooMessenger/GoesOffline.pm »