Auxilium_Server.Program.HandleSignInPacket C# (CSharp) Method

HandleSignInPacket() static private method

static private HandleSignInPacket ( Client c, string name, string pass ) : void
c Auxilium_Server.Classes.Client
name string
pass string
return void
        static void HandleSignInPacket(Client c, string name, string pass)
        {
            string n = name.Trim();

            if (n.Length == 0 || n.Length > 16 || pass.Length != 40 || !IsValidName(n))
            {
                byte[] fail = Packer.Serialize((byte)ServerPacket.SignIn, false, false);
                c.Send(fail);
                return;
            }

            MySqlCommand q = new MySqlCommand("SELECT Points, Username, Rank, Ban, Mute, Email FROM users WHERE (Username=@Username OR Email=@Username) AND Password=@Password;", SQL);
            q.Parameters.AddWithValue("@Username", n);
            q.Parameters.AddWithValue("@Password", pass);

            MySqlDataReader r = q.ExecuteReader();
            bool success = r.Read();

            if (success)
            {
                int points = r.GetInt32("Points");
                byte rank = r.GetByte("Rank");
                bool ban = r.GetBoolean("Ban");
                string email = r.GetString("Email");
                string username = r.GetString("Username");

                r.Close(); //TODO: Restructure this.

                r.Dispose();

                if (ban)
                {
                    c.Disconnect();
                    return;
                }

                //Second ban check, checks ip table.
                q = new MySqlCommand(string.Empty, SQL) {CommandText = "SELECT * FROM ipbans WHERE ip=@ip;"};
                q.Parameters.AddWithValue("@ip", c.EndPoint.Address.ToString());
                r = q.ExecuteReader();
                bool sCheck = r.Read();

                r.Close();

                if (sCheck)
                {
                    c.Disconnect();
                    return;
                }

                q = new MySqlCommand("SELECT * FROM authcodes WHERE User=@User;", SQL);
                q.Parameters.AddWithValue("@User", username);
                r = q.ExecuteReader();
                //Pending email verification
                bool unverified = r.Read() && r.GetByte("AuthType") == (byte)AuthType.AccountVerification;

                r.Close();

                if (unverified)
                {
                    byte[] notVerified = Packer.Serialize((byte)ServerPacket.NotVerified);
                    c.Send(notVerified);
                }

                byte[] data = Packer.Serialize((byte)ServerPacket.SignIn, true, !unverified);
                c.Send(data);

                //If this user is already logged in from somewhere else then disconnect them.
                Client existing = ClientFromUsername(n);
                if (existing != null && existing != c)
                {
                    existing.Disconnect();
                }

                c.Value.UserId = RunningID++;
                c.Value.Username = username;

                c.Value.Points = points;
                c.Value.Rank = rank;

                c.Value.Mute = new List<Client>();

                c.Value.LastPayout = DateTime.Now;
                c.Value.LastAction = DateTime.Now;
                c.Value.Email = email;

                c.Value.Authenticated = true;
                c.Value.Verified = !unverified;

                if (!unverified)
                {
                    SendProfile(c);
                    SendLoginBarrage(c);
                }
            }
            else
            {
                r.Close();//TODO: Restructure this.
                byte[] data1 = Packer.Serialize((byte)ServerPacket.SignIn, false, false);
                c.Send(data1);
            }
        }