Ejemplo n.º 1
0
        public void Execute(Message message)
        {
            Manager.BotClient.DeleteMessageAsync(message.Chat.Id, message.MessageId);

            string text = Utils.Parsers.VariablesParser(CacheData.SysConfigs
                                                        .Single(x => x.SysConfigId == "HelpMenu")
                                                        .Value);

            if (ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id))
            {
                text += Environment.NewLine;
                text += CacheData.SysConfigs
                        .Single(x => x.SysConfigId == "HelpMenuAdmin")
                        .Value;
            }

            if (BotTools.IsUserOperator(message.From.Id))
            {
                text += Environment.NewLine;
                text += CacheData.SysConfigs
                        .Single(x => x.SysConfigId == "HelpMenuOperatorBase")
                        .Value;

                if (BotTools.IsUserOperator(message.From.Id, Models.Operator.Levels.Advanced))
                {
                    text += Environment.NewLine;
                    text += CacheData.SysConfigs
                            .Single(x => x.SysConfigId == "HelpMenuOperatorAdv")
                            .Value;
                }
            }

            text += Environment.NewLine;
            text += Environment.NewLine;
            text += "* usernames are saved in cache and never stored on database or file. The cache is cleared at every reboot or update.";

            MessageQueueManager.EnqueueMessage(
                new Models.ChatMessage()
            {
                Timestamp = DateTime.UtcNow,
                Chat      = message.Chat,
                ParseMode = ParseMode.Html,
                Text      = text
            });
        }
Ejemplo n.º 2
0
        public void Execute(Message message)
        {
            Manager.BotClient.DeleteMessageAsync(message.Chat.Id, message.MessageId);

            if (!BotTools.IsUserOperator(message.From.Id) &&
                !ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id))
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = message.Chat,
                    ReplyToMessageId = message.MessageId,
                    Text             = CacheData.GetTranslation("en", "error_not_auth_command")
                });
                return;
            }
            int userToKick;

            if (message.ReplyToMessage == null)
            {
                if (message.Text.Split(" ")[1].StartsWith("@"))
                {
                    if (!CacheData.Usernames.Keys.Contains(message.Text.Split(" ")[1].Remove(0, 1)))
                    {
                        MessageQueueManager.EnqueueMessage(
                            new Models.ChatMessage()
                        {
                            Timestamp = DateTime.UtcNow,
                            Chat      = message.Chat,
                            Text      = CacheData.GetTranslation("en", "kick_command_error_invalidUsername")
                        });
                        return;
                    }
                    userToKick = CacheData.Usernames[message.Text.Split(" ")[1].Remove(0, 1)];
                }
                else
                {
                    bool isValid = int.TryParse(message.Text.Split(" ")[1], out userToKick);
                    if (!isValid)
                    {
                        MessageQueueManager.EnqueueMessage(
                            new Models.ChatMessage()
                        {
                            Timestamp = DateTime.UtcNow,
                            Chat      = message.Chat,
                            Text      = CacheData.GetTranslation("en", "kick_command_error_invalidUserId")
                        });
                        return;
                    }
                }
            }
            else
            {
                userToKick = message.ReplyToMessage.From.Id;
            }

            if (userToKick == 777000) // Telegram's official updateServiceNotification
            {
                Manager.BotClient.SendTextMessageAsync(
                    chatId: message.Chat.Id,
                    parseMode: ParseMode.Markdown,
                    text: String.Format(
                        "*[Error]*\n" +
                        "This is an official Telegram's user/id.")
                    );

                return;
            }

            if (BotTools.IsUserOperator(userToKick))
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    Text      = CacheData.GetTranslation("en", "command_to_operator_not_allowed")
                });

                return;
            }

            try
            {
                Manager.BotClient.KickChatMemberAsync(message.Chat.Id, userToKick);
                if (message.Chat.Type == ChatType.Supergroup)
                {
                    Manager.BotClient.UnbanChatMemberAsync(message.Chat.Id, userToKick);
                }
            }
            catch
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    ParseMode = ParseMode.Markdown,
                    Text      = CacheData.GetTranslation("en", "command_kick_error")
                });
                return;
            }

            UserTools.AddPenalty(message.Chat.Id, userToKick,
                                 Models.TrustFactorLog.TrustFactorAction.kick, Manager.MyId);
        }
