Shoutbox

Help - /commands! - 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: Help - /commands! (/showthread.php?tid=88858)

Help - /commands! by whiz on 02-01-2009 at 12:38 PM

Okay, here goes...

I'm making an Instant Response script, which is currently controlled entirely from the script menu, but I'm now adding /commands, which are as follows:

js code:
function OnGetScriptCommands()
{
    var ScriptCommands = "<ScriptCommands>";

    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.enable</Name>";
    ScriptCommands    +=         "<Description>Turn Instant Response on.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.disable</Name>";
    ScriptCommands    +=         "<Description>Turn Instant Response off.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.message</Name>";
    ScriptCommands    +=         "<Description>Change the Instant Response message.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.timer</Name>";
    ScriptCommands    +=         "<Description>Change the Instant Response timer.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.pluson</Name>";
    ScriptCommands    +=         "<Description>Turn the Instant Response Plus! Style on.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.plusoff</Name>";
    ScriptCommands    +=         "<Description>Turn the Instant Response Plus! Style off.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.alerton</Name>";
    ScriptCommands    +=         "<Description>Turn the Instant Response sign-in alert on.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.alertoff</Name>";
    ScriptCommands    +=         "<Description>Turn the Instant Response sign-in alert off.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    +=     "<Command>";
    ScriptCommands    +=         "<Name>ir.status</Name>";
    ScriptCommands    +=         "<Description>Check the status of Instant Response.</Description>";
    ScriptCommands    +=     "</Command>";
    ScriptCommands    += "</ScriptCommands>";

    return ScriptCommands;
}
That bit works fine, but it's the programming bit that's causing trouble...
js code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage)
{
    if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null)
    {
        var command = RegExp.$1.toLowerCase();
        var parameter = RegExp.$2;
        switch (command)
        {
            case 'ir.enable':
                 if(settingEnable)
                {
                }
                else
                {
                    settingEnable = true;
                    var Message = "Activation: enabled";
                    Message = MsgPlus.RemoveFormatCodes(Message);
                    MsgPlus.DisplayToast("Instant Response", Message);
                    Debug.Trace("Instant Response  |  Activation: enabled");
                }
            return "";
            case 'ir.disable':
                if(settingEnable)
                {
                    settingEnable = false;
                    var Message = "Activation: disabled";
                    Message = MsgPlus.RemoveFormatCodes(Message);
                    MsgPlus.DisplayToast("Instant Response", Message);
                    Debug.Trace("Instant Response  |  Activation: disabled");
                }
                return "";
            case 'ir.message':

                return "";
            case 'ir.timer':

                return "";
            case 'ir.pluson':
                if(setMsgPlusAM)
                {
                }
                else
                {
                    setMsgPlusAM = true;
                    var Message = "Plus! Style: on";
                    Message = MsgPlus.RemoveFormatCodes(Message);
                    MsgPlus.DisplayToast("Instant Response", Message);
                    Debug.Trace("Instant Response  |  Plus! Style: on");
                }
                return "";
            case 'ir.plusoff':
                if(setMsgPlusAM)
                {
                    setMsgPlusAM = false;
                    var Message = "Plus! Style: off";
                    Message = MsgPlus.RemoveFormatCodes(Message);
                    MsgPlus.DisplayToast("Instant Response", Message);
                    Debug.Trace("Instant Response  |  Plus! Style: off");
                }
                return "";
            case 'ir.alerton':
                if(setSignInAlert)
                {
                }
                else
                {
                    setSignInAlert = true;
                    var Message = "Sign-in Alert: enabled";
                    Message = MsgPlus.RemoveFormatCodes(Message);
                    MsgPlus.DisplayToast("Instant Response", Message);
                    Debug.Trace("Instant Response  |  Sign-in Alert: on");
                }
                return "";
            case 'ir.alertoff':
                if(setSignInAlert)
                {
                    setSignInAlert = false;
                    var Message = "Sign-in Alert: disabled";
                    Message = MsgPlus.RemoveFormatCodes(Message);
                    MsgPlus.DisplayToast("Instant Response", Message);
                    Debug.Trace("Instant Response  |  Sign-in Alert: off");
                }
                return "";
            case 'ir.status':
                if (settingEnable)
                {
                    var Message1 = "Message: " + settingMessage + "\nTimer: " + settingTimer + " milliseconds" + "\nPlus! Style: "
                    if (setMsgPlusAM) { var PMessage = "on" } else { var PMessage = "off" }
                    var Message2 = "\nSign-in Alert: "
                    if (setSignInAlert) { var SMessage = "on" } else { var SMessage = "off" }
                    Message = MsgPlus.RemoveFormatCodes(Message);
                    MsgPlus.DisplayToast("Instant Response", Message1 + PMessage + Message2 + SMessage);
                    Debug.Trace("Instant Response  |  Message: " + settingMessage);
                    Debug.Trace("Instant Response  |  Timer: " + settingTimer + " milliseconds");
                    if (setMsgPlusAM) {    Debug.Trace("Instant Response  |  Plus! Style: on"); }
                    else { Debug.Trace("Instant Response  |  Plus! Style: off"); }
                    if (setSignInAlert) { Debug.Trace("Instant Response  |  Sign-in Alert: on"); }
                    else { Debug.Trace("Instant Response  |  Sign-in Alert: off"); }
                }
                else
                {
                    var Message = "Unable to show the status.  You need to enable the script first!";
                    Message = MsgPlus.RemoveFormatCodes(Message);
                    MsgPlus.DisplayToast("Instant Response", Message);
                    Debug.Trace("Instant Response  |  Status check error.");
                }
                return "";
            default:
                return 0;
        }
    }
}

