Beispiel #1
0
        private async Task DeleteNickInGroups(List <string> groupNames, string firstPartOfThePast, string lastPartOfThePast, string typeGroups)
        {
            if (groupNames != null)
            {
                foreach (var groupName in groupNames)
                {
                    var path            = Path.Combine(firstPartOfThePast, groupName, lastPartOfThePast);
                    var needDeleteGroup = false;
                    await FileMaster.UpdateFile <string>(path, (users) =>
                    {
                        users.Remove(userNickname);
                        if (lastPartOfThePast == "users.json")
                        {
                            if (users.Count == 0)
                            {
                                needDeleteGroup = true;
                            }
                        }
                        return(users, true);
                    });

                    if (needDeleteGroup)
                    {
                        GroupDeleter groupDeleter = new GroupDeleter(groupName,
                                                                     Path.Combine(firstPartOfThePast, groupName),
                                                                     typeGroups,
                                                                     messenger.Server.UsersPath);
                        await groupDeleter.Run();
                    }
                }
            }
        }
Beispiel #2
0
        private async Task AddUserToGroups(string userPath, string nameGroup, string typeGroup, User user)
        {
            string partPath;

            switch (typeGroup)
            {
            case "public":
                partPath = "userGroups.json";
                break;

            case "secret":
                partPath = "secretGroups.json";
                break;

            default:
                return;
            }
            await FileMaster.UpdateFile(Path.Combine(userPath, partPath), FileMaster.AddData(nameGroup));

            await FileMaster.UpdateFile <string>(Path.Combine(PathChat, "users.json"), users =>
            {
                return(new List <string>()
                {
                    user.Nickname
                }, true);
            });
Beispiel #3
0
        private async Task <bool> DeleteData(string[] pathsElement)
        {
            var needDeleteGroup = false;
            await FileMaster.UpdateFile <string>(Path.Combine(PathGroup, "users.json"), users =>
            {
                users.Remove(UserNick);
                if (users.Count == 0)
                {
                    needDeleteGroup = true;
                    return(users, false);
                }
                return(users, true);
            });

            if (needDeleteGroup)
            {
                await DeleteGroup();
            }

            if (pathsElement[0] == "peopleChatsBeen.json")
            {
                await DeletePeopleChatsBeen(pathsElement[0]);
            }
            else
            {
                await FileMaster.UpdateFile <string>(Path.Combine(UsersPath, UserNick, pathsElement[0]), nameChats =>
                {
                    return((nameChats ?? new List <string>())
                           .Where(group => group != NameGroup)
                           .ToList(), true);
                });
        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);
        }
Beispiel #5
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;
 }
Beispiel #6
0
 private async Task <bool> CheckDeleteInvitation(string path, string data)
 {
     return(await FileMaster.UpdateFile <string>(path, invitations =>
     {
         if (invitations == null)
         {
             return (invitations, false);
         }
         return (invitations, invitations.Remove(data));
     }));
Beispiel #7
0
 private async Task ReadWriteData(string path)
 {
     await FileMaster.UpdateFile <string>(path, (messages) =>
     {
         if (messages != null)
         {
             return(messages.
                    Select(message => userNickname == message.Remove(userNickname.Length) ? $"{userNickname} was banned" : message)
                    .ToList(), true);
         }
Beispiel #8
0
 private async Task DeleteLeavedPeople(List <string> paths)
 {
     foreach (var path in paths)
     {
         await FileMaster.UpdateFile <PersonChat>(path, nameChats =>
         {
             return((nameChats ?? new List <PersonChat>())
                    .Where(chat => chat.NameChat != NameChat)
                    .ToList(), true);
         });
Beispiel #9
0
 private async Task SetUpPort()
 {
     await FileMaster.UpdateFile <int>(Path.Combine(mainDirectoryPath, "mainDirectoryPath"), ports =>
     {
         if (ports == null)
         {
             ports = new List <int>();
         }
         ShowPorts(ports);
         ReadPort(ports);
         return(SavePort(ports));
     });
 }
Beispiel #10
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);
         });
     }
 }
        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);
        }
