Esempio n. 1
0
        public TelegramGroup Update(TelegramGroup telegramGroup, int callerId)
        {
            using (UBContext ubc = new UBContext())
            {
                TelegramGroup exists = ubc.Group_TelegramGroups
                                       .Where(x => x.GroupId == telegramGroup.GroupId)
                                       .FirstOrDefault();
                if (exists == null)
                {
                    return(null);
                }

                try
                {
                    exists.TelegramChatId   = telegramGroup.TelegramChatId;
                    exists.Title            = telegramGroup.Title;
                    exists.State            = telegramGroup.State;
                    exists.Configuration    = telegramGroup.Configuration;
                    exists.WelcomeText      = telegramGroup.WelcomeText;
                    exists.ChatLanguage     = telegramGroup.ChatLanguage;
                    exists.SettingsLanguage = telegramGroup.SettingsLanguage;
                    exists.ReportChatId     = telegramGroup.ReportChatId;
                    exists.RulesText        = telegramGroup.RulesText;

                    ubc.SaveChanges();
                    return(telegramGroup);
                }
                catch (Exception ex)
                {
                    Utils.Logging.AddLog(new SystemLog()
                    {
                        LoggerName = "Unifiedban",
                        Date       = DateTime.Now,
                        Function   = "Unifiedban.Data.TelegramGroupService.Update",
                        Level      = SystemLog.Levels.Warn,
                        Message    = ex.Message,
                        UserId     = callerId
                    });
                    if (ex.InnerException != null)
                    {
                        Utils.Logging.AddLog(new SystemLog()
                        {
                            LoggerName = "Unifiedban.Data",
                            Date       = DateTime.Now,
                            Function   = "Unifiedban.Data.TelegramGroupService.Update",
                            Level      = SystemLog.Levels.Warn,
                            Message    = ex.InnerException.Message,
                            UserId     = callerId
                        });
                    }
                }
                return(null);
            }
        }
Esempio n. 2
0
        public static bool RegisterGroup(Message message)
        {
            if (CacheData.Groups.ContainsKey(message.Chat.Id))
            {
                return(false);
            }

            BusinessLogic.Group.TelegramGroupLogic telegramGroupLogic =
                new BusinessLogic.Group.TelegramGroupLogic();
            TelegramGroup registered = telegramGroupLogic.Add(
                message.Chat.Id, message.Chat.Title, TelegramGroup.Status.Active,
                configuration: Newtonsoft.Json.JsonConvert.SerializeObject(CacheData.GroupDefaultConfigs),
                welcomeText: CacheData.GetTranslation("en", "message_welcome_default"),
                chatLanguage: "en",
                settingsLanguage: "en",
                reportChatId: CacheData.ControlChatId,
                rulesText: "No rules defined yet by the group admins. Just... be nice!",
                callerId: -2);

            if (registered == null)
            {
                return(false);
            }

            CacheData.Groups.Add(message.Chat.Id, registered);
            CacheData.GroupConfigs.Add(message.Chat.Id, CacheData.GroupDefaultConfigs);
            if (MessageQueueManager.AddGroupIfNotPresent(registered))
            {
                Manager.BotClient.SendTextMessageAsync(
                    chatId: message.Chat.Id,
                    parseMode: ParseMode.Markdown,
                    text: CacheData.GetTranslation("en", "message_group_first_join"),
                    disableWebPagePreview: true
                    );

                Manager.BotClient.SendTextMessageAsync(
                    chatId: CacheData.ControlChatId,
                    parseMode: ParseMode.Markdown,
                    text: String.Format(
                        "*[Log]*\n" +
                        "New group has chosen unified/ban 🥳\n" +
                        "\nChat: {0}" +
                        "\nChatId: {1}" +
                        "\n\n*hash_code:* #UB{2}-{3}",
                        message.Chat.Title,
                        message.Chat.Id,
                        message.Chat.Id.ToString().Replace("-", ""),
                        Guid.NewGuid())
                    );
            }

            return(true);
        }