Ejemplo n.º 3
0
        public void Execute(Message message)
        {
            Manager.BotClient.DeleteMessageAsync(message.Chat.Id, message.MessageId);

            if (!BotTools.IsUserOperator(message.From.Id) &&
                !ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id))
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = message.Chat,
                    ReplyToMessageId = message.MessageId,
                    Text             = CacheData.GetTranslation("en", "error_not_auth_command")
                });
                return;
            }

            int userId;

            if (message.ReplyToMessage == null)
            {
                if (message.Text.Split(" ")[1].StartsWith("@"))
                {
                    if (!CacheData.Usernames.Keys.Contains(message.Text.Split(" ")[1].Remove(0, 1)))
                    {
                        MessageQueueManager.EnqueueMessage(
                            new Models.ChatMessage()
                        {
                            Timestamp = DateTime.UtcNow,
                            Chat      = message.Chat,
                            Text      = CacheData.GetTranslation("en", "mute_command_error_invalidUsername")
                        });
                        return;
                    }
                    userId = CacheData.Usernames[message.Text.Split(" ")[1].Remove(0, 1)];
                }
                else
                {
                    bool isValid = int.TryParse(message.Text.Split(" ")[1], out userId);
                    if (!isValid)
                    {
                        MessageQueueManager.EnqueueMessage(
                            new Models.ChatMessage()
                        {
                            Timestamp = DateTime.UtcNow,
                            Chat      = message.Chat,
                            Text      = CacheData.GetTranslation("en", "mute_command_error_invalidUserId")
                        });
                        return;
                    }
                }
            }
            else
            {
                userId = message.ReplyToMessage.From.Id;
            }

            if (BotTools.IsUserOperator(userId))
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    Text      = CacheData.GetTranslation("en", "command_to_operator_not_allowed")
                });

                return;
            }

            try
            {
                Manager.BotClient.RestrictChatMemberAsync(
                    message.Chat.Id,
                    userId,
                    new ChatPermissions()
                {
                    CanSendMessages       = false,
                    CanAddWebPagePreviews = false,
                    CanChangeInfo         = false,
                    CanInviteUsers        = false,
                    CanPinMessages        = false,
                    CanSendMediaMessages  = false,
                    CanSendOtherMessages  = false,
                    CanSendPolls          = false
                });

                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    Text      = CacheData.GetTranslation("en", "mute_command_success")
                });
            }
            catch
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    ParseMode = ParseMode.Markdown,
                    Text      = CacheData.GetTranslation("en", "command_mute_error")
                });
            }
        }
Ejemplo n.º 4
0
        public void Execute(Message message)
        {
            if (!BotTools.IsUserOperator(message.From.Id, Models.Operator.Levels.Basic) &&
                !ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id))
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    Text      = CacheData.GetTranslation("en", "ban_command_error_notadmin")
                });
                return;
            }

            if (Manager.BotClient.GetChatAdministratorsAsync(message.Chat.Id).Result
                .Single(x => x.User.Id == message.From.Id)
                .CanRestrictMembers == false)
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    Text      = CacheData.GetTranslation("en", "ban_command_error_adminPrivilege")
                });
                return;
            }

            int userToKick;

            if (message.ReplyToMessage == null)
            {
                if (message.Text.Split(" ")[1].StartsWith("@"))
                {
                    if (!CacheData.Usernames.Keys.Contains(message.Text.Split(" ")[1].Remove(0, 1)))
                    {
                        MessageQueueManager.EnqueueMessage(
                            new Models.ChatMessage()
                        {
                            Timestamp = DateTime.UtcNow,
                            Chat      = message.Chat,
                            Text      = CacheData.GetTranslation("en", "ban_command_error_invalidUsername")
                        });
                        return;
                    }
                    userToKick = CacheData.Usernames[message.Text.Split(" ")[1].Remove(0, 1)];
                }
                else
                {
                    bool isValid = int.TryParse(message.Text.Split(" ")[1], out userToKick);
                    if (!isValid)
                    {
                        MessageQueueManager.EnqueueMessage(
                            new Models.ChatMessage()
                        {
                            Timestamp = DateTime.UtcNow,
                            Chat      = message.Chat,
                            Text      = CacheData.GetTranslation("en", "ban_command_error_invalidUserId")
                        });
                        return;
                    }
                }
            }
            else
            {
                userToKick = message.ReplyToMessage.From.Id;
            }

            if (userToKick == 777000) // Telegram's official updateServiceNotification
            {
                Manager.BotClient.SendTextMessageAsync(
                    chatId: message.Chat.Id,
                    parseMode: ParseMode.Markdown,
                    text: String.Format(
                        "*[Error]*\n" +
                        "This is an official Telegram's user/id.")
                    );

                return;
            }

            if (BotTools.IsUserOperator(userToKick))
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    Text      = CacheData.GetTranslation("en", "command_to_operator_not_allowed")
                });

                return;
            }

            try
            {
                Manager.BotClient.KickChatMemberAsync(message.Chat.Id, userToKick,
                                                      DateTime.UtcNow.AddMinutes(-5));
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    Text      = CacheData.GetTranslation("en", "ban_command_success")
                });
            }
            catch
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    Text      = CacheData.GetTranslation("en", "ban_command_error")
                });
                return;
            }

            UserTools.AddPenalty(message.Chat.Id, userToKick,
                                 Models.TrustFactorLog.TrustFactorAction.ban, Manager.MyId);
        }
