示例#1
0
        /// <summary>
        /// Initializes the server connection with the set tabs to use.
        /// Requests for a username and servername, and then attempt to connect.
        /// </summary>
        /// <param name="channelTabs">The tabs to fill with channels.</param>
        /// <returns>Returns true if successful, false otherwise.</returns>
        public bool Initialize(TabControl channelTabs)
        {
            // Check if the user already setup a server with this control.
            if (userInfo != null)
            {
                // Dipose of and set the user info form to null.
                userInfo.Dispose();
                userInfo = null;
            }

            // Create a new user info form.
            userInfo = new UserInfoForm();

            // Check that the dialog's results were ok.
            if (userInfo.ShowDialog() != DialogResult.OK)
            {
                // If not, return false.
                return false;
            }

            // Set the channel tabs.
            this.channelTabs = channelTabs;

            // Setup the manage data method to handle any data recieved
            // from the server.
            manageData = new GetData(recievedData);
            // Setup the connection to the server's socket.
            mainSocket = connectToServer(userInfo.server, 6667);

            // Check that the socket was successfully created.
            if (mainSocket == null)
            {
                // If not, display an error message.
                MessageBox.Show("Error connecting to server!");
                // Then return false.
                return false;
            }
            else
            {
                // Set the callback method for when data is recieved.
                mainSocket.BeginReceive(dataBytes, 0, dataBytes.Length, SocketFlags.None, new AsyncCallback(dataRecieved), mainSocket);
            }

            // Return true if everything was successful.
            return true;
        }
示例#2
0
        /// <summary>
        /// This creates a new server tab and sets the data string, data bytes, and requests
        /// for the user's info, as well as initializes the list of channels.
        /// </summary>
        public ServerTab()
        {
            InitializeComponent();

            // Initialize the data string.
            dataString = new StringBuilder();
            // Set the new size to the size of the recieved buffer data.
            dataBytes = new byte[BUFFER_LENGTH];

            // Create the user info form.
            userInfo = new UserInfoForm();

            // Setup the list of channels.
            channels = new SortedDictionary<string, UserControl>();
        }
