Exemplo n.º 1
0
        /// <summary>
        /// Calls all the events bound to the specified event name
        /// </summary>
        /// <param name="event_name">event name</param>
        /// <param name="packet">dAmnPacket object</param>
        public static void CallEvent(String event_name, dAmnPacket packet)
        {
            if (Program.Bot == null) return;
            lock (_events)
            {
                if (_events.ContainsKey(event_name))
                {
                    HitCounts[event_name]++;
                    foreach (Event callback in _events[event_name])
                    {
                        try
                        {
                            if (Program.Bot == null) return;
                            if (Core._disabled_extensions.Contains(callback.Extension.Name.ToLower())) return;
                            callback.Method.Invoke(callback.Class, new object[] { Program.Bot, packet });
                        }
                        catch (Exception E)
                        {
                            ConIO.Warning("Extension", String.Format("Failed to call event {0}.{1}: {2}", callback.ClassName, callback.Method.Name, E.InnerException.Message));
                        }
                    }

                    // External events can't have anything to do with datashare
                    if (packet != null && packet.Parameter.ToLower() != "chat:datashare")
                        CallExternalEvent(event_name, packet);
                }
                else
                {
                    ConIO.Write("Unknown event: " + event_name, "Events");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Calls an external event. Only to be called from CallEvent()
        /// </summary>
        /// <param name="event_name"></param>
        /// <param name="packet"></param>
        private static void CallExternalEvent(String event_name, dAmnPacket packet)
        {
            if (Program.Bot == null) return;

            // We will not let it see its own messages! This avoids infinite loops.
            if (event_name == "recv_msg" && packet.Arguments["from"].ToLower() == Program.Bot.Config.Username.ToLower())
                return;

            lock (_external_events)
            {
                // We shouldn't have to go through the shenanigans of re-checking, but for safety sake..
                if (_external_events.ContainsKey(event_name))
                {
                    foreach (Event callback in _external_events[event_name])
                    {
                        try
                        {
                            if (Program.Bot == null) return;
                            if (Core._disabled_extensions.Contains(callback.Extension.Name.ToLower())) return;
                            callback.Method.Invoke(callback.Class, new object[] { packet });
                        }
                        catch (Exception E)
                        {
                            ConIO.Warning("Extension", String.Format("Extension bound to event [{0}] threw exception: {1}", event_name, E.InnerException.Message));
                        }
                    }
                }
                else
                {
                    ConIO.Write("Unknown external event: " + event_name, "Events");
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Calls the command bound to the specified command name
        /// </summary>
        /// <param name="cmd_name">command name</param>
        public static void CallCommand(String cmd_name, dAmnPacket packet)
        {
            if (Core._disabled_commands.Contains(cmd_name.ToLower())) return;

            bool a = false, b = false;
            if ((a = _commands.ContainsKey(cmd_name.ToLower())) || (b = _external_commands.ContainsKey(cmd_name.ToLower())))
            {
                Command callback = null;

                String from = String.Empty;
                if (packet.Arguments.ContainsKey("from"))
                    from = packet.Arguments["from"];

                if (!_last_command.ContainsKey(from))
                    _last_command.Add(from, 0);

                if (Bot.EpochTimestampMS - _last_command[from] < 1000) return;

                String[] cmd_args;
                String msg = packet.Body.Substring(Program.Bot.Config.Trigger.Length);

                if (packet.Body.Contains(" "))
                    cmd_args = msg.Split(' ');
                else
                    cmd_args = new String[1] { msg };

                if (a)
                {
                    callback = _commands[cmd_name.ToLower()];

                    if (Core._disabled_extensions.Contains(callback.Extension.Name.ToLower())) return;

                    if (cmd_args.Length >= 2 && (cmd_args[1] == "?" || cmd_args[1] == "-?"))
                    {
                        Program.Bot.Say(packet.Parameter, "<b>&raquo; Help for command <i>" + cmd_name.ToLower() + "</i>:</b><br/>" + callback.Help.Replace("[trig]", Program.Bot.Config.Trigger));
                        return;
                    }

                    // Access denied
                    if (!Users.CanAccess(from, GetCommandAccess(cmd_name), cmd_name.ToLower()))
                        return;

                    _last_command[from] = Bot.EpochTimestampMS;

                    try
                    {
                        callback.Method.Invoke(callback.Class, new object[] { Program.Bot, packet.Parameter, cmd_args, msg, from, packet });
                    }
                    catch (Exception E)
                    {
                        ConIO.Warning("Extension", String.Format("Failed to call command {0} ({1}): {2}", callback.Method.Name, callback.Description, E.InnerException.Message));
                    }
                }

                if (b)
                    CallExternalCommand(cmd_name, Tools.FormatChat(packet.Parameter), msg, cmd_args, from);
            }
            else
            {
                ConIO.Write("Unknown command: " + cmd_name.ToLower(), "Events");
            }
        }