#!/usr/bin/perl # lots of things have been hardcoded. search for #d# to find the places use KGS::Protocol; use KGS::Listener::Debug; use IO::Socket::INET; use Event; use Carp; $SIG{QUIT} = sub { Carp::confess "SIGQUIT" }; print "Welcome to kgsclient, a simple text-only kgs chat client, barely an example.\n"; print "Connecting...\n"; my $kgs; package kgs; use base KGS::Listener; my $conn = new KGS::Protocol; sub new { my $class = shift; my $self = bless { @_ }, $class; my $sock = new IO::Socket::INET PeerHost => KGS::Protocol::KGSHOST, PeerPort => KGS::Protocol::KGSPORT or die "connect: $!"; $sock->blocking (1); $conn->handshake ($sock); $self->listen ($conn, "any"); # Listener for kgs data $self->{w} = Event->io (fd => $sock, poll => 'r', cb => sub { my $len = sysread $sock, my $buf, 16384; if ($len) { $conn->feed_data($buf); } elsif (defined $len || (!$!{EINTR} and !$!{EAGAIN})) { print "disconnected\n"; Event::unloop; } }); # Listener for keyboard input. Needs a tty $self->{kbd} = Event->io (fd => \*STDIN, poll => 'r', cb => sub { $self->inject_cmd (); }); $conn->login ("kgsclient 0.0", $self->{user}, delete $self->{password}); $self; } sub inject_login { my ($self, $msg) = @_; print "$msg->{message}\n"; $self->send (join_room => channel => 13, user => { name => $self->{user} }); } sub inject_msg_room { my ($self, $msg) = @_; print "$msg->{name}#$msg->{channel}: $msg->{message}\n"; } sub inject_any { my ($self, $msg) = @_; print "DEBUG: $msg->{type}#$msg->{channel}"; for (sort keys %$msg) { print " $_<$msg->{$_}>"; } print "\n"; } # user input sub inject_cmd { my ($self, $cmd) = @_; chomp $cmd; print ">>>>> $cmd\n"; $self->send (msg_room => channel => 13, name => $self->{user}, message => $cmd); } $kgs = new kgs user => "kgsclient", undef; #d# Event::loop; 1;