예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="trigger"></param>
        /// <param name="cmdValidator">Validates whether the given trigger may execute. Second parameter is line no.</param>
        public void ExecFile(string filename, CmdTrigger <C> trigger, Func <CmdTrigger <C>, int, bool> cmdValidator)
        {
            var line = 0;

            using (var reader = new StreamReader(filename))
            {
                string cmd;
                while ((cmd = reader.ReadLine()) != null)
                {
                    line++;
                    cmd = cmd.Trim();
                    // '#' starts a comment
                    if (!cmd.StartsWith("#") && cmd.Length > 0)
                    {
                        var text = new StringStream(cmd);
                        trigger.Text = text;
                        if (cmdValidator != null && !cmdValidator(trigger, line))
                        {
                            continue;
                        }

                        if (!Execute(trigger))
                        {
                            trigger.Reply("Could not execute Command from file \"{0}\" (line {1}): \"{2}\"", filename, line,
                                          cmd);
                        }
                    }
                }
            }
        }
예제 #2
0
        /// <summary>Lets the given CmdTrigger trigger the given Command.</summary>
        /// <param name="trigger"></param>
        /// <param name="cmd"></param>
        /// <param name="silentFail">Will not reply if it failed due to target restrictions or privileges etc</param>
        /// <returns></returns>
        public virtual object Eval(CmdTrigger <C> trigger, BaseCommand <C> cmd, bool silentFail)
        {
            if (cmd.Enabled)
            {
                Command <C> rootCmd = cmd.RootCmd;
                if (!rootCmd.MayTrigger(trigger, cmd, silentFail) || !this.TriggerValidator(trigger, cmd, silentFail))
                {
                    return((object)false);
                }
                trigger.cmd = cmd;
                try
                {
                    object obj = cmd.Eval(trigger);
                    rootCmd.ExecutedNotify(trigger);
                    return(obj);
                }
                catch (Exception ex)
                {
                    rootCmd.FailNotify(trigger, ex);
                }

                return((object)true);
            }

            trigger.Reply("Command is disabled: " + (object)cmd);
            return((object)false);
        }
