ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-Knuddels/Net/Knuddels.pm
Revision: 1.8
Committed: Wed Jan 12 20:37:07 2005 UTC (19 years, 4 months ago) by root
Branch: MAIN
Changes since 1.7: +43 -6 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 =head2 CLASS Net::Knuddels::Protocol
65
66 You B<must> call the C<destroy> method of this class when you no longer
67 use it, as circular references will keep the object alive otherwise.
68
69 =over 4
70
71 =cut
72
73 package Net::Knuddels::Protocol;
74
75 =item new
76
77 Create a new C<Net::Knuddels::Protocol> object.
78
79 =cut
80
81 sub new {
82 my $class = shift;
83
84 my %data;
85
86 my $self = bless {
87 @_
88 }, $class;
89
90 $self->register ("(" => sub {
91 $self->{login_challenge} = $_[1];
92 $self->{login_room} = $_[2];
93 $self->feed_event ("login_info");
94 });
95
96 $self;
97 }
98
99 =item $protocol->feed_data ($octets)
100
101 Feed raw protocol data into the decoder.
102
103 =cut
104
105 sub feed_data($$) {
106 my ($self, $data) = @_;
107
108 # split data stream into packets
109
110 $data = "$self->{rbuf}$data";
111
112 while () {
113 1 <= length $data or last;
114 my $len = ord substr $data, 0, 1;
115
116 my $skip;
117 if ($len & 0x80) {
118 my $tail = (($len >> 5) & 3) - 1;
119 $len = ($len & 0x1f) + 1;
120
121 $tail < length $data or last;
122 $len += (ord substr $data, $_ + 1, 1) << ($_ * 8 + 5)
123 for 0 .. $tail;
124
125 $skip = 2 + $tail;
126 } else {
127 $skip = 1;
128 $len++;
129 }
130
131 $len + $skip <= length $data or last;
132 substr $data, 0, $skip, "";
133 my $msg = substr $data, 0, $len, "";
134
135 $self->feed_msg ($msg);
136 }
137
138 $self->{rbuf} = $data;
139 }
140
141 my $RE_dec = join "|", keys %$Net::Knuddels::Dictionary;
142
143 sub feed_msg($$) {
144 my ($self, $msg) = @_;
145
146 my $bin = unpack "b*", $msg;
147 my $res = "";
148
149 while ($bin =~ /\G($RE_dec)/cmog) {
150 my $frag = $Net::Knuddels::Dictionary->{$1};
151 $frag = pack "b*", $bin =~ /\G.{16}/cmg && $1 if $frag eq "\\\\\\";
152 $res .= $frag;
153 }
154 $bin =~ /\G(.*[^0].*)$/ and die "Net::Knuddels::Receiver: undecodable message tail '$1'";
155
156 $self->feed_event (split /\0/, $res);
157 }
158
159 sub feed_event($@) {
160 my ($self, $type, @arg) = @_;
161
162 for ($type, "ALL") {
163 my $ev = $self->{cb}{$_};
164 $_->($type, @arg) for values %$ev;
165 }
166 }
167
168 =item $protocol->register ($type => $callback)
169
170 Register a callback for events of type C<$type>, which is either the name
171 of a low-level event sent by the server (such as "k" for dialog box) or
172 the name of a generated event, such as C<login_info>.
173
174 =cut
175
176 sub register {
177 my ($self, $type, $cb) = @_;
178
179 $self->{cb}{$type}{$cb} = $cb;
180 }
181
182 =item $protocol->destroy
183
184 I<MUST> be called to destroy teh object, otherwise it will leak (no automatic cleanup).
185
186 =cut
187
188 sub destroy {
189 my ($self) = @_;
190
191 delete $self->{cb};
192 }
193
194 =back
195
196 =head2 CLASS Net::Knuddels::Client
197
198 =over 4
199
200 =cut
201
202 package Net::Knuddels::Client;
203
204 =back
205
206 =head1 AUTHOR
207
208 Marc Lehmann <pcg@goof.com>
209 http://home.schmorp.de/
210
211 =cut
212
213 1;
214