Exemplo n.º 1
0
            public static void RegisterUser(Global.Messages.Registration package)
            {
                Global.User user = new Global.User();
                user.username    = package.username;
                user.password    = BCryptHelper.HashPassword(package.password, BCryptHelper.GenerateSalt());
                user.banned      = false;
                user.banReason   = "";
                user.bannedUntil = "";

                WriteUserToXML(user);
            }
Exemplo n.º 2
0
        private void ConnectionHandler(Socket client)
        {
            byte[] rawPackage = new byte[256];
            client.Receive(rawPackage);

            if (!Encoding.UTF8.GetString(rawPackage).Contains("registration"))
            {
                Global.Messages.Login loginPackage = new Global.Messages.Login();
                loginPackage = JsonConvert.DeserializeObject <Global.Messages.Login>(@Encoding.UTF8.GetString(rawPackage));

                if (Utils.XmlHandler.ValidateUser(loginPackage))
                {
                    Global.Messages.LoginResponse loginResponse = new Global.Messages.LoginResponse();
                    loginResponse.status       = "OK";
                    loginResponse.loginMessage = "Welcome to the server! Read the rules before chatting!";

                    client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(loginResponse)));

                    string info = loginPackage.username + " joined the server";
                    messages.Invoke(new MethodInvoker(delegate { messages.Items.Add(info); }));

                    users.Invoke(new MethodInvoker(delegate { users.Items.Add(loginPackage.username); }));
                    while (client.Connected)
                    {
                        try
                        {
                            rawPackage = new byte[4096];
                            client.Receive(rawPackage);

                            messages.Invoke(new MethodInvoker(delegate { messages.Items.Add(loginPackage.username + " : " + JsonConvert.DeserializeObject <Global.Messages.Message>(Encoding.UTF8.GetString(rawPackage)).message); }));
                        }
                        catch (Exception e) { }
                    }
                }
                else
                {
                    Global.Messages.LoginResponse loginResponse = new Global.Messages.LoginResponse();
                    loginResponse.status       = "USER NOT FOUND";
                    loginResponse.loginMessage = "User does not exists or bad login details";

                    client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(loginResponse)));
                }
            }
            else if (Encoding.UTF8.GetString(rawPackage).Contains("registration"))
            {
                Global.Messages.Registration registrationPackage = new Global.Messages.Registration();
                registrationPackage = JsonConvert.DeserializeObject <Global.Messages.Registration>(Encoding.UTF8.GetString(rawPackage));

                Global.Messages.LoginResponse loginResponse = new Global.Messages.LoginResponse();
                //Check if the user alredy exists
                if (Utils.XmlHandler.UserExists(registrationPackage.username))
                {
                    loginResponse.status       = "USER ALREDY EXISTS";
                    loginResponse.loginMessage = "This user alredy exists";
                }
                Utils.XmlHandler.RegisterUser(registrationPackage);
                loginResponse.status       = "OK";
                loginResponse.loginMessage = "Welcome to the server! Read the rules before chatting!";

                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(loginResponse)));

                string info = registrationPackage.username + " + joined the server";
                messages.Invoke(new MethodInvoker(delegate { messages.Items.Add(info); }));
                users.Invoke(new MethodInvoker(delegate { users.Items.Add(registrationPackage.username); }));
            }
        }