var arrEnabled = new Boolean(); var arrTimers = new Array(); var arrMessages = GetAllMessages();
////////////////////////////////////////////////
// FUNCTIONS DIRECTLY RELATED TO THE MESSAGES //
//////////////////////////////////////////////// function GetAllMessages() { // Normally you read in all the possible messages from a file in here // since that is easier to update.
return Array( "MessageA", "MessageB", "MessageC", "MessageD", "MessageE", "MessageF"
);
}
function GetRandomMessage() { // Get a random message from the array. // Note that in this function you could build in a system // to make sure that random messages aren't repeated too much. // For example, see: CookieRevised's reply to Random numbers, prevent from being used more than once return arrMessages[Math.floor(arrMessages.length * Math.random())];
}
/////////////////////////////////////////////////////
// FUNCTIONS TO ENABLED/DISABLE THE MESSAGE SYSTEM //
///////////////////////////////////////////////////// function OnEvent_SigninReady() { // Check the status of the user and enable/disable everything accordingly.
OnEvent_MyStatusChange(Messenger.MyStatus);
}
function OnEvent_Signout() { // User signed out, so remove all timers. This is highly important and // quite often forgotten in many scripts. If we don't do this there is // a chance that the timers will still be running when another user signs // in and thus they will conflict with his/her timers.
OnEvent_MyStatusChange(1 /* OFFLINE */);
}
function OnEvent_MyStatusChange(eNewStatus) { if (eNewStatus === 6 /* IDLE */ || eNewStatus === 7 /* AWAY */) { // Enable everything when the status of the user is set to Away or Idle.
arrEnabled = true;
} else { // Disable everything and clear the timer array.
arrEnabled = false; for (Id in arrTimers) {
MsgPlus.CancelTimer("TmrMsg" + Id); delete arrTimers[Id];
}
}
}
///////////////////////////////////////////////////////
// MAIN FUNCTION WHICH CHECKS AND SENDS THE MESSAGES //
/////////////////////////////////////////////////////// function OnEvent_ChatWndReceiveMessage(pChatWnd, sOrigin, sMessage, eMsgKind) { // First check if the away message system is active or not. if (arrEnabled) { // Check if the recieved message comes from the user or not. // There are better ways to do this though since this will fail if // the contact happens to have the same display name as the user. if (sOrigin !== Messenger.MyName) { // Check if there is already a timer running for this conversation. // If so, we have already send a message in the last 10 seconds. if (arrTimers[pChatWnd.Handle] !== true) { // There isn't a timer running for this conversation yet, // so we may send a new random message. // But first we should always check if Plus! can send a message. if (pChatWnd.EditChangeAllowed) pChatWnd.SendMessage(GetRandomMessage()); // Add the timer id to our global array so we can check later // if it is running or not. Note that this is an unique id. // This makes that if there are multiple conversations, they // wont influence the delay of other conversations. To make an // unique id we use the window handle of the conversation which // is always an unique number in Windows.
arrTimers[pChatWnd.Handle] = true;
} // Initialize the timer for this chat window (again). Note that this // is placed outside that last check. This makes that the timer will // also be reset in case the contact typed a message before the // timeout occured. This means that the contact will never see a new // message again as long as he is sending messages within 10 seconds. // If you don't want this, and you want the contact to see a new // message after 10 seconds, even if he is constantly sending messages // himself, then move the next line right after the above line within // the previous IF-block.
MsgPlus.AddTimer(pChatWnd.Handle, TimeOut * 1000);
}
}
}
////////////////////////////////////
// FUNCTIONS TO HANDLE THE TIMERS //
//////////////////////////////////// function OnEvent_Timer(sTimerId) { // Timeout has elapsed, so remove its indication from the global timer array. // Note: the unique identifier, the window handle, starts after "TmrMsg". delete arrTimers[sTimerId.substr( 6)];
}
function OnEvent_ChatWndDestroyed(pChatWnd) { // A chat window is being closed, so we don't need the timer // for this window anymore. Removing this timer makes that // we can immediatly reuse its unique value in case a new // window with the same handle is created.
MsgPlus.CancelTimer("TmrMsg" + pChatWnd.Handle); delete arrTimers[pChatWnd.Handle];
}
This post was edited on 06-30-2008 at 01:38 AM by CookieRevised.