예제 #1
0
파일: Form1.cs 프로젝트: Klako/ShitChat
 public ShitChatForm()
 {
     InitializeComponent();
     connection = new ChatConnection();
     connection.Connected += Connection_Connected;
     connection.Disconnected += Connection_Disconnected;
     connection.ReceivedAccountList += Connection_ReceivedAccountList;
     connection.ReceivedCommand += Connection_ReceivedCommand;
     connection.ReceivedLogin += Connection_ReceivedLogin;
     connection.ReceivedMessage += Connection_ReceivedMessage;
     connection.ReceivedRegister += Connection_ReceivedRegister;
 }
예제 #2
0
파일: Program.cs 프로젝트: Klako/ShitChat
        static void Main(string[] args)
        {
            shitchat = new ChatConnection();
            shitchat.Connected += new EventHandler<ConnectedEventArgs>(shitchat_Connected);
            shitchat.Disconnected += new EventHandler<DisconnectedEventArgs>(shitchat_Disconnected);
            shitchat.ReceivedAccountList += new EventHandler<AccountListEventArgs>(shitchat_ReceivedAccountList);
            shitchat.ReceivedCommand += new EventHandler<CommandEventArgs>(shitchat_ReceivedCommand);
            shitchat.ReceivedLogin += new EventHandler<LoginEventArgs>(shitchat_ReceivedLogin);
            shitchat.ReceivedMessage += new EventHandler<MessageEventArgs>(shitchat_ReceivedMessage);
            shitchat.ReceivedRegister += new EventHandler<RegisterEventArgs>(shitchat_ReceivedRegister);

            username = "";
            accountList = new List<Account>();
            loggedIn = false;
            connected = false;
            versionValid = false;

            string input = "";
            while (input != "quit")
            {
                input = Console.ReadLine().ToLower();
                string[] pars = input.Split(' ');

                if (pars.Length > 0)
                {
                    if (connected && versionValid && !loggedIn)
                    {
                        if (pars[0] == "login")
                        {
                            if (pars.Length == 3)
                            {
                                LoginErrors error = shitchat.SendLogin(pars[1], pars[2]);
                                if (error == LoginErrors.None)
                                {
                                    Console.WriteLine("Requesting login...");
                                    username = pars[1];
                                }
                                else
                                {
                                    Console.WriteLine("Could not send login request");
                                }
                            }
                            else
                            {
                                Console.WriteLine("The correct syntax is: login username password");
                            }
                        }
                        else if (pars[0] == "register")
                        {
                            if (pars.Length == 3)
                            {
                                RegisterErrors error = shitchat.SendRegister(pars[1], pars[2]);
                                if (error == RegisterErrors.None)
                                {
                                    Console.WriteLine("requesting registering...");
                                }
                                else
                                {
                                    Console.WriteLine("Could not send register request");
                                }
                            }
                            else
                            {
                                Console.WriteLine("The correct syntax is: register username password");
                            }
                        }
                    }
                    if (loggedIn && connected && versionValid)
                    {
                        if (pars[0] == "command")
                        {
                            if (pars.Length > 1)
                            {
                                Commands command;
                                if (Enum.TryParse<Commands>(pars[1], true, out command))
                                {
                                    string[] commandPars;
                                    if (pars.Length > 2)
                                    {
                                        commandPars = new string[pars.Length - 2];
                                        Array.Copy(pars, 2, commandPars, 0, pars.Length - 2);
                                    }
                                    else
                                    {
                                        commandPars = new string[] { "" };
                                    }
                                    CommandErrors error = shitchat.SendCommand(command, commandPars);
                                    if (error == CommandErrors.None)
                                    {
                                        Console.WriteLine("Sent command...");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Error: " + EnumName(error));
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("That command doesn't exist\nfag");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Possible commands: ");
                                Console.WriteLine(string.Join("\n", Enum.GetNames(typeof(Commands))));
                            }
                        }
                        else if (pars[0] == "message")
                        {
                            if (pars.Length > 1)
                            {
                                MessageTypes messageType;
                                if (Enum.TryParse<MessageTypes>(pars[1], true, out messageType))
                                {
                                    string[] messagePars = new string[1];
                                    if (messageType == MessageTypes.AdminMessage)
                                    {
                                        if (pars.Length > 2)
                                        {
                                            messagePars = new string[1];
                                            messagePars[0] = "";
                                            for (int i = 2; i < pars.Length; i++)
                                            {
                                                messagePars[0] += pars[i] + " ";
                                            }
                                        }
                                    }
                                    else if (messageType == MessageTypes.ChannelMessage)
                                    {
                                        if (pars.Length > 3)
                                        {
                                            messagePars = new string[2];
                                            messagePars[0] = pars[2];
                                            messagePars[1] = "";
                                            for (int i = 3; i < pars.Length; i++)
                                            {
                                                messagePars[1] += pars[i] + " ";
                                            }
                                        }
                                    }
                                    else if (messageType == MessageTypes.PersonalMessage)
                                    {
                                        if (pars.Length > 3)
                                        {
                                            messagePars = new string[2];
                                            messagePars[0] = pars[2];
                                            messagePars[1] = "";
                                            for (int i = 3; i < pars.Length; i++)
                                            {
                                                messagePars[1] += pars[i] + " ";
                                            }
                                        }
                                    }
                                    MessageErrors error = shitchat.SendMessage(messageType, messagePars);
                                    if (error == MessageErrors.None)
                                    {
                                        Console.WriteLine("Sent message...");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Error: " + EnumName(error));
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("That command doesn't exist\nfag");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Possible message types: ");
                                Console.WriteLine(string.Join("\n", Enum.GetNames(typeof(MessageTypes))));
                            }
                        }
                        else if (pars[0] == "listaccounts")
                        {
                            foreach (Account account in accountList)
                            {
                                Console.WriteLine("Name: " + account.Username + ", Friend: " + account.IsFriend + ", Online: " + account.IsOnline + ", AccountType: " + account.AccountType);
                                Console.WriteLine("Channels: " + string.Join(", ", account.ChannelList));
                            }
                        }

                    }
                    if (pars[0] == "help")
                    {
                        Console.WriteLine("connect: connects you to the server and checks the version");
                        Console.WriteLine("disconnect: forces you to disconnects, only works when connected");
                        Console.WriteLine("login: Need to be connected, not logged in, and with a compatible version");
                        Console.WriteLine("register: Need to be connected, not logged in, and with a compatible version");
                        Console.WriteLine("command: Need to be logged in");
                        Console.WriteLine("message: Need to be logged in");
                        Console.WriteLine("listaccounts: Need to be logged in");
                    }
                    else if (pars[0] == "connect" && !connected)
                    {
                        Console.WriteLine("Connecting...");
                        ConnectedErrors error = shitchat.Connect();
                        if (error == ConnectedErrors.AlreadyConnected)
                        {
                            Console.WriteLine("You are already connected to the server");
                        }
                    }
                    else if (pars[0] == "disconnect" && connected)
                    {
                        shitchat.Disconnect("Forced disconnection");
                    }
                }

            }
        }