ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-Knuddels/Net/Knuddels.pm
Revision: 1.10
Committed: Thu Jan 13 00:51:18 2005 UTC (19 years, 4 months ago) by root
Branch: MAIN
Changes since 1.9: +19 -15 lines
Log Message:
*** empty log message ***

File Contents

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