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 ##########   #####"
**
**  $Id: EmailAddress.java,v 1.6 1997/07/31 14:00:18 subtle Exp subtle $
**
**  Class History
**
**  Date        Name         Description
**  ---------|------------|-----------------------------------------------
**  25Jul97     subtle      added configuration
**  26Jul97     subtle      set name in EDV thread. (email delay verifier)
**
*/

package key.primitive;

import key.util.SMTP;
import key.util.HTUU;
import key.Event;
import key.Reference;
import key.EmailSetter;
import key.Player;
import key.Factory;
import key.Key;
import key.Daemon;
import key.Daemon;
import key.InteractiveConnection;
import key.InvalidEmailException;

import java.io.*;
import java.net.UnknownHostException;
import java.net.ProtocolException;
import java.util.Random;
import java.util.StringTokenizer;

public class EmailAddress implements java.io.Serializable
{
	private String validationCode;
	protected String validAddress;
	protected String address;
	protected Reference setter;
	
	public EmailAddress()
	{
		validationCode = null;
		validAddress = null;
		address = null;
		setter = null;
	}
	
	public String toString()
	{
		if( validAddress != null )
			return( validAddress );
		else
			return( "<not set>" );
	}
	
	public String get()
	{
		return( validAddress );
	}

	public boolean isValid()
	{
		return( validAddress != null );
	}
	
	public boolean isSet()
	{
		return( address != null || validAddress != null );
	}
	
	public void set( String newAddress, InteractiveConnection ic, Player p )
	{
			//  simply checks that there is something between the seperators
			//  and that both of them (@ and .) exist in the string, somewhere.
		try
		{
			boolean containsAt = false;
			boolean containsDot = false;
			boolean lastWasSeperator = true;
			boolean firstseperator = true;
			
			StringTokenizer st = new StringTokenizer( newAddress, "@.", true );
			
			while( st.hasMoreTokens() )
			{
				String s = st.nextToken();
				
				if( s.charAt( 0 ) == '@' )
				{
					if( lastWasSeperator )
						throw new InvalidEmailException();
						
					if( !firstseperator )
						throw new InvalidEmailException();

					firstseperator = false;
					lastWasSeperator = true;
					containsAt = true;
				}
				else if( s.charAt( 0 ) == '.' )
				{
					if( lastWasSeperator )
						throw new InvalidEmailException();
						
					if( firstseperator )
						lastWasSeperator = false;
					else
					{
						lastWasSeperator = true;
						containsDot = true;
					}
				}
				else
				{
					lastWasSeperator = false;
				}
			}
			
			if( !containsAt || !containsDot || lastWasSeperator )
				throw new InvalidEmailException();
		}
		catch( InvalidEmailException e )
		{
			ic.sendError( "That is not the correct form of an email address.  Please do not use this command frivolously" );
			return;
		}
		
		if( address != null && (setter == null || !setter.isValid()) )
		{
			//  some sort of message here saying the previous
			//  email was never registered?
			ic.sendFeedback( "\nThe previous email address you set was never validated.  Please do not use this command frivolously.\n" );
		}
		
		validationCode = HTUU.encode( Integer.toString( new Random().nextInt() ) );
		address = newAddress;
		
		if( setter == null || !setter.isValid() )
		{
			EmailSetter es = (EmailSetter) Factory.makeAtom( EmailSetter.class, p.getName() );
			try
			{
				es.ensureInKey();
			}
			catch( Exception e )
			{
				ic.sendError( "An error occurred while attempting to send your email: " + e.toString() );
				return;
			}
			
			setter = es.getThis();
			es = null;
		}
		
		((EmailSetter)setter.get()).set( p.getThis(), address, validationCode );
		
		ic.sendFeedback( "\nAn email will be sent to " + address + " shortly, containing the verification code that you need to make this new email address active.  If you have made a mistake, please set your email address again immediately." );
	}
	
	public void verify( String code, InteractiveConnection ic )
	{
		if( address == null )
			ic.sendError( "You don't have a pending email verification code" );
		else if( code.equals( validationCode ) )
		{
			validAddress = address;
			address = null;
			validationCode = null;
			ic.sendFeedback( "Verification code correct, your email address has been set to: " + validAddress );
		}
		else
			ic.sendError( "Invalid verification code" );
	}
}