#!/afs/athena/contrib/perl/perl

&initialize();

# First room
$here=($maxid++);
$dname[$here]='Room';
$description[$here]='This is a plain room.';
$lock[$here]='';
$flags[$here]='J';
$exits[$here]="~out;exit$here2";
$owner[$here]=0;
$fail[$here]='';
$ofail[$here]='';
$succ[$here]='';
$osucc[$here]='';
$drop[$here]='';
$odrop[$here]='';
$contents[$here]='';
$location[$here]=$here;

print "Listening for connection 1....\n";
$/ = "\n";
for(;;) {
    chop($fhs);
    $rin = &fhbits($fhs);
    $orin = $rin;
    #Get bitmask

    $fhs .= ' ';
    $ein = $rin;

    $nfound = select($rin, undef, undef, 1);
    #Do select

    if(vec($rin, fileno(S), 1)){
	&new_connection();
    }

    while($nfound){


	print("There are $nfound pending ports.Bitmask: ($rin)\n") if $debug;


	for $f (reverse(1..$#fha)){

	    print("Checking port $f in bitmask $rin\n") if $debug;

	    if(vec($rin,fileno($fha[$f]),1)){
		$foo = $fha[$f];
		print("Reading from $f,$foo...\n") if $debug;
		$nbread = sysread($foo, $output, 128);
		$fhs = join(' ', @fha);
		$fhs .=' ';
		chop($output);    #chop linefeed
		if(substr($output, length($output)-1, 1) eq "\r"){
		    chop($output);} #chop CR
		print("Read from $f,$foo: [$output] (cleaned up)\n") if $debug;
		#say hello
		if(!$name[$f]){
		    print("Port $f does not have a name. Granting name $output\n") if $debug;
		    eval(&parse_connect($output));
		    }
		if(!$nbread){
		    #Dead connection
		    $ofh = splice(@fha, $f,1);
		    $fhs = join(' ', @fha);
		    $fhs .=' ';
		    print("Connection port $f dead.  Removing name and fh entry.\n") if $debug;
		    $name = splice(@name, $f, 1);
		    $me = $id{$name};
		    &tell("$name has disconnected\n", &others_in_room($here));
		    $output = 'bluh';
		}
		&user_input($f, $name[$f], $output);

		print("\n") if $debug;
	    }
	}
	print("Done.  Doing select again.\n") if $debug;
	&myselect();
    }

	&regular_command();

}

sub regular_command{
}

sub fhbits {
    local(@fhlist) = split(' ',$_[0]);
    local($bits);
    for (@fhlist) {
	vec($bits,fileno($_),1) = 1;
	$obits = ord($bits);
	$fno = fileno($_);
	print(",$_($bits)($obits)($fno),") if $debug;
    }
    print("\n") if $debug;
    $bits;
}

sub new_connection {
    ($addr = accept($fh,S)) || die $!;
    push(@fha, $fh);
    $fhs =join(' ', @fha).' ';
    $fh++;
    ($af,$port,$inetaddr) = unpack($sockaddr,$addr);
    $date = time;
    ($hostname,$aliases,$addrtype,$length,@addrs) = gethostbyaddr($inetaddr, 2);
    $fn = fileno($fha[$#fha]);
    $hostname[fileno($fha[$#fha])] = $hostname;
    print("Connection made from $hostname($fn) at $date. ($fhs)\n") if $debug;
    select(@fha[$#fha]); $|=1;
    print("connect <name>\n");
    select(stdout);
}

sub user_input {
    local($num, $name, $text)=@_;


    $me = $id{$name[$f]};
    $here = $location[$me];
    
    $text =~ s/[\e\b\cg\ch\ci\cj\ck\cl\cm\cn\co]//g;
    print("$name//$text//\n");
    $lasttime[$id{$name}]=time;
    if($name){
	if($longexpl[$me]){
	    if($text eq '.'){
		&tell("You speak:\n".$long[$me], $port[$me]);
		&tell("$name speaks:\n".$long[$me], &others_in_room($here));
		$long[$me]='';
		$longexpl[$me]=0;
	    }
	    else{
		$long[$me].="--> ".$text."\n";
	    }
	}
	elsif(substr($text, 0, 1) eq ':'){
	    if($gag[$me]){
		&tell("You are gagged.\n", $port[$me]);
	    }
	    else{
		$rest = substr($text, 1);
		$tell = "$name $rest\n";
		&tell($tell, &others_in_room($here));
		&tell($tell, $port[$me]);
	    }
	}
	elsif(substr($text, 0, 1) eq '"'){
	    if($gag[$me]){
		&tell("You are gagged.\n", $port[$me]);
	    }
	    print("$name is saying something\n") if $debug;
	    $rest = substr($text, 1);
	    $tell = "$name says, \"$rest\"\n";
	    @foo = &others_in_room($here);
	    &tell($tell, @foo);
	    $tell = "You say, \"$rest\"\n";
	    &tell($tell, $port[$me]);
	}
	else{
	    &parse_command($name, $text);
	}
    }
    else
    {
	$tell = $text;
    } 
}

sub tstring{
    local($time)=@_;
    local($sec, $min, $hour, $day, @rest, $diff, $ret);

    $diff = time-$time;
    ($sec, $min, $hour, $day, @rest)= gmtime($diff);
    $day -=1;
    if($day){
	$ret = $day.'d '.$hour.':'.$min;
    }
    elsif($hour){
	$ret = $hour.':'.$min;
    }
    elsif($min){
	$ret = $min.'m '.$sec.'s';
    }
    else{
	$ret = $sec.'s';
    }
    return $ret;
}

sub parse_command{
    local($name, $command) = @_;

    if($command eq 'who'){
	&tell("Name\t\t\tLogin time\tIdle\tWhere\n", $port[$me]);
	$i=1;
	for $w (1..$#name){
	    if($who=$name[$w]){
	    $hname = $hostname[fileno($fha[$i])];
	    $ltime = &tstring($logintime[$id{$who}]);
	    $itime = &tstring($lasttime[$id{$who}]);
	    if(($l=length($who))<10){$who .= ' 'x(10-$l);}
	    &tell("$who\t\t$ltime\t\t$itime\t$hname\n", $port[$me]);
	    $i++;
	}
	}
    }
    elsif($command eq 'QUIT'){
	close($fha[$f]);
	splice(@fha, $f,1);
	$fhs = join(' ', @fha);
	$fhs .=' ';
	print("Connection port $f dead.  Removing name and fh entry.\n") if $debug;
	$name = splice(@name, $f, 1);
#	$contents[$here] =~ s/$id{$name}//;
	&tell("$name has disconnected\n", &others_in_room($here));
    }
    elsif($command eq 'hello'){
	&tell("You are now connected.\n", $port[$me]);
	&tell("$name is now connected.\n", &others_in_room($here));
    }
    elsif($command =~ /^chairman( .+)*/){
	print("Chairman is set to $chairman, who is $dname[$chairman]\n");
	if($chairman == $me){
	    print("I am the chairman!\n");
	    if($1){
		$chairman = $id{$nch = substr($1,1)};
		&tell("The new chairman is $nch\n", $port[$me]);
		&tell("The new chairman is $nch\n", &others_in_room($here));
	    }
	    else{
		&tell("You are the current chairman\n", $port[$me]);
	    }
	}
	elsif(($chairman)){
	    print("There is a chairman...\n");
	    &tell("The current chairman is $dname[$chairman]\n", $port[$me]);
	}
    
	else{
	    print("No chairman, yet\n");
	    $chairman = $me;
	    $nch = $dname[$me];
	    &tell("The new chairman is $nch\n", $port[$me]);
	    &tell("The new chairman is $nch\n", &others_in_room($here));
	}
    }
    elsif($command eq 'long'){
	if($gag[$me]){
	    &tell("You are gagged.\n", $port[$me]);
	}
	else{
	    $longexpl[$me]=1;
	    &tell("Begin.  End with a . on a line by itself\n", $port[$me]);
	}
    }
    elsif($command =~ /^boot /){

	if($chairman == $me){
	    $who = substr($command, 5);
	    foreach $hand (0..$#fha){
		print("Comparing ($who) to ($name[$hand])\n");
		if($who eq $name[$hand]){
		    &tell("You are being booted.\n", $fha[$hand]);
		    &disconnect($hand);
		}
	    }
	}
	else{
	    &tell("You can't do that.\n", $port[$me]);
	}
    }
    elsif($command eq 'help'){
	&tell("Current available commands are: who, QUIT, chairman, long\n", $port[$me]);
    }
    else{
	if(!&exit_somewhere($command)){
	    &tell("Huh?\n", $port[$me]);
	}
    }
    
}
sub exit_somewhere {
    local($exit) = @_;


    return(0);
}



sub get_descrip{
    local($string) = @_;
    @others = split('', $contents[$here]);
    $match = 0;
    for $n (1..$#others){
	print("Checking -$string- against -$dname[$others[$n]]-\n");
	if($dname[$others[$n]] =~ /$string/i){
	    print("Aha, a match\n");
	    $ret = $description[$others[$n]];
	    $match = 1;
	}
    }
    if(!$match){$ret='You don\'t see that here.';}
    elsif(!$ret){$ret='You see nothing remarkable.';}
    $ret."\n";

}

sub load_db{
    open(DB, "perlmud-db");
    while(<DB>){
	if(substr($_,0,1) eq '#'){
	    &retrieve_object(int(substr($_,1)));
	}
    }
    close(DB);
    $maxid++;
}

sub retrieve_object {
    local($id) = @_;

    chop($dname[$id]=substr((<DB>),1));
    print("Retrieved object {$dname[$id]}($id)\n");
    chop($description[$id]=substr((<DB>),1));
    chop($lock[$id]=substr((<DB>),1));
    chop($flags[$id]=substr((<DB>),1));
    chop($exits[$id]=substr((<DB>),1));
    chop($owner[$id]=substr((<DB>),1));
    chop($fail[$id]=substr((<DB>),1));
    chop($ofail[$id]=substr((<DB>),1));
    chop($succ[$id]=substr((<DB>),1));
    chop($osucc[$id]=substr((<DB>),1));
    chop($drop[$id]=substr((<DB>),1));
    chop($odrop[$id]=substr((<DB>),1));
    chop($contents[$id]=substr((<DB>),1));
    chop($location[$id]=substr((<DB>),1));
}


sub save_db{

    if($flags[$me] =~ /W/){
	open(DB, ">>perlmud-db");
	select(DB);
	for $idnum (0..$maxid-1){
	    print("#$idnum\n");
	    print(";$dname[$idnum]\n");
	    print(";$description[$idnum]\n");
	    print(";$lock[$idnum]\n");
	    print(";$flags[$idnum]\n");
	    print(";$exits[$idnum]\n"); 
	    print(";$owner[$idnum]\n");
	    print(";$fail[$idnum]\n");
	    print(";$ofail[$idnum]\n");
	    print(";$succ[$idnum]\n");
	    print(";$osucc[$idnum]\n");
	    print(";$drop[$idnum]\n");
	    print(";$odrop[$idnum]\n");
	    print(";$contents[$idnum]\n");
	    print(";$location[$idnum]\n");
	    print("\n");
	}	
	select(stdout);
	close(DB);
	&tell("Done\n", $port[$me]);
    }
    else{
	&tell("You can't do that\n", $port[$me]);
    }
}
    
sub list_contents{
    ($room) = @_;
    local(@ret);
    @others = split('', $contents[$room]);
    print("Contentlist: $contents[$room]\n");

    for $n (1..$#others){
	    push(@ret, $dname[$others[$n]]);
	}
    $ret = join("\n", @ret)."\n";
    return $ret;
}

sub parse_connect {
    local($text) = @_;

    print("Checking connect string [$text]\n");
    ($command, $name, $pword) = split(' ', $text);
    if($command eq 'connect'){
	$id{$name}=($maxid++);
	print("Connecting and creating player $name($id{$name})\n");
	$dname[$id{$name}]= $name;
	$contents[0] .= "$id{$name}";
	$port[$id{$name}]=$fha[$f];
	&create_player($id{$name});
	$output = "hello";
	$logintime[$id{$name}]=time;
	$lasttime[$id{$name}]=time;
	$me=$id{$name};
	
	
	&tell("Welcome to PerlConference\n", $fha[$f]);

	return '$name[$f]="$name";';

    }
    else{
	&tell("Please type 'connect name'", $fha[$f]);
	return '$output = "hello";';
    }
}

sub create_player {
    local($id) = @_;
    $description[$id] ='';
    $flags[$id]='';
    $exits[$id]="~home0$id";
    $owner[$id]=0;
    $fail[$id]='You can\'t pick up a person';
    $ofail[$id]='acts silly';
    $succ[$id]='Home sweet home...\n';
    $osucc[$id]='goes home.';
    $drop[$id]='';
    $odrop[$id]='';
    $location[$id]=0;
}

sub others_in_room {
    ($room) = @_;
    local(@ret);
    @others = split('', $contents[$room]);
    for $n (1..$#others){
	if(defined($port[$others[$n]]) && $others[$n] ne $me){
	    push(@ret, $port[$others[$n]]);
	}
    }
    return(@ret);

}


sub tell {
	local($message, @handles)=@_;
	for $h (@handles) {
	    select($h); $|=1;
	    print($message);
	    select(stdout);
	}
}

sub myselect{

    chop($fhs);
    $rin = &fhbits($fhs);
    $orin = $rin;
    #Get bitmask

    $fhs .= ' ';
    $ein = $rin;
    $nfound = select($rin, undef, undef, 1);
    #Do select


    if(vec($rin, fileno(S), 1)){
	&new_connection();
    }


}

sub disconnect {
    local($f) = @_;
    
	close($fha[$f]);
	splice(@fha, $f,1);
	$fhs = join(' ', @fha);
	$fhs .=' ';
	print("Connection port $f dead.  Removing name and fh entry.\n") if $debug;
	$name = splice(@name, $f, 1);
#	$contents[$here] =~ s/$id{$name}//;
	&tell("$name has disconnected\n", &others_in_room($here));
}

sub initialize{
($port) = @ARGV;
$port = 2345 unless $port;

$AF_INET = 2;
$SOCK_STREAM = 1;

$sockaddr = 'S n a4 x8';

($name, $aliases, $proto) = getprotobyname('tcp');
if ($port !~ /^\d+$/) {
    ($name, $aliases, $port) = getservbyport($port, 'tcp');
}

print "Port = $port\n";
$this = pack($sockaddr, $AF_INET, $port, "\0\0\0\0");

select(NS); $| = 1; select(stdout);

socket(S, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
bind(S,$this) || die "bind: $!";
listen(S,5) || die "connect: $!";

select(S); $| = 1; select(stdout);

$fn = fileno(S);
print("Base socket fileno: $fn\n");

$con = 0;
$fh = 'AA';
$fhs = 'S ';
$bitn=1;
$fha[0]='S';
$debug = 0;
$maxid = 0;

}
__END__

