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;

import key.primitive.DateTime;
import key.primitive.Duration;
import key.util.SMTP;

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

public class EmailSetter extends Event
{
	public static final String VAR_ENTRY_NAME = "emailsetters";
	public final static String EMAIL_LOG = "email";
	
	DateTime created;
	boolean resetSince;
	String validationCode;
	String address;
	Reference player;
	String ip = "";
	
	public EmailSetter()
	{
	}

	public void ensureInKey() throws NonUniqueKeyException,BadKeyException
	{
		Key.instance().ensureAndGetVarEntry( VAR_ENTRY_NAME ).addInternal( getThis() );
	}

	public String getName()
	{
		return( "EmailSetter for: " + getKey().toString() );
	}
	
	public void set( Reference p, String ad, String vc )
	{
		player = p;
		created = new DateTime();
		resetSince = true;
		
		try
		{
			Player pl = (Player) p.get();
			ip = pl.getLastConnectFrom();
		}
		catch( OutOfDateReferenceException e )
		{
			if( scheduledFor != null )
			{
					//  remove from old schedule first
				Key.instance().getScheduler().remove( this );
				scheduledFor = null;
			}
			
			return;
		}
		
		address = ad;
		validationCode = vc;
		
		if( scheduledFor != null )
		{
				//  remove from old schedule first
			Key.instance().getScheduler().remove( this );
		}
		
		scheduledFor = DateTime.nowPlus( new Duration( 5 * 1000 * 60 ) );  //  5 minutes
		Key.instance().getScheduler().add( this );
	}
	
	public void run( Daemon scheduler )
	{
		Player p;

		try
		{
			p = (Player) player.get();
		}
		catch( OutOfDateReferenceException e )
		{
			cleanup();
			return;
		}
		
		p.putCode( 'p', p.getName() );
		p.putCode( 'i', ip );
		p.putCode( 'd', created.toString() );
		p.putCode( 'v', validationCode );
		
		String cn = Key.instance().getName();
		p.putCode( 'c', cn );
		
		String ra = (String) Key.instance().getConfig().getProperty( "returnAddress" );
		p.putCode( 'r', ra );
		
		TextParagraph para = (TextParagraph) Key.instance().getConfig().getProperty( "emailForm" );
		
		if( para != null )
		{
			para = (TextParagraph) Grammar.substitute( para, p.getCodes() );
			
			try
			{
				SMTP mailer = new SMTP( (String) Key.instance().getConfig().getProperty( "emailServer" ) );
				Log.log( EMAIL_LOG, "sending message for " + player.getKey() + ", code '" + validationCode + "'" );
				mailer.send( ra, address, "[automated] Verification of " + cn + " email", para.getText() );
			}
			catch( ProtocolException e )
			{
				Log.log( EMAIL_LOG, e.toString() + " while sending email verification for player " + player.getKey() + ", address: " + address );
			}
			catch( UnknownHostException e )
			{
				Log.log( EMAIL_LOG, e.toString() + " while sending email verification for player " + player.getKey() + ", address: " + address );
			}
			catch( IOException e )
			{
				Log.log( EMAIL_LOG, e.toString() + " while sending email verification for player " + player.getKey() + ", address: " + address );
			}
			finally
			{
				cleanup();
			}
		}
		else
			cleanup();
	}
	
	private void cleanup()
	{
		player = null;
		
		try
		{
			Key.instance().ensureAndGetVarEntry( VAR_ENTRY_NAME ).removeInternal( this.getThis() );
		}
		catch( Exception e )
		{
			System.out.println( "while removing emailsetters: " + e.toString() );
		}
	}
}