Пример #1
0
 private async Task<bool> AddNewUser(IEnumerable<string> userData)
 {
     var IPs = new List<string>();
     IPs.Add(IP);
     UserNicknameAndPasswordAndIPs userNicknameAndPassword = new UserNicknameAndPasswordAndIPs(userData.First(), userData.Last(), IPs);
     var result = await FileMaster.UpdateFile<UserNicknameAndPasswordAndIPs>(FilePath, oldData =>
     {
         if (oldData == null)
         {
             oldData = new List<UserNicknameAndPasswordAndIPs>();
         }
         else
         {
             if (!LastCheck(oldData, userNicknameAndPassword.Nickname))
             {
                 return (oldData, false);
             }
         }
         oldData.Add(userNicknameAndPassword);
         return (oldData, true);
     });
     if (result)
     {
         await communication.SendMessage("You enter to messenger");
         FileMaster.CreateDirectory(Path.Combine(messenger.Server.UsersPath, userNicknameAndPassword.Nickname));
         nickname = userNicknameAndPassword.Nickname;
     }
     else
     {
         await communication.SendMessage("This nickname is currently in use, bye");
     }
     return result;
 }
Пример #2
0
 private async Task<bool> CheckPasswordCondition(UserNicknameAndPasswordAndIPs userNicknameAndPassword, bool checkHavingPassword, string password, int i)
 {
     if (password.Length < 8)
     {
         if (i == 1)
         {
             await communication.SendMessage("Number of attempts 0, so bye");
         }
         else
         {
             await communication.SendMessage($"Password length < 8, enter another password, a number of attempts left = {i - 1}");
         }
         return false;
     }
     if (checkHavingPassword)
     {
         if (password != userNicknameAndPassword.Password)
         {
             if (i == 1)
             {
                 await communication.SendMessage($"Number of attempts 0, so bye");
             }
             else
             {
                 await communication.SendMessage($"Wrong password, a number of attempts left = {i - 1}");
             }
             return false;
         }
     }
     return true;
 }
Пример #3
0
        private async Task <(bool, UserNicknameAndPasswordAndIPs)> RemoveUser(string nickname)
        {
            var user = new UserNicknameAndPasswordAndIPs();

            return(await FileMaster.UpdateFile <UserNicknameAndPasswordAndIPs>
                       (Path.Combine(messenger.Server.NicknamesAndPasswordsPath, "users.json"), users =>
            {
                if (users == null)
                {
                    return (users, false);
                }
                var findUser = false;
                foreach (var oneUser in users)
                {
                    if (nickname == oneUser.Nickname)
                    {
                        findUser = true;
                        user = oneUser;
                        break;
                    }
                }
                if (findUser)
                {
                    users.Remove(user);
                    return (users, true);
                }
                return (users, false);
            }), user);
        }
Пример #4
0
        private async Task <bool> CheckBans(UserNicknameAndPasswordAndIPs user)
        {
            var banUsers = await FileMaster.ReadAndDeserialize <string>(Path.Combine(messenger.Server.BansPath, "nicknamesBun.json"));

            if (banUsers == null)
            {
                return(false);
            }
            return(banUsers.Contains(user.Nickname));
        }
Пример #5
0
 private async Task<string> CheckPassword(UserNicknameAndPasswordAndIPs userNicknameAndPassword, bool checkHavingPassword)
 {
     await communication.SendMessage("Enter password bigger than 7 symbols");
     for (int i = 5; i > 0; i--)
     {
         var password = await communication.ListenClient();
         if (await CheckPasswordCondition(userNicknameAndPassword, checkHavingPassword, password, i))
         {
             return password;
         }
     }
     return "";
 }
Пример #6
0
 private async Task AddIP(UserNicknameAndPasswordAndIPs userNicknameAndPasswordAndIPs)
 {
     if (!userNicknameAndPasswordAndIPs.IPs.Contains(IP))
     {
         await FileMaster.UpdateFile<UserNicknameAndPasswordAndIPs>(FilePath, users =>
         {
             var userWithNewIP = users
             .Where(acc => acc.Nickname == userNicknameAndPasswordAndIPs.Nickname && acc.Password == userNicknameAndPasswordAndIPs.Password)
             .First();
             users.Remove(userWithNewIP);
             userWithNewIP.IPs.Add(IP);
             users.Add(userWithNewIP);
             return (users, true);
         });
     }
 }
Пример #7
0
        private async void BanOnNickname(UserNicknameAndPasswordAndIPs user)
        {
            await FileMaster.UpdateFile(Path.Combine(messenger.Server.BansPath, "nicknamesBun.json"), FileMaster.AddData(user.Nickname));

            lock (messenger.OnlineLock)
            {
                foreach (var userOnline in messenger.online)
                {
                    if (userOnline.Nickname == user.Nickname)
                    {
                        userOnline.communication.EndTask = true;
                        break;
                    }
                }
            }
            await userDeleter.Run(user.Nickname, true);
        }
Пример #8
0
 private async void BanOnIP(UserNicknameAndPasswordAndIPs user)
 {
     BanOnNickname(user);
     await FileMaster.UpdateFile(Path.Combine(messenger.Server.BansPath, "IPsBun.json"), FileMaster.AddSomeData(user.IPs));
 }