Shoutbox

Window Close after Signout - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Window Close after Signout (/showthread.php?tid=65170)

Window Close after Signout by Eagle_Erwin on 08-20-2006 at 09:07 PM

I'm trying to make a script that closes the conversation-window when a contact signs out.

The code I have now:

code:
function OnEvent_ContactSignout(Email){
    var contact=Messenger.MyContacts.GetContact(Email);
    var chats = Messenger.CurrentChats;
        var e = new Enumerator(contacts);
        for(; !e.atEnd(); e.moveNext()){
            var chat = e.item();
            var contact1=chat.Contacts
            if(contact==contact1){
                chat.Close();
            }
        }
}


But, this doesn't work. What is wrong in this script, or what script can I use to close the conversation window of the person that signs out?
RE: Window Close after Signout by Spunky on 08-20-2006 at 09:42 PM

Does chat.Contacts return the email or name of the contact, couldn't tell by quickly glancing at the documentation... It could just that.

Also, it may possibly be that "chat.Close()" should be "chat.Close(0)", as I think it MUST have an exit code. :p


RE: Window Close after Signout by artfuldodga on 08-20-2006 at 09:48 PM

I'd like to use this script when its finished :) .. any way to get rid of that popup 'you are signing out of messenger all conversations will be closed' ?


RE: Window Close after Signout by Spunky on 08-20-2006 at 09:50 PM

I'm guessing that would require some way of hooking the WLM itself... Some sort of SendKeys command maybe, but that would be a bit messy and awkward to do


RE: RE: Window Close after Signout by Eagle_Erwin on 08-20-2006 at 10:00 PM

quote:
Originally posted by SpunkyLoveMuff
Does chat.Contacts return the email or name of the contact, couldn't tell by quickly glancing at the documentation... It could just that.

Also, it may possibly be that "chat.Close()" should be "chat.Close(0)", as I think it MUST have an exit code. :p

chat.Contacts returns an enumeration of all contacts active in the chat window. So it returns an object of the type contact.
The use of an exit-code didn't have the right effect, it still doesn't work.

Edit:
I already found an error in this script:
code:
function OnEvent_ContactSignout(Email){
var contact=Messenger.MyContacts.GetContact(Email);
var chats = Messenger.CurrentChats;
var e = new Enumerator(chats);
for(; !e.atEnd(); e.moveNext()){
var chat = e.item();
var contact1=chat.Contacts
if(contact==contact1){
chat.Close();
}
}
}

In the declaration of var e "contacts" must be "chats". The problem isn't solved by this change.
RE: Window Close after Signout by Spunky on 08-20-2006 at 10:06 PM

code:
function OnEvent_ContactSignout(Email){
var contact=Messenger.MyContacts.GetContact(Email);
var chats = Messenger.CurrentChats;
var e = new Enumerator(chats);
for(; !e.atEnd(); e.moveNext()){
var chat = e.item();
var contact1=chat.Contacts
if(contact==contact1){
chat.Close();
}
}
}


EDIT - Beat me to it... lol. That didn't fix it?
RE: Window Close after Signout by Eagle_Erwin on 08-20-2006 at 10:08 PM

Nope, it didn't.

Great minds think alike :P


RE: Window Close after Signout by CookieRevised on 08-20-2006 at 10:20 PM

quote:
Originally posted by Eagle_Erwin
I'm trying to make a script that closes the conversation-window when a contact signs out.

The code I have now:
(...)

But, this doesn't work. What is wrong in this script, or what script can I use to close the conversation window of the person that signs out?
There is too much wrong to list, so here is the fixed script. Things you got wrong are corrected in in red:

code:
function OnEvent_ContactSignout(Email) {
    var contact = Email;
    var chats = Messenger.CurrentChats;
    var e = new Enumerator(chats);
    for(; !e.atEnd(); e.moveNext()) {
        var chat = e.item();
        var f = new Enumerator(chat.Contacts);
        for(; !f.atEnd(); f.moveNext()) {

            var contact1 = f.item();
            if (contact == contact1.Email) {
                Interop.Call("User32", "SendMessageA", chat.Handle, 0x10, 0, 0)
                break;
            }
        }
    }
}
in short:
code:
function OnEvent_ContactSignout(Email) {
    for(var e = new Enumerator(Messenger.CurrentChats); !e.atEnd(); e.moveNext()) {
        var chat = e.item();
        for(var f = new Enumerator(chat.Contacts); !f.atEnd(); f.moveNext()) {
            if (Email == f.item().Email) {
                Interop.Call("User32", "SendMessageA", chat.Handle, 0x10, 0, 0);
                break;
            }
        }
    }
}

