1 |
root |
1.1 |
package Net::Knuddels; |
2 |
|
|
|
3 |
root |
1.3 |
use Net::Knuddels::Dictionary; |
4 |
|
|
|
5 |
root |
1.2 |
use strict; |
6 |
|
|
use utf8; |
7 |
|
|
|
8 |
root |
1.4 |
use Carp; |
9 |
|
|
use Math::BigInt; |
10 |
|
|
|
11 |
|
|
sub hash_pw($$) { |
12 |
|
|
my ($challenge, $pw) = @_; |
13 |
|
|
|
14 |
|
|
my $l1 = length $pw; |
15 |
|
|
my $l2 = length $challenge; |
16 |
|
|
|
17 |
|
|
my $k = chr ($l1 ^ ($l2 << 4)); |
18 |
|
|
|
19 |
|
|
my $l = $l1 < $l2 ? $l2 : $l1; |
20 |
|
|
|
21 |
|
|
my $xor = substr +($pw x 100) ^ ($challenge x 100) ^ ($k x 100), 0, $l; |
22 |
|
|
|
23 |
|
|
my ($i, $j); |
24 |
|
|
|
25 |
|
|
--$l; |
26 |
|
|
|
27 |
|
|
if ($l <= 17) { |
28 |
|
|
for (0 .. $l) { |
29 |
|
|
$i = $i * 3 + ord substr $xor, $l - $_; |
30 |
|
|
$j = $j * 5 + ord substr $xor, $_; |
31 |
|
|
|
32 |
|
|
$i = unpack "l", pack "L", (new Math::BigInt $i) & 0xffffffff; |
33 |
|
|
$j = unpack "l", pack "L", (new Math::BigInt $j) & 0xffffffff; |
34 |
|
|
} |
35 |
|
|
} else { |
36 |
|
|
for ($_ = $l; $_ >= 0; $_ -= int $_/19) { |
37 |
|
|
$i = $i * 5 + ord substr $xor, $_; |
38 |
|
|
$j = $j * 3 + ord substr $xor, $l - $_; |
39 |
|
|
|
40 |
|
|
$i = unpack "l", pack "L", (new Math::BigInt $i) & 0xffffffff; |
41 |
|
|
$j = unpack "l", pack "L", (new Math::BigInt $j) & 0xffffffff; |
42 |
|
|
} |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
$i ^= $j; |
46 |
|
|
|
47 |
|
|
($i & 0xffffff) ^ ($i >> 24); |
48 |
|
|
} |
49 |
|
|
|
50 |
root |
1.5 |
package Net::Knuddels::Protocol; |
51 |
|
|
|
52 |
|
|
=head2 CLASS Net::Knuddels::Protocol |
53 |
|
|
|
54 |
|
|
You B<must> call the C<destroy> method of this class when you no longer |
55 |
|
|
use it, as circular references will keep the object alive otherwise. |
56 |
|
|
|
57 |
root |
1.6 |
=over 4 |
58 |
|
|
|
59 |
|
|
=item new |
60 |
|
|
|
61 |
|
|
Create a new C<Net::Knuddels::Protocol> object. |
62 |
|
|
|
63 |
root |
1.5 |
=cut |
64 |
root |
1.2 |
|
65 |
|
|
sub new { |
66 |
|
|
my $class = shift; |
67 |
|
|
|
68 |
root |
1.4 |
my %data; |
69 |
|
|
|
70 |
|
|
my $self = bless { |
71 |
|
|
@_ |
72 |
|
|
}, $class; |
73 |
|
|
|
74 |
|
|
$self->register ("(" => sub { |
75 |
|
|
$self->{challenge} = $_[1]; |
76 |
|
|
$self->{room} = $_[2]; |
77 |
|
|
$self->feed_event ("connected"); |
78 |
|
|
}); |
79 |
|
|
|
80 |
|
|
$self; |
81 |
root |
1.2 |
} |
82 |
|
|
|
83 |
root |
1.6 |
=item $protocol->feed_data ($octets) |
84 |
|
|
|
85 |
|
|
Feed raw protocol data into the decoder. |
86 |
|
|
|
87 |
|
|
=cut |
88 |
|
|
|
89 |
root |
1.2 |
sub feed_data($$) { |
90 |
|
|
my ($self, $data) = @_; |
91 |
|
|
|
92 |
|
|
# split data stream into packets |
93 |
|
|
|
94 |
|
|
$data = "$self->{rbuf}$data"; |
95 |
|
|
|
96 |
|
|
while () { |
97 |
|
|
1 <= length $data or last; |
98 |
|
|
my $len = ord substr $data, 0, 1; |
99 |
|
|
|
100 |
|
|
my $skip; |
101 |
|
|
if ($len & 0x80) { |
102 |
|
|
my $tail = (($len >> 5) & 3) - 1; |
103 |
|
|
$len = ($len & 0x1f) + 1; |
104 |
|
|
|
105 |
|
|
$tail < length $data or last; |
106 |
|
|
$len += (ord substr $data, $_ + 1, 1) << ($_ * 8 + 5) |
107 |
|
|
for 0 .. $tail; |
108 |
|
|
|
109 |
|
|
$skip = 2 + $tail; |
110 |
|
|
} else { |
111 |
|
|
$skip = 1; |
112 |
|
|
$len++; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
$len + $skip <= length $data or last; |
116 |
|
|
substr $data, 0, $skip, ""; |
117 |
|
|
my $msg = substr $data, 0, $len, ""; |
118 |
|
|
|
119 |
|
|
$self->feed_msg ($msg); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
$self->{rbuf} = $data; |
123 |
|
|
} |
124 |
|
|
|
125 |
root |
1.3 |
my $RE_dec = join "|", keys %$Net::Knuddels::Dictionary; |
126 |
root |
1.1 |
|
127 |
root |
1.2 |
sub feed_msg($$) { |
128 |
|
|
my ($self, $msg) = @_; |
129 |
root |
1.1 |
|
130 |
|
|
my $bin = unpack "b*", $msg; |
131 |
|
|
my $res = ""; |
132 |
|
|
|
133 |
root |
1.2 |
while ($bin =~ /\G($RE_dec)/cmog) { |
134 |
root |
1.3 |
my $frag = $Net::Knuddels::Dictionary->{$1}; |
135 |
root |
1.6 |
$frag = pack "b*", $bin =~ /\G.{16}/cmg ? && $1 if $frag eq "\\\\\\"; |
136 |
root |
1.1 |
$res .= $frag; |
137 |
|
|
} |
138 |
root |
1.2 |
$bin =~ /\G(.*[^0].*)$/ and die "Net::Knuddels::Receiver: undecodable message tail '$1'"; |
139 |
|
|
|
140 |
|
|
$self->feed_event (split /\0/, $res); |
141 |
root |
1.1 |
} |
142 |
|
|
|
143 |
root |
1.2 |
sub feed_event($@) { |
144 |
|
|
my ($self, $type, @arg) = @_; |
145 |
root |
1.1 |
|
146 |
root |
1.2 |
for ($type, "ALL") { |
147 |
|
|
my $ev = $self->{cb}{$_}; |
148 |
|
|
$_->($type, @arg) for values %$ev; |
149 |
root |
1.1 |
} |
150 |
root |
1.2 |
} |
151 |
root |
1.1 |
|
152 |
root |
1.6 |
=item $protocol->register ($type => $callback) |
153 |
|
|
|
154 |
|
|
Register a callback for events of type C<$type>, which is either the name |
155 |
|
|
of a low-level event sent by the server (such as "k" for dialog box) or |
156 |
|
|
the name of a generated event, such as C<connected>. |
157 |
|
|
|
158 |
|
|
=cut |
159 |
|
|
|
160 |
root |
1.2 |
sub register { |
161 |
|
|
my ($self, $type, $cb) = @_; |
162 |
root |
1.1 |
|
163 |
root |
1.2 |
$self->{cb}{$type}{$cb} = $cb; |
164 |
root |
1.1 |
} |
165 |
|
|
|
166 |
root |
1.5 |
sub destroy { |
167 |
|
|
my ($self) = @_; |
168 |
|
|
|
169 |
|
|
delete $self->{cb}; |
170 |
|
|
} |
171 |
|
|
|
172 |
root |
1.6 |
=back |
173 |
|
|
|
174 |
|
|
=cut |
175 |
|
|
|
176 |
root |
1.2 |
1; |
177 |
|
|
|