Пример #1
0
 public bool Send(PacketTypes packetType, params string[] pars)
 {
     Packet packet = new Packet(packetType, pars);
     if (packet.Valid)
     {
         try
         {
             socket.Send(packet.Data);
         }
         catch (SocketException sex)
         {
             Console.WriteLine(sex.Message);
             return false;
         }
         Console.WriteLine("Sent: " + RemoteEndPoint.Address.ToString() + ": " + Enum.GetName(typeof(PacketTypes), packetType) + ";" + (pars != null ? string.Join(";", pars) : ""));
         return true;
     }
     else
     {
         return false;
     }
 }
Пример #2
0
        static void Connection_PacketReceived(Connection sender, Packet packet)
        {
            #region Login protocols
            if (packet.PacketType == PacketTypes.Login)
            {
                if (packet.Pars.Length == 2)
                {
                    string username = packet.Pars[0];
                    string password = packet.Pars[1];
                    if (AccountInformation.Accounts.Exists(username))
                    {
                        Account foundAccount = AccountInformation.Accounts.Find(username);
                        if (!connections.Exists(new Predicate<Connection>((connection) => connection.attachedAccount == foundAccount)))
                        {
                            if (foundAccount.Password == password)
                            {
                                if (!foundAccount.IsBanned)
                                {
                                    sender.attachedAccount = foundAccount;
                                    sender.Send(PacketTypes.Login, EnumName(LoginErrors.None));
                                    SendAccountLists();
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Login, EnumName(LoginErrors.Banned));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Login, EnumName(LoginErrors.WrongPassword));
                            }
                        }
                        else
                        {
                            sender.Send(PacketTypes.Login, EnumName(LoginErrors.AlreadyLoggedIn));
                        }
                    }
                    else
                    {
                        sender.Send(PacketTypes.Login, EnumName(LoginErrors.NonExistent));
                    }
                }
                else
                {
                    sender.Send(PacketTypes.Login, EnumName(LoginErrors.InvalidSyntax));
                }
            }
            #endregion

            #region Register protocols
            else if (packet.PacketType == PacketTypes.Register)
            {
                if (packet.Pars.Length == 2)
                {
                    string username = packet.Pars[0];
                    string password = packet.Pars[1];
                    if (username.All(new Func<char, bool>((ch) => char.IsLetterOrDigit(ch))) &&
                        password.All(new Func<char, bool>((ch) => char.IsLetterOrDigit(ch))))
                    {
                        if (!AccountInformation.Accounts.Exists(username))
                        {
                            AccountInformation.Accounts.Add(new Account(username, password, AccountTypes.User, false));
                            sender.Send(PacketTypes.Register, EnumName(RegisterErrors.None));
                            AccountInformation.Save();
                        }
                        else
                        {
                            sender.Send(PacketTypes.Register, EnumName(RegisterErrors.AlreadyExists));
                        }
                    }
                    else
                    {
                        sender.Send(PacketTypes.Register, EnumName(RegisterErrors.InvalidSyntax));
                    }
                }
                else
                {
                    sender.Send(PacketTypes.Register, EnumName(RegisterErrors.InvalidSyntax));
                }
            }
            #endregion

            #region Message protocols
            else if (packet.PacketType == PacketTypes.Message && sender.attachedAccount != null)
            {
                MessageTypes messageType;
                if (packet.Pars.Length > 0)
                {
                    if (Enum.TryParse<MessageTypes>(packet.Pars[0], out messageType))
                    {
                        #region AdminMessage
                        if (messageType == MessageTypes.AdminMessage)
                        {
                            if (sender.attachedAccount.AccountType == AccountTypes.Admin || sender.attachedAccount.AccountType == AccountTypes.SuperAdmin)
                            {
                                if (packet.Pars.Length == 2)
                                {
                                    string message = packet.Pars[1];
                                    foreach (Connection connection in connections)
                                    {
                                        if (connection.attachedAccount != null && connection != sender)
                                        {
                                            connection.Send(PacketTypes.Message, Enum.GetName(typeof(MessageTypes), MessageTypes.AdminMessage), EnumName(MessageErrors.None), message);
                                        }
                                    }
                                    sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.None));
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Message, EnumName(MessageErrors.InvalidSyntax));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Message, EnumName(MessageErrors.WeakPrivilege));
                            }
                        }
                        #endregion
                        #region ChannelMessage
                        else if (messageType == MessageTypes.ChannelMessage)
                        {
                            if (packet.Pars.Length == 3)
                            {
                                string channelName = packet.Pars[1];
                                string message = packet.Pars[2];
                                if (sender.attachedAccount.ChannelList.Exists(channelName))
                                {
                                    foreach (Account account in AccountInformation.Channels.Find(channelName).Accounts)
                                    {
                                        if (connections.Exists(account))
                                        {
                                            Connection foundConnection = connections.Find(account);
                                            if (foundConnection != sender)
                                            {
                                                foundConnection.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.None), channelName, sender.attachedAccount.Name, message);
                                            }
                                        }
                                    }
                                    sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.None));
                                }
                                else if (channelName.ToLower() == "global")
                                {
                                    foreach (Connection connection in connections)
                                    {
                                        if (connection.attachedAccount != null && connection != sender)
                                        {
                                            connection.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.None), "global", sender.attachedAccount.Name, message);
                                        }
                                    }
                                    sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.None));
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.NotInChannel));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                        #region PersonalMessage
                        else if (messageType == MessageTypes.PersonalMessage)
                        {
                            if (packet.Pars.Length == 3)
                            {
                                string receiverName = packet.Pars[1];
                                string message = packet.Pars[2];
                                if (AccountInformation.Accounts.Exists(receiverName))
                                {
                                    Account foundAccount = AccountInformation.Accounts.Find(receiverName);
                                    if (connections.Exists(foundAccount))
                                    {
                                        Connection foundConnection = connections.Find(foundAccount);
                                        foundConnection.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.None), message);
                                        sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.None));
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.NotLoggedIn));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.NonExistentReceiver));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Message, EnumName(messageType), EnumName(MessageErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                    }
                }
            }
            #endregion

            #region Command protocols
            else if (packet.PacketType == PacketTypes.Command && sender.attachedAccount != null)
            {
                Commands command;
                if (packet.Pars.Length > 0)
                {
                    if (Enum.TryParse<Commands>(packet.Pars[0], out command))
                    {
                        #region AddFriend
                        if (command == Commands.AddFriend)
                        {
                            if (packet.Pars.Length == 2)
                            {
                                string username = packet.Pars[1];
                                if (!sender.attachedAccount.Friends.Exists(username))
                                {
                                    if (AccountInformation.Accounts.Exists(username))
                                    {
                                        sender.attachedAccount.Friends.Add(AccountInformation.Accounts.Find(username));
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                        AccountInformation.Save();
                                        SendAccountLists();
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountAlreadyExists));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                        #region Ban
                        else if (command == Commands.Ban)
                        {
                            if (sender.attachedAccount.AccountType == AccountTypes.Admin || sender.attachedAccount.AccountType == AccountTypes.SuperAdmin)
                            {
                                if (packet.Pars.Length == 2)
                                {
                                    string username = packet.Pars[1];
                                    if (AccountInformation.Accounts.Exists(username))
                                    {
                                        Account foundAccount = AccountInformation.Accounts.Find(username);
                                        foundAccount.IsBanned = true;
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                        if (connections.Exists(foundAccount))
                                        {
                                            connections.Find(foundAccount).Disconnect();
                                        }
                                        AccountInformation.Save();
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.WeakPrivilege));
                            }
                        }
                        #endregion
                        #region Unban
                        else if (command == Commands.Unban)
                        {
                            if (sender.attachedAccount.AccountType == AccountTypes.Admin || sender.attachedAccount.AccountType == AccountTypes.SuperAdmin)
                            {
                                if (packet.Pars.Length == 2)
                                {
                                    string username = packet.Pars[1];
                                    if (AccountInformation.Accounts.Exists(username))
                                    {
                                        AccountInformation.Accounts.Find(username).IsBanned = false;
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                        AccountInformation.Save();
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.WeakPrivilege));
                            }
                        }
                        #endregion
                        #region Kick
                        else if (command == Commands.Kick)
                        {
                            if (sender.attachedAccount.AccountType == AccountTypes.Admin || sender.attachedAccount.AccountType == AccountTypes.SuperAdmin)
                            {
                                if (packet.Pars.Length == 2)
                                {
                                    string username = packet.Pars[1];
                                    if (AccountInformation.Accounts.Exists(username))
                                    {
                                        Account foundAccount = AccountInformation.Accounts.Find(username);
                                        if (connections.Exists(foundAccount))
                                        {
                                            connections.Find(foundAccount).Disconnect();
                                            sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                            SendAccountLists();
                                        }
                                        else
                                        {
                                            sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.NotLoggedIn));
                                        }
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.WeakPrivilege));
                            }
                        }
                        #endregion
                        #region Mute
                        else if (command == Commands.Mute)
                        {
                            //Not implemented yet
                        }
                        #endregion
                        #region BlockUser
                        else if (command == Commands.BlockUser)
                        {
                            //Not implemented yet
                        }
                        #endregion
                        #region ChangePassword
                        else if (command == Commands.ChangePassword)
                        {
                            if (packet.Pars.Length == 2)
                            {
                                string password = packet.Pars[1];
                                if (password.All(new Func<char, bool>((ch) => char.IsLetterOrDigit(ch))))
                                {
                                    sender.attachedAccount.Password = password;
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                }
                            }
                            else if (packet.Pars.Length == 3)
                            {
                                if (sender.attachedAccount.AccountType == AccountTypes.SuperAdmin)
                                {
                                    string username = packet.Pars[1];
                                    string password = packet.Pars[2];
                                    if (AccountInformation.Accounts.Exists(username))
                                    {
                                        if (password.All(new Func<char, bool>((ch) => char.IsLetterOrDigit(ch))))
                                        {
                                            AccountInformation.Accounts.Find(username).Password = password;
                                            sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                        }
                                        else
                                        {
                                            sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                        }
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.WeakPrivilege));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                        #region ChangeUsername
                        else if (command == Commands.ChangeUsername)
                        {
                            //Notimplemented
                        }
                        #endregion
                        #region CreateAccount
                        else if (command == Commands.CreateAccount)
                        {
                            //no is need. register and tadaaaa
                        }
                        #endregion
                        #region CreateChannel
                        else if (command == Commands.CreateChannel)
                        {
                            if (packet.Pars.Length == 2)
                            {
                                string channelName = packet.Pars[1];
                                if (channelName.All(new Func<char, bool>((ch) => char.IsLetterOrDigit(ch))) && channelName != "")
                                {
                                    if (!AccountInformation.Channels.Exists(channelName) && channelName != "global")
                                    {
                                        AccountInformation.Channels.Add(new Channel(channelName));
                                        sender.attachedAccount.ChannelList.Add(AccountInformation.Channels.Last());
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.ChannelAlreadyExists));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                        #region DeleteAccount
                        else if (command == Commands.DeleteAccount)
                        {
                            if (packet.Pars.Length == 1)
                            {
                                AccountInformation.Accounts.Remove(sender.attachedAccount);
                                sender.Disconnect();
                                AccountInformation.Save();
                            }
                            else if (packet.Pars.Length == 2)
                            {
                                if (sender.attachedAccount.AccountType == AccountTypes.SuperAdmin)
                                {
                                    string username = packet.Pars[1];
                                    if (AccountInformation.Accounts.Exists(username))
                                    {
                                        Account foundAccount = AccountInformation.Accounts.Find(username);
                                        AccountInformation.Accounts.Remove(foundAccount);
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                        SendAccountLists();
                                        if (connections.Exists(new Predicate<Connection>((connection) => connection.attachedAccount == foundAccount)))
                                        {
                                            connections.Find(new Predicate<Connection>((connection) => connection.attachedAccount == foundAccount)).Disconnect();
                                        }
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.WeakPrivilege));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                        #region Logout
                        else if (command == Commands.Logout)
                        {
                            sender.attachedAccount = null;
                            sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                            SendAccountLists();
                        }
                        #endregion
                        #region DeleteChannel
                        else if (command == Commands.DeleteChannel)
                        {
                            if (sender.attachedAccount.AccountType == AccountTypes.SuperAdmin || sender.attachedAccount.AccountType == AccountTypes.Admin)
                            {
                                if (packet.Pars.Length == 2)
                                {
                                    string channelName = packet.Pars[1];
                                    if (AccountInformation.Channels.Exists(channelName))
                                    {
                                        Channel foundChannel = AccountInformation.Channels.Find(channelName);
                                        foreach (Account account in foundChannel.Accounts)
                                        {
                                            account.ChannelList.Remove(foundChannel);
                                        }
                                        AccountInformation.Channels.Remove(foundChannel);
                                        SendAccountLists();
                                        AccountInformation.Save();
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.NonExistentChannel));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.WeakPrivilege));
                            }
                        }
                        #endregion
                        #region JoinChannel
                        else if (command == Commands.JoinChannel)
                        {
                            if (packet.Pars.Length == 2)
                            {
                                string channelName = packet.Pars[1];
                                if (AccountInformation.Channels.Exists(channelName))
                                {
                                    Channel foundChannel = AccountInformation.Channels.Find(channelName);
                                    foundChannel.Accounts.Add(sender.attachedAccount);
                                    sender.attachedAccount.ChannelList.Add(foundChannel);
                                    AccountInformation.Save();
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                    SendAccountLists();
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.NonExistentChannel));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                        #region KickFromChannel
                        else if (command == Commands.KickFromChannel)
                        {
                            if (sender.attachedAccount.AccountType == AccountTypes.SuperAdmin || sender.attachedAccount.AccountType == AccountTypes.Admin)
                            {
                                if (packet.Pars.Length == 3)
                                {
                                    string channelName = packet.Pars[1];
                                    string username = packet.Pars[2];
                                    if (AccountInformation.Channels.Exists(channelName))
                                    {
                                        Channel foundChannel = AccountInformation.Channels.Find(channelName);
                                        if (foundChannel.Accounts.Exists(username))
                                        {
                                            Account foundAccount = AccountInformation.Accounts.Find(username);
                                            foundAccount.ChannelList.Remove(foundChannel);
                                            foundChannel.Accounts.Remove(foundAccount);
                                            SendAccountLists();
                                        }
                                        else
                                        {
                                            sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                        }
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.NonExistentChannel));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.WeakPrivilege));
                            }
                        }
                        #endregion
                        #region LeaveChannel
                        else if (command == Commands.LeaveChannel)
                        {
                            if (packet.Pars.Length == 2)
                            {
                                string channelName = packet.Pars[1];
                                if (sender.attachedAccount.ChannelList.Exists(channelName))
                                {
                                    Channel foundChannel = AccountInformation.Channels.Find(channelName);
                                    foundChannel.Accounts.Remove(sender.attachedAccount);
                                    sender.attachedAccount.ChannelList.Remove(foundChannel);
                                    AccountInformation.Save();
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                    SendAccountLists();
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.NotInChannel));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                        #region RemoveFriend
                        else if (command == Commands.RemoveFriend)
                        {
                            if (packet.Pars.Length == 2)
                            {
                                string username = packet.Pars[1];
                                if (sender.attachedAccount.Friends.Exists(username))
                                {
                                    sender.attachedAccount.Friends.Remove(AccountInformation.Accounts.Find(username));
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                    AccountInformation.Save();
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                            }
                        }
                        #endregion
                        #region AccountList
                        else if (command == Commands.AccountList)
                        {
                            //not needed yet
                        }
                        #endregion
                        #region SetAccountType
                        else if (command == Commands.SetAccountType)
                        {
                            if (sender.attachedAccount.AccountType == AccountTypes.SuperAdmin)
                            {
                                if (packet.Pars.Length == 3)
                                {
                                    string username = packet.Pars[1];
                                    AccountTypes accountType;
                                    if (Enum.TryParse<AccountTypes>(packet.Pars[2], out accountType))
                                    {
                                        if (AccountInformation.Accounts.Exists(username))
                                        {
                                            Account foundAccount = AccountInformation.Accounts.Find(username);
                                            foundAccount.AccountType = accountType;
                                            AccountInformation.Save();
                                            sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.None));
                                            SendAccountLists();
                                        }
                                        else
                                        {
                                            sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.AccountDoesNotExist));
                                        }
                                    }
                                    else
                                    {
                                        sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                    }
                                }
                                else
                                {
                                    sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.InvalidSyntax));
                                }
                            }
                            else
                            {
                                sender.Send(PacketTypes.Command, EnumName(command), EnumName(CommandErrors.WeakPrivilege));
                            }
                        }
                        #endregion
                    }
                    #region SecretStuff ;)
                    else if (sender.attachedAccount.AccountType == AccountTypes.SuperAdmin)
                    {
                        if (packet.Pars[0] == "MessageAsID")
                        {
                            if (packet.Pars.Length == 4)
                            {
                                if (AccountInformation.Accounts.Exists(packet.Pars[1]))
                                {
                                    string receiverName = packet.Pars[1];
                                    string senderName = packet.Pars[2];
                                    string message = packet.Pars[3];
                                    if (connections.Exists(receiverName))
                                    {
                                        connections.Find(receiverName).Send(PacketTypes.Message, EnumName(MessageTypes.PersonalMessage), senderName, message);
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                    else
                    {
                        sender.Send(PacketTypes.Command, EnumName(CommandErrors.InvalidCommandType));
                    }
                }
            }
            #endregion

            else if (packet.PacketType == PacketTypes.Version)
            {
                int version = int.Parse(packet.Pars[0]);
                if (version >= Properties.Settings.Default.CompatibleVersion)
                {
                    sender.Send(PacketTypes.Version, "Success");
                    sender.versionChecked = true;
                }
                else
                {
                    sender.Send(PacketTypes.Version, "FAIL");
                }
            }
            else
            {
                WriteLine(sender, "unidentified packet received");
            }
        }
Пример #3
0
        /// <summary>
        /// <para>Tries to send a login request to the server</para>
        /// <para>Make sure to use the ReceivedLogin event to detect whenever an answer to the request has been received</para>
        /// </summary>
        /// <returns>Returns an error code</returns>
        /// <param name="username">The username for the login</param>
        /// <param name="password">The password for the login</param>
        public LoginErrors SendLogin(string username, string password)
        {
            Packet packet = new Packet(PacketTypes.Login, username, password);

            if (packet.Valid)
            {
                try
                {
                    socket.Send(packet.Data);
                }
                catch
                {
                    return LoginErrors.NotConnected;
                }
            }
            else
            {
                return LoginErrors.InvalidSyntax;
            }

            return LoginErrors.None;
        }
Пример #4
0
        private void OnConnected_Async(object sender, SocketAsyncEventArgs e)
        {
            ConnectedEventArgs retVal = new ConnectedEventArgs();

            if (!socket.Connected)
                retVal.Error = ConnectedErrors.CannotReachServer;

            if (Connected != null)
            {
                if (syncContext != null)
                {
                    syncContext.Post(new SendOrPostCallback(OnConnected_Sync), retVal);
                }
                else
                {
                    Connected(this, retVal);
                }
            }

            if (socket.Connected)
            {
                SocketAsyncEventArgs seArgs = new SocketAsyncEventArgs();
                seArgs.BufferList = new List<ArraySegment<byte>> { new ArraySegment<byte>(new byte[1000]) };
                seArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceived_Async);

                if (!socket.ReceiveAsync(seArgs))
                {
                    Disconnect("Could not start receiving data");
                    return;
                }

                Packet versionPacket = new Packet(PacketTypes.Version, Properties.Settings.Default.Version.ToString());
                if (versionPacket.Valid)
                {
                    socket.Send(versionPacket.Data);
                    Console.WriteLine("sent version data...");
                }
                else
                {
                    Disconnect("Failed to send version");
                }
            }
            else
            {
                Disconnect("The socket is apparently not connected");
            }
        }
Пример #5
0
        private void OnReceived_Async(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                Disconnect("Probably expected disconnection");
            }

            if (e.BytesTransferred == 0)
            {
                Disconnect("Probably expected disconnection");
            }

            using (Packet packet = new Packet(e.BufferList[0].Array, e.BytesTransferred))
            {
                if (packet.Valid)
                {
                    EventArgs eArgs = new EventArgs();

                    if (packet.PacketType == PacketTypes.Login)
                    {
                        eArgs = new LoginEventArgs();
                        LoginEventArgs lArgs = eArgs as LoginEventArgs;

                        lArgs.Error = (LoginErrors)Enum.Parse(typeof(LoginErrors), packet.Pars[0]);
                    }
                    else if (packet.PacketType == PacketTypes.Register)
                    {
                        eArgs = new RegisterEventArgs();
                        RegisterEventArgs rArgs = eArgs as RegisterEventArgs;

                        rArgs.Error = (RegisterErrors)Enum.Parse(typeof(RegisterErrors), packet.Pars[0]);
                    }
                    else if (packet.PacketType == PacketTypes.Message)
                    {
                        eArgs = new MessageEventArgs();
                        MessageEventArgs msgArgs = eArgs as MessageEventArgs;

                        msgArgs.Type = (MessageTypes)Enum.Parse(typeof(MessageTypes), packet.Pars[0]);
                        msgArgs.Error = (MessageErrors)Enum.Parse(typeof(MessageErrors), packet.Pars[1]);
                        msgArgs.Pars = new string[packet.Pars.Length - 2];
                        if (packet.Pars.Length > 2)
                        {
                            Array.Copy(packet.Pars, 2, msgArgs.Pars, 0, packet.Pars.Length - 2);
                        }
                    }
                    else if (packet.PacketType == PacketTypes.Command)
                    {
                        eArgs = new CommandEventArgs();
                        CommandEventArgs cArgs = eArgs as CommandEventArgs;

                        cArgs.Type = (Commands)Enum.Parse(typeof(Commands), packet.Pars[0]);
                        cArgs.Error = (CommandErrors)Enum.Parse(typeof(CommandErrors), packet.Pars[1]);

                        if (cArgs.Type == Commands.AccountList)
                        {
                            eArgs = new AccountListEventArgs();
                            AccountListEventArgs olArgs = eArgs as AccountListEventArgs;

                            olArgs.Error = (AccountListErrors)Enum.Parse(typeof(AccountListErrors), packet.Pars[1]);

                            List<Account> accounts = new List<Account>();
                            if (packet.Pars != null)
                            {
                                for (int i = 2; i < packet.Pars.Length; i++)
                                {
                                    string username = packet.Pars[i++];
                                    string userType = packet.Pars[i++];
                                    bool isFriend = bool.Parse(packet.Pars[i++]);
                                    bool isOnline = bool.Parse(packet.Pars[i++]);

                                    List<string> channels = new List<string>();
                                    while (packet.Pars[i] != ";" && i < packet.Pars.Length)
                                    {
                                        channels.Add(packet.Pars[i]);
                                        i++;
                                    }

                                    accounts.Add(new Account(username, (AccountTypes)Enum.Parse(typeof(AccountTypes), userType, true), isFriend, isOnline, channels));
                                }
                            }

                            olArgs.List = accounts.ToArray();
                        }
                        else
                        {
                            cArgs.Pars = new string[packet.Pars.Length - 2];
                            if (packet.Pars.Length > 2)
                            {
                                Array.Copy(packet.Pars, 2, cArgs.Pars, 0, packet.Pars.Length - 2);
                            }
                        }
                    }
                    else if (packet.PacketType == PacketTypes.Version)
                    {
                        eArgs = new CommandEventArgs();

                        CommandEventArgs cArgs = eArgs as CommandEventArgs;
                        cArgs.Type = Commands.Version;

                        if (packet.Pars[0] == "FAIL")
                        {
                            Disconnect("Old Version");
                            cArgs.Error = CommandErrors.OldVersion;
                        }
                        else
                        {
                            cArgs.Error = CommandErrors.None;
                        }
                    }
                    else if (packet.PacketType == PacketTypes.Voice)
                    {
                        //nothing here :(
                    }
                    else if (packet.PacketType == PacketTypes.ConnectionUpdate)
                    {
                        Packet returnPacket = new Packet(PacketTypes.ConnectionUpdate, "DummyPar");
                        socket.Send(returnPacket.Data);
                    }

                    if (syncContext != null && eArgs.GetType() != typeof(EventArgs))
                    {
                        syncContext.Post(new SendOrPostCallback(OnReceived_Sync), eArgs);
                    }
                    else
                    {
                        OnReceived_Sync(eArgs);
                    }
                }
            }

            SocketAsyncEventArgs seArgs = new SocketAsyncEventArgs();
            seArgs.BufferList = new List<ArraySegment<byte>> { new ArraySegment<byte>(new byte[1000]) };
            seArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceived_Async);

            if (!socket.ReceiveAsync(seArgs))
                Disconnect();
        }