Esempio n. 3
0
        private static void HandleWsCommand(string dashboardUserId, string groupId, string parameter, string value)
        {
            TelegramGroup group = CacheData.Groups.Values
                                  .SingleOrDefault(x => x.GroupId == groupId);

            if (group == null)
            {
                return;
            }

            if (!UserTools.CanHandleGroup(dashboardUserId, groupId))
            {
                return;
            }

            if (parameter == "WelcomeText")
            {
                CacheData.Groups[group.TelegramChatId].WelcomeText = value;
                return;
            }
            else if (parameter == "RulesText")
            {
                CacheData.Groups[group.TelegramChatId].RulesText = value;
                return;
            }
            else if (parameter == "ReportChatId")
            {
                if (!Int64.TryParse(value, out long reportChatId))
                {
                    return;
                }
                CacheData.Groups[group.TelegramChatId].ReportChatId = reportChatId;
                return;
            }

            ConfigurationParameter config = CacheData.GroupConfigs[group.TelegramChatId]
                                            .Where(x => x.ConfigurationParameterId == parameter)
                                            .SingleOrDefault();

            if (config == null)
            {
                return;
            }

            CacheData.GroupConfigs[group.TelegramChatId]
            [CacheData.GroupConfigs[group.TelegramChatId]
             .IndexOf(config)]
            .Value = value;
        }
Esempio n. 4
0
        public static bool RemoveGroupIfNotPresent(TelegramGroup group)
        {
            // Do not accept new chat if going to shutdown
            if (isDisposing)
            {
                return(false);
            }

            if (!GroupChats.ContainsKey(group.TelegramChatId))
            {
                return(false);
            }

            bool removed = GroupChats.TryRemove(group.TelegramChatId, out MessageQueue messageQueue);

            return(removed);
        }
Esempio n. 5
0
        public static bool AddGroupIfNotPresent(TelegramGroup group)
        {
            // Do not accept new chat if going to shutdown
            if (isDisposing)
            {
                return(false);
            }

            if (GroupChats.ContainsKey(group.TelegramChatId))
            {
                return(false);
            }

            bool added = GroupChats.TryAdd(group.TelegramChatId,
                                           new MessageQueue(group.TelegramChatId, 20));

            return(added);
        }
Esempio n. 6
0
        public SystemLog.ErrorCodes Remove(TelegramGroup telegramGroup, int callerId)
        {
            using (UBContext ubc = new UBContext())
            {
                TelegramGroup exists = ubc.Group_TelegramGroups
                                       .Where(x => x.GroupId == telegramGroup.GroupId)
                                       .FirstOrDefault();
                if (exists == null)
                {
                    return(SystemLog.ErrorCodes.Error);
                }

                try
                {
                    ubc.SaveChanges();
                    return(SystemLog.ErrorCodes.OK);
                }
                catch (Exception ex)
                {
                    Utils.Logging.AddLog(new SystemLog()
                    {
                        LoggerName = "Unifiedban",
                        Date       = DateTime.Now,
                        Function   = "Unifiedban.Data.TelegramGroupService.Remove",
                        Level      = SystemLog.Levels.Warn,
                        Message    = ex.Message,
                        UserId     = callerId
                    });
                    if (ex.InnerException != null)
                    {
                        Utils.Logging.AddLog(new SystemLog()
                        {
                            LoggerName = "Unifiedban.Data",
                            Date       = DateTime.Now,
                            Function   = "Unifiedban.Data.TelegramGroupService.Remove",
                            Level      = SystemLog.Levels.Warn,
                            Message    = ex.InnerException.Message,
                            UserId     = callerId
                        });
                    }
                }
                return(SystemLog.ErrorCodes.Error);
            }
        }
Esempio n. 7
0
        public static void MigrateToChatId(Message message)
        {
            if (!CacheData.Groups.ContainsKey(message.Chat.Id))
            {
                return;
            }

            BusinessLogic.Group.TelegramGroupLogic telegramGroupLogic =
                new BusinessLogic.Group.TelegramGroupLogic();

            TelegramGroup updated = telegramGroupLogic.UpdateChatId(message.Chat.Id, message.MigrateToChatId, -2); // TODO - log operation

            if (updated == null)
            {
                return;
            }

            CacheData.Groups.Add(updated.TelegramChatId, updated);
            MessageQueueManager.AddGroupIfNotPresent(updated);

            MessageQueueManager.RemoveGroupIfNotPresent(message.Chat.Id);
            CacheData.Groups.Remove(message.Chat.Id);
        }
