Send() public method

public Send ( byte data ) : void
data byte
return void
Esempio n. 1
0
        //TODO: Don't disconnect people, instead return an error code.
        static void HandleSignInPacket(Client c, string name, string pass)
        {
            if (name.Length == 0 || name.Length > 16 || pass.Length != 40)
            {
                c.Disconnect();
                return;
            }

            //Don't let them join if they were banned.
            GetBanList();
            if (BanList.Contains(name.ToLower(), StringComparer.OrdinalIgnoreCase))
            {
                c.Disconnect();
                return;
            }

            MySqlCommand q = new MySqlCommand(string.Empty, SQL);
            q.CommandText = "SELECT Count(*) FROM users WHERE Username=@Username AND Password=@Password;";
            q.Parameters.AddWithValue("@Username", name);
            q.Parameters.AddWithValue("@Password", pass);

            MySqlDataReader r = q.ExecuteReader();
            bool success = r.Read() && (r.GetInt16("Count(*)") != 0);
            r.Close();

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

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

                c.Value.UserID = RunningID++;
                c.Value.Username = name;

                c.Value.Authenticated = true;
                SendLoginBarrage(c);
            }
        }
Esempio n. 2
0
        static void SendChannelList(Client c)
        {
            List<object> values = new List<object>();
            values.Add((byte)ServerPacket.ChannelList);
            values.AddRange(Channels);

            byte[] data = Packer.Serialize(values.ToArray());
            c.Send(data);
        }
Esempio n. 3
0
        //TODO: Don't disconnect people, instead return an error code.
        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);
            }
        }
Esempio n. 4
0
        static void HandleRegisterPacket(Client c, string name, string pass)
        {
            if (name.Length == 0 || name.Length > 16 || pass.Length != 40)
            {
                c.Disconnect();
                return;
            }

            MySqlCommand q = new MySqlCommand(string.Empty, SQL);
            q.CommandText = "INSERT INTO users VALUES (@Username,@Password);";
            q.Parameters.AddWithValue("@Username", name);
            q.Parameters.AddWithValue("@Password", pass);

            bool success = (q.ExecuteNonQuery() != 0);

            byte[] data = Packer.Serialize((byte)ServerPacket.Register, success);
            c.Send(data);
        }
Esempio n. 5
0
        static void HandlePMPacket(Client c, string recipient, string message, string subject, ushort id = default(ushort))
        {
            if (!(IsValidName(recipient) && IsValidData(message) && IsValidData(subject)))
            {
                c.Disconnect();
                return;
            }

            Client u = ClientFromUsername(recipient);
            if (u == null)
                return;

            PrivateMessage pm = null;

            if (id == default(ushort) && !privateMessages.Exists(x => x.Id == id))
            {
                pm = new PrivateMessage(id = (ushort)_random.Next(ushort.MinValue, ushort.MaxValue), subject, c.Value.Username, DateTime.Now, message);
                privateMessages.Add(pm);

                byte[] confirm = Packer.Serialize((byte)ServerPacket.PMConfirm, id, u.Value.Username, message, subject, DateTime.Now);
                c.Send(confirm);
            }
            else
            {
                pm = privateMessages.Find(x => x.Id == id);
                pm.Message.Add(message);
            }

            byte[] data = Packer.Serialize((byte)ServerPacket.PM, id, c.Value.Username, u.Value.Username, message, subject, DateTime.Now);
            u.Send(data);
        }
