YMSG/Event.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 196 CC 20 SCAR 25.6
CC
20
Cov
0%
_get_by_name
42.0
_get_multiple_by_name
20.0
_set_by_name
20.0
CRAP 12.9
1 package Net::YMSG::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 DATA_TYPE => {
11 ID => 0,
12 NICKNAME => 1,
13 PROPOSERS_ID => 3,
14 FROM => 4,
15 TO => 5,
16 CRYPTED_PASSWORD => 6,
17 BUDDY_ID => 7,
18 ONLINE_BUDDY_NUMBER => 8,
19 STATUS_CODE => 10,
20 SESSION_ID => 11,
21 IS_ONLINE => 13,
22 MESSAGE => 14,
23 RECEIVING_TIME => 15,
24 ERROR_MESSAGE => 16,
25 STATUS_MESSAGE => 19,
26 BUSY_CODE => 47,
27 BUDDY_LIST => 87,
28 CHALLENGE_STRING => 94,
29 CHATFROM => 109,
30 CHATMESSAGE => 117,
31 };
32  
33  
34 sub new
35 {
36 my $class = shift;
37 my $connection = shift;
38 bless {
39 source => '',
40 code => undef,
41 option => 0,
42 connection => $connection,
43 }, $class;
44 }
45  
46  
47 sub source
48 {
49 my $self = shift;
50 $self->{source} = shift if @_;
51 return $self->{source};
52 }
53  
54 sub _get_multiple_by_name
55 {
56 my $self = shift;
57 my $name = shift;
58 my $raw_event = $self->source;
59 return unless exists DATA_TYPE->{$name};
60 my @param = split /\xC0\x80/, $raw_event;
61 my @result;
62 for (my $i=0; $i < scalar @param; $i+=2) {
63 my $value_type = $param[$i];
64 next unless $value_type =~ /^\d+$/;
65 next if DATA_TYPE->{$name} != $value_type;
66 push @result, $param[$i+1];
67 }
68 return join("\x80",@result);
69  
70 }
71  
72  
73  
74 sub _get_by_name
75 {
76 my $self = shift;
77 my $name = shift;
78 my $raw_event = $self->source;
79  
80 return unless exists DATA_TYPE->{$name};
81 my @param = split /\xC0\x80/, $raw_event;
82 my @result;
83 for (my $i=0; $i < scalar @param; $i+=2) {
84 my $value_type = $param[$i];
85 next unless $value_type =~ /^\d+$/;
86 next if DATA_TYPE->{$name} != $value_type;
87 push @result, $param[$i+1];
88 }
89 if((DATA_TYPE->{$name} == 14 || DATA_TYPE->{$name} == 4) && $#result >=1 ) {
90 return 'Multiple';
91 }
92 else {
93 return scalar @result <= 1 ?
94 $result[0] : @result;
95 }
96 }
97  
98  
99 sub _set_by_name
100 {
101 my $self = shift;
102 my $name = shift;
103 my $value = shift;
104  
105 my ($current_value) = $self->_get_by_name($name);
106 if (defined $current_value) {
107 my @raw_data = split /\xC0\x80/, $self->source;
108 my $new_raw_data;
109 for (my $i=0; $i < scalar @raw_data; $i+=2) {
110 next unless $raw_data[$i] =~ /^\d+$/;
111 if (DATA_TYPE->{$name} == $raw_data[$i]) {
112 $raw_data[$i+1] = $value;
113 }
114 $new_raw_data .= $raw_data[$i]. YMSG_SEPARATOR.
115 $raw_data[$i+1]. YMSG_SEPARATOR;
116 }
117 $self->source($new_raw_data);
118 } else {
119 my $raw_event = $self->source;
120 $raw_event .= DATA_TYPE->{$name}. YMSG_SEPARATOR.
121 $value. YMSG_SEPARATOR;
122 $self->source($raw_event);
123 }
124 }
125  
126  
127 sub to_raw_string
128 {
129 my $self = shift;
130 my $identifier = eval { $self->get_connection->identifier; };
131 my $body = $self->source;
132 $body = '' unless defined $body;
133  
134 my $header = pack 'a4Cx3nnNN',
135 YMSG_SIGNATURE,
136 9,
137 length $body,
138 $self->code,
139 $self->option,
140 $identifier || "\x00" x 4;
141 return $header. $body;
142 }
143  
144  
145 sub get_connection
146 {
147 my $self = shift;
148 $self->{connection} = shift if @_;
149 $self->{connection};
150 }
151  
152 sub connection
153 {
154 my $self = shift;
155 $self->get_connection(@_);
156 }
157  
158  
159 sub from
160 {
161 my $self = shift;
162 $self->_set_by_name('FROM', shift) if @_;
163 return $self->_get_by_name('FROM');
164 }
165  
166  
167 sub body
168 {
169 my $self = shift;
170 $self->_set_by_name('MESSAGE', shift) if @_;
171 return $self->_get_by_name('MESSAGE');
172 }
173  
174  
175 sub to
176 {
177 my $self = shift;
178 $self->_set_by_name('TO', shift) if @_;
179 return $self->_get_by_name('TO');
180 }
181  
182  
183 sub option
184 {
185 my $self = shift;
186 $self->{option} = shift if @_;
187 $self->{option};
188 }
189  
190  
191 sub code
192 {
193 my $self = shift;
194 $self->{code} = shift if @_;
195 return $self->{code};
196 }
197  
198  
199 sub is_enable { 1 }
200  
201 sub to_string
202 {
203 my $self = shift;
204 # event handler;
205 }
206  
207 1;
208 __END__