Shoutbox

Nickname highlight - 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: Nickname highlight (/showthread.php?tid=86201)

Nickname highlight by Johnny09 on 09-28-2008 at 04:02 PM

Hello community,

those users who use irc may already know it. once your nickname is mentioned in a channel a small box appears in the lower right corner of your desktop and you get informed about this (e.g. Nickname 'Johnny' was mentioned in #irc).

is there a similar function to this for msgplus?

it would be useful e.g. in a mass conversation.

a normal msgplus message (MsgPlus.DisplayToastContact("Information","Your nickname has been mentioned by User")) should appear.

im also a scripter, but not experienced in msgplus scripting.
so it would be nice if you could help me.


its okay.. i figured it out myself:

--
var highwords = new Array ("johnny", "jay");

function OnEvent_Initialize(MessengerStart)
{
}

function OnEvent_Uninitialize(MessengerExit)
{
}

function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MsgKind)
{
     var e = new Enumerator(ChatWnd.Contacts);
     var contact = e.item();

    checkAndDisplay (Message, contact.Name);
}

function checkAndDisplay (Message, my)
{
    for (i in highwords) {
        if (Message.toLowerCase().search(highwords[i]) != -1) {
            MsgPlus.DisplayToastContact("Nick Highlighter", "[c=#3682B4]Highlight[/c]", "'"+ucfirst(highwords[i])+"' has been mentioned by: " + my, null, "locateDetectedAddress");
        }
    }
}

function ucfirst(str) {
    return str.substr(0,1).toUpperCase() + str.substr(1,str.length);
}
RE: Nickname highlight by Spunky on 09-28-2008 at 05:53 PM

code:
var keywords= new Array("Spunky","SLM","Shane");

function OnEvent_ChatWndReceiveMessage(ChatWnd, Message){
    if(Origin!==Messenger.MyName){
        for(var s in keywords){
            if(Message.search(keywords[s])!==-1){
                 MsgPlus.DisplayToastContact("Nick Highlighter", "[c=#3682B4]Highlight[/c]",keywords[s] + " has been mentioned by " + MsgPlus.RemoveFormatCodes(Origin);
            }
        }
    }
}


Should do the same thing, but more efficiently... Could probably be improved upon though. Also, you can add other words into the array apart from names such as words relating to certain topics :p
RE: Nickname highlight by markee on 09-29-2008 at 11:01 AM

An alternative to Spunky's code is to use a regular expression, which would make the code look something like this.

code:
var keywords= new Array("Spunky","SLM","Shane");
var re = new RegExp("\\b("+keywords.join("|")+")\\b","ig");

function OnEvent_ChatWndReceiveMessage(ChatWnd, Message){
    if(Origin!==Messenger.MyName){
        var arr;
        while((arr = re.exec(Message))!==null){
            MsgPlus.DisplayToastContact("Nick Highlighter", "[c=#3682B4][b]Highlight[/b][/c]",RegExp.$1 + " has been mentioned by " + MsgPlus.RemoveFormatCodes(Origin);
        }
    }
}

Or if you didn't want it to list every word that is matched, which was sent in a single message (something like "Spunky SLM" would generate 2 toasts). then you could use the following.

code:
var keywords= new Array("Spunky","SLM","Shane");
var re = new RegExp("\\b("+keywords.join("|")+")\\b","i");

function OnEvent_ChatWndReceiveMessage(ChatWnd, Message){
    if(Origin!==Messenger.MyName && re.exec(Message)!==null) MsgPlus.DisplayToastContact("Nick Highlighter", "[c=#3682B4][b]Highlight[/b][/c]",RegExp.$1 + " has been mentioned by " + MsgPlus.RemoveFormatCodes(Origin);
}

Also, I should note that the \\b used in the regex means that it must be the whole word (unlike spunky's which will match even "ASLMNDER" or something similar if it were ever to occur).  The i modifier allows for the letters to be in upper or lower case, just so that it doesn't matter if your friend types your name in caps for some unknown reason.

Obviously you could make the code smaller by writing straight into the regex, however I would recommend keeping the regex as a global variable unless it became contact dependant as this means it only needs to loaded the once.
RE: Nickname highlight by Spunky on 09-29-2008 at 11:05 AM

Lol. I could have added a space to either side of the word when checking... I should have tested it lol :p


RE: Nickname highlight by markee on 09-29-2008 at 11:22 AM

quote:
Originally posted by SpunkyLoveMuff
Lol. I could have added a space to either side of the word when checking... I should have tested it lol :p
And what about if it was a comma, or full-stop, or semicolon, or exclamation mark, etc....?
RE: Nickname highlight by Spunky on 09-29-2008 at 03:34 PM

quote:
Originally posted by markee
quote:
Originally posted by SpunkyLoveMuff
Lol. I could have added a space to either side of the word when checking... I should have tested it lol :p
And what about if it was a comma, or full-stop, or semicolon, or exclamation mark, etc....?

RegExp :p

use \W to check that it's not an alphanumeric character before the word. I do agree RegExps are great, but I tried to keep it simple.

It's because of you I started using them in the first place :p
RE: Nickname highlight by Johnny09 on 10-03-2008 at 05:26 PM

yeah, regexp is also a good solution,
thought to use also a reg.expression but at this time ive had a blackout and have forgotten that "new RegExp()" exists ... :P

i have another question,
on highlighting, a message (ToastContact) appears ...

can we handle it like this, when you click on that message, the window where they highlighted your name opens at the same time? (set a focus on that msn window).


// Edit
found something nice on the web

....
MsgPlus.DisplayToastContact("Highlighter", "[c=#3682B4]Highlight[/c]", "'"+ucfirst(highwords[i])+"' has been mentioned by: " + MsgPlus.RemoveFormatCodes(contact.Name), null, "focusMyWin", ChatWnd.Handle);
...

code:
function focusMyWin (dow) {
    Interop.Call('User32','OpenIcon', dow);
    Interop.Call('User32','BringWindowToTop', dow);
    Interop.Call('User32','SetForegroundWindow', dow);
    Interop.Call('User32','ShowWindow', dow, 1);
    Interop.Call('User32','SwitchToThisWindow', dow, "FALSE");
}