Пример #1
0
        public override void Handle(virtualUser User)
        {
            try
            {
                int     Operation = Convert.ToInt32(getBlock(0));
                Subtype sub       = (Subtype)Operation;
                switch (sub)
                {
                case Subtype.FriendList:
                {
                    //32256 1 5606 3 <- [Count] 0 FrostyPrime 1 0 1 SiroSick 1 0 -1 tishina 1 4
                    User.LoadFriends();
                    User.send(new SP_MESSENGER_FRIENDS(User));
                    break;
                }

                case Subtype.SendMessage:
                {
                    string nickname = getBlock(1), message = "";
                    if (nickname.Length > 0 && nickname.Length <= 32)
                    {
                        message = getBlock(2).Trim();         // Remove unused spaces. (Just incase)

                        virtualUser friend = UserManager.getUser(nickname);
                        if (friend != null)
                        {
                            virtualMessenger vm = User.getFriend(nickname);

                            if (vm != null && (vm.Status == 0 || vm.Status == 1))
                            {
                                User.send(new SP_MESSENGER_MESSAGE(User.Nickname, friend.Nickname, message));
                                friend.send(new SP_MESSENGER_MESSAGE(User.Nickname, friend.Nickname, message));
                            }
                        }
                    }
                    break;
                }

                case Subtype.AvaialbleToChat:
                {
                    string nickname = getBlock(1);
                    if (nickname.Length > 0 && nickname.Length <= 32)
                    {
                        virtualMessenger vm         = User.getFriend(nickname);
                        virtualUser      tempClient = UserManager.getUser(nickname);
                        bool             ChatAble   = false;
                        if (tempClient != null)
                        {
                            ChatAble = true;
                        }
                        if (vm != null && (vm.Status == 0 || vm.Status == 1) && ChatAble)
                        {
                            User.send(new PacketUnknown(32256, 1, 5609, vm.Nickname, 0));
                        }
                    }
                    break;
                }

                case Subtype.BlockUnblock:
                {
                    string nickname = getBlock(1);
                    if (nickname.Length > 0 && nickname.Length <= 32)
                    {
                        int FriendID = Convert.ToInt32(DB.runReadRow("SELECT id FROM users WHERE nickname='" + DB.Stripslash(nickname) + "'")[0]);
                        if (FriendID > 0)
                        {
                            virtualMessenger v = User.getFriend(FriendID);

                            if (v.Status == 1)
                            {
                                v.Status = 2;
                            }
                            else if (v.Status == 2)
                            {
                                v.Status = 1;
                            }

                            DB.runQuery("UPDATE friends SET status='" + v.Status + "' WHERE id1='" + FriendID + "' AND id2='" + User.UserID + "' OR id1='" + User.UserID + "' AND id2='" + FriendID + "'");

                            virtualUser friend = UserManager.getUser(FriendID);
                            if (friend != null)
                            {
                                friend.send(new SP_MESSENGER_FRIENDS(friend));

                                virtualMessenger u2 = User.getFriend(FriendID);
                                if (u2 != null)
                                {
                                    u2.Status = v.Status;
                                }
                            }

                            User.send(new SP_MESSENGER_FRIENDS(User));
                        }
                    }
                    break;
                }

                case Subtype.DeleteFriend:
                {
                    string   Nickname = getBlock(1);
                    string[] Query    = DB.runReadRow("SELECT id FROM users WHERE nickname='" + Nickname + "'");
                    if (Query.Length > 0)
                    {
                        int FriendID = Convert.ToInt32(Query[0]);
                        if (FriendID > 0)
                        {
                            DB.runQuery("DELETE FROM friends WHERE id1='" + FriendID + "' AND id2='" + User.UserID + "' OR id1='" + User.UserID + "' AND id2='" + FriendID + "'");

                            virtualUser friend = UserManager.getUser(FriendID);
                            if (friend != null)
                            {
                                friend.Friends.Remove(User.UserID);
                                friend.send(new SP_MESSENGER_FRIENDS(User));
                            }

                            User.Friends.Remove(FriendID);
                            User.send(new SP_MESSENGER_FRIENDS(User));
                        }
                    }
                    break;
                }
                //case Subtype.FriendRequest:
                //    {
                //        string Nickname = getBlock(1);
                //        string[] SearchUser = DB.runReadRow("SELECT id FROM users WHERE nickname='" + Nickname + "'");
                //        if (SearchUser.Length > 0 && SearchUser[0] != User.UserID.ToString())
                //        {
                //            if (User.UserID == -1 || SearchUser[0] == "-1") return;
                //            DB.runQuery("INSERT INTO friends (id1, id2, requesterid, status) VALUES ('" + User.UserID + "', '" + SearchUser[0] + "', '" + User.UserID + "', '5')");
                //            virtualUser Friend = Managers.UserManager.getUser(Nickname);

                //            User.send(new SP_FRIEND_REQUEST(User.Nickname, Nickname));

                //            if (Friend != null)
                //            {
                //                User.send(new SP_FRIEND_REQUEST(User.Nickname, Nickname));
                //            }
                //            User.LoadFriends();
                //            User.send(new SP_MESSENGER_FRIENDS(User));
                //        }
                //        else
                //        {
                //            User.send(new PacketUnknown(32256, -11));
                //            //User.send(new SP_WARROCK_MESSENGER(SP_WARROCK_MESSENGER.Subtype.InvalidNickname));
                //        }
                //        break;
                //    }
                case Subtype.FriendRequest:
                {
                    string   Nickname   = getBlock(1);
                    string[] SearchUser = DB.runReadRow("SELECT id FROM users WHERE nickname='" + Nickname + "'");
                    if (SearchUser.Length > 0 && SearchUser[0] != User.UserID.ToString())
                    {
                        DB.runQuery("INSERT INTO friends (id1, id2, requesterid, status) VALUES ('" + User.UserID + "', '" + SearchUser[0] + "', '" + User.UserID + "', '5')");
                        virtualUser Friend = Managers.UserManager.getUser(Nickname);

                        User.send(new SP_FRIEND_REQUEST(User.Nickname, Nickname));

                        if (Friend != null)
                        {
                            User.send(new SP_FRIEND_REQUEST(User.Nickname, Nickname));
                        }
                        User.LoadFriends();
                        User.send(new SP_MESSENGER_FRIENDS(User));
                    }
                    else
                    {
                        User.send(new PacketUnknown(32256, -11));
                    }
                    break;
                }
                //case Subtype.FriendRequest:
                //    {
                //        //32256 1 5615 xK1llSt3am Realiity <- Server
                //        string Nickname = getBlock(1);
                //        string[] SearchUser = DB.runReadRow("SELECT id, nickname FROM users WHERE nickname='" + Nickname + "'");
                //        if (SearchUser.Length > 0 && SearchUser[0] != User.UserID.ToString())
                //        {
                //            int FriendID = Convert.ToInt32(SearchUser[0]);
                //            if (FriendID > 0)
                //            {
                //                if (User.UserID == -1 || FriendID <= 0) return;
                //                DB.runQuery("INSERT INTO friends (id1, id2, requesterid, status, nick1, nick2) VALUES ('" + User.UserID + "', '" + SearchUser[0] + "', '" + User.UserID + "', '5', '" + User.Nickname + "', '" + SearchUser[1] + "')");
                //                virtualUser Friend = UserManager.getUser(Convert.ToInt32(SearchUser[0]));

                //                User.send(new SP_FRIEND_REQUEST(User.Nickname, Nickname));
                //                if (Friend != null)
                //                {
                //                    Friend.AddFriend(User.UserID, User.UserID, User.Nickname);
                //                    Friend.send(new SP_FRIEND_REQUEST(User.Nickname, Nickname));
                //                    Friend.send(new SP_MESSENGER_FRIENDS(User));
                //                }

                //                User.AddFriend(Convert.ToInt32(SearchUser[0]), User.UserID, SearchUser[1]);
                //                User.send(new SP_FRIEND_REQUEST(User.Nickname, Nickname));
                //                User.LoadFriends();
                //                User.send(new SP_MESSENGER_FRIENDS(User));
                //            }
                //        }
                //        else
                //        {
                //            User.send(new PacketUnknown(32256, -11));
                //        }
                //        break;
                //    }
                case Subtype.FriendAccept:
                {
                    //5610 xK1llSt3am 0  <- Client
                    string Nickname = getBlock(1).Trim();
                    User.LoadFriends();
                    if (Nickname.Length > 0 && Nickname.Length <= 32)                                                  // Check for the nickname length for safety
                    {
                        string[] SearchUser = DB.runReadRow("SELECT id FROM users WHERE nickname='" + Nickname + "'"); // Search the user in the database
                        if (SearchUser.Length > 0 && SearchUser[0] != User.UserID.ToString())                          // If the user is in the database
                        {
                            int FriendID = Convert.ToInt32(SearchUser[0]);
                            if (FriendID > 0)
                            {
                                DB.runQuery("UPDATE friends SET requesterid='-1', status='1' WHERE id1='" + FriendID + "' AND id2='" + User.UserID + "' OR id1='" + User.UserID + "' AND id2='" + FriendID + "'");

                                virtualUser f = UserManager.getUser(FriendID);
                                if (f != null)
                                {
                                    virtualMessenger u = f.getFriend(User.UserID);
                                    if (u != null)
                                    {
                                        u.Status = 1;
                                    }
                                    f.send(new SP_MESSENGER_FRIENDS(f));
                                }

                                virtualMessenger u2 = User.getFriend(FriendID);
                                if (u2 != null)
                                {
                                    u2.Status = 1;
                                }
                                User.send(new SP_MESSENGER_FRIENDS(User));
                                break;
                            }
                        }
                    }

                    break;
                }

                case Subtype.FriendDecline:
                {
                    string Nickname = getBlock(1).Trim();
                    if (Nickname.Length > 0 && Nickname.Length <= 32)                                                  // Check for the nickname length for safety
                    {
                        string[] SearchUser = DB.runReadRow("SELECT id FROM users WHERE nickname='" + Nickname + "'"); // Search the user in the database
                        if (SearchUser.Length > 0)                                                                     // If the user is in the database
                        {
                            int FriendID = Convert.ToInt32(SearchUser[0]);
                            if (FriendID > 0)
                            {
                                DB.runQuery("DELETE FROM friends WHERE id1='" + FriendID + "' AND id2='" + User.UserID + "' OR id1='" + User.UserID + "' AND id2='" + FriendID + "'");
                                virtualUser f = UserManager.getUser(FriendID);
                                if (f != null)
                                {
                                    User.RemoveFriend(User.UserID);
                                    f.send(new SP_MESSENGER_FRIENDS(f));
                                }

                                User.RemoveFriend(FriendID);
                                User.LoadFriends();
                                User.send(new SP_MESSENGER_FRIENDS(User));
                                break;
                            }
                        }
                    }
                    break;
                }
                }
            }
            catch { Log.AppendError("Error on the Messenger: " + User.Nickname + " - " + string.Join(" ", getAllBlocks())); }
        }