Esempio n. 1
0
 private async Task <bool> CheckPerson(string namePerson)
 {
     return(((await FileMaster.ReadAndDeserialize <UserNicknameAndPasswordAndIPs>(Path.Combine(messenger.Server.NicknamesAndPasswordsPath, "users.json")))
             ?? new List <UserNicknameAndPasswordAndIPs>())
            .Select(user => user.Nickname)
            .Contains(namePerson));
 }
Esempio n. 2
0
        public async Task <List <string> > FindInvitations()
        {
            Invitations = await FileMaster.ReadAndDeserialize <string>
                              (Path.Combine(messenger.Server.UsersPath, Nick, "invitation.json"));

            return(Invitations);
        }
        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);
        }
Esempio n. 4
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);
            });
Esempio n. 5
0
        public async Task <List <string> > FindSecretGroups()
        {
            SecretGroups = await FileMaster.ReadAndDeserialize <string>
                               (Path.Combine(messenger.Server.UsersPath, Nick, "secretGroups.json"));

            return(SecretGroups);
        }
Esempio n. 6
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();
                    }
                }
            }
        }
Esempio n. 7
0
        public async Task Run(string nick, bool needChangeMessages)
        {
            userNickname = nick;
            await DeleteData(messenger.Server.PublicGroupPath,
                             await FileMaster.ReadAndDeserialize <string>(Path.Combine(messenger.Server.UsersPath, userNickname, "userGroups.json")),
                             await FileMaster.ReadAndDeserialize <string>(Path.Combine(messenger.Server.UsersPath, userNickname, "leavedUserGroups.json")),
                             "ug",
                             needChangeMessages);

            await DeleteData(messenger.Server.SecretGroupPath,
                             await FileMaster.ReadAndDeserialize <string>(Path.Combine(messenger.Server.UsersPath, userNickname, "secretGroups.json")),
                             await FileMaster.ReadAndDeserialize <string>(Path.Combine(messenger.Server.UsersPath, userNickname, "leavedSecretGroups.json")),
                             "sg",
                             needChangeMessages);
            await DeleteData(messenger.Server.PeopleChatsPath,
                             ((await FileMaster.ReadAndDeserialize <PersonChat>(Path.Combine(messenger.Server.UsersPath, userNickname, "peopleChatsBeen.json")))
                              ?? new List <PersonChat>()).Select(chat => chat.NameChat).ToList(),
                             ((await FileMaster.ReadAndDeserialize <PersonChat>(Path.Combine(messenger.Server.UsersPath, userNickname, "leavedPeopleChatsBeen.json")))
                              ?? new List <PersonChat>()).Select(chat => chat.NameChat).ToList(),
                             "pp",
                             needChangeMessages);
            await DeleteInvitations();

            //fileMaster.DeleterFolder($"{Users}\\{user}");
        }
Esempio n. 8
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;
 }
Esempio n. 9
0
 public List <string> FindPublicGroups()
 {
     PublicGroups = FileMaster.GetDirectories(messenger.Server.PublicGroupPath)
                    .Select(path => FileMaster.GetFileName(path))
                    .ToList();
     return(PublicGroups);
 }
Esempio n. 10
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);
                });
Esempio n. 11
0
 private async Task <List <string> > FindChatsPeople()
 {
     return(((await FileMaster.ReadAndDeserialize <UserNicknameAndPasswordAndIPs>(Path.Combine(messenger.Server.NicknamesAndPasswordsPath, "users.json")))
             ?? new List <UserNicknameAndPasswordAndIPs>())
            .Select(user => user.Nickname)
            .Where(nickname => nickname != user.Nickname)
            .ToList());
 }
Esempio n. 12
0
 private void DeleteDirectories()
 {
     FileMaster.DeleterFolder(messenger.Server.BansPath);
     FileMaster.DeleterFolder(messenger.Server.NicknamesAndPasswordsPath);
     FileMaster.DeleterFolder(messenger.Server.PeopleChatsPath);
     FileMaster.DeleterFolder(messenger.Server.PublicGroupPath);
     FileMaster.DeleterFolder(messenger.Server.SecretGroupPath);
     FileMaster.DeleterFolder(messenger.Server.UsersPath);
 }
Esempio n. 13
0
 public async Task <List <string> > FindChatsWithPeople()
 {
     ChatsWithPeople = ((await FileMaster.ReadAndDeserialize <PersonChat>
                             (Path.Combine(messenger.Server.UsersPath, Nick, "peopleChatsBeen.json")))
                        ?? new List <PersonChat>())
                       .Select(chat => chat.Nicknames[0] != Nick ? chat.Nicknames[0] : chat.Nicknames[1])
                       .ToList();
     return(ChatsWithPeople);
 }
Esempio n. 14
0
 public void CreateDirectories()
 {
     FileMaster.CreateDirectory(BansPath);
     FileMaster.CreateDirectory(NicknamesAndPasswordsPath);
     FileMaster.CreateDirectory(PeopleChatsPath);
     FileMaster.CreateDirectory(PublicGroupPath);
     FileMaster.CreateDirectory(SecretGroupPath);
     FileMaster.CreateDirectory(UsersPath);
 }
Esempio n. 15
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));
     }));
Esempio n. 16
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);
         }
Esempio n. 17
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));
        }
Esempio n. 18
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);
         });
