=head1 NAME Net::Knuddels - www.knuddels.de protocol implementation. =head1 SYNOPSIS use Net::Knuddels; =head1 DESCRIPTION RTSL. =cut package Net::Knuddels; use Net::Knuddels::Dictionary; use strict; use utf8; use Carp; use Math::BigInt; sub hash_pw($$) { my ($challenge, $pw) = @_; my $l1 = length $pw; my $l2 = length $challenge; my $k = chr ($l1 ^ ($l2 << 4)); my $l = $l1 < $l2 ? $l2 : $l1; my $xor = substr +($pw x 100) ^ ($challenge x 100) ^ ($k x 100), 0, $l; my ($i, $j); --$l; if ($l <= 17) { for (0 .. $l) { $i = $i * 3 + ord substr $xor, $l - $_; $j = $j * 5 + ord substr $xor, $_; $i = unpack "l", pack "L", (new Math::BigInt $i) & 0xffffffff; $j = unpack "l", pack "L", (new Math::BigInt $j) & 0xffffffff; } } else { for ($_ = $l; $_ >= 0; $_ -= int $_/19) { $i = $i * 5 + ord substr $xor, $_; $j = $j * 3 + ord substr $xor, $l - $_; $i = unpack "l", pack "L", (new Math::BigInt $i) & 0xffffffff; $j = unpack "l", pack "L", (new Math::BigInt $j) & 0xffffffff; } } $i ^= $j; ($i & 0xffffff) ^ ($i >> 24); } my $RE_dec = join "|", keys %$Net::Knuddels::Dictionary; sub decode { my $bin = unpack "b*", $_[0]; my $res = ""; while ($bin =~ /\G($RE_dec)/cog) { my $frag = $Net::Knuddels::Dictionary->{$1}; $frag = chr unpack "v", pack "b*", $bin =~ /\G.{16}/cg && $1 if $frag eq "\\\\\\"; $res .= $frag; } $bin =~ /\G(.*[^0].*)$/ and die "Net::Knuddels::Receiver: undecodable message tail '$1'"; $res } my %encode = reverse %$Net::Knuddels::Dictionary; my $RE_enc = join "|", map quotemeta, sort { (length $b) <=> (length $a) } keys %encode; sub encode($) { my ($msg) = @_; my $data = ""; while () { $data .= $encode{$1} while $msg =~ /\G($RE_enc)/cog; $msg =~ /\G./csog or last; $data .= $encode{"\\\\\\"} . unpack "b*", pack "v", ord $1; } pack "b*", $data } =head2 CLASS Net::Knuddels::Protocol You B call the C method of this class when you no longer use it, as circular references will keep the object alive otherwise. =over 4 =cut package Net::Knuddels::Protocol; =item new Create a new C object. =cut sub new { my $class = shift; my %data; my $self = bless { @_ }, $class; $self->register ("(" => sub { $self->{login_challenge} = $_[1]; $self->{login_room} = $_[2]; $self->feed_event ("login_info"); }); $self; } =item $protocol->feed_data ($octets) Feed raw protocol data into the decoder. =cut sub feed_data($$) { my ($self, $data) = @_; # split data stream into packets $data = "$self->{rbuf}$data"; while () { 1 <= length $data or last; my $len = ord substr $data, 0, 1; my $skip; if ($len & 0x80) { my $tail = (($len >> 5) & 3) - 1; $len = ($len & 0x1f) + 1; $tail < length $data or last; $len += (ord substr $data, $_ + 1, 1) << ($_ * 8 + 5) for 0 .. $tail; $skip = 2 + $tail; } else { $skip = 1; $len++; } $len + $skip <= length $data or last; substr $data, 0, $skip, ""; my $msg = substr $data, 0, $len, ""; $self->feed_msg ($msg); } $self->{rbuf} = $data; } sub feed_msg($$) { my ($self, $msg) = @_; $self->feed_event (split /\0/, Net::Knuddels::decode $msg); } sub feed_event($@) { my ($self, $type, @arg) = @_; for ($type, "ALL") { my $ev = $self->{cb}{$_}; $_->($type, @arg) for values %$ev; } } =item $msg = $protocol->encode_msg (@strings) Join the strings with C<\0>, encode the result into a protocol packet and return it. =cut sub encode_msg($@) { my ($self, @args) = @_; my $msg = Net::Knuddels::encode join "\0", @args; my $len = (length $msg) - 1; if ($len < 0x80) { (chr $len) . $msg } else { (chr 0x80 | 0x40 | ($len & 0x1f)) . (chr +($len >> 5) % 0xff) . (chr +($len >> 13) % 0xff) . $msg } } =item $protocol->register ($type => $callback) Register a callback for events of type C<$type>, which is either the name of a low-level event sent by the server (such as "k" for dialog box) or the name of a generated event, such as C. =cut sub register { my ($self, $type, $cb) = @_; $self->{cb}{$type}{$cb} = $cb; } =item $protocol->destroy I be called to destroy the object, otherwise it will leak (no automatic cleanup). =cut sub destroy { my ($self) = @_; delete $self->{cb}; } =back =head2 CLASS Net::Knuddels::Client Implement a Knuddels client connection. =over 4 =cut package Net::Knuddels::Client; =item new Net::Knuddels::Client [IO::Socket::new arguments] Create a new client connection. =cut use IO::Socket::INET; sub new { my ($class, @arg) = @_; my $fh = new IO::Socket::INET @arg or Carp::croak "Net::Knuddels::Client::new: $!"; my $self = bless { fh => $fh, proto => (new Net::Knuddels::Protocol), }, $class; syswrite $fh, "\0"; $self } =item $client->fh Return the fh used for communications. You are responsible for calling C<< $client->fh_ready >> whenever the fh becomes ready for reading. =cut sub fh { $_[0]->{fh} } =item $client->command ($type => @args) Send a message of type C<$type> and the given arguments to the server. =cut sub command { my ($self, $type, @args) = @_; syswrite $self->{fh}, Net::Knuddels::encode join "\0", $type, @args; } =item $client->login ($url, $unknown) Send a 't' message. The default for C<$url> is C and C<$unknown> is C<6>. =cut sub login { } =item $client->register ($type => $cb) See L. =cut sub register { my ($self, $type, $cb) = @_; $self->{protocol}->register ($type, $cb); } =back =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ =cut 1;