Shoutbox

[Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin - 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: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin (/showthread.php?tid=70921)

[Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by markee on 01-22-2007 at 03:05 AM


Please  refer to CookieRevised's reply to [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin as this give proper code.



The rest of this post has been kept for understanding the flow of the thread, the code in it has some flaws and is recommended not to be used, sorry.


A note to script developers:

Because of the StuffPlug Chat Only names feature you will have to use a different method when checking the origin of a received message.

Instead of using:
code:
function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageKind){//When a message is recieved in a chat window
     if(Origin !== Messenger.MyName){//If the sender's name is not my name
          //Do stuff here
     }
     //Do more here if you like
}

You should use the following code:
code:
function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageKind){//When a message is recieved in a chat window
     var WSH = new ActiveXObject("WScript.Shell");
     try{
          var enabled = WSH.RegRead("HKLM\\SOFTWARE\\iAvatars.com\\StuffPlug\\"+Messenger.MyUserId+"\\ChatName");//check id a StuffPlug Registry for Chat Only names exists
          if(enabled === 1){//Check if Chat Only names are enabled
               try{
                    var nick = WSH.RegRead("HKLM\\SOFTWARE\\iAvatars.com\\StuffPlug\\"+Messenger.MyUserId+"\\szChatName");//Test for what the Chat Only name currently is
               }catch(e){//if there is no registry for the Chat Only name
                    var nick = Messenger.MyName;
               }
          }else{//if Chat Only names are _NOT_ enabled
               var nick = Messenger.MyName;
          }
     }catch(e){//if there is no registry for if Chat Only names (ie. StuffPlug is not installed)
          var nick = Messenger.MyName;
     }
     if(Origin !== nick){//If the sender's name is not my name
          //Do stuff here
     }
     //Do more here if you like
}

I do realise this is a little more complex to do, but it will fix this problem that may be effecting your scripts.

Thanks Cookie and deAd for informing me about my mistakes
RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by deAd on 01-22-2007 at 03:26 AM

First of all, Messenger.MyUserId is not going to equal the origin unless someone has set their name to it :S (typo/mixup?).

Also, there's an easier method. If you're trying to just find which message is yours, you can do this:

code:
var bSent = false; // a global variable

function OnEvent_ChatWndSendMessage(ChatWnd, Message){
bSent = true; // we've sent a message so set bSent to true
}

function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind){
if(bSent){ // if bSent is true, meaning we just sent a message
bSent = false; // we caught our message so reset it
} else {
// a contact sent the message, so do whatever
}
}
This code ignores the first message that's been received after you send one, which is almost always yours because messenger places your sent message into the textbox immediately after you press Send or Enter.
RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by CookieRevised on 01-22-2007 at 03:36 AM

- 'Origin' is not equal to a user id, it is the nickname
- You need to check on reg setting 'DisabledForxxxxxxxxxx' first
- Reg setting 'ChatName' is a DWORD not a string, thus it will be returned as a number, using ==='1' will always fail.
- You must take in account that 'szChatName' doesn't exist even if ChatName is enabed, in that case the name is 'StuffPlug User'
- Some comments are not prefixed with //
- Code can be optimized a lot


EDIT: I see dead posted first.... anyhow, dead's code can be optimized too...


RE: RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by deAd on 01-22-2007 at 03:42 AM

quote:
Originally posted by CookieRevised
EDIT: I see dead posted first.... anyhow, dead's code can be optimized too...
I never said it was perfect :P However, even without optimization I'd consider it to be better than what markee posted. It doesn't rely on the registry, which could change in future SP3 versions (unlikely but possible!). It also doesn't read from the registry every time you receive a message :P .. It's also shorter, less confusing, etc. But yes I agree it could be optimized at least a little.
RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by markee on 01-22-2007 at 03:45 AM

@ deAd: Your code has a chance of not working properly.  There is a chance that a contact sends a message between these events being called and that will cause problems.

@ Cookie: Where is the registry 'DisabledForxxxxxxxxxx' I have found nothing with a search of the registry with a search for 'DisabledFor'.  Also I wouldn't mind seeing the code optimised either, that was just the easiest way I could think about doing it.


RE: RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by deAd on 01-22-2007 at 04:11 AM

quote:
Originally posted by markee
@ deAd: Your code has a chance of not working properly.  There is a chance that a contact sends a message between these events being called and that will cause problems.
This is highly, highly, highly unlikely. Right from the Plus! documentation:

quote:
Originally posted by Documentation
Because Messenger Plus! analyses messages received only when they reach the chat window, it is not possible to determine exactly from which contact the message came from. However, several methods can be used to guess if the message was actually sent by the current user. For example, to determine whether or not the personalized status messages should be sent when a message is received, Messenger Plus! compares the time of the last ChatWndSendMessage event with the current time. If it is less than 1 second, the message is considered to come from the current user and the previously recorded time is reset. Even if this method of analysis can seem too simplistic, it works in almost every scenario as it is extremely rare to receive a message sent by a contact between the time the "Send" button was pressed and the time the message of the current user was added to the window (as Messenger does not wait to receive a reply from the server before adding the message to the history control).