But I also advise not to use this script as-is. 2 major drawbacks:
1) Closing a convo automatically like that will make you often miss messages send by people (even when you implement a delay).
2) Using the logic you used, it will also close conversation with multiple contacts whenever 1 of those contacts signs out. See mickael9's script in the next post for a better logic and a way to fix this.

RE: Window Close after Signout by mickael9 on 08-20-2006 at 10:22 PM

quote:
Originally posted by Eagle_Erwin
I'm trying to make a script that closes the conversation-window when a contact signs out.

The code I have now:

code:
function OnEvent_ContactSignout(Email){
    var contact=Messenger.MyContacts.GetContact(Email);
    var chats = Messenger.CurrentChats;
        var e = new Enumerator(contacts);
        for(; !e.atEnd(); e.moveNext()){
            var chat = e.item();
            var contact1=chat.Contacts
            if(contact==contact1){
                chat.Close();
            }
        }
}


But, this doesn't work. What is wrong in this script, or what script can I use to close the conversation window of the person that signs out?


Hi,
this should work :
code:
function OnEvent_ContactSignout(sEmail)
{
    CloseWindows();
}
function OnEvent_ChatWndContactRemoved(ChatWnd, sEmail)
{
    CloseWindows();
}
function CloseWindows()
{
    var chats = Messenger.CurrentChats;
    var e = new Enumerator(chats);

    for(; !e.atEnd(); e.moveNext())
    {
        var chat = e.item();
        var e2 = new Enumerator(chat.Contacts);
        var bCanClose = true;
        for (; !e2.atEnd(); e2.moveNext())
        {
            var contact = e2.item();
            bCanClose &= (contact.Status != 1);
        }
        if (bCanClose)
            Interop.Call("user32","SendMessageW", chat.Handle, 16, 0, 0);
    }
}

It works when :
You are in a conversation with 1 contact and he signs out
You are in a conversation with 5 contacts, 2 signs out and 3 leaves the conversation.
RE: Window Close after Signout by Spunky on 08-20-2006 at 10:23 PM

Cookie, you beat me to a couple of the things in there... I was looking through the documentation (like a good little boy) and realised he would need to enumerate chat.Contacts. I also started thinking about the Interop.Call as I wasn't able to find a ChatWnd.Close function


RE: Window Close after Signout by absorbation on 08-20-2006 at 10:28 PM

I suggest adding a timer to the script. Which the user can customise to what they want, e.g. make the window close after 10 seconds of the contact signing out. I personally would want it 10 minutes after a contact signed out. I often have tabs with tons of offline contacts who I stopped speaking to say, an hour ago :).

Adding a timer would make the script a lot better :P.


RE: RE: Window Close after Signout by Eagle_Erwin on 08-20-2006 at 10:31 PM

quote:
Originally posted by CookieRevised
quote:
Originally posted by Eagle_Erwin
I'm trying to make a script that closes the conversation-window when a contact signs out.

The code I have now:
(...)

But, this doesn't work. What is wrong in this script, or what script can I use to close the conversation window of the person that signs out?
There is too much wrong to list, so here is the fixed script. Things you got wrong are corrected in in red:

code:
function OnEvent_ContactSignout(Email) {
        var contact = Email;
        var chats = Messenger.CurrentChats;
        var e = new Enumerator(chats);
        for(; !e.atEnd(); e.moveNext()) {
                var chat = e.item();
                var f = new Enumerator(chat.Contacts);
                for(; !f.atEnd(); f.moveNext()) {

                        var contact1 = f.item();
                        if (contact == contact1.Email) {
                                Interop.Call("User32", "SendMessageA", chat.Handle, 0x10, 0, 0)
                                break;
                        }
                }
        }
}


But I also advise not to use this script as-is. 2 major drawbacks:
1) It will also close conversation with multiple contacts whenever 1 of those contacts signs out
2) Closing a convo automatically like that will make you often miss messages send by people

Thank you very much, never doubt about it: it works!

And about your advice: The script is used in a WLM-bot, so multiple contacts in one chat doesn't exists in this case. And about your second drawback: Because it is a bot, it doen't matter to ME what the last was the contact said, as long as the bot knows it. And the bot is fast enough, it will 'read' it ;)
RE: Window Close after Signout by CookieRevised on 08-20-2006 at 10:49 PM

IF you will never encounter multiple contacts in one chat you can remove the for...loop:

code:
for(; !f.atEnd(); f.moveNext()) {
      var contact1 = f.item();
      if (contact == contact1.Email) {
            Interop.Call("User32", "SendMessageA", chat.Handle, 0x10, 0, 0)
            break;
      }
}
thus, replace all the above with:
code:
var contact1 = f.item();
if (contact == contact1.Email) {
      Interop.Call("User32", "SendMessageA", chat.Handle, 0x10, 0, 0)
}
or thus simple one line:
code:
if (contact == f.item().Email) Interop.Call("User32", "SendMessageA", chat.Handle, 0x10, 0, 0)

