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

# User Rev Content
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.5 =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 root 1.6 =over 4
70    
71 root 1.8 =cut
72    
73     package Net::Knuddels::Protocol;
74    
75 root 1.6 =item new
76    
77     Create a new C<Net::Knuddels::Protocol> object.
78    
79 root 1.5 =cut
80 root 1.2
81     sub new {
82     my $class = shift;
83    
84 root 1.4 my %data;
85    
86     my $self = bless {
87     @_
88     }, $class;
89    
90     $self->register ("(" => sub {
91 root 1.8 $self->{login_challenge} = $_[1];
92     $self->{login_room} = $_[2];
93     $self->feed_event ("login_info");
94 root 1.4 });
95    
96     $self;
97 root 1.2 }
98    
99 root 1.6 =item $protocol->feed_data ($octets)
100    
101     Feed raw protocol data into the decoder.
102    
103     =cut
104    
105 root 1.2 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 root 1.3 my $RE_dec = join "|", keys %$Net::Knuddels::Dictionary;
142 root 1.1
143 root 1.2 sub feed_msg($$) {
144     my ($self, $msg) = @_;
145 root 1.1
146     my $bin = unpack "b*", $msg;
147     my $res = "";
148    
149 root 1.2 while ($bin =~ /\G($RE_dec)/cmog) {
150 root 1.3 my $frag = $Net::Knuddels::Dictionary->{$1};
151 root 1.7 $frag = pack "b*", $bin =~ /\G.{16}/cmg && $1 if $frag eq "\\\\\\";
152 root 1.1 $res .= $frag;
153     }
154 root 1.2 $bin =~ /\G(.*[^0].*)$/ and die "Net::Knuddels::Receiver: undecodable message tail '$1'";
155    
156     $self->feed_event (split /\0/, $res);
157 root 1.1 }
158    
159 root 1.2 sub feed_event($@) {
160     my ($self, $type, @arg) = @_;
161 root 1.1
162 root 1.2 for ($type, "ALL") {
163     my $ev = $self->{cb}{$_};
164     $_->($type, @arg) for values %$ev;
165 root 1.1 }
166 root 1.2 }
167 root 1.1
168 root 1.6 =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 root 1.8 the name of a generated event, such as C<login_info>.
173 root 1.6
174     =cut
175    
176 root 1.2 sub register {
177     my ($self, $type, $cb) = @_;
178 root 1.1
179 root 1.2 $self->{cb}{$type}{$cb} = $cb;
180 root 1.1 }
181    
182 root 1.8 =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 root 1.5 sub destroy {
189     my ($self) = @_;
190    
191     delete $self->{cb};
192     }
193    
194 root 1.6 =back
195    
196 root 1.8 =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 root 1.6 =cut
212    
213 root 1.2 1;
214