#!/usr/bin/perl # vladimirbot-core.pl - Core daemon for Vladimir # # History / Notes # 2003.12.22 # - Created use strict; use VladimirBot; use IO::Socket; use IO::Select; my $port = 42001; my $max_clients = 10; my $new_client = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $port, Listen => $max_clients, Reuse => 1); my $sel = IO::Select->new($new_client); print "Listening on port $port.\n"; while(my @ready = $sel->can_read) { foreach my $client (@ready) { if($client == $new_client) { # New connection my $add = $client->accept; $add->blocking(0); $sel->add($add); #my $msg = "[Welcome user $add->peerhost:$add->peerport]\n"; #sendall($msg); } else { my $msg = ""; #my $nread = sysread($client, $msg, 1024); #chop($msg); chop($msg); $msg = getmsg($client); if($msg eq 'quit') { $sel->remove($client); close $client; } elsif ($msg eq 'reload') { syswrite($client,"--- Reloading Vladimir ---\n"); do "Vladimir.pm"; } else { my $out = VladimirBot::process($client->peerhost, $msg); syswrite($client,"Vladimir: $out\n"); } } } } sub sendall { my ($msg) = @_; foreach($sel->can_write) { syswrite($_, $msg, length($msg)); } } sub getmsg { my ($handle) = @_; my $msg = ""; my $nread; do { my $in; $nread = sysread($handle, $in, 1024); $msg .= $in; } while ($nread > 0); chop $msg; chop $msg; return $msg; }