RE: RE: RE: Window Close after Signout by Jesus on 08-20-2006 at 11:19 PM

quote:
Originally posted by Eagle_Erwin
Thank you very much, never doubt about it: it works!

And about your advice: The script is used in a WLM-bot, so multiple contacts in one chat doesn't exists in this case. And about your second drawback: Because it is a bot, it doen't matter to ME what the last was the contact said, as long as the bot knows it. And the bot is fast enough, it will 'read' it ;)

Well for your purpose it is fine this way, but to make it really useful it should detect whether the window is "flashing" or not, so it doesn't close unread messages.
I know it's harder to code, but it would be neat to have that functionality :D
RE: RE: Window Close after Signout by mickael9 on 08-20-2006 at 11:33 PM

quote:
Originally posted by absorbation
I suggest adding a timer to the script. Which the user can customise to what they want, e.g. make the window close after 10 seconds of the contact signing out. I personally would want it 10 minutes after a contact signed out. I often have tabs with tons of offline contacts who I stopped speaking to say, an hour ago :).

Adding a timer would make the script a lot better :P.

Done :D
code:
var Timers     = {};
var TimerCount = 0;

function OnEvent_ContactSignout(sEmail)
{
    CloseWindows();
}
function OnEvent_ChatWndContactRemoved(ChatWnd, sEmail)
{
    CloseWindows();
}
function OnEvent_Timer(sTimerId)
{
    if (sTimerId.substr(0, 13) == "AutoCloseWin_")
    {       
        try
        {
            Interop.Call("User32","SendMessageW", Timers[sTimerId].Wnd.Handle, 16, 0, 0);
        }
        catch (e){};
        delete(Timers[sTimerId]);
        TimerCount--;
    }
    else if (sTimerId.substr(0, 19) == "AutoCloseWinCancel_")
    {
        var oParams      = Timers[sTimerId];
        var sOldText     = oParams.EditText;
        var nOldSelStart = oParams.SelStart;
        var nOldSelEnd   = oParams.SelEnd;
        var Wnd          = oParams.Wnd;
       
        Wnd.EditText = sOldText;
        Wnd.EditText_SetCurSel(nOldSelStart, nOldSelEnd);
        MsgPlus.DisplayToast("","Canceled !");
       
        delete(Timers[sTimerId]);
        TimerCount--;
    }
}
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage)
{
    if (sMessage.substr(0,8) == "!cancel ")
    {
        var regex = /^!cancel ([0-9]+)/;
        var match = regex.exec(sMessage);
        if (!match)
            return sMessage;

        var sId = match[1];
        var sTimerId = "AutoCloseWin_" + sId;
       
        if (typeof(Timers[sTimerId]) != "undefined")
        {
            MsgPlus.CancelTimer(sTimerId);
            var Params = Timers[sTimerId];
            delete(Timers[sTimerId]);
           
            sTimerId = "AutoCloseWinCancel_" + sId;
            Timers[sTimerId] = Params;
            MsgPlus.AddTimer(sTimerId, 100);
            return '';
        }
    }
}
function CloseWindows()
{
    var chats = Messenger.CurrentChats;
    var e = new Enumerator(chats);

    for(; !e.atEnd(); e.moveNext())
    {
        var chat = e.item();
        var e2 = new Enumerator(chat.Contacts);
        var bCanClose = true;
        for (; !e2.atEnd(); e2.moveNext())
        {
            var contact = e2.item();
            bCanClose &= (contact.Status == 1);
        }
        if (bCanClose)
            CloseWindow(chat);
    }
}
function CloseWindow(oWnd)
{
    var nId      = (++TimerCount);
    var sTimerId = "AutoCloseWin_" + nId;
   
    var sText = "!cancel " + nId + " [This conversation will be closed in 10 seconds; send this text to cancel and restore the text typed here :)]";
   
    var nSelStart = oWnd.EditText_GetCurSelStart();
    var nSelEnd   = oWnd.EditText_GetCurSelEnd();
    var sOldText  = oWnd.EditText;
   
    var Params   = {"Wnd" : oWnd, "SelStart" : nSelStart, "SelEnd" : nSelEnd, "EditText" : oWnd.EditText};

    oWnd.EditText = sText;
    oWnd.EditText_SetCurSel(0, sText.length);
   
    Timers[sTimerId] = Params;
    MsgPlus.AddTimer(sTimerId, 10000);
}