#include <stdlib.txt>

sleep(200);
if (getActiveWindow() == "MessengerScreen") {
	// We landed on a conversation window - jump directly to the buddy list
	click(BACK_BUTTON);
	sleep(500);
} else if (getActiveWindow() == "LogonWindow") {
	// If we're not signed in, attempt to do so
	if (isButtonEnabled("Sign")) {   // "Sign On" for AOL, "Sign In" for Yahoo and MSN
		executeButton("Sign");
		sleep(15000);
	} else if (isButtonEnabled("Cancel")) {
		// We may be auto-logging-in after a reboot
		sleep(15000);
	} else {
		// The screenname and password are not already filled in
		sendService("scripttestcase", "Couldn't sign in");
		return;
	}
}
waitForWindowRetries("BuddyBrowser", 50);
// Make sure we are now on the buddy list
if (getActiveWindow() != "BuddyBrowser") {
	sendService("scripttestcase", "Didn't get BuddyBrowser");
	return;
}
sleep(3000);
// Close all existing conversations
click(CANCEL_BUTTON, SHIFT);
sleep(1000);

if (isActionMenuItemEnabled ("Send IM..." )) {
	// This works for AOL and Yahoo
	executeActionMenuItem ("Send IM..." );
	sleep(500);
	keystring("DangerSmokeBot", 300);
	executeButton("Start Conversation");
	sleep(500);
} else {
	if (isActionMenuItemEnabled ("Sort By")) {
		// This is for MSN.  There is no "Send IM...", so we have to select a contact.
		// This relies on DangerSmokeBot being the only contact
		executeActionMenuItem ("Groups");  // Sort by groups (eliminates categories)
		sleep(200);
		click(WHEEL_BUTTON);               // Select the "DangerSmokeBot" contact
		sleep(500);
	} else {
		sendService("scripttestcase", "Couldn't send IM");
		return;
	}
}

keystring("This is a test IM", 300);
keypress(13);
sleep(10000);
// If our buddy is not connected, an alert will happen.  Might happen for other reasons too?
if (getActiveWindow() == "AlertWindow") {
	executeButton("OK");
	sendService("scripttestcase", "Couldn't send IM (DangerSmokeBot not online?)");
	return;
}
// Now, to get the result of our conversation out of this script, we copy it to the pasteboard
if (isActionMenuItemEnabled ("Copy Conversation" )) {
	executeActionMenuItem ("Copy Conversation" );
} else {
	sendService("scripttestcase", "Couldn't copy conversation");
	return;
}
sleep(3000);
sendService("scripttestcase", "DONE");
