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

#
#  Initialize everything and create a socket
#
($port) = @ARGV;
$|=1;
$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");




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


#
#  Bootstrap in case there is no db
#
# Wizard
$idn = ($maxid++);
$dname[$idn]='Wizard';
$description[$idn]='You are filled with awe';
$lock[$idn]='barfoo';
$flags[$idn]='W';
$exits[$idn]="~home1$idn";
$owner[$idn]=0;
$fail[$idn]='You can\'t pick up a person';
$ofail[$idn]='acts silly';
$succ[$idn]='Home sweet home...\n';
$osucc[$idn]='goes home.';
$drop[$idn]='';
$odrop[$idn]='';
$contents[$idn]='';
$location[$idn]=1;

# 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;

# The exit
$ex = ($maxid++);
$dname[$ex]='Simple Exit';
$description[$ex]='This is your basic exit.  It comes back here.';
$lock[$ex]='';
$flags[$ex]='';
$exits[$ex]='';
$owner[$ex]=0;
$fail[$ex]='The exit is locked.';
$ofail[$ex]='wanders around.';
$succ[$ex]='You go through the exit';
$osucc[$ex]='steps out.';
$drop[$ex]='';
$odrop[$ex]='steps in.';
$contents[$ex]='';
$location[$ex]=$here;


#
#  Hopefully, there will be a database
#
print("Loading Database...\n");
&load_db();
print("...done\n");


#
#  Parse the database
#
print("Creating id's array\n");
for $nn (0..$#dname){
    $id{$dname[$nn]}=$nn;
}

#
#  Main loop (do everything)
#
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);    # Anyone saying anything?


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

    while($nfound){
	&deal_with_pending_ports;
#	&regular_command;
    }
}




#
#  Manage connections and annoying stuff like that
#
sub deal_with_pending_ports {
    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();
}


#
#  Duh, do nothing right now
#
sub regular_command{
}


#
#  Handy Dandy and stolen from the man page
#
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;
}


#
#  Cope with new connections
#
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 please\n");
    select(stdout);
}



#
#  The main parsing routine
#
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(substr($text, 0, 1) eq ':'){
	    $rest = substr($text, 1);
	    $tell = "$name $rest\n";
	    &tell($tell, &others_in_room($here));
	    &tell($tell, $port[$me]);
	}
	elsif(substr($text, 0, 1) eq "\""){
	    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]);
	}
	elsif(substr($text, 0, 1) eq '@'){
	    &build_command($name, $text);
	}
	else{
	    &parse_command($name, $text);
	}
    }
    else
    {
	$tell = $text;
    } 
}



