| 1 | package Net::YahooMessenger::Event; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use constant YMSG_SIGNATURE => 'YMSG'; | ||
| 5 | use constant YMSG_SEPARATOR => "\xC0\x80"; | ||
| 6 | use constant YMSG_SALT => '_2S43d5f'; | ||
| 7 | use constant DEFAULT_OPTION => 1515563605; | ||
| 8 | use constant HIDE_LOGIN => 12; | ||
| 9 | |||
| 10 | use constant YMSG_PROTOCOL_VERSION => '16'; | ||
| 11 | |||
| 12 | use constant DATA_TYPE => { | ||
| 13 | ID => 0, | ||
| 14 | PROPOSERS_ID => 3, | ||
| 15 | FROM => 1, | ||
| 16 | RECV_FROM => 4, | ||
| 17 | TO => 5, | ||
| 18 | CRYPTED_PASSWORD => 6, | ||
| 19 | BUDDY_ID => 7, | ||
| 20 | ONLINE_BUDDY_NUMBER => 8, | ||
| 21 | STATUS_CODE => 10, | ||
| 22 | SESSION_ID => 11, | ||
| 23 | IS_ONLINE => 13, | ||
| 24 | MESSAGE => 14, | ||
| 25 | RECEIVING_TIME => 15, | ||
| 26 | ERROR_MESSAGE => 16, | ||
| 27 | STATUS_MESSAGE => 19, | ||
| 28 | BUSY_CODE => 47, | ||
| 29 | BUDDY_LIST => 87, | ||
| 30 | CHALLENGE_STRING => 94, | ||
| 31 | }; | ||
| 32 | |||
| 33 | sub new { | ||
| 34 | my $class = shift; | ||
| 35 | my $connection = shift; | ||
| 36 | bless { | ||
| 37 | source => '', | ||
| 38 | code => undef, | ||
| 39 | option => 0, | ||
| 40 | connection => $connection, | ||
| 41 | }, $class; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub source { | ||
| 45 | my $self = shift; | ||
| 46 | $self->{source} = shift if @_; | ||
| 47 | return $self->{source}; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _get_by_name { | ||
| 51 | my $self = shift; | ||
| 52 | my $name = shift; | ||
| 53 | my $raw_event = $self->source; | ||
| 54 | return unless exists DATA_TYPE->{$name}; | ||
| 55 | my @param = split /\xC0\x80/, $raw_event; | ||
| 56 | my @result; | ||
| 57 | for ( my $i = 0 ; $i < scalar @param ; $i += 2 ) { | ||
| 58 | my $value_type = $param[$i]; | ||
| 59 | next unless $value_type =~ /^\d+$/; | ||
| 60 | next if DATA_TYPE->{$name} != $value_type; | ||
| 61 | push @result, $param[ $i + 1 ]; | ||
| 62 | } | ||
| 63 | return scalar @result <= 1 ? $result[0] : @result; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _set_by_name { | ||
| 67 | my $self = shift; | ||
| 68 | my $name = shift; | ||
| 69 | my $value = shift; | ||
| 70 | my ($current_value) = $self->_get_by_name($name); | ||
| 71 | if ( defined $current_value ) { | ||
| 72 | my @raw_data = split /\xC0\x80/, $self->source; | ||
| 73 | my $new_raw_data; | ||
| 74 | for ( my $i = 0 ; $i < scalar @raw_data ; $i += 2 ) { | ||
| 75 | next unless $raw_data[$i] =~ /^\d+$/; | ||
| 76 | if ( DATA_TYPE->{$name} == $raw_data[$i] ) { | ||
| 77 | $raw_data[ $i + 1 ] = $value; | ||
| 78 | } | ||
| 79 | $new_raw_data .= | ||
| 80 | $raw_data[$i] | ||
| 81 | . YMSG_SEPARATOR | ||
| 82 | . $raw_data[ $i + 1 ] | ||
| 83 | . YMSG_SEPARATOR; | ||
| 84 | } | ||
| 85 | $self->source($new_raw_data); | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | my $raw_event = $self->source; | ||
| 89 | $raw_event .= | ||
| 90 | DATA_TYPE->{$name} . YMSG_SEPARATOR . $value . YMSG_SEPARATOR; | ||
| 91 | $self->source($raw_event); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub to_raw_string { | ||
| 96 | my $self = shift; | ||
| 97 | my $identifier = eval { $self->get_connection->identifier; }; | ||
| 98 | my $body = $self->source; | ||
| 99 | $body = '' unless defined $body; | ||
| 100 | |||
| 101 | my $header = pack 'a4xCx2nnNN', | ||
| 102 | YMSG_SIGNATURE, | ||
| 103 | YMSG_PROTOCOL_VERSION, | ||
| 104 | length $body, | ||
| 105 | $self->code, | ||
| 106 | $self->option, | ||
| 107 | $identifier || "\x00" x 4; | ||
| 108 | return $header . $body; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub get_connection { | ||
| 112 | my $self = shift; | ||
| 113 | $self->{connection} = shift if @_; | ||
| 114 | $self->{connection}; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub connection { | ||
| 118 | my $self = shift; | ||
| 119 | $self->get_connection(@_); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub from { | ||
| 123 | my $self = shift; | ||
| 124 | $self->_set_by_name( 'FROM', shift ) if @_; | ||
| 125 | return $self->_get_by_name('FROM'); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub body { | ||
| 129 | my $self = shift; | ||
| 130 | $self->_set_by_name( 'MESSAGE', shift ) if @_; | ||
| 131 | return $self->_get_by_name('MESSAGE'); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub to { | ||
| 135 | my $self = shift; | ||
| 136 | $self->_set_by_name( 'TO', shift ) if @_; | ||
| 137 | return $self->_get_by_name('TO'); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub option { | ||
| 141 | my $self = shift; | ||
| 142 | $self->{option} = shift if @_; | ||
| 143 | $self->{option}; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub code { | ||
| 147 | my $self = shift; | ||
| 148 | $self->{code} = shift if @_; | ||
| 149 | return $self->{code}; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub is_enable { 1 } | ||
| 153 | |||
| 154 | sub to_string { | ||
| 155 | my $self = shift; | ||
| 156 | |||
| 157 | # event handler; | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | __END__ |