Esempio n. 6
0
        static void HandleRegisterPacket(Client c, string name, string pass, string email)
        {
            string n = name.Trim();

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

            MySqlCommand q = new MySqlCommand("SELECT Count(*) FROM users WHERE Username=@Username", SQL);
            q.Parameters.AddWithValue("@Username", n);

            MySqlDataReader r = q.ExecuteReader();
            bool available = r.Read() && (r.GetInt16(0) == 0);
            r.Close();

            if (available)
            {
                MySqlCommand q2 = new MySqlCommand(string.Empty, SQL);
                q2.CommandText = "INSERT INTO users VALUES (@Username,@Password,@Email,0,0,0,0,\"\",\"\",\"\");";
                q2.Parameters.AddWithValue("@Username", n);
                q2.Parameters.AddWithValue("@Password", pass);
                q2.Parameters.AddWithValue("@Email", email);

                //If registration fails, this will return false.
                bool success = (q2.ExecuteNonQuery() != 0);

                if (success)
                {
                    c.Value.Username = name;
                    c.Value.Email = email;
                    SendEmail(email, n);
                }

                byte[] data = Packer.Serialize((byte)ServerPacket.Register, success);
                c.Send(data);
            }
            else
            {
                byte[] data = Packer.Serialize((byte)ServerPacket.Register, false);
                c.Send(data);
            }
        }
Esempio n. 7
0
 static void HandleKeepAlivePacket(Client c)
 {
     byte[] data = Packer.Serialize((byte)ServerPacket.KeepAlive);
     c.Send(data);
 }
Esempio n. 8
0
 static void SendProfile(Client c)
 {
     if (c.Value.Authenticated)
     {
         byte[] profile = Packer.Serialize(GetProfile(ServerPacket.Profile, c.Value.Username));
         c.Send(profile);
     }
 }
Esempio n. 9
0
        static void HandleAuthCodePacket(Client c, string authCode)
        {
            MySqlCommand q = new MySqlCommand("SELECT AuthType, Value FROM authcodes WHERE (User=@Username) AND Value=@Value;", SQL);
            q.Parameters.AddWithValue("@Username", c.Value.Username);
            q.Parameters.AddWithValue("@Value", authCode);

            MySqlDataReader r = q.ExecuteReader();
            bool read = r.Read() && r.HasRows;

            if (read)
            {
                AuthType authType = (AuthType) r.GetByte("AuthType");

                r.Close();

                q.CommandText = "DELETE FROM `authcodes` WHERE `Value`=@Value AND `User`=@Username LIMIT 1;";

                bool success = q.ExecuteNonQuery() != 0;

                byte[] data = Packer.Serialize((byte) ServerPacket.AuthResponse,(byte)authType, success);
                c.Send(data);
            }
            else
            {
                byte[] data = Packer.Serialize((byte) ServerPacket.AuthResponse, (byte)AuthType.Unknown, false);
                c.Send(data);
            }
        }
Esempio n. 10
0
        static void HandleEditProfilePacket(Client c, string avatar, string bio, string profile)
        {
            if (!(avatar.Contains("http://") || avatar.Contains("https://")))
                avatar = avatar.Insert(0, "http://");
            if (!(profile.Contains("http://") || profile.Contains("https://")))
                profile = profile.Insert(0, "http://");

            bool validProfile = Regex.IsMatch(profile, @"^(http:\/\/|https:\/\/)*(www\.)*hackforums\.net\/member\.php\?action=profile&uid=\d{1,7}$");

            MySqlCommand q = new MySqlCommand("UPDATE users SET Avatar=@Avatar, Bio=@Bio, ProfileLink=@ProfileLink WHERE Username=@Username;", SQL);
            q.Parameters.AddWithValue("@Username", c.Value.Username);
            q.Parameters.AddWithValue("@Bio", bio);
            q.Parameters.AddWithValue("@Avatar", avatar);
            q.Parameters.AddWithValue("@ProfileLink", validProfile ? profile : "");

            bool success = q.ExecuteNonQuery() == 0;

            byte[] data = Packer.Serialize((byte)ServerPacket.EditProfile, success);
            c.Send(data);
        }
