Shoutbox

check whether if the contact is in a special list? - 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: check whether if the contact is in a special list? (/showthread.php?tid=94920)

check whether if the contact is in a special list? by ultimatebuster on 07-01-2010 at 10:24 PM

Is this a good way check if a contact is in a trusted list?

Or is there a more efficient way to do it?

code:
var TrustedEmails = "john@hotmail.com,smith@hotmail.com";

function ConfirmTrusted(ChatWnd){
// pretend i passed the ChatWnd from OnEvent_ChatWndReceiveMessage
    var e = new Enumerator(ChatWnd.Contacts);
    for (; !e.atEnd(); e.moveNext()){
        if (TrustedEmails.indexOf(e.item().Email) != -1){
            return true;
        }
    }
    return false;
}


RE: check whether if the contact is in a special list? by djdannyp on 07-01-2010 at 10:38 PM

What would the purpose of this trusted list be?


RE: check whether if the contact is in a special list? by ultimatebuster on 07-01-2010 at 10:55 PM

some evil purpose that you shouldn't know about :P

while i'm on the topic of asking questions, how do i tell between a regular message, and an offline message?

nvm i figured out that you can't. Back to the original topic.


RE: check whether if the contact is in a special list? by Matti on 07-02-2010 at 10:05 AM

Your code isn't too bad actually, however it can indeed be optimized. It's better to store your trusted emails in an array rather than a comma-separated string. This also allows you to loop over the array instead of the chat contacts.

js code:
var TrustedEmails = ["john@hotmail.com", "smith@hotmail.com"];

function ConfirmTrusted(ChatWnd){
    var i = 0, item;
    while(item = TrustedEmails[i++]) {
        if(ChatWnd.Contacts.GetContact(item) !== null) return true;
    }
    return false;
}
First, the used variables are declared at the top: i is the index iterator and item is the item iterator.

The while-condition combines the iterating (incrementing i and assigning item to the i-th array element) and checking the array length (if i exceeds the array's length, item will be undefined and thus the condition is false), making it one of the fastest ways to loop through arrays in JScript. Note that this only works for arrays where each element equals true, your array can't contain zero, false, null, undefined or empty strings.

Inside the loop, we try to find the current item in the Contacts object using GetContact(). This function returns a Contact object when the passed email is in the list, and null otherwise. Thus, to check whether the trusted email is in the chat, we simply have to see if the return value is not null. If it's not null, it means the trusted email is in the chat and thus we should return true.

As you can see, this optimizes the function by reducing the work on the JScript side. Your original code loops through all contacts in the chat and then performs an indexOf operation to see if the current chat contact is in the trusted emails list. This code loops through all trusted emails and calls a Plus! function to see if the current trusted email is in the chat contacts list. By changing the looped list, we can let Plus! do some of the work for us, which is probably much faster than any script code you write.
RE: check whether if the contact is in a special list? by ultimatebuster on 07-02-2010 at 02:11 PM

Interesting..
I thought indexOf uses a hashtable algorithm rather than a brute force search trhough all the elements.


RE: check whether if the contact is in a special list? by Matti on 07-02-2010 at 06:24 PM

quote:
Originally posted by ultimatebuster
Interesting..
I thought indexOf uses a hashtable algorithm rather than a brute force search trhough all the elements.
That may be the case for an Array.indexOf() implementation (which is not available in JScript), but String.indexOf() looks for substrings in a search string. It first has to find the first substring character in the search string and then check whether the other characters are also there, or look elsewhere. To find an item in an array in JScript, we need to rely on loops as there's no such function provided by the Windows Script Engine.
RE: check whether if the contact is in a special list? by ultimatebuster on 07-02-2010 at 06:51 PM

ah, thank you.