| 1 |
root |
1.2 |
#!/opt/bin/perl |
| 2 |
|
|
|
| 3 |
root |
1.7 |
# Usage: ./chat_server |
| 4 |
|
|
# implement a chat server using "traditional message passing" |
| 5 |
|
|
|
| 6 |
root |
1.4 |
use common::sense; |
| 7 |
elmex |
1.1 |
use AnyEvent::MP; |
| 8 |
|
|
|
| 9 |
root |
1.8 |
configure; |
| 10 |
elmex |
1.3 |
|
| 11 |
root |
1.4 |
my %clients; |
| 12 |
root |
1.2 |
|
| 13 |
root |
1.4 |
sub msg { |
| 14 |
|
|
print "relaying: $_[0]\n"; |
| 15 |
|
|
snd $_, $_[0] |
| 16 |
|
|
for values %clients; |
| 17 |
|
|
} |
| 18 |
|
|
|
| 19 |
root |
1.7 |
our $server = port; |
| 20 |
|
|
|
| 21 |
|
|
rcv $server, join => sub { |
| 22 |
|
|
my ($client, $nick) = @_; |
| 23 |
|
|
|
| 24 |
|
|
$clients{$client} = $client; |
| 25 |
root |
1.4 |
|
| 26 |
|
|
mon $client, sub { |
| 27 |
|
|
delete $clients{$client}; |
| 28 |
root |
1.7 |
msg "$nick (quits, @_)"; |
| 29 |
root |
1.4 |
}; |
| 30 |
root |
1.7 |
msg "$nick (joins)"; |
| 31 |
|
|
}; |
| 32 |
elmex |
1.1 |
|
| 33 |
root |
1.7 |
rcv $server, privmsg => sub { |
| 34 |
|
|
my ($nick, $msg) = @_; |
| 35 |
|
|
msg "$nick: $msg"; |
| 36 |
|
|
}; |
| 37 |
elmex |
1.3 |
|
| 38 |
root |
1.10 |
db_set eg_chat_server => $server; |
| 39 |
elmex |
1.1 |
|
| 40 |
root |
1.4 |
warn "server ready.\n"; |
| 41 |
elmex |
1.1 |
|
| 42 |
root |
1.4 |
AE::cv->recv; |
| 43 |
root |
1.10 |
|