Ejemplo n.º 5
0
        public ControlResult DoCheck(Message message)
        {
            if (ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id))
            {
                return(new ControlResult()
                {
                    CheckName = "Anti Flood",
                    Result = IControl.ControlResultType.skipped
                });
            }

            if (message.Date < DateTime.UtcNow.AddMinutes(-1))
            {
                return new ControlResult()
                       {
                           CheckName = "Anti Flood",
                           Result    = IControl.ControlResultType.skipped
                       }
            }
            ;

            Models.Group.ConfigurationParameter configValue = CacheData.GroupConfigs[message.Chat.Id]
                                                              .Where(x => x.ConfigurationParameterId == "FloodControl")
                                                              .SingleOrDefault();
            if (configValue != null)
            {
                if (configValue.Value == "false")
                {
                    return new ControlResult()
                           {
                               CheckName = "Anti Flood",
                               Result    = IControl.ControlResultType.skipped
                           }
                }
            }
            ;

            Dictionary <int, Flood> floodCounter = FloodCounter.GetValueOrDefault(message.Chat.Id);

            if (floodCounter == null)
            {
                FloodCounter.TryAdd(message.Chat.Id, new Dictionary <int, Flood>());
            }

            if (!FloodCounter[message.Chat.Id].TryGetValue(message.From.Id, out Flood currentValue))
            {
                FloodCounter[message.Chat.Id].Add(message.From.Id, new Flood()
                {
                    UserId      = message.From.Id,
                    Messages    = 1,
                    LastMessage = DateTime.UtcNow
                });
            }
            else
            {
                if (currentValue.LastMessage < DateTime.UtcNow.AddSeconds(-3))
                {
                    FloodCounter[message.Chat.Id][message.From.Id] = new Flood()
                    {
                        UserId      = message.From.Id,
                        Messages    = 1,
                        LastMessage = DateTime.UtcNow
                    };

                    return(new ControlResult()
                    {
                        CheckName = "Anti Flood",
                        Result = IControl.ControlResultType.negative
                    });
                }

                currentValue.Messages   += 1;
                currentValue.LastMessage = DateTime.UtcNow;
                FloodCounter[message.Chat.Id][message.From.Id] = currentValue;
            }

            if (FloodCounter[message.Chat.Id][message.From.Id].Messages >= 5)
            {
                int minutes = 5;

                SysConfig floodBanMinutes = CacheData.SysConfigs.Where(x => x.SysConfigId == "FloodBanInMinutes")
                                            .SingleOrDefault();
                if (floodBanMinutes != null)
                {
                    int.TryParse(floodBanMinutes.Value, out minutes);
                }

                if (limitedUsers.ContainsKey(message.From.Id))
                {
                    if (limitedUsers[message.From.Id] > DateTime.UtcNow.AddMinutes(-minutes))
                    {
                        goto skipLimitAndPenality;
                    }
                }

                limitedUsers.AddOrUpdate(message.From.Id, DateTime.UtcNow,
                                         (key, value) => value = DateTime.UtcNow);

                Bot.Manager.BotClient.RestrictChatMemberAsync(
                    message.Chat.Id,
                    message.From.Id,
                    new ChatPermissions()
                {
                    CanSendMessages       = false,
                    CanAddWebPagePreviews = false,
                    CanChangeInfo         = false,
                    CanInviteUsers        = false,
                    CanPinMessages        = false,
                    CanSendMediaMessages  = false,
                    CanSendOtherMessages  = false,
                    CanSendPolls          = false
                },
                    DateTime.UtcNow.AddMinutes(minutes)
                    ).Wait();

                string author = message.From.Username == null
                    ? message.From.FirstName + " " + message.From.LastName
                    : "@" + message.From.Username;
                MessageQueueManager.EnqueueLog(new ChatMessage()
                {
                    ParseMode = ParseMode.Markdown,
                    Text      = String.Format(
                        "*[Report]*\n" +
                        "User muted for {0} minutes due to flood.\n" +
                        "\nChat: `{1}`" +
                        "\nAuthor: `{3}`" +
                        "\nUserId: `{2}`" +
                        "\n\n*hash_code:* #UB{4}-{5}",
                        minutes,
                        message.Chat.Title,
                        message.From.Id,
                        author,
                        message.Chat.Id.ToString().Replace("-", ""),
                        Guid.NewGuid())
                });

                UserTools.AddPenalty(message.Chat.Id, message.From.Id,
                                     TrustFactorLog.TrustFactorAction.limit, Bot.Manager.MyId);

                MessageQueueManager.EnqueueMessage(
                    new ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = message.Chat,
                    ParseMode = ParseMode.Markdown,
                    Text      = $"User {message.From.Username} has been limited for {minutes} minutes due to flood.\n"
                                + "An admin can immediately remove this limitation by clicking the button below.",
                    ReplyMarkup = new InlineKeyboardMarkup(
                        InlineKeyboardButton.WithCallbackData(
                            CacheData.GetTranslation("en", "button_removeFlood", true),
                            $"/RemoveFlood " + message.From.Id
                            )
                        )
                });

skipLimitAndPenality:
                return(new ControlResult()
                {
                    CheckName = "Anti Flood",
                    Result = IControl.ControlResultType.positive
                });
            }
            return(new ControlResult()
            {
                CheckName = "Anti Flood",
                Result = IControl.ControlResultType.negative
            });
        }
    }