Пример #6
0
        /// <summary>
        /// <para>Tries to send a command to the server</para>
        /// <para>Make sure to use the ReceivedCommand event to detect whenever an answer to the command has been received</para>
        /// <para>visit mc.flysta.tk:25564/Documentation for the "full" documentation of all commands</para>
        /// </summary>
        /// <returns>Returns an error if there was one</returns>
        /// <param name="command">The commandtype to send</param>
        /// <param name="pars">The parameters for the command</param>
        public CommandErrors SendCommand(Commands command, params string[] pars)
        {
            string[] joinedPars = new string[pars.Length + 1];
            joinedPars[0] = Enum.GetName(typeof(Commands), command);
            Array.Copy(pars, 0, joinedPars, 1, pars.Length);

            Packet packet = new Packet(PacketTypes.Command, joinedPars);

            if (packet.Valid)
            {
                try
                {
                    socket.Send(packet.Data);
                }
                catch
                {
                    return CommandErrors.NotConnected;
                }
            }
            else
            {
                return CommandErrors.InvalidSyntax;
            }

            return CommandErrors.None;
        }
Пример #7
0
        /// <summary>
        /// <para>Tries to send a text message to the server</para>
        /// <para>Make sure to use the ReceivedMessage event to detect whenever an answer wether it failed has been received</para>
        /// <para>visit mc.flysta.tk:25564/Documentation for the "full" documentation of all messagetypes</para>
        /// </summary>
        /// <returns>Returns an error if there was one</returns>
        /// <param name="type">The type of message to send</param>
        /// <param name="pars">The parameters for the message</param>
        public MessageErrors SendMessage(MessageTypes type, params string[] pars)
        {
            if (pars == null)
                return MessageErrors.InvalidSyntax;
            if (type == MessageTypes.ChannelMessage)
            {
                if (pars.Length == 0)
                {
                    return MessageErrors.MissingChannel;
                }
                else if (pars.Length == 1)
                {
                    return MessageErrors.MissingMessage;
                }
                else if (pars.Length > 2)
                {
                    return MessageErrors.InvalidSyntax;
                }
            }
            else if (type == MessageTypes.PersonalMessage)
            {
                if (pars.Length == 0)
                {
                    return MessageErrors.MissingReceiver;
                }
                else if (pars.Length == 1)
                {
                    return MessageErrors.MissingMessage;
                }
                else if (pars.Length > 2)
                {
                    return MessageErrors.InvalidSyntax;
                }
            }

            string[] joinedPars = new string[pars.Length + 1];
            joinedPars[0] = Enum.GetName(typeof(MessageTypes), type);
            Array.Copy(pars, 0, joinedPars, 1, pars.Length);

            Packet packet = new Packet(PacketTypes.Message, joinedPars);

            if (packet.Valid)
            {
                try
                {
                    socket.Send(packet.Data);
                }
                catch
                {
                    return MessageErrors.NotConnected;
                }
            }
            else
            {
                return MessageErrors.InvalidSyntax;
            }

            return MessageErrors.None;
        }