示例#3
0
        /// <summary>
        /// Perform the actual commands in a specific chat tab.
        /// </summary>
        /// <param name="currentChannel">The channel to perform the command in.</param>
        /// <param name="command">The command to perform.</param>
        /// <param name="parameters">The parameters for the command</param>
        public void doCommand(ChannelTab currentChannel, string command, string[] parameters)
        {
            // Check if the command was a ping command.
            if (command.ToUpper().Equals("PING"))
            {
                // Send the pong response as unicode bytes.
                mainSocket.Send(Encoding.UTF8.GetBytes("PONG :" + parameters[0] + "\r\n"));
            }
            // Check if the command was a pong commands.
            else if (command.ToUpper().Equals("PONG"))
            {
                // Signal that a pong response was gotten.
                currentChannel.pingDone();
            }
            // Check if the command was a notice command.
            else if (command.ToUpper().Equals("NOTICE"))
            {
                // Print out the command with a space afterwards.
                this.msgRecvBox.AppendText(command + " ");

                // Loop through all of the parameters.
                foreach (string param in parameters)
                {
                    // Print out each parameter to the message box.
                    this.msgRecvBox.AppendText(param);

                    // Check if the parameters contains the strings "response".
                    if (param.Contains("response"))
                    {
                        // If the command contains the text "response", then the nickname and
                        // username are sent over in that order.
                        mainSocket.Send(Encoding.UTF8.GetBytes("NICK " + userInfo.nickname + "\r\n"));
                        mainSocket.Send(Encoding.UTF8.GetBytes("USER " + userInfo.username + " 8 * :" + userInfo.realName + "\r\n"));
                    }
                }
                // End the message box with a newline.
                this.msgRecvBox.Text += "\n";
            }
            // Check if the command is 433, which signals a nickname conflict.
            else if (command.Equals("433"))
            {
                // Alert the user that the name is already taken.
                currentChannel.msgRecvBox.Text += "Name already taken!\n";
                // Recreate the user info form and show it.
                userInfo = new UserInfoForm();
                userInfo.ShowDialog();
                // Retry sending the username and nickname.
                mainSocket.Send(Encoding.UTF8.GetBytes("NICK " + userInfo.nickname + "\r\n"));
                mainSocket.Send(Encoding.UTF8.GetBytes("USER " + userInfo.username + " 8 * :" + userInfo.realName + "\r\n"));
            }
            // Check if command is 376, whichs signals the end of the motd command.
            else if (command.Equals("376"))
            {
            #if DEBUG
                // Print out the command if debugging.
                currentChannel.msgRecvBox.AppendText(command + "\n");
            #endif
            }
            // Check if the command is 332, which signals the channel topic command.
            else if (command.Equals("332"))
            {
                // Set the channel topic text to be empty.
                currentChannel.ChannelTopic.Text = String.Empty;
                // Loop through all of the parameters after the first one.
                for (int i = 1; i < parameters.Length; i += 1)
                {
                    // Append each parameter onto the channel topic's text.
                    currentChannel.ChannelTopic.Text += parameters[i] + ":";
                }
                // Trim the ending of any spaces, newlines, or line returns.
                currentChannel.ChannelTopic.Text.TrimEnd(new char[] { ':', ' ', '\n', '\r' });
            }
            // Check for commands 001, 002, 003, 004, 005, 250, 251, 252, 253, 254, 255, 265, 266, 372, and 375
            // all of which are information about the server.
            else if (command.Equals("001") || command.Equals("002") ||
                command.Equals("003") || command.Equals("004") ||
                command.Equals("005") || command.Equals("250") ||
                command.Equals("251") || command.Equals("252") ||
                command.Equals("253") || command.Equals("254") ||
                command.Equals("255") || command.Equals("265") ||
                command.Equals("266") || command.Equals("372") ||
                command.Equals("375"))
            {
                // Enable the join channel toolstrip item.
                joinChannelToolStripMenuItem.Enabled = true;
                // Enable the connection image.
                currentChannel.connectionImage.Enabled = true;

                // Loop through all of the parameters after the first one.
                for (int i = 1; i < parameters.Length; i += 1)
                {
                    // Append each parameter onto the messsage box with a colon after since that
                    // is what separaters them.
                    this.msgRecvBox.AppendText(parameters[i] + ":");
                }
                // Trim the ending of the text of any colons that are still there.
                this.msgRecvBox.Text = this.msgRecvBox.Text.TrimEnd(new char[] { ':' });
                // Append a newline character to the end as well.
                this.msgRecvBox.AppendText("\n");

                // Check if the command is not 372.
                if (!command.Equals("372"))
                {
                    // If so, append another newline character for readability.
                    this.msgRecvBox.AppendText("\n");
                }
            }
            // Check if the command is 353, which signals the list of users in the channel.
            else if (command.Equals("353"))
            {
                // Create an old tab variable.
                ChannelTab oldTab = currentChannel;
                // Check if the channel is on another tab.
                if (channels.ContainsKey(parameters[0].Split(' ')[3].ToLower()))
                {
                    // If so, set the current channel.
                    currentChannel = (ChannelTab)channels[parameters[0].Split(' ')[3].ToLower()];
                }

                // Clear the old username list.
                currentChannel.userListBox.Items.Clear();
                // Split the list of users in the room.
                string[] userList = parameters[1].Split(' ');
                // Now loop through the list of users.
                foreach (string username in userList)
                {
                    // Check for any invalid characters at the beginning of
                    // the user's name.
                    if (usernameInvalidChars.Contains<char>(username.ToCharArray()[0]))
                    {
                        // Check that the user is not already in the list.
                        if (!currentChannel.userListBox.Items.Contains(username.Substring(1)))
                        {
                            // If there is an invalid character, ignore the first character
                            // and add the name to the username list.
                            currentChannel.userListBox.Items.Add(username.Substring(1));
                        }
                    }
                    else
                    {
                        // Otherwise, also check that the user's name isn't an empty string.
                        if (!username.Equals(string.Empty))
                        {
                            // Check that the user is not already in the list.
                            if (!currentChannel.userListBox.Items.Contains(username))
                            {
                                // Add the username to the list.
                                currentChannel.userListBox.Items.Add(username);
                            }
                        }
                    }
                }
            #if DEBUG
                // Print out the command recieved to the screen if debugging.
                currentChannel.msgRecvBox.AppendText(command + "\n");
            #else
                // If not debugging, nicely print out a message depending on the number of users
                // in the channel.
                switch (currentChannel.userListBox.Items.Count - 1)
                {
                    case 0:
                        currentChannel.msgRecvBox.Text += "No users in the current channel.";
                        break;
                    case 1:
                        currentChannel.msgRecvBox.Text += "Current user in channel: ";
                        break;
                    default:
                        currentChannel.msgRecvBox.Text += "Current users in channel: ";
                        break;
                }
                // Then print out each user that is in the channel.
                foreach (string user in currentChannel.userListBox.Items)
                {
                    if (!user.Equals(userInfo.username))
                    {
                        currentChannel.msgRecvBox.AppendText(user + ", ");
                    }
                }
                // Trim the ending of any invalid characters.
                currentChannel.msgRecvBox.Text = currentChannel.msgRecvBox.Text.TrimEnd(new char[] { ',', ' ' }) + "\n";
            #endif
                // Enable the connection image.
                currentChannel.connectionImage.Enabled = true;
                // Set the current channel back to it's original channel.
                currentChannel = oldTab;
            }
            // Check if the command is a nickname change (denoted by having NICK).
            else if (command.ToUpper().Equals("NICK"))
            {
                for(int i = 0;i < currentChannel.userListBox.Items.Count;i += 1)
                {
                    if (currentChannel.userListBox.Items[i].Equals(parameters[parameters.Length - 1]))
                    {
                        currentChannel.userListBox.Items[i] = parameters[0];
                        currentChannel.msgRecvBox.AppendText(parameters[parameters.Length - 1] + " has changed their nickname to " + parameters[0] + "!\n");
                        break;
                    }
                }
            }
            // Check if the command is a message (denoted by having PRIVMSG).
            else if (command.ToUpper().Equals("PRIVMSG"))
            {
                // Then check if the command contains the channel name.
                if (parameters[0].ToLower().Contains(currentChannel.joinChannelForm.channel.ToLower()) ||
                    parameters[parameters.Length - 1].ToLower().Contains(currentChannel.joinChannelForm.channel.ToLower()))
                {
                    // If so, filter out the username of whoever sent it, and
                    // display the response in the message recieved text box.
                    currentChannel.msgRecvBox.AppendText(parameters[parameters.Length - 1] + ": ");

                    // Then loop through all of the paremeters.
                    for (int i = 1; i < parameters.Length - 1; i += 1)
                    {
                        // Append the message box with the parameter and a colon at the end
                        // since they are colon separated.
                        currentChannel.msgRecvBox.AppendText(parameters[i] + ":");
                    }
                    // Trim the ending of any leftover colons.
                    currentChannel.msgRecvBox.Text = currentChannel.msgRecvBox.Text.TrimEnd(new char[] { ':' });
                    // Append a newline to the end of it.
                    currentChannel.msgRecvBox.AppendText("\n");

                    // Check if the selected tab is the one that received the message.
                    if (!currentChannel.joinChannelForm.channel.ToLower().Equals(channelTabs.SelectedTab.Text.ToLower()))
                    {
                        // If not, set the chat notification.
                        currentChannel.notification = Chat_Notifications.Message;
                    }
                }
                // If not, then it was a private messgae most likely.
                else
                {
                    // Create a new private chat.
                    newPrivateChat(parameters[parameters.Length - 1]);

                    // Display the user the message is from.
                    currentChannel.msgRecvBox.AppendText(parameters[parameters.Length - 1] + ": ");

                    // Then loop through all of the paremeters.
                    for (int i = 1; i < parameters.Length - 1; i += 1)
                    {
                        // Append the message box with the parameter and a colon at the end
                        // since they are colon separated.
                        currentChannel.msgRecvBox.AppendText(parameters[i] + ":");
                    }
                    // Trim the ending of any leftover colons.
                    currentChannel.msgRecvBox.Text = currentChannel.msgRecvBox.Text.TrimEnd(new char[] { ':' });
                    // Append a newline to the end of it.
                    currentChannel.msgRecvBox.AppendText("\n");
                }
            }
            // Check if the command is PART, KILLED, or QUIT.
            else if (command.ToUpper().Equals("PART") || command.ToUpper().Equals("KILLED") || command.ToUpper().Equals("QUIT"))
            {
                // If so, print out that the user has quit.
                currentChannel.msgRecvBox.AppendText(parameters[parameters.Length - 1] + " has quit:");

                // Then loop through all of the paremeters.
                for (int i = 1; i < parameters.Length - 1; i += 1)
                {
                    // Append the message box with the parameter and a colon at the end
                    // since they are colon separated.
                    currentChannel.msgRecvBox.AppendText(parameters[i] + ":");
                }
                // Trim the ending of any leftover colons.
                currentChannel.msgRecvBox.Text = currentChannel.msgRecvBox.Text.TrimEnd(new char[] { ':' });
                // Append a newline to the end of it.
                currentChannel.msgRecvBox.AppendText("\n");

                // Then remove the user from the user list box.
                currentChannel.userListBox.Items.Remove(parameters[parameters.Length - 1]);

                // Check if the selected tab is the one that received the message.
                if (!currentChannel.joinChannelForm.channel.ToLower().Equals(channelTabs.SelectedTab.Text.ToLower()))
                {
                    // If not, set the chat notification.
                    currentChannel.notification = Chat_Notifications.UserLeft;
                }
            }
            // Check if the command is JOIN.
            else if (command.ToUpper().Equals("JOIN"))
            {
                // If so, print out that the user has joined.
                currentChannel.msgRecvBox.AppendText(parameters[parameters.Length - 1] + " has joined!\n");

                // Check that the user is not already in the list.
                if (!currentChannel.userListBox.Items.Contains(parameters[parameters.Length - 1]))
                {
                    // Then add the user to the user list box.
                    currentChannel.userListBox.Items.Add(parameters[parameters.Length - 1]);
                }

                // Check if the selected tab is the one that received the message.
                if (!currentChannel.joinChannelForm.channel.ToLower().Equals(channelTabs.SelectedTab.Text.ToLower()))
                {
                    // If not, set the chat notification.
                    currentChannel.notification = Chat_Notifications.UserJoined;
                }
            }
            #if DEBUG
            else
            {
                // Otherwise, just print out the command if debugging and it is not a newline.
                currentChannel.msgRecvBox.AppendText(command + " ");
                foreach (string param in parameters)
                {
                    currentChannel.msgRecvBox.AppendText(param);
                }
                currentChannel.msgRecvBox.Text += "\n";
            }
            #endif
            // Check if the text box length is greater than 0.
            if (currentChannel.msgRecvBox.Text.Length > 0)
            {
                // If so, set the selection starting position (the carret) and scroll to the
                // carret.
                currentChannel.msgRecvBox.SelectionStart = currentChannel.msgRecvBox.Text.Length - 1;
                currentChannel.msgRecvBox.ScrollToCaret();
            }
            else
            {
                // Otherwise, reset the selection starting position (the carret) and scroll to the
                // carret.
                currentChannel.msgRecvBox.SelectionStart = 0;
                currentChannel.msgRecvBox.ScrollToCaret();
            }

            // Refresh the channel tabs to display changes.
            channelTabs.Refresh();
        }