key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
**
**  Class History
**  
**  Date        Name         Description
**  ---------|------------|-----------------------------------------------
**  22Jul97     merlin       created this command
**
*/

package key.commands;

import key.*;
import key.primitive.DateTime;
import java.io.*;
import java.util.StringTokenizer;

/**
  *  This command is intended to unban a player after
  *  receiving a sneeze, splat, or banish
 */
public class Unbanish extends Command
{
	public static final AtomicElement[] ELEMENTS =
	{
		AtomicElement.construct( Unbanish.class, String.class, "commandType",
			AtomicElement.PUBLIC_FIELD,
			"the default type of the banish to undo" )
	};
	
	public static final AtomicStructure STRUCTURE = new AtomicStructure( Command.STRUCTURE, ELEMENTS );

	public String commandType = "";
	
	public Unbanish()
	{
		setKey( "unbanish" );
		usage = "<player>";
	}
	
	public AtomicStructure getDeclaredStructure()
	{
		return( STRUCTURE );
	}

	public void run( Player p, StringTokenizer args, String fullLine, CategoryCommand caller, InteractiveConnection ic, Flags flags ) throws IOException
	{
		if( args.hasMoreTokens() )
		{
			Player causePlayer = null;
			String name = args.nextToken();
			
			causePlayer = (Player) getPlayer( ic, name );
			
			if( causePlayer == null )
				return;
			
			if( p == causePlayer )
			{
				ic.sendFeedback( "Try using a name other than your own" );
				return;
			}
			
			String playerBanType = (String) causePlayer.getProperty( "banishType" );
			if( !playerBanType.equals( (String) getProperty( "commandType" ) ) )
			{
				ic.sendFeedback( name + " is not currently " + getCommandType() );
				return;
			}
			
			DateTime until = (DateTime) causePlayer.getProperty( "banishedUntil" );
			
			causePlayer.setProperty( "banishedUntil", null );
			causePlayer.setProperty( "banishType", "" );
			causePlayer.sync();

			ic.sendFeedback( causePlayer.getName() + " unbanned..." );

				// if the command Type is "P", then check the site, and
				// remove the site ban if it is the same end date  
			if( playerBanType.equals( "P" ) )
			{
				Site si = (Site) causePlayer.getProperty( "bannedWithSite" );
				DateTime siteUntil = (DateTime) si.getProperty( "bannedUntil" );

				if( si == null || siteUntil == null )
				{
					return;
				}

				if( siteUntil.getTime() == until.getTime() )
				{
					si.setBan( "", p, new DateTime(), null, "Unbanish of Site" );
					ic.sendFeedback( si + " unbannned..." );
				}
				else
				{
					ic.sendFeedback( si + " has different ban information... skipping unban..." );
				}
			}
		}
		else
			usage( ic );
	}

	private String getCommandType()
	{
		return( new String( getName() + "ed" ) );
	}
}