1 |
root |
1.8 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
Net::Knuddels - www.knuddels.de protocol implementation. |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use Net::Knuddels; |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
RTSL. |
12 |
|
|
|
13 |
|
|
=cut |
14 |
|
|
|
15 |
root |
1.1 |
package Net::Knuddels; |
16 |
|
|
|
17 |
root |
1.3 |
use Net::Knuddels::Dictionary; |
18 |
|
|
|
19 |
root |
1.2 |
use strict; |
20 |
|
|
use utf8; |
21 |
|
|
|
22 |
root |
1.4 |
use Carp; |
23 |
|
|
use Math::BigInt; |
24 |
|
|
|
25 |
|
|
sub hash_pw($$) { |
26 |
|
|
my ($challenge, $pw) = @_; |
27 |
|
|
|
28 |
|
|
my $l1 = length $pw; |
29 |
|
|
my $l2 = length $challenge; |
30 |
|
|
|
31 |
|
|
my $k = chr ($l1 ^ ($l2 << 4)); |
32 |
|
|
|
33 |
|
|
my $l = $l1 < $l2 ? $l2 : $l1; |
34 |
|
|
|
35 |
|
|
my $xor = substr +($pw x 100) ^ ($challenge x 100) ^ ($k x 100), 0, $l; |
36 |
|
|
|
37 |
|
|
my ($i, $j); |
38 |
|
|
|
39 |
|
|
--$l; |
40 |
|
|
|
41 |
|
|
if ($l <= 17) { |
42 |
|
|
for (0 .. $l) { |
43 |
|
|
$i = $i * 3 + ord substr $xor, $l - $_; |
44 |
|
|
$j = $j * 5 + ord substr $xor, $_; |
45 |
|
|
|
46 |
|
|
$i = unpack "l", pack "L", (new Math::BigInt $i) & 0xffffffff; |
47 |
|
|
$j = unpack "l", pack "L", (new Math::BigInt $j) & 0xffffffff; |
48 |
|
|
} |
49 |
|
|
} else { |
50 |
|
|
for ($_ = $l; $_ >= 0; $_ -= int $_/19) { |
51 |
|
|
$i = $i * 5 + ord substr $xor, $_; |
52 |
|
|
$j = $j * 3 + ord substr $xor, $l - $_; |
53 |
|
|
|
54 |
|
|
$i = unpack "l", pack "L", (new Math::BigInt $i) & 0xffffffff; |
55 |
|
|
$j = unpack "l", pack "L", (new Math::BigInt $j) & 0xffffffff; |
56 |
|
|
} |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
$i ^= $j; |
60 |
|
|
|
61 |
|
|
($i & 0xffffff) ^ ($i >> 24); |
62 |
|
|
} |
63 |
|
|
|
64 |
root |
1.9 |
my %encode = reverse %$Net::Knuddels::Dictionary; |
65 |
|
|
|
66 |
|
|
my $RE_enc = join "|", keys %encode; |
67 |
|
|
|
68 |
|
|
sub encode { |
69 |
|
|
my ($msg) = @_; |
70 |
|
|
|
71 |
|
|
my $data = ""; |
72 |
|
|
|
73 |
|
|
while () { |
74 |
|
|
$data .= $encode{$1} while $msg =~ /\G($RE_enc)/cog; |
75 |
|
|
|
76 |
|
|
$msg =~ /\G./csog |
77 |
|
|
or last; |
78 |
|
|
|
79 |
|
|
$data .= $encode{"\\\\\\"} . unpack "b*", pack "v", ord $1; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
pack "b*", $data |
83 |
|
|
} |
84 |
|
|
|
85 |
root |
1.5 |
=head2 CLASS Net::Knuddels::Protocol |
86 |
|
|
|
87 |
|
|
You B<must> call the C<destroy> method of this class when you no longer |
88 |
|
|
use it, as circular references will keep the object alive otherwise. |
89 |
|
|
|
90 |
root |
1.6 |
=over 4 |
91 |
|
|
|
92 |
root |
1.8 |
=cut |
93 |
|
|
|
94 |
|
|
package Net::Knuddels::Protocol; |
95 |
|
|
|
96 |
root |
1.6 |
=item new |
97 |
|
|
|
98 |
|
|
Create a new C<Net::Knuddels::Protocol> object. |
99 |
|
|
|
100 |
root |
1.5 |
=cut |
101 |
root |
1.2 |
|
102 |
|
|
sub new { |
103 |
|
|
my $class = shift; |
104 |
|
|
|
105 |
root |
1.4 |
my %data; |
106 |
|
|
|
107 |
|
|
my $self = bless { |
108 |
|
|
@_ |
109 |
|
|
}, $class; |
110 |
|
|
|
111 |
|
|
$self->register ("(" => sub { |
112 |
root |
1.8 |
$self->{login_challenge} = $_[1]; |
113 |
|
|
$self->{login_room} = $_[2]; |
114 |
|
|
$self->feed_event ("login_info"); |
115 |
root |
1.4 |
}); |
116 |
|
|
|
117 |
|
|
$self; |
118 |
root |
1.2 |
} |
119 |
|
|
|
120 |
root |
1.6 |
=item $protocol->feed_data ($octets) |
121 |
|
|
|
122 |
|
|
Feed raw protocol data into the decoder. |
123 |
|
|
|
124 |
|
|
=cut |
125 |
|
|
|
126 |
root |
1.2 |
sub feed_data($$) { |
127 |
|
|
my ($self, $data) = @_; |
128 |
|
|
|
129 |
|
|
# split data stream into packets |
130 |
|
|
|
131 |
|
|
$data = "$self->{rbuf}$data"; |
132 |
|
|
|
133 |
|
|
while () { |
134 |
|
|
1 <= length $data or last; |
135 |
|
|
my $len = ord substr $data, 0, 1; |
136 |
|
|
|
137 |
|
|
my $skip; |
138 |
|
|
if ($len & 0x80) { |
139 |
|
|
my $tail = (($len >> 5) & 3) - 1; |
140 |
|
|
$len = ($len & 0x1f) + 1; |
141 |
|
|
|
142 |
|
|
$tail < length $data or last; |
143 |
|
|
$len += (ord substr $data, $_ + 1, 1) << ($_ * 8 + 5) |
144 |
|
|
for 0 .. $tail; |
145 |
|
|
|
146 |
|
|
$skip = 2 + $tail; |
147 |
|
|
} else { |
148 |
|
|
$skip = 1; |
149 |
|
|
$len++; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
$len + $skip <= length $data or last; |
153 |
|
|
substr $data, 0, $skip, ""; |
154 |
|
|
my $msg = substr $data, 0, $len, ""; |
155 |
|
|
|
156 |
|
|
$self->feed_msg ($msg); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
$self->{rbuf} = $data; |
160 |
|
|
} |
161 |
|
|
|
162 |
root |
1.3 |
my $RE_dec = join "|", keys %$Net::Knuddels::Dictionary; |
163 |
root |
1.1 |
|
164 |
root |
1.2 |
sub feed_msg($$) { |
165 |
|
|
my ($self, $msg) = @_; |
166 |
root |
1.1 |
|
167 |
|
|
my $bin = unpack "b*", $msg; |
168 |
|
|
my $res = ""; |
169 |
|
|
|
170 |
root |
1.9 |
while ($bin =~ /\G($RE_dec)/cog) { |
171 |
root |
1.3 |
my $frag = $Net::Knuddels::Dictionary->{$1}; |
172 |
root |
1.9 |
$frag = chr unpack "v", pack "b*", $bin =~ /\G.{16}/cg && $1 if $frag eq "\\\\\\"; |
173 |
root |
1.1 |
$res .= $frag; |
174 |
|
|
} |
175 |
root |
1.2 |
$bin =~ /\G(.*[^0].*)$/ and die "Net::Knuddels::Receiver: undecodable message tail '$1'"; |
176 |
|
|
|
177 |
|
|
$self->feed_event (split /\0/, $res); |
178 |
root |
1.1 |
} |
179 |
|
|
|
180 |
root |
1.2 |
sub feed_event($@) { |
181 |
|
|
my ($self, $type, @arg) = @_; |
182 |
root |
1.1 |
|
183 |
root |
1.2 |
for ($type, "ALL") { |
184 |
|
|
my $ev = $self->{cb}{$_}; |
185 |
|
|
$_->($type, @arg) for values %$ev; |
186 |
root |
1.1 |
} |
187 |
root |
1.2 |
} |
188 |
root |
1.1 |
|
189 |
root |
1.6 |
=item $protocol->register ($type => $callback) |
190 |
|
|
|
191 |
|
|
Register a callback for events of type C<$type>, which is either the name |
192 |
|
|
of a low-level event sent by the server (such as "k" for dialog box) or |
193 |
root |
1.8 |
the name of a generated event, such as C<login_info>. |
194 |
root |
1.6 |
|
195 |
|
|
=cut |
196 |
|
|
|
197 |
root |
1.2 |
sub register { |
198 |
|
|
my ($self, $type, $cb) = @_; |
199 |
root |
1.1 |
|
200 |
root |
1.2 |
$self->{cb}{$type}{$cb} = $cb; |
201 |
root |
1.1 |
} |
202 |
|
|
|
203 |
root |
1.8 |
=item $protocol->destroy |
204 |
|
|
|
205 |
root |
1.9 |
I<MUST> be called to destroy the object, otherwise it will leak (no automatic cleanup). |
206 |
root |
1.8 |
|
207 |
|
|
=cut |
208 |
|
|
|
209 |
root |
1.5 |
sub destroy { |
210 |
|
|
my ($self) = @_; |
211 |
|
|
|
212 |
|
|
delete $self->{cb}; |
213 |
|
|
} |
214 |
|
|
|
215 |
root |
1.6 |
=back |
216 |
|
|
|
217 |
root |
1.8 |
=head2 CLASS Net::Knuddels::Client |
218 |
|
|
|
219 |
root |
1.9 |
Implement a Knuddels client connection. |
220 |
|
|
|
221 |
root |
1.8 |
=over 4 |
222 |
|
|
|
223 |
|
|
=cut |
224 |
|
|
|
225 |
|
|
package Net::Knuddels::Client; |
226 |
|
|
|
227 |
root |
1.9 |
=item new Net::Knuddels::Client [IO::Socket::new arguments] |
228 |
|
|
|
229 |
|
|
Create a new client connection. |
230 |
|
|
|
231 |
|
|
=cut |
232 |
|
|
|
233 |
|
|
use IO::Socket::INET; |
234 |
|
|
|
235 |
|
|
sub new { |
236 |
|
|
my ($class, @arg) = @_; |
237 |
|
|
|
238 |
|
|
my $fh = new IO::Socket::INET @arg |
239 |
|
|
or Carp::croak "Net::Knuddels::Client::new: $!"; |
240 |
|
|
|
241 |
|
|
my $self = bless { |
242 |
|
|
fh => $fh, |
243 |
|
|
proto => (new Net::Knuddels::Protocol), |
244 |
|
|
}, $class; |
245 |
|
|
|
246 |
|
|
syswrite $fh, "\0"; |
247 |
|
|
|
248 |
|
|
$self |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
=item $client->fh |
252 |
|
|
|
253 |
|
|
Return the fh used for communications. You are responsible for calling C<< |
254 |
|
|
$client->fh_ready >> whenever the fh becomes ready for reading. |
255 |
|
|
|
256 |
|
|
=cut |
257 |
|
|
|
258 |
|
|
sub fh { |
259 |
|
|
$_[0]->{fh} |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
=item $client->command ($type => @args) |
263 |
|
|
|
264 |
|
|
Send a message of type C<$type> and the given arguments to the server. |
265 |
|
|
|
266 |
|
|
=cut |
267 |
|
|
|
268 |
|
|
sub command { |
269 |
|
|
my ($self, $type, @args) = @_; |
270 |
|
|
|
271 |
|
|
syswrite $self->{fh}, Net::Knuddels::encode join "\0", $type, @args; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
=item $client->login ($url, $unknown) |
275 |
|
|
|
276 |
|
|
Send a 't' message. The default for C<$url> is |
277 |
|
|
C<http://www.knuddels.de/applet.html?v=86a&c=0> and C<$unknown> is C<6>. |
278 |
|
|
|
279 |
|
|
=cut |
280 |
|
|
|
281 |
|
|
sub login { |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
=item $client->register ($type => $cb) |
285 |
|
|
|
286 |
|
|
See L<Net::Knuddels::Protocol::register>. |
287 |
|
|
|
288 |
|
|
=cut |
289 |
|
|
|
290 |
|
|
sub register { |
291 |
|
|
my ($self, $type, $cb) = @_; |
292 |
|
|
|
293 |
|
|
$self->{protocol}->register ($type, $cb); |
294 |
|
|
} |
295 |
|
|
|
296 |
root |
1.8 |
=back |
297 |
|
|
|
298 |
|
|
=head1 AUTHOR |
299 |
|
|
|
300 |
|
|
Marc Lehmann <pcg@goof.com> |
301 |
|
|
http://home.schmorp.de/ |
302 |
|
|
|
303 |
root |
1.6 |
=cut |
304 |
|
|
|
305 |
root |
1.2 |
1; |
306 |
|
|
|