Msmgr/Session.pm untested

filter: Statementstmt 0.0% 0 / 231 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 34 totaltotal 0.0% 0 / 359 CC 20 SCAR 22.2
CC
20
Cov
0%
_handle_xfr
30.0
_handle_usr
30.0
Login
20.0
CRAP 9.2
1 #
2 # $Id: Session.pm,v 0.16 2003/08/07 00:01:59 lawrence Exp $
3 #
4  
5 package Net::Msmgr::Session;
6 use strict;
7 use warnings;
8 use Net::Msmgr qw(:debug GetVersion8Response );
9 use Net::Msmgr::Object;
10 use Net::Msmgr::Switchboard;
11 use Net::Msmgr::Conversation;
12 use Carp;
13  
14 our @ISA = qw (Net::Msmgr::Object);
15  
16 sub _fields { return shift->SUPER::_fields,( proto => 'MSNP2',
17 user => undef,
18 debug => 0,
19 list_version => 0,
20 list => undef,
21 roster => undef,
22 login_handler => undef,
23 logout_handler => undef,
24 connect_handler => undef,
25 disconnect_handler => undef,
26 conversation => {},
27 _conv_by_handle => {},
28 ns => undef) ; }
29  
30 =pod
31  
32 =head1 NAME
33  
34 Net::Msmgr::Session -- The main point of contact in a Perl MSN Client
35  
36 =head1 SYNOPSIS
37  
38 our $session = new Net::Msmgr::Session;
39 our $user = new Net::Msmgr::User ( user => 'username@msn.com',
40 password => 'my_password' ) ;
41  
42 $session->user($user);
43 $session->login_handler( sub { shift->Logout } ) ;
44 $session->Login;
45  
46 Event::loop;
47  
48  
49 =head1 DESCRIPTION
50  
51 Net::Msmgr::Session is the encapsulation for an
52 entire MSN Messenger Client session. You
53 register handlers to deal with various inbound
54 messages such as incoming chat requests or
55 state change notifications. When you are
56 finished, you call instance method Logout and
57 everything goes away.
58  
59 =head1 CONSTRUCTOR
60  
61 my $session = new Net::Msmgr::Session (...);
62  
63 - or -
64  
65 my $session = Net::Msmgr::Session->new(...);
66  
67  
68 Constructor parameters are:
69  
70 =over
71  
72 =item user (mandatory)
73  
74 The container for the Authentication Data for the
75 session.
76  
77  
78 =item login_handler (optional)
79  
80 A code reference to a subroutine that will be
81 called upon successful authentication and
82 connection to the notification server.
83  
84 =item connect_handler (optional)
85  
86 A code reference to a subroutine that will be called after each
87 connection to a server.
88  
89 =item disconnect_handler (optional)
90  
91 A code reference to a subroutine that will be called just before each
92 close call when disconnecting from a server.
93  
94 =back
95  
96 =cut
97  
98 ###
99 ### These are methods for handling common user-interface requests
100 ###
101  
102 =pod
103  
104 =head1 INSTANCE METHODS
105  
106 =over
107  
108 =item $session->ui_new_conversation;
109  
110 Instantiate a new conversation (which you can later invite people to)
111  
112 =cut
113  
114 sub ui_new_conversation( $ )
115 {
116 my $self = shift;
117 my $conversation = new Net::Msmgr::Conversation( session => $self );
118 $self->add_conversation($conversation);
119
120 my $command = Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
121 cmd => 'XFR',
122 params => [ 'SB' ] );
123 $command->send($self->ns);
124 $self->{_conv_by_handle}->{$command->trid} = $conversation;
125 return $conversation;
126 }
127  
128 =pod
129  
130 =item $session->ui_invite_conversation($conversation, $username);
131  
132 Send an invitation to another user.
133
134 =cut
135  
136 =pod
137  
138 =item $session->ui_sync;
139  
140 Initiate list syncronization.
141  
142 =cut
143
144 sub ui_sync($)
145 {
146 my ($self) = @_;
147 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
148 cmd => 'SYN',
149 params => [ '0' ] )->send($self->ns);
150 }
151  
152 =pod
153  
154 =item $session->ping;
155  
156 Ping the Notification Server.
157  
158 =cut
159  
160 sub ui_ping
161 {
162 my ($self) = @_;
163 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Async,
164 cmd => 'PNG')->send($self->ns);
165 }
166  
167 =pod
168  
169 =item $session->ui_send_message($ssid, $message);
170  
171 Send a message to all users in Switchboard Connection $ssid. $message
172 must be a properly MIME formatted message.
173  
174 =cut
175  
176 sub ui_send_message($$$;$)
177 {
178 carp 'use $conversation->send_message instead';
179 }
180  
181 =pod
182  
183 =item $session->ui_state_nln;
184  
185 =item $session->ui_state_bsy;
186  
187 =item $session->ui_state_idl;
188  
189 =item $session->ui_state_brb;
190  
191 =item $session->ui_state_awy;
192  
193 =item $session->ui_state_phn;
194  
195 =item $session->ui_state_lun;
196  
197 =item $session->ui_state_hdn;
198  
199 Set your Notification Status to one of Online (NLN), Busy (BSY), Idle (IDL),
200 Be Right Back (BRB), Away (AWY), Phone (PHN), Out To Lunch (LUN), or Hidden (HDN).
201 All of these are advisory, except hidden, which makes you appear offline, and disallows
202 initiation of Switchboard sessions.
203  
204 =cut
205  
206 sub ui_state_nln
207 {
208 my ($self) = @_;
209 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
210 cmd => 'CHG',
211 params => [ 'NLN' ] )->send($self->ns);
212 }
213  
214 sub ui_state_bsy
215 {
216 my ($self) = @_;
217 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
218 cmd => 'CHG',
219 params => [ 'BSY' ] )->send($self->ns);
220 }
221  
222 sub ui_state_idl
223 {
224 my ($self) = @_;
225 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
226 cmd => 'CHG',
227 params => [ 'IDL' ] )->send($self->ns);
228 }
229  
230 sub ui_state_brb
231 {
232 my ($self) = @_;
233 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
234 cmd => 'CHG',
235 params => [ 'BRB' ] )->send($self->ns);
236 }
237  
238 sub ui_state_awy
239 {
240 my ($self) = @_;
241 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
242 cmd => 'CHG',
243 params => [ 'AWY' ] )->send($self->ns);
244 }
245  
246 sub ui_state_phn
247 {
248 my ($self) = @_;
249 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
250 cmd => 'CHG',
251 params => [ 'PHN' ] )->send($self->ns);
252 }
253  
254 sub ui_state_lun
255 {
256 my ($self) = @_;
257 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
258 cmd => 'CHG',
259 params => [ 'LUN' ] )->send($self->ns);
260 }
261  
262 sub ui_state_hdn
263 {
264 my ($self) = @_;
265 Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
266 cmd => 'CHG',
267 params => [ 'HDN' ] )->send($self->ns);
268 }
269  
270 sub add_default_handlers
271 {
272 my $self = shift;
273 return unless $self && $self->ns;
274  
275 $self->ns->add_handler('_handle_xfr','XFR');
276 $self->ns->add_handler('_handle_usr','USR');
277 $self->ns->add_handler('_handle_rng','RNG');
278 $self->ns->add_handler('_handle_chg','CHG');
279 $self->ns->add_handler('_handle_iln','ILN');
280 $self->ns->add_handler('_handle_nln','NLN');
281 $self->ns->add_handler('_handle_fln','FLN');
282 $self->ns->add_handler('_handle_lst','LST');
283  
284 $self->ns->add_handler('_handle_misc','GTC','BLP','PRP','LSG','ADD','REM','REA','ADG','RMG','REG');
285 return $self;
286 }
287  
288 sub dispatch_all
289 {
290 my ($self, $connection, $command) = @_;
291 foreach my $handler (@{$connection->{handlers}->{$command->cmd}})
292 {
293 if ($self->debug & DEBUG_HANDLER)
294 {
295 print STDERR $connection->name ," is calling a " . $command->cmd ." handler\n";
296 }
297 $self->dispatch($handler,$command);
298 }
299 }
300  
301 #
302 # Currently - dispatch takes a single method-name
303 # and calls it within session
304 #
305 # sub dispatch
306 # {
307 # my ($self, $handler, $command) = @_;
308 # my ($method, @params) = @$handler;
309 # $self->$method($command, @params);
310 # }
311  
312  
313 #
314 # Instead, let us do it more like Perl/Tk callbacks
315 # so - it can be
316 # [ \&subroutine @params ] -- call subroutine ($command, @params )
317 # [ 'method' ] -- call $self->method ($command, @params)
318 # [ $object, 'method' ] -- call $object->method ($command, @params)
319 #
320  
321 sub dispatch
322 {
323 my ($self, $handler, $command) = @_;
324
325 my ($first, @params) = @$handler;
326 if (ref($first) eq 'CODE')
327 {
328 \&$first($command, @params)
329 }
330 elsif (ref($first)) # is this an object reference
331 {
332 my $method = shift @params; # pick up method name
333 $first->$method($command, @params);
334 }
335 else
336 {
337 $self->$first($command, @params);
338 }
339 }
340  
341 sub list
342 {
343 my $self = shift;
344 $self->{list} = { AL => [], FL => [], BL => [], RL => [] } unless $self->{list};
345 return $self->{list};
346 }
347  
348 sub online
349 {
350 my ($self, $user, $sub_status) = @_;
351 $self->{roster}->{$user} = $sub_status;
352 }
353  
354 sub offline
355 {
356 my ($self, $user) = @_;
357 delete $self->{roster}->{$user};
358 }
359  
360 =pod
361  
362 =item $session->Logout;
363  
364 The end of the road.
365  
366 =cut
367  
368  
369 sub Logout
370 {
371 my $self = shift;
372 foreach my $conversation (values(%{$self->conversation}))
373 {
374 $self->del_conversation($conversation);
375 }
376 $self->ns->shutdown;
377 $self->ns(undef);
378 }
379  
380 =pod
381  
382 =item $session->Login;
383  
384 The beginning of the road. If
385 $session->login_handler is defined, it will be
386 treated as a callback when the session connects,
387 and passed the single parameter of the
388 Net::Msmgr::Session object
389  
390 =cut
391  
392 sub Login( $ )
393 {
394 my $self = shift;
395 unless ($self->ns)
396 {
397 $self->ns( new Net::Msmgr::Connection(session => $self ) );
398
399 $self->ns->debug($self->debug);
400 $self->add_default_handlers;
401 $self->ns->add_handler($self->login_handler,'login');
402 }
403 $self->ns->connect;
404 &{$self->{connect_handler}}($self->ns) if $self->{connect_handler};
405 my $status;
406 $status = Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
407 cmd => 'VER',
408 params => [ $self->proto , 'CVR0' ] )->send($self->ns) ;
409
410 if ($self->proto eq 'MSNP8')
411 {
412 $status = Net::Msmgr::Command->(type => Net::Msmgr::Command::Normal,
413 cmd => 'CVR',
414 params => [qw ( 0x0409 winnt 5.1 i386 MSNMSGR 5.0.0515 MSMSGS ) , $self->user->user ])->send($self->ns);
415
416 $status = Net::Msmgr::Command (type => Net::Msmgr::Command::Normal,
417 cmd => 'USR',
418 params => [ 'TWN', 'I', $self->user->user ] )->send($self->ns);
419
420 }
421 else
422 {
423 $status = Net::Msmgr::Command->new( type => Net::Msmgr::Command::Normal,
424 cmd => 'USR',
425 params => [ 'MD5','I', $self->user->user ] )->send($self->ns);
426
427 }
428 }
429  
430 ### <<< XFR 2 NS 207.46.106.145:1863 0 207.46.104.20:1863
431 ### <<< XFR 10 SB 207.46.108.37:1863 CKI 17262740.1050826919.32308
432 ### 0 1 2 3 4
433 sub _handle_xfr($$)
434 {
435 my ($self, $command) = @_;
436
437 my @param = @{$command->params};
438  
439 $param[2] =~ m/(\d+\.\d+\.\d+\.\d+):(\d+)/;
440 my ($nserver, $nsport) = ($1,$2);
441  
442 if ($param[1] eq 'NS') # going from DS to NS
443 {
444 my $oldns = $self->ns;
445  
446 $self->ns(new Net::Msmgr::Connection ( nserver => $nserver,
447 nsport => $nsport,
448 debug => $oldns->debug,
449 session => $self,
450 handlers => $oldns->handlers,
451 name => 'Notification Server'));
452 $self->Login;
453 $oldns->close; # destroy old session
454 }
455 elsif ($param[1] eq 'SB') # adding a connection to a switchboard
456 {
457 $param[4] =~ m/(\d*)\..*/;
458 my $ssid = $1;
459 my $conversation = $self->{_conv_by_handle}->{$param[0]};
460 my $sb = new Net::Msmgr::Switchboard ( nserver => $nserver,
461 nsport => $nsport,
462 debug => $self->ns->debug,
463 session => $self,
464 ssid => $ssid,
465 name => 'Switchboard Channel ' . $ssid );
466 $sb->connect;
467 &{$self->{connect_handler}}($sb) if $self->{connect_handler};
468
469 $command = new Net::Msmgr::Command ( type => Net::Msmgr::Command::Normal,
470 cmd => 'USR',
471 params => [ $self->user->user, $param[4] ] );
472 if (my $conversation = $self->{_conv_by_handle}->{$param[0]})
473 {
474 $conversation->switchboard($sb);
475 $sb->add_handler([$conversation, '_handle_usr'], 'USR');
476 $sb->add_handler([$conversation, '_handle_iro'], 'IRO');
477 $sb->add_handler([$conversation, '_handle_bye'], 'BYE');
478 $sb->add_handler([$conversation, '_handle_joi'], 'JOI');
479 my $command = new Net::Msmgr::Command ( type => Net::Msmgr::Command::Pseudo,
480 cmd => 'conversation',
481 params => [ $conversation ] );
482 $self->dispatch_all($self->ns, $command);
483 }
484  
485 $command->send($sb);
486  
487  
488 }
489 else
490 {
491 warn "unheard of XFR target `$param[1]'";
492 }
493 }
494  
495 sub _handle_usr
496 {
497 my ($self, $command) = @_;
498 # my $command = shift;
499 my @param = @{$command->params};
500  
501 ### <-- USR 5 OK arrow@sandbox.cluon.com Arrow%20The%20Dog
502 ### 0 1 2 3
503 if ($param[1] eq 'OK') # special case
504 {
505 $self->ns->add_handler('Logout','close');
506 my $pseudo = new Net::Msmgr::Command;
507 $pseudo->type(Net::Msmgr::Command::Pseudo);
508 $pseudo->cmd('login');
509 $self->dispatch_all($self->ns, $pseudo);
510 }
511 elsif ($param[1] eq 'TWN' && $param[2] eq 'S')
512 {
513 my $magic_token = GetVersion8Response($self->user, @param);
514 warn "No authentication token" unless $magic_token;
515 Net::Msmgr::Command->new( type => Net::Msmgr::Command::Normal,
516 cmd => 'USR',
517 params => [ 'TWN', 'S', $magic_token ] )->send($self->ns);
518 }
519 elsif ($param[1] eq 'MD5' && $param[2] eq 'S')
520 {
521 $self->user->salt($param[3]);
522 Net::Msmgr::Command->new( type => Net::Msmgr::Command::Normal,
523 cmd => 'USR',
524 params => [ 'MD5', 'S', $self->user->crypto_passwd ] )->send($self->ns);
525 }
526 }
527  
528 sub _handle_rng
529 {
530 ## RNG 111920 207.46.108.76:1863 CKI 1059841202.25757 lawrenceeu@hotmail.com FriendlyName
531 ## 0 1 2 3 4 5
532 my ($self, $command) = @_;
533 my @params = @{$command->params};
534  
535 $params[1] =~ m/(\d+\.\d+\.\d+\.\d+):(\d+)/;
536 my ($server, $port) = ($1,$2);
537  
538 my $sb = new Net::Msmgr::Switchboard( nserver => $server,
539 nsport => $port,
540 name => 'Switchboard Channel ' . $params[0],
541 debug => $self->debug,
542 ssid => $params[0],
543 session => $self)->connect;
544 &{$self->{connect_handler}}($sb) if $self->{connect_handler};
545  
546 my $conversation = new Net::Msmgr::Conversation ( session => $self,
547 switchboard => $sb );
548 $self->add_conversation($conversation);
549  
550 $sb->add_handler([$conversation, '_handle_ans'], 'ANS');
551 $sb->add_handler([$conversation, '_handle_iro'], 'IRO');
552 $sb->add_handler([$conversation, '_handle_bye'], 'BYE');
553 $sb->add_handler([$conversation, '_handle_joi'], 'JOI');
554 $command = new Net::Msmgr::Command ( type => Net::Msmgr::Command::Pseudo,
555 cmd => 'conversation',
556 connection => $sb,
557 params => [ $conversation ] );
558 $self->dispatch_all($self->ns, $command);
559  
560 $command = Net::Msmgr::Command->new(type => Net::Msmgr::Command::Normal,
561 cmd => 'ANS',
562 params => [ $self->user->user, $params[3], $params[0] ] );
563  
564 $command->send($sb);
565  
566 }
567  
568 sub _handle_ans
569 {
570 }
571  
572 ### <<< LST trid ln 12182 1 3 myname@msn.com My%20Name\r\n
573 ### <<< LST trid ln 12182 2 3 example@passport.com Mike\r\n
574 ### <<< LST trid ln 12182 3 3 name_123@hotmail.com Name_123\r\n
575 ### 0 1 2 3 4 5 6
576 ### $pr->[1] is one of AL, FL, BL, or RL
577 ### $pr->[2] is the list version number (if any of the four lists change, the version number bumps)
578 ### $pr->[3] is sequence number
579 ### $pr->[4] is # of items in the list
580 ### $pr->[5] is the email and $pr->[6] is the friendly name
581 sub _handle_lst
582 {
583 my ($self, $command) = @_;
584 my $pr = $command->params;
585 if ($pr->[4])
586 {
587 $self->list_version($pr->[2]);
588 $self->list->{$pr->[1]}->[$pr->[3] -1 ] = $pr->[5];
589 }
590 else
591 {
592 $self->list_version($pr->[2]);
593 $self->list->{$pr->[1]} = [];
594 }
595 }
596  
597  
598 our %_status_words = ( NLN => 'Online', BSY => 'Busy', IDL => 'Idle', BRB => 'Be Right Back',
599 AWY => 'Away', PHN => 'On the phone', LUN => 'Out to Lunch', HDN => 'Hidden', FLN => 'Offline' );
600  
601  
602 ### Async message -- no trid
603 ### <<< NLN NLN msnuser@msn.com Friendly%20User
604 ### 0 1 2
605  
606 sub _handle_nln
607 {
608 my ($self, $command) = @_;
609 my $pr = $command->params;
610 $self->online($pr->[1], $_status_words{$pr->[0]});
611 }
612  
613 sub _handle_fln
614 {
615 my ($self, $command) = @_;
616 my $pr = $command->params;
617 $self->offline($pr->[0]);
618 }
619  
620 ### Syncronous - response to your first CHG trid NLN
621 ### <<< ILN trid NLN msnuser@msn.com Friendly%20User
622 ### 0 1 2 3
623 sub _handle_iln
624 {
625 my ($self, $command) = @_;
626 my $pr = $command->params;
627 $self->online($pr->[2], $_status_words{$pr->[1]});
628 }
629  
630 ### <<< CHG trid NLN
631 ### <<< CHG trid BSY
632 ### <<< CHG trid IDL
633 ### <<< CHG trid BRB
634 ### <<< CHG trid AWY
635 ### <<< CHG trid PHN
636 ### <<< CHG trid LUN
637  
638 sub _handle_chg
639 {
640 my ($self, $command) = @_;
641 my $pr = $command->params;
642 }
643  
644 sub _handle_misc
645 {
646 my ($self, $command) = @_;
647 print STDERR $command->as_text if $self->{debug} & DEBUG_NOTIFICATION;
648 }
649  
650 sub add_conversation($$)
651 {
652 my ($self, $conversation) = @_;
653 $self->conversation->{$conversation} = $conversation;
654 return $conversation;
655 }
656  
657 sub del_conversation($$)
658 {
659 my ($self, $conversation) = @_;
660 $self->conversation->{$conversation}->shutdown;
661 delete $self->conversation->{$conversation};
662 }
663  
664 sub list_conversations($)
665 {
666 my ($self) = @_;
667 return values(%{$self->{conversation}});
668 }
669  
670 # sub add_sb
671 # {
672 # my $self = shift;
673 # my $sb = shift;
674 # my $ssid = shift;
675 # $self->{sb}->{$ssid} = $sb;
676 # }
677  
678 # sub del_sb
679 # {
680 # my $self = shift;
681 # my $ssid = shift;
682 # return undef unless $ssid && exists ($self->{sb}->{$ssid});
683 # $self->{sb}->{$ssid}->shutdown;
684 # delete $self->{sb}->{$ssid};
685 # }
686  
687 # sub all_sb
688 # {
689 # my $self = shift;
690 # return keys(%{$self->{sb}});
691 # }
692  
693 1;
694  
695 #
696  
697 # $Log
698 #