1 |
root |
1.2 |
#!/opt/bin/perl |
2 |
|
|
|
3 |
root |
1.8 |
# Usage: ./chat_client nickname optional-servername |
4 |
|
|
# implement a chat client using "traditional message passing" |
5 |
|
|
|
6 |
root |
1.4 |
use common::sense; |
7 |
elmex |
1.1 |
use AnyEvent::MP; |
8 |
|
|
|
9 |
root |
1.13 |
my $nick = shift || "anonymous"; |
10 |
elmex |
1.1 |
|
11 |
root |
1.11 |
configure; |
12 |
elmex |
1.3 |
|
13 |
root |
1.4 |
$| = 1; |
14 |
elmex |
1.3 |
|
15 |
root |
1.4 |
my ($client, $server); |
16 |
elmex |
1.1 |
|
17 |
root |
1.4 |
sub server_connect { |
18 |
root |
1.13 |
my $db_mon; $db_mon = db_mon eg_chat_server => sub { |
19 |
|
|
return unless %{ $_[0] }; |
20 |
|
|
undef $db_mon; |
21 |
|
|
|
22 |
|
|
print "\rconnecting...\n"; |
23 |
|
|
|
24 |
|
|
$client = port { print "\r \r@_\n> " }; |
25 |
|
|
mon $client, sub { |
26 |
|
|
print "\rdisconnected @_\n"; |
27 |
|
|
&server_connect; |
28 |
|
|
}; |
29 |
elmex |
1.1 |
|
30 |
root |
1.13 |
$server = (keys %{ $_[0] })[0]; |
31 |
root |
1.7 |
|
32 |
root |
1.13 |
snd $server, join => $client, $nick; |
33 |
|
|
mon $server, $client; |
34 |
root |
1.7 |
}; |
35 |
root |
1.4 |
} |
36 |
elmex |
1.1 |
|
37 |
root |
1.4 |
server_connect; |
38 |
elmex |
1.1 |
|
39 |
root |
1.4 |
my $w = AE::io 0, 0, sub { |
40 |
|
|
chomp (my $line = <STDIN>); |
41 |
|
|
print "> "; |
42 |
root |
1.8 |
snd $server, privmsg => $nick, $line |
43 |
|
|
if $server; |
44 |
root |
1.2 |
}; |
45 |
elmex |
1.1 |
|
46 |
root |
1.4 |
print "> "; |
47 |
|
|
AE::cv->recv; |
48 |
elmex |
1.1 |
|