Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jadex.bdi.planlib.messaging;
  
  import java.net.Socket;

AIM The class provides utilities to connect to the AIM instant messaging network and send messages using TOC version 2.
 
 public class AIM
 {

The host address of the TOC server.
 
 	public String			tocHost"toc.oscar.aol.com";

The port used to connect to the TOC server.
 
 	public int				tocPort		= 9898;

The OSCAR authentication server.
 
 	public String			authHost"login.oscar.aol.com";

The OSCAR authentication server's port.
 
 	public int				authPort	= 5190;

What language to use.
 
 	public String			language"english";

The version of the client.
 
 	public String			version"TIC:TOC";

The string used to "roast" passwords. See the roastPassword method for more info.
 
 	public String			roastString"Tic/Toc";

The sequence number used for FLAP packets.
 
 	protected short			sequence;

The connection to the TOC server.
 
 	protected Socket		connection;

An InputStream to the connection.
 
 	protected InputStream	is;

An OutputStream to the connection.
 
 	protected OutputStream	os;

Screen name of current user.
 
 	protected String		id;
 
 	private boolean	loggedin;
 
 	private String	error;

The ID number for a SIGNON packet(FLAP).
 
 	public static final int	SIGNON		= 1;

The ID number for a DATA packet (flap).
 
 	public static final int	DATA		= 2;

Constructor for AIM.

Parameters:
id
password
 
 	public AIM(String idString password)
 	{
 		=login(idpassword);
 	}

Log in to TOC

Parameters:
id The screen name to login with
password The screen name's password
Returns:
true on success
 
 	public boolean login(String idString password)
 	{
 		this. = id;
 		try
 		{
 			sendRaw("FLAPON\r\n\r\n");
 			getFlap();
 			String command = "toc2_signon " +  + " " +  + " " + id + " "
 					+ roastPassword(password) + " " +  + " \"" + this. + "\" 160 "
 					+ calculateCode(idpassword);
 
 
 			sendFlap(command);
 			String str = getFlap();
 
 			if(str.toUpperCase().startsWith("ERROR:"))
			{
				=str;
				return false;
			}
			this.sendFlap("toc_add_buddy " + this.);
			this.sendFlap("toc_init_done");
			this.sendFlap("toc_set_caps 09461343-4C7F-11D1-8222-444553540000 09461348-4C7F-11D1-8222-444553540000");
			this.sendFlap("toc_add_permit ");
			this.sendFlap("toc_add_deny ");
			return true;
		}
		catch(IOException ioe)
		{
			 = ioe.getMessage();
		}
		return false;
	}

Logout of toc and close the socket
	public void logout()
	{
		try
		{
			=false;
		}
		catch(IOException e)
		{
			/* NOP */
		}
	}

Called to roast the password. Passwords are roasted when sent to the host. This is done so they aren't sent in "clear text" over the wire, although they are still trivial to decode. Roasting is performed by first xoring each byte in the password with the equivalent modulo byte in the roasting string. The result is then converted to ascii hex, and prepended with "0x". So for example the password "password" roasts to "0x2408105c23001130"

Parameters:
str The password to roast
Returns:
The password roasted
	protected String roastPassword(String str)
	{
		byte xor[] = .getBytes();
		int xorIndex = 0;
		String rtn = "0x";
		for(int i = 0; i < str.length(); i++)
		{
			String hex = Integer.toHexString(xor[xorIndex] ^ (int)str.charAt(i));
			if(hex.length() == 1) hex = "0" + hex;
			rtn += hex;
			xorIndex++;
			if(xorIndex == xor.lengthxorIndex = 0;
		}
		return rtn;
	}

Calculate a login security code from the user id and password.

Parameters:
uid The user id to encode
pwd The password to encoude
Returns:
The code, which is used to login
	protected int calculateCode(String uidString pwd)
	{
		int sn = uid.charAt(0) - 96;
		int pw = pwd.charAt(0) - 96;
		int a = sn * 7696 + 738816;
		int b = sn * 746512;
		int c = pw * a;
		return (c - a + b + 71665152);
	}

Send a string over the socket as raw bytes

Parameters:
str The string to send
Throws:
java.io.IOException
	protected void sendRaw(String strthrows IOException
	{
	}

Write a little endian word

Parameters:
word A word to write
Throws:
java.io.IOException
	protected void writeWord(short wordthrows IOException
	{
		.write((byte)((word >> 8) & 0xff));
		.write((byte)(word & 0xff));
	}

Send a FLAP signon packet

	protected void sendFlapSignon() throws IOException
	{
		int length = 8 + .length();
		.write((byte)'*');
		.write((byte));
		writeWord((short)length);
		writeWord((short).length());
	}

Send a FLAP packet

Parameters:
type The type DATA or SIGNON
str The string message to send
Throws:
java.io.IOException
	protected void sendFlap(int typeString strthrows IOException
	{
		int length = str.length() + 1;
		.write((byte)'*');
		.write((byte)type);
		writeWord((short)length);
	}

Get a FLAP packet

Returns:
The data as a string
Throws:
java.io.IOException
	protected String getFlap() throws IOException
	{
		if(.read() != '*'return null;
		int length = (.read() * 0x100) + .read();
		byte b[] = new byte[length];
		.read(b);
		return new String(b);
	}

Send a IM

Parameters:
to Screen name to send an IM to
msg The instant message
	public void send(String toString msg)
	{
		try
		{
			this.sendFlap("toc_send_im " + normalize(to) + " \"" + encode(msg) + "\"");
		}
		{
			/* NOP */
		}
	}

Called to normalize a screen name. This removes all spaces and converts the name to lower case.

Parameters:
name The screen name
Returns:
The normalized screen name
	protected String normalize(String name)
	{
		String rtn = "";
		for(int i = 0; i < name.length(); i++)
		{
			if(name.charAt(i) == ' 'continue;
			rtn += Character.toLowerCase(name.charAt(i));
		}
		return rtn;
	}

Called to encode a message. Convert carige returns to
's and put \'s infront of quotes, etc.

Parameters:
str The string to be encoded
Returns:
The string encoded
	protected String encode(String str)
	{
		String rtn = "";
		for(int i = 0; i < str.length(); i++)
		{
			switch(str.charAt(i))
			{
				case '\r':
					rtn += "<br>";
					break;
				case '{':
				case '}':
				case '\\':
				case '"':
					rtn += "\\";
				default:
					rtn += str.charAt(i);
			}
		}
		return rtn;
	}

Getter for error

Returns:
Returns error.
	public String getError()
	{
		return this.;
	}

Getter for loggedin

Returns:
Returns loggedin.
	public boolean isLoggedin()
	{
		return this.;
	}
New to GrepCode? Check out our FAQ X