package test;

import com.wilko.jaim.*;


/**
 * Example test code for Jaimlib. Login to AIM, send a message,
 * and disconnect.
 *
 * Run this program with:
 *   java test.JaimMessageTest yourname yourpass buddyname
 * It will login using yourname and yourpass (password), and it'll
 * send a rather dull message to the buddy specified as buddyname.
 * (Maybe best to have that be yourself under a different name, or
 * one of your teammates testing this out with you.)
 */

public class JaimMessageTest implements JaimEventListener {

  // Hostname and port number to connect to.
  //
  private static String HOST = "toc.oscar.aol.com";
  private static int PORT = 9898;

  // text for message
  //
  private static String MSGTXT = "This is a boring test message.";

  // Our connection to the AIM server.
  //
  private JaimConnection connection;


  public JaimMessageTest(String name, String pass) {
    try {
      connection = new JaimConnection(HOST, PORT);
      connection.connect();
      connection.watchBuddy(name);
      connection.logIn(name, pass, 15000);
      connection.addEventListener(this);
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(1);
    }
  }

  public void sendMessage(String buddy) {
    try {
      connection.sendIM(buddy, MSGTXT);
      System.out.println("Message successfully sent.");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void close() {
    connection.logOut();
  }


  // This method is requied by the JaimEventListener interface. It
  // processes notification events from the JaimConnection object.
  // We don't do anything here except note that they arrived. (The useful
  // information, though, is in the TocResponse.)
  // 
  public void receiveEvent(JaimEvent ev) {
    System.out.println("Received " + ev.getTocResponse().getClass());
  }


  public static void main(String args[]) {
    if (args.length != 3) {
      System.err.println("Usage: java test.JaimMessageTest yourname yourpassword buddyname");
      System.exit(1);
    }
    JaimMessageTest test = new JaimMessageTest(args[0], args[1]);
    test.sendMessage(args[2]);
    test.close();
  }

}