The code I posted works in a similar way to what Plus! describes. However, instead of using a timer, it simply checks the next message. In this way, it is susceptible to the same extremely rare problem that Plus!'s detection has, but also accounts for messenger hanging for any reason and the message being delivered after more than just a second.

The way you are doing it is more prone to error and confusion because somebody can set their name/chat-only name to yours. The chance of this is much, much higher than the chance of a message being received between the time the Send button was pressed and the sent message is added to the history box.
RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by CookieRevised on 01-22-2007 at 04:14 AM

quote:
Originally posted by deAd
The code I posted works in a similar way to what Plus! describes.
not at all... it will fail in many occasions...
quote:
Originally posted by deAd
However, even without optimization I'd consider it to be better than what markee posted.
it is worse though...


----------------------------





Code which checks upon the registry for StuffPlug's ChatOnly name:
code:
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind) {

    var sMyChatName = Messenger.MyName;
    var sStuffPlugPath = "HKLM\\SOFTWARE\\iAvatars.com\\StuffPlug\\";
    var oWSH = new ActiveXObject("WScript.Shell");
    try {
        var bStuffPlugEnabled = true;
        var bStuffPlugEnabled = oWSH.RegRead(sStuffPlugPath + "DisabledFor" + Messenger.MyUserId) == 0;
    } catch( e){}
    try {
        if (bStuffPlugEnabled) {
            if (oWSH.RegRead(sStuffPlugPath + Messenger.MyUserId + "\\ChatName") == 1) {
                var sMyChatName = "StuffPlug User";
                var sMyChatName = oWSH.RegRead(sStuffPlugPath + Messenger.MyUserId + "\\szChatName");
            }
        }
    } catch( e){}

    if (Origin !== sMyChatName) {
        // a contact sent the message, so do whatever
    }
}
- will fail if contact has same chat name as user.




Code without checking (maybe version dependant) registry settings:
code:
var aSent = new Array();

function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
    aSent[ChatWnd.Handle] = Message;
}

function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind) {
    if (aSent[ChatWnd.Handle] === Message) {
        delete aSent[ChatWnd.Handle];
    } else {
        // a contact sent the message, so do whatever
    }
}

RE: RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by deAd on 01-22-2007 at 04:17 AM

quote:
Originally posted by CookieRevised
quote:
Originally posted by deAd
The code I posted works in a similar way to what Plus! describes.
not at all... it will fail in many occasions...
It will fail in the same occasions as Plus! does -- when a message is received between the press of the Send button/enter key and when it is entered to the history box.

It does do what the documentation describes, except without a timer. This just accounts for one other potential (and unlikely) problem.
RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by CookieRevised on 01-22-2007 at 04:22 AM

quote:
Originally posted by deAd
It will fail in the same occasions as Plus! does
No... it will fail in much more occasions...
quote:
Originally posted by deAd
It does do what the documentation describes, except without a timer.
The timer is there for a reason! But even without the timer and the issue of 'pending' messages, your code will still fail in many more occasions than Plus! does.

---

For both, please see the codes I posted...

sorry to be so short, but I haven't the time atm
RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by markee on 01-22-2007 at 04:48 AM

Cookie, I changed your code because I still could not find that registry on my computer and so I would imagine other people would have the same problem as I am having if this was used.

code:
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind) {

    var sMyChatName = Messenger.MyName;
    var sStuffPlugPath = "HKLM\\SOFTWARE\\iAvatars.com\\StuffPlug\\";
    var oWSH = new ActiveXObject("WScript.Shell");
    try {
        try{
            var reg = oWSH.RegRead(sStuffPlugPath + "DisabledFor" + Messenger.MyUserId)
            throw "";
        }catch( e){
            if(reg !== undefined && reg !== 0){
                throw "";
            }
            if (oWSH.RegRead(sStuffPlugPath + Messenger.MyUserId + "\\ChatName") === 1) {
                var sMyChatName = "StuffPlug User";
                try {
                    var sMyChatName = oWSH.RegRead(sStuffPlugPath + Messenger.MyUserId + "\\szChatName");
                } catch( e){}
            }
        }
    } catch( e){}

    if (Origin !== sMyChatName) {
        // Do stuff here
    }
    // Do more here if you like
}

RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by CookieRevised on 01-22-2007 at 05:00 AM

'DisabledFor' is only created when you disable/enable StuffPlug. Before that it indeed doesn't exist... This is fixed (in a shorter/elegant way) in my code.

PS: there is a reason why I used equality operators instead of identity operators though. Don't change that or the function will fail again in some (very rare) cases. ;)


RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by bigbob85 on 12-14-2007 at 10:49 PM

CookieRevised, what sort of instances would it fail?


RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by CookieRevised on 12-14-2007 at 11:52 PM