#
#  They typed something that begins with '@'
#
sub build_command{
    local($who, $what) = @_;

    ($cmd, @args)=split(' ', $what);
    if($cmd eq '@open'){
	if($owner[$here] eq $me || $flags[$here] =~ /B/){
	    ($names, $to) = split('=', join(' ', @args));
	    if(($owner[$to] eq $me || $flags[$to] =~ /B|L/) && $flags[$to] =~/R/){
		&create_exit($names, $to, $maxid++);
	    }
	    else{
		&tell("Invalid destination\n", $port[$me]);
	    }
	}
	else{
	    &tell("You can't open an exit here\n", $port[$me]);
	}
    }
    elsif($cmd eq '@desc'){
	#Add permissions and stuff here
	
	($item, $descrip)=split('=', join(' ', @args));
	$item=~s/me/$me/;$item=~s/here/$here/;
	$description[$item]=$descrip;
	&tell("Description changed.\n", $port[$me]);
    }
    elsif($cmd eq '@dig'){
	&create_room($rname = join(' ', @args), $maxid++);
	&tell("Room \"$rname\" created with id #".($maxid-1)."\n", $port[$me]);
    }
    elsif($cmd eq '@field'){
	if($flags[$me]=~/W/){
	    $field = $args[0];
	    ($item, $val)=split('=', join(' ', @args[1..$#args]));
	    $item=~s/me/$me/;$item=~s/here/$here/;
	    eval("\$$field[$item]=$val");
	    &tell("Done.\n", $port[$me]);
	}
	else{
	    &tell("You aren't allowed to do that\n", $port[$me]);
	}
    }
    else{
	&tell("That command is not supported.\n", $port[$me]);
    }
}


#
#  Yucky time string stuff
#
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;
}


#
#  Parse commands that don't begin with ", :, or @
#
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 'ids'){
	if($flags[$me] =~ /W/){
	    &tell("Id numbers of stuff\n", $port[$me]);
	    for $idn (0..$#dname){
		&tell("#$idn:\t$dname[$idn]\n", $port[$me]);
	    }
	}
	else{
	    &tell("You can't do that\n", $port[$me]); 
	}
    }
    elsif($command eq 'hello'){
	&tell("You are now connected.\n", $port[$me]);
    }
    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 =~ /^xyzzy (.*)$/){
	&tell("Huh??\n", $port[$me]);
	&tell($1."\n", &others_in_room($here));
    }
    elsif($command =~ /(^l\b|^look)/){
	if(($ind = index($command, ' ')) ==-1){
	    print("In $here.  $dname[$here], $description[$here]\n");
	    &tell("$dname[$here]\n", $port[$me]);
	    if($description[$here]){
		&tell("$description[$here]\n", $port[$me]);
	    }
	    &tell("Contents:\n", $port[$me]);
	    &tell(&list_contents($here), $port[$me]);
	}
	else{
	    $opts = substr($command, $ind+1);
	    &tell(&get_descrip($opts), $port[$me]);
	}
    }
    elsif($command =~ /^examine\b/){
	if(($ind = index($command, ' ')) ==-1){
	    print("In $here.  $dname[$here], $description[$here]\n");
	    &tell("$dname[$here](#$here)\n", $port[$me]);
	    if($description[$here]){
		&tell("$description[$here]\n", $port[$me]);
	    }
	    &tell("Contents:\n", $port[$me]);
	    &tell(&list_contents($here), $port[$me]);
	}
	else{
	    $opts = substr($command, $ind+1);
	    $descrip_got= &get_descrip($opts);
	    &tell("(#$mtchnum)".$descrip_got, $port[$me]);
	}
    }
    elsif($command eq 'save'){
	if($flags[$me]=~/W/){
	    &save_db();
	}
	else{
	    &tell("Low low prices!\n", $port[$me]);
	}
    }
    elsif($command =~ /^boot /){

	if($flags[$me] =~ /W/){
	    $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("None available yet\n", $port[$me]);
    }
    else{
	if(!&exit_somewhere($command)){
	    &tell("Huh?\n", $port[$me]);
	}
    }
    
}



#
#  Try to find an exit by the appropriate name
#
sub exit_somewhere {
    local($exit) = @_;
    local($moved) = 0;
    @waysout = split('~', $exits[$here]);
    for $way (@waysout){
	($ens, $info) = split('', $way);
	@enams = split(';', $ens);
	($goto, $enum) = split('', $info);
	for $en (@enams){
	    if($exit eq $en){
		&move_to($goto,$enum);
		$moved = 1;
	    }
	}
    }
    return($moved);
}

#
#  Move a player and tell everyone about it
#
sub move_to{
    local($where,$exitnum) = @_;
    local($msg, $omsg);
    if($lock[$exitnum]){
	# need to check if the lock actually applies
	# and then use the correct messages
	$omsg = "Lock!!!";
	$msg = "It's locked.  You can't go there.";
    }
    else{
	$omsg = $osucc[$exitnum];
	$msg = $succ[$exitnum];
    }
    &tell("$name $omsg\n", &others_in_room($location[$me])) if $omsg;
    &tell("$msg\n", $port[$me]) if $msg;


    # Move him actually
    $location[$me]=$goto;
    @cntnts = split('', $contents[$here]);
    $foocnt = '';
    for $litem (@cntnts){
	next if ($litem == $me);
	$foocnt .= "$litem" if $litem ne '';
    }
    print("Setting contents of $here to $foocnt from being $contents[$here]\n");
    $contents[$here] = $foocnt;
    $contents[$goto] .= "$me";
    $here = $goto;

    #Done with the move.  No tell everyone


    &tell("$name $odrop[$exitnum]\n", &others_in_room($location[$me])) if $odrop[$exitnum];
    &tell("$drop[$exitnum]\n", $port[$me]) if $drop[$exitnum];

    print("In $here.  $dname[$here], $description[$here]\n");
    &tell("$dname[$here]\n", $port[$me]);
    if($description[$here]){
	&tell("$description[$here]\n", $port[$me]);
    }
    &tell("Contents:\n", $port[$me]);
    &tell(&list_contents($here), $port[$me]);
}


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

}

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


