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/
/**
  *  Command: Default
  *
  *
 */

package key.commands;
import key.*;
import java.util.StringTokenizer;
import java.io.IOException;

public class Default extends Command
{
	public Default()
	{
		setKey( "default" );
		usage = "<player | rank | @clanRank> [<action>]";
	}

	public void run( Player p, StringTokenizer args, String fullLine, CategoryCommand caller, InteractiveConnection ic, Flags flags ) throws IOException
	{
		Atom context = p.getContext();
		PermissionList pl = context.getPermissionList();
		
		if( args.hasMoreTokens() )
		{
			String firstArg = args.nextToken();
			
			if( args.hasMoreTokens() )
			{
				String player = firstArg;
				String action = args.nextToken();
				
					//  first find the player
				Reference s;
				Clan c = p.getClan();
				
				if( player.startsWith( "@" ) && c != null )
					s = getReferenceElementInside( ic, player.substring( 1 ), c );
				else
					s = getReferenceElementInside( ic, player, Key.shortcuts() );
				
				if( s != null )
				{
					boolean isPlayer = false;
					boolean isFriends = false;
					boolean isRank = false;
					boolean isClan = false;
					
					if( s.isLoaded() )
					{
						Atom q = s.get();
						
						if( q instanceof Player )
							isPlayer = true;
						else if( q instanceof Friends )
							isFriends = true;
						else if( q instanceof Rank )
							isRank = true;
						else if( q instanceof Clan )
							isClan = true;
					}
					else
					{
							//  only players can be offline atm.  in
							//  the future, this might become more of
							//  a problem.
						isPlayer = true;
					}
					
					if( isPlayer || isRank || isClan || isFriends )
					{
							//  now find the target action
						Action a = context.getAction( action );
						if( a != null )
						{
							pl.clear( s, a );
							
							if( isPlayer )
								ic.sendFeedback( s.getName() + " no longer has a specific entry for '" + a.getName() + " " + context.getName() + "'" );
							else if( isFriends )
								ic.sendFeedback( "Your friends no longer have a specific entry for '" + a.getName() + " " + context.getName() + "'" );
							else
								ic.sendFeedback( s.getName() + " no longer has a specific entry for " + a.getName() + " " + context.getName() );
						}
						else
							ic.sendError( "Could not find action '" + action + "' on " + context.getName() );
					}
					else
						ic.sendError( "That can't be put on a permission list." );
				}
				else
					ic.sendError( "Could not find anything answering to '" + firstArg + "'." );
			}
			else
			{
				Reference s;
				Clan c = p.getClan();
				
				if( firstArg.startsWith( "@" ) && c != null )
					s = getReferenceElementInside( ic, firstArg.substring( 1 ), c );
				else
					s = getReferenceElementInside( ic, firstArg, Key.shortcuts() );
				
				if( s != null )
				{
					boolean isPlayer = false;
					boolean isFriends = false;
					boolean isRank = false;
					boolean isClan = false;
					
					if( s.isLoaded() )
					{
						Atom q = s.get();
						
						if( q instanceof Player )
							isPlayer = true;
						else if( q instanceof Friends )
							isFriends = true;
						else if( q instanceof Rank )
							isRank = true;
						else if( q instanceof Clan )
							isClan = true;
					}
					else
					{
							//  only players can be offline atm.  in
							//  the future, this might become more of
							//  a problem.
						isPlayer = true;
					}
					
					if( isPlayer || isRank || isClan || isFriends )
					{
						pl.deleteEntryFor( s );
						
						if( isPlayer )
							ic.sendFeedback( s.getName() + " no longer has a specific entry on " + context.getName() );
						else if( isFriends )
							ic.sendFeedback( "Your friends no longer have a specific entry on " + context.getName() );
						else
							ic.sendFeedback( s.getName() + " no longer has a specific entry on " + context.getName() );
					}
					else
						ic.sendError( "That can't be put on a permission list." );
				}
				else
					ic.sendError( "Could not find anything answering to '" + firstArg + "'." );
			}
		}
		else
			usage( ic );
	}
}