Esempio n. 8
0
 public TelegramGroup Add(TelegramGroup telegramGroup, int callerId)
 {
     using (UBContext ubc = new UBContext())
     {
         try
         {
             ubc.Add(telegramGroup);
             ubc.SaveChanges();
             return(telegramGroup);
         }
         catch (Exception ex)
         {
             Utils.Logging.AddLog(new SystemLog()
             {
                 LoggerName = "Unifiedban",
                 Date       = DateTime.Now,
                 Function   = "Unifiedban.Data.TelegramGroupService.Add",
                 Level      = SystemLog.Levels.Warn,
                 Message    = ex.Message,
                 UserId     = callerId
             });
             if (ex.InnerException != null)
             {
                 Utils.Logging.AddLog(new SystemLog()
                 {
                     LoggerName = "Unifiedban.Data",
                     Date       = DateTime.Now,
                     Function   = "Unifiedban.Data.TelegramGroupService.Add",
                     Level      = SystemLog.Levels.Warn,
                     Message    = ex.InnerException.Message,
                     UserId     = callerId
                 });
             }
         }
         return(null);
     }
 }
Esempio n. 9
0
        private static void connectToHub()
        {
            if (CacheData.FatalError ||
                CacheData.IsDisposing)
            {
                return;
            }

            connection = new HubConnectionBuilder()
                         .WithUrl(CacheData.Configuration["HubServerAddress"])
                         .WithAutomaticReconnect()
                         .Build();

            connection.Reconnected += connectionId =>
            {
                Data.Utils.Logging.AddLog(new Models.SystemLog()
                {
                    LoggerName = CacheData.LoggerName,
                    Date       = DateTime.Now,
                    Function   = "connectToHub()",
                    Level      = Models.SystemLog.Levels.Info,
                    Message    = "Reconnecting Hub Server",
                    UserId     = -1
                });

                connection.InvokeAsync("Identification", CacheData.Configuration["HubServerToken"]);

                return(Task.CompletedTask);
            };

            Data.Utils.Logging.AddLog(new Models.SystemLog()
            {
                LoggerName = CacheData.LoggerName,
                Date       = DateTime.Now,
                Function   = "connectToHub()",
                Level      = Models.SystemLog.Levels.Info,
                Message    = "Connecting and autenticating to Hub Server",
                UserId     = -1
            });
            connection.StartAsync().Wait();
            connection.InvokeAsync("Identification", CacheData.Configuration["HubServerToken"]);

            connection.On <string, string>("MessageToBot",
                                           (functionName, data) =>
            {
                switch (functionName)
                {
                case "Identification":
                    if (data == "KO")
                    {
                        Data.Utils.Logging.AddLog(new Models.SystemLog()
                        {
                            LoggerName = CacheData.LoggerName,
                            Date       = DateTime.Now,
                            Function   = "MessageToBot",
                            Level      = Models.SystemLog.Levels.Error,
                            Message    = "Hub Server answered KO on authentication",
                            UserId     = -1
                        });
                        connection.DisposeAsync();
                    }
                    break;
                }
            });

            connection.On <string, string, string, string>("UpdateSetting",
                                                           (dashboardUserId, groupId, parameter, value) =>
            {
                if (CacheData.FatalError ||
                    CacheData.IsDisposing)
                {
                    return;
                }

                HandleWsCommand(dashboardUserId, groupId, parameter, value);
            });

            connection.On <string, string, string, bool>("ToggleStatus",
                                                         (dashboardUserId, groupId, parameter, value) =>
            {
                if (CacheData.FatalError ||
                    CacheData.IsDisposing)
                {
                    return;
                }

                TelegramGroup group = CacheData.Groups.Values
                                      .SingleOrDefault(x => x.GroupId == groupId);
                if (group == null)
                {
                    return;
                }

                if (!UserTools.CanHandleGroup(dashboardUserId, groupId))
                {
                    return;
                }

                group.State = value ? TelegramGroup.Status.Active : TelegramGroup.Status.Inactive;
            });
        }