1 |
pcg |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
# lots of things have been hardcoded. search for #d# to find the places |
4 |
|
|
|
5 |
|
|
use KGS::Protocol; |
6 |
|
|
use KGS::Listener::Debug; |
7 |
|
|
|
8 |
|
|
use IO::Socket::INET; |
9 |
|
|
use Event; |
10 |
|
|
|
11 |
|
|
use Carp; |
12 |
|
|
|
13 |
|
|
$SIG{QUIT} = sub { Carp::confess "SIGQUIT" }; |
14 |
|
|
|
15 |
|
|
print "Welcome to kgsclient, a simple text-only kgs chat client, barely an example.\n"; |
16 |
|
|
print "Connecting...\n"; |
17 |
|
|
|
18 |
|
|
my $kgs; |
19 |
|
|
|
20 |
|
|
package kgs; |
21 |
|
|
|
22 |
|
|
use base KGS::Listener; |
23 |
|
|
|
24 |
|
|
my $conn = new KGS::Protocol; |
25 |
|
|
|
26 |
|
|
sub new { |
27 |
|
|
my $class = shift; |
28 |
|
|
my $self = bless { @_ }, $class; |
29 |
|
|
|
30 |
pcg |
1.2 |
my $sock = new IO::Socket::INET PeerHost => KGS::Protocol::KGSHOST, PeerPort => KGS::Protocol::KGSPORT |
31 |
pcg |
1.1 |
or die "connect: $!"; |
32 |
|
|
|
33 |
|
|
$sock->blocking (1); |
34 |
|
|
$conn->handshake ($sock); |
35 |
|
|
|
36 |
|
|
$self->listen ($conn, "any"); |
37 |
|
|
|
38 |
|
|
# Listener for kgs data |
39 |
|
|
$self->{w} = Event->io (fd => $sock, poll => 'r', cb => sub { |
40 |
|
|
my $len = sysread $sock, my $buf, 16384; |
41 |
|
|
if ($len) { |
42 |
|
|
$conn->feed_data($buf); |
43 |
|
|
} elsif (defined $len || (!$!{EINTR} and !$!{EAGAIN})) { |
44 |
|
|
print "disconnected\n"; |
45 |
|
|
Event::unloop; |
46 |
|
|
} |
47 |
|
|
}); |
48 |
|
|
|
49 |
|
|
# Listener for keyboard input. Needs a tty |
50 |
|
|
$self->{kbd} = Event->io (fd => \*STDIN, poll => 'r', cb => sub { |
51 |
|
|
$self->inject_cmd (<STDIN>); |
52 |
|
|
}); |
53 |
|
|
|
54 |
|
|
$conn->login ("kgsclient 0.0", $self->{user}, delete $self->{password}); |
55 |
|
|
|
56 |
|
|
$self; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
sub inject_login { |
60 |
|
|
my ($self, $msg) = @_; |
61 |
|
|
print "$msg->{message}\n"; |
62 |
|
|
|
63 |
|
|
$self->send (join_room => |
64 |
|
|
channel => 13, |
65 |
|
|
user => { name => $self->{user} }); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
sub inject_msg_room { |
69 |
|
|
my ($self, $msg) = @_; |
70 |
|
|
print "$msg->{name}#$msg->{channel}: $msg->{message}\n"; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
sub inject_any { |
74 |
|
|
my ($self, $msg) = @_; |
75 |
|
|
print "DEBUG: $msg->{type}#$msg->{channel}"; |
76 |
|
|
for (sort keys %$msg) { |
77 |
|
|
print " $_<$msg->{$_}>"; |
78 |
|
|
} |
79 |
|
|
print "\n"; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
# user input |
83 |
|
|
sub inject_cmd { |
84 |
|
|
my ($self, $cmd) = @_; |
85 |
|
|
chomp $cmd; |
86 |
|
|
print ">>>>> $cmd\n"; |
87 |
|
|
$self->send (msg_room => |
88 |
|
|
channel => 13, |
89 |
|
|
name => $self->{user}, |
90 |
|
|
message => $cmd); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
$kgs = new kgs user => "kgsclient", undef; #d# |
94 |
|
|
|
95 |
|
|
Event::loop; |
96 |
|
|
|
97 |
|
|
1; |
98 |
|
|
|
99 |
|
|
|