示例#1
0
        internal void ProcessMessage(object sender, IrcEventArgs eventArgs)
        {
            if (eventArgs.Data.Message == null)
            {
                return;
            }
            string message = eventArgs.Data.Message.TrimStart(CommandPrefix);
            string nick    = eventArgs.Data.Nick.Split('!')[0];

            _log.Debug($"Got message '{message}' and nick '{nick}'");

            if (_config.IgnoreList.Contains(nick))
            {
                _log.Debug($"{nick} was in IgnoreList");
                return;
            }

            if (_config.IgnoreNonVoicedUsers)
            {
                NonRfcChannelUser user = _client.GetChannelUser(eventArgs.Data.Channel, nick) as NonRfcChannelUser;
                if (user == null)
                {
                    _log.Info(
                        $"Got null when getting {nick} in {eventArgs.Data.Channel}, can't verify if they're non voiced, ignoring.");
                    return;
                }

                if (!user.IsVoice && !user.IsOp && !user.IsHalfop && !user.IsOwner && !user.IsChannelAdmin &&
                    !user.IsIrcOp)
                {
                    _log.Info($"Ignored {nick} because IgnoreNonVoicedUsers is set to {_config.IgnoreNonVoicedUsers}");
                    return;
                }
            }

            foreach (ICommand command in Commands)
            {
                NoPrefix      noPrefix      = (NoPrefix)Attribute.GetCustomAttribute(command.GetType(), typeof(NoPrefix));
                UseMainThread useMainThread = (UseMainThread)Attribute.GetCustomAttribute(command.GetType(),
                                                                                          typeof(UseMainThread));

                bool runInMainThread     = useMainThread != null;
                bool prefixRequired      = noPrefix == null;
                bool breakAfterExecution =
                    (BreakAfterExecution)Attribute.GetCustomAttribute(command.GetType(),
                                                                      typeof(BreakAfterExecution)) != null;
                bool matched = false;

                if (prefixRequired)
                {
                    if (eventArgs.Data.Message[0] != _config.Prefix)
                    {
                        continue;
                    }
                }

                foreach (Regex regex in command.Patterns)
                {
                    Match match = regex.Match(message);
                    if (!match.Success)
                    {
                        continue;
                    }
                    matched = true;

                    _log.Debug($"Successfully matched message to '{command.CommandName}' with regex '{regex}'");
                    if (runInMainThread)
                    {
                        RunCommand(eventArgs, command, match);
                        continue;
                    }

                    Thread commandThread = new Thread(() => RunCommand(eventArgs, command, match));
                    Threads.Add(commandThread);
                    commandThread.Start();
                }

                if (breakAfterExecution && matched)
                {
                    break;
                }
            }
        }
示例#2
0
        public void ProcessMessage(object sender, IrcEventArgs eventArgs)
        {
            _log.Debug($"Processing message: {eventArgs.Data.Message}");
            //if (eventArgs.Data.Message[0] != Config.CommandPrefix) return;

            if (eventArgs.Data.Message == null)
            {
                return;
            }
            string message = eventArgs.Data.Message.TrimStart(Config.CommandPrefix);
            string nick    = eventArgs.Data.From.Split('!')[0];

            AdminConfigModel.Right inferredRight = Config.AdminChannels.Contains(eventArgs.Data.Channel) ? AdminConfigModel.Right.Admin : AdminConfigModel.Right.Guest;
            _log.Debug($"Inferred right for {nick}: {inferredRight} (level {(int) inferredRight})");
            AdminConfigModel.User user = Config.Users.FirstOrDefault(configUser => configUser.Nick == nick) ?? new AdminConfigModel.User
            {
                Nick = nick
            };
            _log.Debug($"User instance right for {nick}: {user.Right} (level {(int) user.Right})");

            foreach (IAdminCommand command in Commands)
            {
                foreach (Regex regex in command.Patterns)
                {
                    Match match = regex.Match(message);
                    if (!match.Success)
                    {
                        continue;
                    }

                    _log.Debug($"Successfully matched '{match.Value}' with {regex}");

                    RequiredRight requiredRight =
                        (RequiredRight)
                        Attribute.GetCustomAttribute(command.GetType(), typeof(RequiredRight));
                    IgnoreException ignoreException = (IgnoreException)Attribute.GetCustomAttribute(command.GetType(),
                                                                                                    typeof(IgnoreException));
                    NoPrefix noPrefix = (NoPrefix)Attribute.GetCustomAttribute(command.GetType(), typeof(NoPrefix));
                    DoNotBreakAfterExecution doNotBreakAfterExecution =
                        (DoNotBreakAfterExecution)Attribute.GetCustomAttribute(command.GetType(),
                                                                               typeof(DoNotBreakAfterExecution));

                    if (noPrefix == null)
                    {
                        if (eventArgs.Data.Message[0] != Config.CommandPrefix)
                        {
                            continue;
                        }
                    }

                    bool shouldIgnoreException = ignoreException != null;

                    if (requiredRight == null)
                    {
                        _log.Debug($"No permissions defined via RequiredRights attribute for command {command.Name}. Not going to allow command to run as this could be a very dangerous mistake.");
                        break;
                    }

                    _log.Debug($"Required right for {command.Name} is {requiredRight.Right} (level {(int) requiredRight.Right})");

                    if (!(user.Right >= requiredRight.Right || inferredRight >= requiredRight.Right))
                    {
                        _log.Info($"{nick} tried to run '{message}' but does not have permissions.");
                        _client.SendMessage(SendType.Notice, nick, $"You do not have access to {command.Name}");
                        break;
                    }

                    _log.Info($"Running {command.Name} for {nick}");
                    try
                    {
                        command.RunCommand(_client, match.Groups, eventArgs, _ircBot);
                    }
                    catch (Exception e)
                    {
                        if (shouldIgnoreException)
                        {
                            throw;
                        }
                        _backtraceClient?.Attributes.Add("RequestorNick", nick);
                        _backtraceClient?.Attributes.Add("AdminCommandName", command.Name);
                        _backtraceClient?.Attributes.Add("Message", message);
                        _backtraceClient?.Attributes.Add("NetworkUri", _ircBot.Settings.ConnectionUri);
                        _backtraceClient?.Attributes.Add("AdminRight", user.Right);

                        _backtraceClient?.Send(e);

                        if (e.InnerException == null)
                        {
                            _client.SendMessage(SendType.Message, eventArgs.Data.Channel, $"Well this is dumb: {e.Source}: {e.Message}");
                            break;
                        }
                        _backtraceClient?.Send(e.InnerException);
                        _client.SendMessage(SendType.Message, eventArgs.Data.Channel,
                                            $"Well this is dumb: {e.InnerException.Source}: {e.InnerException.Message}");
                        break;
                    }

                    if (doNotBreakAfterExecution == null)
                    {
                        break;
                    }
                }
            }
        }