Esempio n. 11
0
        static void ClientState(Server s, Client c, bool open)
        {
            if (open)
            {
                c.Value = new UserState();
                if (!string.IsNullOrEmpty(MOTD))
                {
                    byte[] data = Packer.Serialize((byte)ServerPacket.MOTD, MOTD);
                    c.Send(data);
                }
            }
            else
            {
                if (c.Value.Authenticated)
                {
                    byte[] data = Packer.Serialize((byte)ServerPacket.UserLeave, c.Value.UserId);
                    Broadcast(c.Value.Channel, data);

                    AwardPoints(c);

                    //Let's save the users data.
                    MySqlCommand q = new MySqlCommand(string.Empty, SQL);
                    q.CommandText = "UPDATE users SET Points=@Points,Rank=@Rank,Mute=@Mute WHERE Username=@Username;";
                    q.Parameters.AddWithValue("@Points", c.Value.Points);
                    q.Parameters.AddWithValue("@Rank", c.Value.Rank);
                    q.Parameters.AddWithValue("@Mute", c.Value.Mute);
                    q.Parameters.AddWithValue("@Username", c.Value.Username);

                    //If it fails there isn't much we can do about it.
                    q.ExecuteNonQuery();
                }
            }
        }
Esempio n. 12
0
        static void SendUserListUpdates(Client c)
        {
            //Let everyone know the life of the party has just arrived.
            byte[] data1 = Packer.Serialize((byte)ServerPacket.UserJoin, c.Value.UserId, c.Value.Username, c.Value.Rank);
            BroadcastExclusive(c.Value.UserId, c.Value.Channel, data1, c);

            //Our guy will probably need to know who he's chatting with, right?
            List<object> cValues = new List<object> {(byte) ServerPacket.UserList};

            foreach (Client t in Listener.Clients)
            {
                if (t.Value.Authenticated && t.Value.Channel == c.Value.Channel)
                {
                    cValues.AddRange(new object[] { t.Value.UserId, t.Value.Username, t.Value.Rank, t.Value.Idle });
                }
            }

            byte[] data2 = Packer.Serialize(cValues.ToArray());
            c.Send(data2);
        }
Esempio n. 13
0
 static void SendProfileTo(Client fromClient, Client toClient)
 {
     if (fromClient.Value.Authenticated)
     {
         byte[] profile = Packer.Serialize(GetProfile(ServerPacket.ViewProfile, toClient.Value.Username));
         fromClient.Send(profile);
     }
 }
Esempio n. 14
0
        static void SendLoginBarrage(Client c)
        {
            SendChannelList(c);
            SendUserListUpdates(c, c.Value.Channel);

            string MOTD = GetMOTD();

            if (!string.IsNullOrEmpty(MOTD))
            {
                byte[] data = Packer.Serialize((byte)ServerPacket.MOTD, MOTD);
                c.Send(data);
            }
        }
Esempio n. 15
0
 static void HandleNewsPacket(Client c)
 {
     byte[] data = Packer.Serialize((byte)ServerPacket.News, GetNews());
     c.Send(data);
 }
Esempio n. 16
0
        static void SendUserListUpdates(Client c, byte oldChannel)
        {
            try
            {
                byte[] data1 = Packer.Serialize((byte)ServerPacket.UserJoin, c.Value.UserID, c.Value.Username);
                BroadcastExclusive(c.Value.UserID, c.Value.Channel, data1);

                List<object> cValues = new List<object>();
                cValues.Add((byte)ServerPacket.UserList);

                //Send updates to old channel.
                List<object> oldValues = new List<object>();
                oldValues.Add((byte)ServerPacket.UserList);

                foreach (Client u in Listener.Clients)
                {
                    if (u.Value.Authenticated && u.Value.Channel == c.Value.Channel)
                    {
                        cValues.Add(u.Value.UserID);
                        cValues.Add(u.Value.Username);
                    } else {
                        oldValues.Add(u.Value.UserID);
                        oldValues.Add(u.Value.Username);
                    }
                }

                byte[] data2 = Packer.Serialize(cValues.ToArray());
                c.Send(data2);
                if (oldValues.Count > 1)
                {
                    byte[] data3 = Packer.Serialize(oldValues.ToArray());
                    Broadcast(oldChannel, data3);
                }
            }
            catch { }
        }
Esempio n. 17
0
 static void SendLoginBarrage(Client c)
 {
     SendChannelList(c);
     SendUserListUpdates(c);
     foreach (ChatMessage cm in RecentMessages)
     {
         byte[] data = Packer.Serialize((byte)ServerPacket.RecentMessages, cm.Time, cm.Username, cm.Value, cm.Rank);
         c.Send(data);
     }
 }