コード例 #1
0
ファイル: Admin.cs プロジェクト: BadAtIrcBots/TrumpBot
        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;
                    }
                }
            }
        }
コード例 #2
0
ファイル: Admin.cs プロジェクト: BadAtIrcBots/TrumpBot
 public RequiredRight(AdminConfigModel.Right right)
 {
     Right = right;
 }