| 1 | package Net::YMSG::EventFactory; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use Net::YMSG; | ||
| 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 => 85; | ||
| 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 | |||
| 27 | use constant EVENT_CLASS => { | ||
| 28 | 1 => 'Net::YMSG::GoesOnline', | ||
| 29 | 2 => 'Net::YMSG::GoesOffline', | ||
| 30 | 3 => 'Net::YMSG::ChangeState', | ||
| 31 | 4 => 'Net::YMSG::ChangeState', | ||
| 32 | 6 => 'Net::YMSG::ReceiveMessage', | ||
| 33 | 15 => 'Net::YMSG::NewFriendAlert', | ||
| 34 | 76 => 'Net::YMSG::ServerIsAlive', | ||
| 35 | 85 => 'Net::YMSG::ReceiveBuddyList', | ||
| 36 | 87 => 'Net::YMSG::ChallengeStart', | ||
| 37 | 152 => 'Net::YMSG::ChatRoomLogon', | ||
| 38 | 168 => 'Net::YMSG::ChatRoomReceive', | ||
| 39 | 155 => 'Net::YMSG::ChatRoomLogoff', | ||
| 40 | }; | ||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | sub new | ||
| 45 | { | ||
| 46 | my $class = shift; | ||
| 47 | my $connection = shift; | ||
| 48 | bless { connection => $connection }, $class; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub create_by_raw_data | ||
| 53 | { | ||
| 54 | my $self = shift; | ||
| 55 | my $connection = $self->{connection}; | ||
| 56 | my ($event_code, $identifier, $body) = eval {$self->_get_message_body;}; | ||
| 57 | if ($@) { | ||
| 58 | # print STDERR $@; | ||
| 59 | require Net::YMSG::InvalidLogin; | ||
| 60 | return Net::YMSG::InvalidLogin->new($connection); | ||
| 61 | } | ||
| 62 | |||
| 63 | my ($code, $recipient, $private, $sender, | ||
| 64 | @baddy) = split /\xC0\x80/, $body; | ||
| 65 | if ($event_code == YMSG_LOGIN && lc $recipient eq lc $sender) { | ||
| 66 | require Net::YMSG::Login; | ||
| 67 | my $event = Net::YMSG::Login->new($connection); | ||
| 68 | $event->source($body); | ||
| 69 | $event->from($sender); | ||
| 70 | $connection->identifier($identifier); | ||
| 71 | return $event if scalar $connection->buddy_list <= 0; | ||
| 72 | |||
| 73 | shift @baddy; | ||
| 74 | my $id = shift @baddy; | ||
| 75 | my $buddy; | ||
| 76 | for (my $i=0; $i < scalar @baddy; $i+=2) { | ||
| 77 | if ($baddy[$i] eq BADDY_NAME) { | ||
| 78 | $buddy = $connection->get_buddy_by_name($baddy[$i+1]); | ||
| 79 | } | ||
| 80 | elsif ($baddy[$i] eq STATE) { | ||
| 81 | $buddy->status($baddy[$i+1]); | ||
| 82 | } | ||
| 83 | elsif ($baddy[$i] eq STATE_OF_USER_DEFINITION) { | ||
| 84 | $buddy->custom_status($baddy[$i+1]); | ||
| 85 | } | ||
| 86 | elsif ($baddy[$i] eq BUSYNESS) { | ||
| 87 | $buddy->busy($baddy[$i+1]); | ||
| 88 | } | ||
| 89 | elsif ($baddy[$i] eq SESSION_ID) { | ||
| 90 | $buddy->session_id($baddy[$i+1]); | ||
| 91 | } | ||
| 92 | elsif ($baddy[$i] eq ONLINE_OR_OFFLINE) { | ||
| 93 | $buddy->online($baddy[$i+1]); | ||
| 94 | } | ||
| 95 | else {} | ||
| 96 | } | ||
| 97 | return $event; | ||
| 98 | |||
| 99 | } else { | ||
| 100 | my $class = EVENT_CLASS->{$event_code} || 'Net::YMSG::DummyEvent'; | ||
| 101 | eval "require $class"; | ||
| 102 | # print STDERR "Calling up class: $class with $body\n"; | ||
| 103 | if ($@) { | ||
| 104 | # print STDERR $@; | ||
| 105 | require Net::YMSG::UnImplementEvent; | ||
| 106 | my $event = Net::YMSG::UnImplementEvent->new($connection); | ||
| 107 | $event->code($event_code); | ||
| 108 | $event->source($body); | ||
| 109 | return $event; | ||
| 110 | } | ||
| 111 | my $event = $class->new($connection); | ||
| 112 | $event->source($body); | ||
| 113 | return $event; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | sub create_by_name | ||
| 119 | { | ||
| 120 | my $self = shift; | ||
| 121 | my $class = 'Net::YMSG::'. shift; | ||
| 122 | eval "require $class"; | ||
| 123 | if ($@) { | ||
| 124 | require Net::YMSG::NullEvent; | ||
| 125 | return Net::YMSG::NullEvent->new; | ||
| 126 | } | ||
| 127 | return $class->new($self->{connection}); | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | sub _get_message_body | ||
| 132 | { | ||
| 133 | my $self = shift; | ||
| 134 | my $handle = $self->{connection}->handle; | ||
| 135 | my %command; | ||
| 136 | |||
| 137 | my $header = $self->_recv_by_length(20); | ||
| 138 | my ( | ||
| 139 | $signature, | ||
| 140 | $version, | ||
| 141 | $length, | ||
| 142 | $event_code, | ||
| 143 | $return, | ||
| 144 | $identifier | ||
| 145 | ) = unpack "a4Cx3nnNN", $header; | ||
| 146 | |||
| 147 | die 'Wring protocol' if $signature ne Net::YMSG->YMSG_STD_HEADER; | ||
| 148 | my $message = $self->_recv_by_length($length); | ||
| 149 | return ($event_code, $identifier, $message); | ||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | sub _recv_by_length | ||
| 155 | { | ||
| 156 | my $self = shift; | ||
| 157 | my $length = shift || 0; | ||
| 158 | my $handle = $self->{connection}->handle; | ||
| 159 | my $message = ''; | ||
| 160 | |||
| 161 | while (length $message < $length) { | ||
| 162 | my $buff = ''; | ||
| 163 | $handle->sysread($buff, 1, 0) | ||
| 164 | or die "Disconnect socket"; | ||
| 165 | $message .= $buff; | ||
| 166 | } | ||
| 167 | if (0) { | ||
| 168 | print "\n"; | ||
| 169 | print map { | ||
| 170 | sprintf "%s ", $_ =~ /[\w\d]/ ? $_ : '.'; | ||
| 171 | } split //, $message; | ||
| 172 | print "\n"; | ||
| 173 | print map { | ||
| 174 | sprintf "%02x ", ord $_; | ||
| 175 | } split //, $message; | ||
| 176 | } | ||
| 177 | return $message; | ||
| 178 | } | ||
| 179 | 1; | ||
| 180 | __END__ |