Esempio n. 19
0
        public async Task <List <string> > FindAllElsePeople()
        {
            await FindChatsWithPeople();

            AllElsePeople = ((await FileMaster.ReadAndDeserialize <UserNicknameAndPasswordAndIPs>
                                  (Path.Combine(messenger.Server.NicknamesAndPasswordsPath, "users.json")))
                             ?? new List <UserNicknameAndPasswordAndIPs>())
                            .Select(x => x.Nickname)
                            .Where(x => x != Nick)
                            .Except(ChatsWithPeople)
                            .ToList();
            return(AllElsePeople);
        }
Esempio n. 20
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));
     });
 }
Esempio n. 21
0
        private async Task <GroupInformation> CreateChat(string namePerson, string typeGroup)
        {
            var        nameChat   = $"{namePerson} {user.Nickname}";
            PersonChat personChat = new PersonChat(new string[] { namePerson, user.Nickname }, nameChat);

            WriteNewPerson(Path.Combine(messenger.Server.UsersPath, user.Nickname, "peopleChatsBeen.json"), personChat);
            WriteNewPerson(Path.Combine(messenger.Server.UsersPath, namePerson, "peopleChatsBeen.json"), personChat);
            var path = Path.Combine(messenger.Server.PeopleChatsPath, nameChat);

            FileMaster.CreateDirectory(path);
            await AddData(Path.Combine(path, "users.json"), user.Nickname);
            await AddData(Path.Combine(path, "users.json"), namePerson);

            return(new GroupInformation(true, typeGroup, nameChat, path));
        }
Esempio n. 22
0
 private async Task<bool> CheckNicknamesBan(string nick)
 {
     var nicksBan = await FileMaster.ReadAndDeserialize<string>(Path.Combine(messenger.Server.BansPath, "nicknamesBun.json"));
     if (nicksBan != null)
     {
         foreach (var nickBan in nicksBan)
         {
             if (nick == nickBan)
             {
                 return true;
             }
         }
     }
     return false;
 }
Esempio n. 23
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);
         });
     }
 }
Esempio n. 24
0
 private async Task<bool> CheckIPsBan()
 {
     var path = Path.Combine(messenger.Server.BansPath, "IPsBun.json");
     var IPsBan = await FileMaster.ReadAndDeserialize<string>(path);
     if (IPsBan != null)
     {
         foreach (var IPBan in IPsBan)
         {
             if (IP == IPBan)
             {
                 await communication.SendMessage("Your IP is in ban, bye");
                 return true;
             }
         }
     }
     return false;
 }
Esempio n. 25
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);
        }
Esempio n. 26
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));
        }
Esempio n. 27
0
        public async Task Run()
        {
            var invitationName = "";
            var pathElement    = "";

            if (TypeChat == "pg" || TypeChat == "ug")
            {
                invitationName = $"public: {NameChat}";
                pathElement    = "leavedUserGroups";
            }
            else if (TypeChat == "sg")
            {
                invitationName = $"secret: {NameChat}";
                pathElement    = "leavedSecretGroups";
            }
            else if (TypeChat == "pp" || TypeChat == "ch")
            {
                pathElement = "leavedPeopleChatsBeen";
            }

            if (invitationName != "")
            {
                var invitationPaths = await FindUserPath(Path.Combine(PathChat, "invitation.json"), "invitation.json");

                if (invitationPaths != null || invitationPaths.Count() != 0)
                {
                    await DeleteExtraData(invitationPaths, invitationName);
                }
            }
            var leavedPaths = await FindUserPath(Path.Combine(PathChat, "leavedPeople.json"), $"{pathElement}.json");

            if (leavedPaths != null || leavedPaths.Count() != 0)
            {
                if (TypeChat == "pp" || TypeChat == "ch")
                {
                    await DeleteLeavedPeople(leavedPaths);
                }
                else
                {
                    await DeleteExtraData(leavedPaths, NameChat);
                }
            }
            FileMaster.DeleterFolder(PathChat);
        }
Esempio n. 28
0
 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);
         }
     });
 }
Esempio n. 29
0
        private async Task <bool> CheckUserPresenceGroup(string namePerson, User user)
        {
            if (!await CheckUsersLeavedPeopleInvitation(Path.Combine(PathChat, "leavedPeople.json")))
            {
                if (!await CheckUsersLeavedPeopleInvitation(Path.Combine(PathChat, "users.json")))
                {
                    if (!await CheckUsersLeavedPeopleInvitation(Path.Combine(PathChat, "invitation.json")))
                    {
                        return(false);
                    }
                    ;
                    await user.communication.SendMessage("This person has an invitation");

                    //await SendMessageAndAddUser("This person has an invitation", user);
                    return(true);
                }
                ;
                await user.communication.SendMessage("This person is in the group");

                //await SendMessageAndAddUser("This person is in the group", user);
                return(true);
            }
            ;
            await user.communication.SendMessage("This person leaved the group");

            //await SendMessageAndAddUser("This person leaved the group", user);
            return(true);

            async Task <bool> CheckUsersLeavedPeopleInvitation(string path)
            {
                var users = await FileMaster.ReadAndDeserialize <string>(path);

                if (users == null)
                {
                    return(false);
                }
                return(users.Contains(namePerson));
                //return ((await fileMaster.ReadAndDesToLString(path))
                //    ?? new List<string>())
                //    .Contains(namePerson);
            }
        }
Esempio n. 30
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);
        }