Beispiel #12
0
        private async Task ReadWriteInvitationOrGroup(string path, string nameGroup, string typeGroup)
        {
            string data;

            if (typeGroup == "pg")
            {
                data = $"public: {nameGroup}";
            }
            else if (typeGroup == "sg")
            {
                data = $"secret: {nameGroup}";
            }
            else
            {
                data = nameGroup;
            }
            await FileMaster.UpdateFile(path, FileMaster.AddData(data));
        }
 private async Task Unban()
 {
     Console.WriteLine("Write name IP");
     var IP = Console.ReadLine();
     await FileMaster.UpdateFile <string>(Path.Combine(messenger.Server.BansPath, "IPsBun.json"), banIPs =>
     {
         if ((banIPs ?? new List <string>()).Contains(IP))
         {
             Console.WriteLine("Okey");
             banIPs.Remove(IP);
             return(banIPs, true);
         }
         else
         {
             Console.WriteLine("Don`t have this IP in ban list");
             return(banIPs, false);
         }
     });
 }
Beispiel #14
0
        private async Task <string> AddGroup(string nameGroup, string typeGroup, List <string> invitedPeople)
        {
            string pathGroup;

            switch (typeGroup)
            {
            case "pg":
                pathGroup = Path.Combine(messenger.Server.PublicGroupPath, nameGroup);
                break;

            default:     //(sg)
                pathGroup = Path.Combine(messenger.Server.SecretGroupPath, nameGroup);
                break;
            }
            FileMaster.CreateDirectory(pathGroup);
            await FileMaster.UpdateFile(Path.Combine(pathGroup, "users.json"), FileMaster.AddData(user.Nickname));

            await FileMaster.UpdateFile(Path.Combine(pathGroup, "invitation.json"), FileMaster.AddSomeData(invitedPeople));

            return(pathGroup);
        }
Beispiel #15
0
 private async Task<bool> DeleteAccount(IEnumerable<UserNicknameAndPasswordAndIPs> usersData)
 {
     var userNicknameAndPassword = await CheckNicknameAndPassword(usersData);
     if (userNicknameAndPassword.Equals(default(UserNicknameAndPasswordAndIPs)))
     {
         return false;
     }
     if (await communication.SendMessageListenClient("Do you realy want, delete your accaunt? if yes, click Enter") != "Yes")
     {
         return await SelectMode(usersData);
     }
     var result = await FileMaster.UpdateFile<UserNicknameAndPasswordAndIPs>(FilePath, oldData =>
     {
         if (oldData == null)
         {
             return (oldData, false);
         }
         else
         {
             if (!LastFindData(oldData, userNicknameAndPassword.Nickname))
             {
                 return (oldData, false);
             }
             oldData = oldData.Where(x => x.Nickname != userNicknameAndPassword.Nickname).ToList();
         }
         return (oldData, true);
     });
     if (result)
     {
         await communication.SendMessage("Account was deleted");
         UserDeleter userDeleter = new UserDeleter(messenger);
         await userDeleter.Run(userNicknameAndPassword.Nickname, false);
         return false;
     }
     else
     {
         await communication.SendMessage("Don`t have this nickname");
         return await SelectMode(usersData);
     }
 }
Beispiel #16
0
        private async Task AddInvites(string namePerson)
        {
            string partInvitation;

            switch (TypeChat)
            {
            case "pg":
            case "ug":
                partInvitation = "public: ";
                break;

            case "sg":
                partInvitation = "secret: ";
                break;

            default:
                partInvitation = "";
                break;
            }
            await FileMaster.UpdateFile(Path.Combine(messenger.Server.UsersPath, namePerson, "invitation.json"), FileMaster.AddData($"{partInvitation}{NameChat}"));

            await FileMaster.UpdateFile(Path.Combine(PathChat, "invitation.json"), FileMaster.AddData(namePerson));
        }
Beispiel #17
0
 private async Task AddInvitation(string path, string data)
 {
     await FileMaster.UpdateFile(path, FileMaster.AddData(data));
 }
 private async void BanOnIP(UserNicknameAndPasswordAndIPs user)
 {
     BanOnNickname(user);
     await FileMaster.UpdateFile(Path.Combine(messenger.Server.BansPath, "IPsBun.json"), FileMaster.AddSomeData(user.IPs));
 }
Beispiel #19
0
 private async void WriteNewPerson(string path, PersonChat personChat)
 {
     await FileMaster.UpdateFile(path, FileMaster.AddData(personChat));
 }