示例#1
0
        void OnMessageReceived(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                Disconnect();
                return;
            }
            if (e.BytesTransferred > 0)
            {
                ChatMessage chatMessage = new ChatMessage(e.Buffer, e.BytesTransferred);

                if (chatMessage.Valid)
                {
                    Console.WriteLine(RemoteEndPoint.Address.ToString() + ", " + attachedAccount.Username + ", Receive: " +
                        Enum.GetName(typeof(ChatMessageSubjects), chatMessage.Subject) + ";" + chatMessage.Text + "\n");

                    if (MessageReceived != null)
                        MessageReceived(this, chatMessage);
                }
                else
                {
                    Console.WriteLine(RemoteEndPoint.Address.ToString() + ", " + attachedAccount.Username + ", FailReceive: " + chatMessage.Error);
                }
            }
            else
            {
                Console.WriteLine("Received a message with 0 bytes");
                Disconnect();
                return;
            }
            e.SetBuffer(new byte[1000], 0, 1000);
            if (!socket.ReceiveAsync(e))
            {
                Disconnect();
            }
        }
示例#2
0
文件: Program.cs 项目: Klako/ShitChat
        static void Program_MessageReceived(Connection sender, ChatMessage chatMessage)
        {
            if (chatMessage.Subject == ChatMessageSubjects.Message)
            {
                if (!mutedAccounts.Exists(new Predicate<Account>((acc) => acc.Username == sender.attachedAccount.Username)))
                {
                    foreach (Connection connection in connections)
                    {
                        if (connection != sender && connection.attachedAccount.Username != "")
                        {
                            connection.Send(Enum.GetName(typeof(AccountTypes), sender.attachedAccount.AccountType) + ";" +
                                            sender.attachedAccount.Username + ";Message;" + chatMessage.Text, ChatMessageSubjects.Message);
                        }
                    }
                }
                else
                {
                    sender.Send("Fail;CheckPrivilege", ChatMessageSubjects.AdminTool);
                }
            }
            else if (chatMessage.Subject == ChatMessageSubjects.Login)
            {
                string username = chatMessage.Text.Split(';')[0];
                string password = chatMessage.Text.Split(';')[1];
                foreach (Connection connection in connections)
                {
                    if (connection.attachedAccount.Username == username)
                    {
                        sender.Send("Fail;LoggedIn", ChatMessageSubjects.Login);
                        return;
                    }
                }
                foreach (Account account in accounts)
                {
                    if (account.Username == username)
                    {
                        foreach (Account bannedAccount in bannedAccounts)
                        {
                            if (bannedAccount.Username == username)
                            {
                                sender.Send("Fail;Banned", ChatMessageSubjects.Login);
                                return;
                            }
                        }
                        if (account.Password == password)
                        {
                            sender.attachedAccount = account;
                            sender.Send("Success", ChatMessageSubjects.Login);
                            SendOnlineList();
                        }
                        else
                        {
                            sender.Send("Fail;WrongPassword", ChatMessageSubjects.Login);
                        }
                        return;
                    }
                }
                sender.Send("Fail;NonExistent", ChatMessageSubjects.Login);
            }
            else if (chatMessage.Subject == ChatMessageSubjects.Register)
            {
                string username = chatMessage.Text.Split(';')[0];
                string password = chatMessage.Text.Split(';')[1];

                foreach (char ch in username)
                {
                    if (!char.IsLetterOrDigit(ch))
                    {
                        sender.Send("Fail;Syntax;Username", ChatMessageSubjects.Register);
                        return;
                    }
                }
                foreach (char ch in password)
                {
                    if (!char.IsLetterOrDigit(ch))
                    {
                        sender.Send("Fail;Syntax;Password", ChatMessageSubjects.Register);
                        return;
                    }
                }

                foreach (Account account in accounts)
                {
                    if (account.Username == username)
                    {
                        sender.Send("Fail;AlreadyExists", ChatMessageSubjects.Register);
                        return;
                    }
                }
                accounts.Add(new Account(username, password, AccountTypes.User));
                sender.Send("Success", ChatMessageSubjects.Register);
                accounts.SaveToFile("accs.dat");
            }
            else if (chatMessage.Subject == ChatMessageSubjects.UserTool)
            {
                if (sender.attachedAccount.Username != "")
                {
                    if (chatMessage.Text.Split(';')[0] == "ChangePassword")
                    {
                        string oldPassword = chatMessage.Text.Split(';')[1];
                        string newPassword = chatMessage.Text.Split(';')[2];
                        if (oldPassword == sender.attachedAccount.Password)
                        {
                            if (newPassword != "")
                            {
                                foreach (char ch in newPassword)
                                {
                                    if (!char.IsLetterOrDigit(ch))
                                    {
                                        sender.Send("ChangePassword;Fail;Syntax", ChatMessageSubjects.UserTool);
                                        return;
                                    }
                                }
                                sender.attachedAccount = new Account(sender.attachedAccount.Username, newPassword, sender.attachedAccount.AccountType);
                                sender.Send("ChangePassword;Success", ChatMessageSubjects.UserTool);

                                sender.attachedAccount.Password = newPassword;
                                accounts.RemoveAll(new Predicate<Account>((acc) => acc.Username == sender.attachedAccount.Username));
                                accounts.Add(sender.attachedAccount);

                                accounts.SaveToFile("accs.dat");
                            }
                            else
                            {
                                sender.Send("ChangePassword;Fail;Syntax", ChatMessageSubjects.UserTool);
                            }
                        }
                        else
                        {
                            sender.Send("ChangePassword;Fail;PasswordMismatch", ChatMessageSubjects.UserTool);
                        }
                    }
                }
            }
            else if (chatMessage.Subject == ChatMessageSubjects.AdminTool)
            {
                if (sender.attachedAccount.Username != "" && sender.attachedAccount.AccountType == AccountTypes.Admin)
                {
                    if (chatMessage.Text.Split(';')[0] == "Kick")
                    {
                        string username = chatMessage.Text.Split(';')[1];
                        foreach (Connection connection in connections.ToArray())
                        {
                            if (connection.attachedAccount.Username == username)
                            {
                                connection.Disconnect();
                                break;
                            }
                        }
                    }
                    else if (chatMessage.Text.Split(';')[0] == "Ban")
                    {
                        string username = chatMessage.Text.Split(';')[1];
                        foreach (Account account in accounts.ToArray())
                        {
                            if (account.Username == username)
                            {
                                if (!bannedAccounts.Exists(new Predicate<Account>((acc) => acc.Username == username)))
                                {
                                    bannedAccounts.Add(account);
                                    bannedAccounts.SaveToFile("bans.dat");
                                    foreach (Connection connection in connections)
                                    {
                                        if (connection.attachedAccount.Username == username)
                                        {
                                            connection.Disconnect();
                                            break;
                                        }
                                    }
                                    sender.Send("Ban;Success;" + username, ChatMessageSubjects.AdminTool);
                                    return;
                                }
                                sender.Send("Ban;Fail;AlreadyBanned", ChatMessageSubjects.AdminTool);
                                return;
                            }
                        }
                        sender.Send("Ban;Fail;NonExistent", ChatMessageSubjects.AdminTool);
                    }
                    else if (chatMessage.Text.Split(';')[0] == "Unban")
                    {
                        string username = chatMessage.Text.Split(';')[1];
                        foreach (Account account in bannedAccounts.ToArray())
                        {
                            if (account.Username == username)
                            {
                                bannedAccounts.Remove(account);
                                bannedAccounts.SaveToFile("bans.dat");
                                sender.Send("Unban;Success;" + username, ChatMessageSubjects.AdminTool);
                                return;
                            }
                        }
                        sender.Send("Unban;Fail;NonExistent", ChatMessageSubjects.AdminTool);
                    }
                    else if (chatMessage.Text.Split(';')[0] == "Mute")
                    {
                        string username = chatMessage.Text.Split(';')[1];
                        foreach (Account account in accounts.ToArray())
                        {
                            if (account.Username == username)
                            {
                                if (!mutedAccounts.Exists(new Predicate<Account>((acc) => acc.Username == username)))
                                {
                                    mutedAccounts.Add(account);
                                    mutedAccounts.SaveToFile("muted.dat");
                                }
                                sender.Send("Mute;Success;" + username, ChatMessageSubjects.AdminTool);
                                return;
                            }
                        }
                    }
                    else if (chatMessage.Text.Split(';')[0] == "Unmute")
                    {
                        string username = chatMessage.Text.Split(';')[1];
                        foreach (Account account in mutedAccounts.ToArray())
                        {
                            if (account.Username == username)
                            {
                                mutedAccounts.Remove(account);
                                mutedAccounts.SaveToFile("muted.dat");
                                sender.Send("Unmute;Success;" + username, ChatMessageSubjects.AdminTool);
                                return;
                            }
                        }
                        sender.Send("Unban;Fail;NonExistent", ChatMessageSubjects.AdminTool);
                    }
                    else if (chatMessage.Text.Split(';')[0] == "ChangeUsername")
                    {

                    }
                    else if (chatMessage.Text.Split(';')[0] == "ChangePassword")
                    {

                    }
                    else if (chatMessage.Text.Split(';')[0] == "RemoveAccount")
                    {
                        string username = chatMessage.Text.Split(';')[1];
                        foreach (Account account in accounts.ToArray())
                        {
                            if (account.Username == username)
                            {
                                accounts.RemoveAll(new Predicate<Account>((acc) => acc.Username == username));
                                accounts.SaveToFile("accs.dat");
                                bannedAccounts.RemoveAll(new Predicate<Account>((acc) => acc.Username == username));
                                bannedAccounts.SaveToFile("bans.dat");
                                mutedAccounts.RemoveAll(new Predicate<Account>((acc) => acc.Username == username));
                                mutedAccounts.SaveToFile("muted.dat");
                            }
                        }
                    }
                    else if (chatMessage.Text.Split(';')[0] == "AddAccount")
                    {

                    }
                }
                else
                {
                    sender.Send("Fail;CheckPrivilege", ChatMessageSubjects.AdminTool);
                }
            }
            else if (chatMessage.Subject == ChatMessageSubjects.None)
            {

            }
        }
示例#3
0
        public void Send(string innerText, ChatMessageSubjects subject)
        {
            ChatMessage chatMessage = new ChatMessage(innerText, subject);

            if (chatMessage.Valid)
            {
                Console.WriteLine(RemoteEndPoint.Address.ToString() + ", " + attachedAccount.Username + ", Send: " +
                    Enum.GetName(typeof(ChatMessageSubjects), subject) + ";" + innerText);

                try
                {
                    socket.Send(chatMessage.Data);
                }
                catch
                {
                    Disconnect();
                }
            }
            else
            {
                Console.WriteLine(RemoteEndPoint.Address.ToString() + ", " + attachedAccount.Username + ", FailSend: " +
                    Enum.GetName(typeof(ChatMessageSubjects), subject) + ";" + innerText);
            }
        }