#
#  Parse my database format
#
sub retrieve_object {
    local($id) = @_;
    $maxid = $id > $maxid ? $id:$maxid;
    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));
}



#
#  Save the database
#
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]);
    }
}


#
#  What else is in $here?
#   
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;
}


#
# Deal with 'connect' and 'create'
#
sub parse_connect {
    local($text) = @_;

    print("Checking connect string [$text]\n");
    ($command, $name, $pword) = split(' ', $text);
    if($command eq 'connect'){
	next if(!defined($id{$name}));
	if(($p=$lock[$id{$name}]) && (!grep(/$name/,@name))){
	    if($p eq $pword){
		print("Connecting player $name($id{$name})\n");
# Not anymore	$contents[$location[$id{$name}]] .= "$id{$name}";
		$port[$id{$name}]=$fha[$f];
		$logintime[$id{$name}]=time;
		$lasttime[$id{$name}]=time;
		$me=$id{$name};
		&tell("$name has connected.\n", &others_in_room($location[$me]));
		return '$name[$f]="$name";$output="hello";';
	    }
	}
	else{
	    &tell("I can't find that charachter with that password\n", $fha[$f]);
	    if(grep(/$name/,@name)){&tell("Already connected\n", $fha[$f]);}
	}
    }
    elsif($command eq 'create'){
	if(!$lock[$id{$name}] || !defined($id{$name})){
	    $id{$name}=($maxid++);
	    print("Connecting and creating player $name($id{$name})\n");
	    $dname[$id{$name}]= $name;
	    $contents[1] .= "$id{$name}";
	    $port[$id{$name}]=$fha[$f];
	    &create_player($id{$name});
	    $output = "hello";
	    $logintime[$id{$name}]=time;
	    $lasttime[$id{$name}]=time;
	    $lock[$id{$name}]=$pword;
	    $me=$id{$name};
	    &tell("$name has connected.\n", &others_in_room($location[$me]));
	    return '$lock[$id{$name}]=$pword;$name[$f]="$name";';
	    }
	else{
	    &tell("That name already exists, try again\n", $fha[$f]);
	    return '$output = "";';
	}
    }
    else{
	&tell("Please connect to an existing charachter or create a new one\nconnect name password\ncreate name password\n", $fha[$f]);
	return '$output = "hello";';
    }
}


#
#  Create a new player
#
sub create_player {
    local($id) = @_;
    $description[$id] ='';
    $lock[$id]=$pword;
    $flags[$id]='P';
    $exits[$id]="~home1$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]=1;
}


#
# Create a room
#
sub create_room {
    local($name, $id) = @_;
    $dname[$id]=$name;
    $description[$id] ='';
    $lock[$id]='';
    $flags[$id]='R';
    $exits[$id]='';
    $owner[$id]=$me;
    $fail[$id]='';
    $ofail[$id]='';
    $succ[$id]='';
    $osucc[$id]='looks around.';
    $drop[$id]='';
    $odrop[$id]='';
    $location[$id]=$id;
}

#
#  Create an exit
#
sub create_exit {
    local($name, $whereto, $id) = @_;
    $dname[$id]=$name;
    $description[$id] ='';
    $lock[$id]='';
    $flags[$id]='E';
    $exits[$id]='';
    $owner[$id]=$me;
    $fail[$id]='';
    $ofail[$id]='';
    $succ[$id]='';
    $osucc[$id]='';
    $drop[$id]='';
    $odrop[$id]='';
    $location[$id]=$here;


    $exits[$here] .= "~$name$whereto$id";
}


#
#  Who else is in the room
#
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);
}


#
#  Say something to a user given by their FH
#
sub tell {
	local($message, @handles)=@_;
	for $h (@handles) {
	    select($h); $|=1;
	    print($message);
	    select(stdout);
	}
}

#
#  I don't remember what this is for really
#
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();
    }
}


#
#  Kill dying connections
#
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)) if $name;
}


__END__