예제 #3
0
        public void DisplayCmd(CmdTrigger <C> trigger, BaseCommand <C> cmd, bool ignoreRestrictions, bool detail)
        {
            trigger.Reply(string.Format("{0}{1}", cmd.CreateUsage(trigger), detail ? " (" + cmd.GetDescription(trigger) + ")" : ""));

            if (cmd.SubCommands.Count > 0)
            {
                trigger.Reply("All SubCommands:");
                foreach (var subCmd in cmd.SubCommands)
                {
                    if (MayDisplay(trigger, subCmd, ignoreRestrictions))
                    {
                        DisplayCmd(trigger, subCmd, ignoreRestrictions, detail);
                    }
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Lets the given CmdTrigger trigger the given Command.
        /// </summary>
        /// <param name="trigger"></param>
        /// <param name="cmd"></param>
        /// <param name="silentFail">Will not reply if it failed due to target restrictions or privileges etc</param>
        /// <returns></returns>
        public virtual object Eval(CmdTrigger <C> trigger, BaseCommand <C> cmd, bool silentFail)
        {
            if (cmd.Enabled)
            {
                var rootCmd = cmd.RootCmd;
                if (rootCmd.MayTrigger(trigger, cmd, silentFail) && TriggerValidator(trigger, cmd, silentFail))
                {
                    trigger.cmd = cmd;

                    try
                    {
                        var obj = cmd.Eval(trigger);

                        rootCmd.ExecutedNotify(trigger);

                        return(obj);
                    }
                    catch (Exception e)
                    {
                        rootCmd.FailNotify(trigger, e);
                    }
                    return(true);
                }
                return(false);
            }
            trigger.Reply("Command is disabled: " + cmd);
            return(false);
        }
예제 #5
0
        /// <summary>Lets the given CmdTrigger trigger the given Command.</summary>
        /// <param name="trigger"></param>
        /// <param name="cmd"></param>
        /// <param name="silentFail">Will not reply if it failed due to target restrictions or privileges etc</param>
        /// <returns></returns>
        public virtual bool Execute(CmdTrigger <C> trigger, BaseCommand <C> cmd, bool silentFail)
        {
            if (cmd.Enabled)
            {
                Command <C> rootCmd = cmd.RootCmd;
                if (!rootCmd.MayTrigger(trigger, cmd, silentFail) || !TriggerValidator(trigger, cmd, silentFail))
                {
                    return(false);
                }
                trigger.cmd = cmd;
                try
                {
                    cmd.Process(trigger);
                    rootCmd.ExecutedNotify(trigger);
                }
                catch (Exception ex)
                {
                    rootCmd.FailNotify(trigger, ex);
                }

                return(true);
            }

            trigger.Reply("Command is disabled: " + cmd);
            return(false);
        }
예제 #6
0
        /// <summary>
        /// Gives help
        /// TODO: Localization
        /// </summary>
        public void TriggerHelp(CmdTrigger <C> trigger, bool ignoreRestrictions)
        {
            if (trigger.Text.HasNext)
            {
                string remainder = trigger.Text.Remainder;
                List <BaseCommand <C> > commands = this.GetCommands(remainder);
                int count = commands.Count;
                foreach (BaseCommand <C> cmd in commands)
                {
                    if (this.MayDisplay(trigger, cmd, ignoreRestrictions))
                    {
                        this.DisplayCmd(trigger, cmd, ignoreRestrictions, true);
                    }
                    else
                    {
                        --count;
                    }
                }

                if (count != 0)
                {
                    return;
                }
                trigger.ReplyFormat("Did not find any Command that matches '{0}'.", (object)remainder);
            }
            else
            {
                int num = 0;
                foreach (Command <C> command in (IEnumerable <Command <C> >) this.Commands)
                {
                    if (this.MayDisplay(trigger, (BaseCommand <C>)command, ignoreRestrictions))
                    {
                        ++num;
                    }
                }

                trigger.ReplyFormat("Use: ? <Alias> [<subalias> [<subalias> ...]] for help on a certain command.");
                trigger.Reply("All {0} available commands:", (object)num);
                foreach (Command <C> command in (IEnumerable <Command <C> >) this.Commands)
                {
                    if (this.MayDisplay(trigger, (BaseCommand <C>)command, ignoreRestrictions))
                    {
                        trigger.Reply(command.CreateUsage(trigger));
                    }
                }
            }
        }
예제 #7
0
 public void DisplayCmd(CmdTrigger <C> trigger, BaseCommand <C> cmd, bool ignoreRestrictions, bool detail)
 {
     trigger.Reply(string.Format("{0}{1}", (object)cmd.CreateUsage(trigger),
                                 detail ? (object)(" (" + cmd.GetDescription(trigger) + ")") : (object)""));
     if (cmd.SubCommands.Count <= 0)
     {
         return;
     }
     trigger.Reply("All SubCommands:");
     foreach (BaseCommand <C> .SubCommand subCommand in cmd.SubCommands)
     {
         if (this.MayDisplay(trigger, (BaseCommand <C>)subCommand, ignoreRestrictions))
         {
             this.DisplayCmd(trigger, (BaseCommand <C>)subCommand, ignoreRestrictions, detail);
         }
     }
 }
예제 #8
0
            public override void Process(CmdTrigger <C> trigger)
            {
                if (!trigger.Text.HasNext)
                {
                    trigger.Reply("No file was specified.");
                    return;
                }
                var file = trigger.Text.NextWord();

                if (!Path.IsPathRooted(file))
                {
                    file = Path.Combine(m_Mgr.ExecFileDir, file);
                }
                if (File.Exists(file))
                {
                    m_Mgr.ExecFile(file, trigger);
                }
                else
                {
                    trigger.Reply("File to execute does not exist: " + file);
                }
            }
예제 #9
0
 public override void Process(CmdTrigger <C> trigger)
 {
     if (!trigger.Text.HasNext)
     {
         trigger.Reply("No file was specified.");
     }
     else
     {
         string str = trigger.Text.NextWord();
         if (!Path.IsPathRooted(str))
         {
             str = Path.Combine(this.m_Mgr.ExecFileDir, str);
         }
         if (File.Exists(str))
         {
             this.m_Mgr.ExecFile(str, trigger);
         }
         else
         {
             trigger.Reply("File to execute does not exist: " + str);
         }
     }
 }
예제 #10
0
        protected void TriggerSubCommand(CmdTrigger <C> trigger)
        {
            string key = trigger.Text.NextWord();

            BaseCommand <C> .SubCommand subCommand;
            if (this.m_subCommands.TryGetValue(key, out subCommand))
            {
                if (!this.RootCmd.MayTrigger(trigger, (BaseCommand <C>)subCommand, false))
                {
                    return;
                }
                subCommand.Process(trigger);
            }
            else
            {
                trigger.Reply("SubCommand not found: " + key);
                trigger.Text.Skip(trigger.Text.Length);
                this.mgr.DisplayCmd(trigger, this, false, false);
            }
        }
예제 #11
0
        protected void TriggerSubCommand(CmdTrigger <C> trigger)
        {
            var subAlias = trigger.Text.NextWord();

            SubCommand subCmd;

            if (m_subCommands.TryGetValue(subAlias, out subCmd))
            {
                if (RootCmd.MayTrigger(trigger, subCmd, false))
                {
                    subCmd.Process(trigger);
                }
            }
            else
            {
                trigger.Reply("SubCommand not found: " + subAlias);
                trigger.Text.Skip(trigger.Text.Length);
                mgr.DisplayCmd(trigger, this, false, false);
            }
        }
예제 #12
0
        /// <summary>
        /// Lets the given CmdTrigger trigger the given Command.
        /// </summary>
        /// <param name="trigger"></param>
        /// <param name="cmd"></param>
        /// <param name="silentFail">Will not reply if it failed due to target restrictions or privileges etc</param>
        /// <returns></returns>
        public virtual bool Execute(CmdTrigger <C> trigger, BaseCommand <C> cmd, bool silentFail)
        {
            if (cmd.Enabled)
            {
                var rootCmd = cmd.RootCmd;
                if (!rootCmd.MayTrigger(trigger, cmd, silentFail))
                {
                    //trigger.Reply("Cannot trigger Command: " + rootCmd);
                }
                else if (TriggerValidator(trigger, cmd, silentFail))
                {
                    trigger.cmd = cmd;

                    try
                    {
                        cmd.Process(trigger);

                        // command callbacks
                        rootCmd.ExecutedNotify(trigger);

                        // TODO: Create a tree of expected responses?
                        //string[] expectedReplies = cmd.ExpectedServResponses;
                        //if (expectedReplies != null) {
                        //    trigger.expectsServResponse = true;

                        //    foreach (string reply in expectedReplies) {
                        //        AddWaitingTrigger(reply, trigger);
                        //    }
                        //}
                    }
                    catch (Exception e)
                    {
                        rootCmd.FailNotify(trigger, e);
                    }
                    return(true);
                }
                return(false);
            }
            trigger.Reply("Command is disabled: " + cmd);
            return(false);
        }
예제 #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="trigger"></param>
        /// <param name="cmdValidator">Validates whether the given trigger may execute. Second parameter is line no.</param>
        public void ExecFile(string filename, CmdTrigger <C> trigger, Func <CmdTrigger <C>, int, bool> cmdValidator)
        {
            int num = 0;

            using (StreamReader streamReader = new StreamReader(filename))
            {
                string str;
                while ((str = streamReader.ReadLine()) != null)
                {
                    ++num;
                    string s = str.Trim();
                    if (!s.StartsWith("#") && s.Length > 0)
                    {
                        StringStream stringStream = new StringStream(s);
                        trigger.Text = stringStream;
                        if ((cmdValidator == null || cmdValidator(trigger, num)) && !this.Execute(trigger))
                        {
                            trigger.Reply("Could not execute Command from file \"{0}\" (line {1}): \"{2}\"",
                                          (object)filename, (object)num, (object)s);
                        }
                    }
                }
            }
        }
예제 #14
0
            public override void Process(CmdTrigger <C> trigger)
            {
                StringStream text = trigger.Text;

                if (!text.HasNext)
                {
                    trigger.Reply("Invalid arguments - " + this.CreateInfo(trigger));
                }
                else
                {
                    IExecutable executable1;
                    if (text.ConsumeNext('-'))
                    {
                        if (!text.HasNext)
                        {
                            trigger.Reply("Invalid arguments - " + this.CreateInfo(trigger));
                            return;
                        }

                        if (char.ToLower(text.Remainder[0]) == 'l')
                        {
                            ++text.Position;
                            string[] strArray = text.Remainder.Split(new char[1]
                            {
                                ' '
                            }, StringSplitOptions.RemoveEmptyEntries);
                            ToolMgr toolMgr = ((CommandMgr <C> .CallCommand) this.RootCmd).ToolMgr;
                            trigger.Reply("Callable functions ({0}):", (object)toolMgr.Executables.Count);
                            for (int index = 0; index < toolMgr.ExecutableList.Count; ++index)
                            {
                                IExecutable executable2 = toolMgr.ExecutableList[index];
                                if (strArray.Length != 0)
                                {
                                    bool flag = false;
                                    foreach (string str in strArray)
                                    {
                                        if (executable2.Name.IndexOf(str, StringComparison.InvariantCultureIgnoreCase) >
                                            -1)
                                        {
                                            flag = true;
                                            break;
                                        }
                                    }

                                    if (!flag)
                                    {
                                        continue;
                                    }
                                }

                                trigger.Reply(" {0}: {1}", (object)index, (object)executable2);
                            }

                            return;
                        }

                        uint num = text.NextUInt(uint.MaxValue);
                        executable1 = (long)num >= (long)this.ToolMgr.ExecutableList.Count
                            ? (IExecutable)null
                            : this.ToolMgr.ExecutableList[(int)num];
                    }
                    else
                    {
                        executable1 = this.ToolMgr.Get(text.NextWord());
                    }

                    if (executable1 == null)
                    {
                        trigger.Reply("Could not find specified Executable.");
                    }
                    else
                    {
                        int      length   = executable1.ParameterTypes.Length;
                        object[] objArray = new object[length];
                        object   obj      = (object)null;
                        for (int index = 0; index < length; ++index)
                        {
                            Type parameterType = executable1.ParameterTypes[index];
                            StringParser.Parse(index == length - 1 ? text.Remainder : text.NextWord(), parameterType,
                                               ref obj);
                            objArray[index] = obj;
                        }

                        executable1.Exec(objArray);
                    }
                }
            }
예제 #15
0
 public override void Reply(string text)
 {
     m_Trigger.Reply(text);
 }
예제 #16
0
            public override void Process(CmdTrigger <C> trigger)
            {
                //var subAlias = trigger.Text.NextWord();

                //SubCommand subCmd;
                //if (m_subCommands.TryGetValue(subAlias, out subCmd))
                //{
                //    subCmd.Process(trigger);
                //}
                //else
                //{
                var txt = trigger.Text;

                if (!txt.HasNext)
                {
                    trigger.Reply("Invalid arguments - " + CreateInfo(trigger));
                }
                else
                {
                    IExecutable exec;
                    if (txt.ConsumeNext('-'))
                    {
                        if (!txt.HasNext)
                        {
                            trigger.Reply("Invalid arguments - " + CreateInfo(trigger));
                            return;
                        }

                        var c = txt.Remainder[0];
                        if (Char.ToLower(c) == 'l')
                        {
                            txt.Position++;
                            var matches = txt.Remainder.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            var toolMgr = ((CallCommand)RootCmd).ToolMgr;
                            trigger.Reply("Callable functions ({0}):", toolMgr.Executables.Count);
                            for (var i = 0; i < toolMgr.ExecutableList.Count; i++)
                            {
                                var executable = toolMgr.ExecutableList[i];
                                if (matches.Length != 0)
                                {
                                    var found = false;
                                    foreach (var match in matches)
                                    {
                                        if (executable.Name.IndexOf(match, StringComparison.InvariantCultureIgnoreCase) > -1)
                                        {
                                            // match
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        continue;
                                    }
                                }
                                trigger.Reply(" {0}: {1}", i, executable);
                            }
                            return;
                        }

                        // specified index
                        var index = txt.NextUInt(uint.MaxValue);
                        if (index < ToolMgr.ExecutableList.Count)
                        {
                            exec = ToolMgr.ExecutableList[(int)index];
                        }
                        else
                        {
                            exec = null;
                        }
                    }
                    else
                    {
                        // name
                        var name = txt.NextWord();
                        exec = ToolMgr.Get(name);
                    }

                    if (exec == null)
                    {
                        trigger.Reply("Could not find specified Executable.");
                    }
                    else
                    {
                        var    len   = exec.ParameterTypes.Length;
                        var    args  = new object[len];
                        object value = null;
                        for (var i = 0; i < len; i++)
                        {
                            var paramType = exec.ParameterTypes[i];
                            var str       = (i == len - 1) ? txt.Remainder : txt.NextWord();                       // check for last argument
                            StringParser.Parse(str, paramType, ref value);
                            args[i] = value;
                        }
                        exec.Exec(args);
                    }

                    //base.Process(trigger);
                    //trigger.Reply("SubCommand not found: " + subAlias);
                    //trigger.Text.Skip(trigger.Text.Length);
                    //mgr.DisplayCmd(trigger, this, false);
                }
            }