RegRead can return other stuff than integers. It depends on the registry key type. So, if the user (for whatever reason) had manually changed the reg key type to a string (which is VERY common actually in scripts made by newbies), then the string "1" is not the same as the number 1 when you use identity operators (===).

When you use an equality operator (==) then the string "1" is the same as 1.

So, if somebody made a script which doesn't take in account that the registry key 'ChatName' is supposed to be of the REG_DWORD type (instead of a REG_SZ) when you change/write the value, then Markee's script will fail.


RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by bigbob85 on 12-15-2007 at 12:16 AM

Ah ok. Makes sense.
Any idea if this is the same in C/C++/Java? Did first year of uni so far, and I know of the ===  and I knew it was more strict or something, but we never learnt them in Java or C/C++...
Maybe they were just trying not to confuse the newbie programmers there...


RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by markee on 12-15-2007 at 05:16 AM

The === is the same in all languages to the best of my knowledge.  And I believe you still have to specify how the variable is stored in the registry, however you can make a fix with stuff like a function to do it for you and using typeOf method with a switch statement (but this can wait for another thread ;)).


RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by Deco on 12-16-2007 at 11:30 AM

Cookie,
that check you sent using message compare will fail with a simple "hi". Maybe you could use all three types sent in so far. Use deads boolean , the check user name and then compare the message. I guess that would make it very unlikely to fail.
Have fun!


RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by CookieRevised on 12-16-2007 at 01:18 PM

quote:
Originally posted by markee
I believe you still have to specify how the variable is stored in the registry
You should, but you don't need to. And that is exactly what many new scripters forget to do. When you don't do it and depending on how you've set up the variable or what method you use to write to the registry, it can revert back to the default REG_SZ, a string, not REG_DWORD, a number, for example.

And that is exactly why I didn't use an identity (===) operator, but a equality (==) operator, because you don't know what _other_ scripts have done with that value...

Bottom line:
When reading something from the registry: always use a equality operator, or use a similar function to handle unexpected types.
When writing to the registry: always define the correct type of the value, and don't trust upon 'default' settings.


quote:
Originally posted by Deco
Cookie,
that check you sent using message compare will fail with a simple "hi".
how?

quote:
Originally posted by Deco
Maybe you could use all three types sent in so far. Use deAd's boolean
His code, unfortunatly, will fail in more situations and should not be used, even not together with other methods (discussed before).

Not only will it fail when a message is recieved between you sending a message and Messenger putting your message in the chat window (which is rare, but it CAN happen; the busier you are chatting with different people at the same time, the more chance you have this will happen).

But the chance of failing will be extremely bigger when you have more than one chat going on (which is far from rare), since he used just 1 global variable for checking every possible chat window.

If you want to use a simply, easy method, use the second snipped I posted. As you will see it is basically the exact same thing as what deAd did, but it handles individual chats properly and thus will work with multipe chats. Though, it still would fail if a message is recieved between you sending a message and Messenger putting it in the chat.

(note: the snippet is just that: a snippet outlining the method. You do need to add some more things like removing the array items when a chat is closed, etc to implement it properly).
RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by markee on 12-16-2007 at 01:49 PM

quote:
Originally posted by CookieRevised
You should, but you don't need to. And that is exactly what many new scripters forget to do.
I meant in other code languages.... :P

quote:
Originally posted by Deco
Cookie,
that check you sent using message compare will fail with a simple "hi". Maybe you could use all three types sent in so far. Use deads boolean , the check user name and then compare the message. I guess that would make it very unlikely to fail.
Have fun!
I advise you not to do that, the message check of Cookie's is reliable, whereas deAd's and the name check method can fail.  Well Cookie's can too, but only when you and the contact in the message send the exact same message, but their's is recieved between your sent message and recieving it back.  But that is a far more extreme case (and deAd's method woul also fail in these circumstances, plus more).  As or the name checking, that can easily be fooled (if you contact steals your name or you use stuff plug for example).  And you cannot combine these in any way either so there is no point, otherwise you just get conflicting ideas....
RE: [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin by bigbob85 on 12-16-2007 at 10:28 PM

What if you check contact name? Will the on recieve msg show their chat only name or the same name from the main messenger window?

I expect this wont work since no one else has said it yet.

EDIT:
Nevermind..

quote:
Script is starting
Script is now loaded and ready
Function called: OnEvent_Initialize
Function called: OnEvent_ChatWndReceiveMessage
Receive Message
Origin - The "Using Chat-Only Name" Bradley
Iteration - The "Showering" Bradley - I (L) BangBang

code:
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind)
{
      Debug.Trace("Receive Message");
      Debug.Trace("Origin - "+Origin);
      var Contacts = ChatWnd.Contacts;
      var e = new Enumerator(Contacts);
      for(; !e.atEnd(); e.moveNext())
      {
            var Contact = e.item();
            Debug.Trace("Iteration - " +Contact.Name);
      }
      Debug.Trace("----------------");
}