What I need, for the message and timer, is that the user types in the command, followed by the message/timer, and the script will set it.
code:
Example: /ir.message Hello there.  -->  (sets message to "Hello there.")
Example: /ir.timer 1000  -->  (sets timer to "1000")

How do I do this?  :S  (edit: codes in JavaScript now)
RE: Help - /commands! by matty on 02-01-2009 at 05:53 PM

js code:
//Add to the top of the script
var strMessage = '';
var intTimer = 0;

js code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage) {
    if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
        var command = RegExp.$1.toLowerCase();
        var parameter = RegExp.$2;
        switch (command) {
                case 'ir.message':
>>>                        strMessage = parameter;<<<
                        return "";
                case 'ir.timer':
>>>                        intTimer = (typeof parseInt(parameter) === 'number' ? parseInt(parameter) : 0); <<<
                        return "";
        }
    }
}

RE: Help - /commands! by whiz on 02-02-2009 at 10:59 AM

Okay, thanks, that works!  :D

Another question, though.

Using my current system of selecting a message from the script menu, if a command is selected (eg. /nudge, /block etc.), the script sends just the message.  If any other message is sent, it adds "AutoMessage:" to the start, so it sends in the Plus! auto-message format.

It does this with two variables, "setMsgPlusAM" (the user's way of turning the Plus! auto-message style on or off), and "setnoteMesCmd" (automatically turned on if the message starts with a /), which, if active, disables the "AutoMessage:" start.  The code below will hopefully explain this better.

js code:
function OnEvent_Timer(TimerID)
{
    Debug.Trace("Instant Response  |  Timer completed!");
    if (setMsgPlusAM)  // user's Plus! auto-message setting on
    {
        if (setnoteMesCmd)  // if message starts with a "/" (/command)
        {
            Messenger.OpenChat(TimerID).SendMessage(settingMessage);
            //  do not add the prefix to the message
        }
        else
        {
            Messenger.OpenChat(TimerID).SendMessage("AutoMessage: " + settingMessage);
            //  add the prefix to the message
        }
    }
    else
    {
        Messenger.OpenChat(TimerID).SendMessage(settingMessage);
        //  do not add the prefix to the message
    }
    Debug.Trace("Instant Response  |  Message sent!");
}

Can I set the script so that when the user types "/ir.message /command", the script can turn the "setnoteMesCmd" variable as true, so that the auto-message prefix will not be added?

Is this possible with a wildcard of some sort?  Like:
js code:
if (parameter=="/" + *)
{
setnoteMesCmd = true;
}
else
{
setnoteMesCmd = false;
}

RE: Help - /commands! by Matti on 02-02-2009 at 03:30 PM

Well, the easiest way to do it would be to check if the first character of "parameter" is a slash, using:

js code:
setnoteMesCmd = (parameter.charAt(0) === "/");
However, this is far from perfect, as Plus! allows commands to be escaped by doubling the slash and you should check that the command itself doesn't contain invalid characters. Therefore, the "best" way to do this is by using regular expressions! :D
js code:
setnoteMesCmd = (/^\/[^\s\/]+\s*[\s\S]*$/.test(parameter));
Basically, this is the same regular expression as you use in your command parser, the only difference is that it doesn't capture anything.
RE: RE: Help - /commands! by whiz on 02-04-2009 at 05:54 PM

Great, that works fine.  That's that script finished...  :D  Thanks!

Actually, I have another question (sorry to keep on at this).  This script blocks the opening of single conversations, group conversations, or both, opened by other users.

js code:
var Window;
var WindowContacts;

var FirstRunSetup = true;
var UserEmail = Messenger.MyEmail;

var SingleEnabled = false;
var GroupEnabled = false;

var SingleMessage = "";
var GroupMessage = "";

function OnEvent_Initialize(MessengerStart)
{
     Debug.Trace("-> Welcome to ChatBlock!")
     Debug.Trace("---> Settings for " + UserEmail + ":")
    Debug.Trace("-----> Single chat block = " + SingleEnabled)
    Debug.Trace("-------> Single chat message = \"" + SingleMessage + "\"")
    Debug.Trace("-----> Group chat block = " + GroupEnabled)
    Debug.Trace("-------> Group chat message = \"" + GroupMessage + "\"")
}

function OnEvent_ChatWndCreated(Wnd)
{
    Window = Wnd;
    WindowContacts = Wnd.Contacts;

    Debug.Trace("-> Conversation window opened!")   
    Debug.Trace("---> Participants = " + WindowContacts.Count);
       
    if(WindowContacts.Count==1)
    {
        Debug.Trace("-> Single conversation detected!")
        if(SingleEnabled==true)
        {
            Debug.Trace("---> Single blocking is enabled!")
            if(SingleMessage=="")
            {
                Debug.Trace("---> No single chat message setup...")
            }
           
            else
            {
                Window.SendMessage(SingleMessage);
                Debug.Trace("---> Single chat message sent!")
            }
           
            Window.SendMessage("/close");
            MsgPlus.DisplayToast("ChatBlock", "Single conversation blocked.")
            Debug.Trace("-----> Single conversation blocked!")
        }
        else
        {
            Debug.Trace("---> Single blocking is disabled!")
        }
    }
    if(WindowContacts.Count > 1)
    {
        Debug.Trace("-> Group conversation detected!")
        if(GroupEnabled==true)
        {
            Debug.Trace("---> Group blocking is enabled!")
            if(GroupMessage=="")
            {
                Debug.Trace("---> No group chat message setup...")
            }
           
            else
            {
                Window.SendMessage(GroupMessage);
                Debug.Trace("---> Group chat message sent!")
            }
           
            Window.SendMessage("/close");
            MsgPlus.DisplayToast("ChatBlock", "Group conversation blocked.")
            Debug.Trace("-----> Group conversation blocked!")
        }
        else
        {
            Debug.Trace("---> Group blocking is disabled!")
        }
    }
}

You'll see that single and group blocking is currently disabled.  However, the problem with this is that not only does it block windows opened by other users, but it also blocks the current user from opening windows as well!  How can I set it so that if the current user opens a window (there's an email variable called "UserEmail"), the blocker will ignore it?
RE: Help - /commands! [NEW QUESTION!] by Jesus on 02-07-2009 at 01:15 PM

quote:
Originally posted by whiz
How can I set it so that if the current user opens a window (there's an email variable called "UserEmail"), the blocker will ignore it?
I don't think it's possible to do that reliably.
The OnEvent_ChatWndCreated event only provides you with a ChatWnd object, AFAIK you can not determine exactly how and why it was opened.

What you could do to "guess" how it was opened, is store each ChatWnd.Handle in a global array, and set up OnEvent_ChatWndReceiveMessage to check who sends the first message to this ChatWnd. If it's not you, you probably didn't open the window.

Please note that above method is not fool-proof at all!
Not only is there no reliable way to check who sent a message to a certain ChatWnd (think Chat-Only names and Custom names etc.), but there's also the possibility the ChatWnd was not opened by a message at all. It could be opened by e.g. a file transfer or a nudge, which don't trigger OnEvent_ChatWndReceiveMessage IIRC.
RE: Help - /commands! by whiz on 02-15-2009 at 11:45 AM

Okay...  thanks anyway.  :)