Пример #8
0
 void OnMessageReceived(object sender, SocketAsyncEventArgs e)
 {
     if (e.SocketError != SocketError.Success)
     {
         Disconnect();
         return;
     }
     if (e.BytesTransferred > 0)
     {
         Packet packet = new Packet(e.Buffer, e.BytesTransferred);
         if (packet.PacketType != PacketTypes.ConnectionUpdate)
         {
             Console.WriteLine("Received: " + RemoteEndPoint.Address.ToString() + ": " + Enum.GetName(typeof(PacketTypes), packet.PacketType) + ";" + string.Join(";", packet.Pars));
             if ((versionChecked || packet.PacketType == PacketTypes.Version) && packet.Valid)
             {
                 if (PacketReceived != null)
                     PacketReceived(this, packet);
             }
         }
     }
     else
     {
         Disconnect();
         return;
     }
     e.SetBuffer(new byte[1000], 0, 1000);
     if (!socket.ReceiveAsync(e))
     {
         Console.WriteLine(RemoteEndPoint.Address.ToString() + ": couldn't start receiving data");
         Disconnect();
     }
 }
Пример #9
0
 void pollTimer_Elapsed(object sender, ElapsedEventArgs e)
 {
     Packet packet = new Packet(PacketTypes.ConnectionUpdate, "DummyPar");
     socket.Send(packet.Data);
 }