コード例 #1
0
ファイル: ServerTab.cs プロジェクト: GEMISIS/IRC-Client
        /// <summary>
        /// Connects to an IRC server at the chosen addres and port.
        /// </summary>
        /// <param name="serverAdddress">The address of the server.</param>
        /// <param name="serverPort">The port to connect to the server on.</param>
        /// <returns>Returns the newly connected socket.</returns>
        private Socket connectToServer(string serverAdddress, int serverPort)
        {
            TcpClient client = new TcpClient(serverAdddress, serverPort);

            // If the socket is connected, return it.
            if (client.Connected)
            {
                // Set the server name by getting the exact host entry from
                // the DNS server.
                this.ServerName.Text = userInfo.server = Dns.GetHostEntry(((IPEndPoint)client.Client.RemoteEndPoint).Address).HostName;

                // Set the current channel to a new chat tab.
                this.currentChannel = new ChannelTab();
                // Set the message box for the current channel to the server message box.
                this.currentChannel.msgRecvBox = this.msgRecvBox;

                // Set the first tab's text to the server name.
                channelTabs.TabPages[0].Text = userInfo.server;

                // Return the socket.
                return client.Client;
            }

            // Return null if the socket couldn't be connected to.
            return null;
        }
コード例 #2
0
ファイル: ServerTab.cs プロジェクト: GEMISIS/IRC-Client
        /// <summary>
        /// Used to join a channel on a server.
        /// </summary>
        /// <returns>Returns true if successful, false otherwise.</returns>
        public bool joinChannel()
        {
            // Check if the user is already in a channel.
            if (joinChannelForm != null)
            {
                // Dipose of and set the join channel form to null.
                joinChannelForm.Dispose();
                joinChannelForm = null;
            }

            // Create a new join channel form.
            joinChannelForm = new JoinChannelForm();

            // Then show the form so the user can type in the channel name and check the results.
            if (joinChannelForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Check that the tab does not already exist.
                if (channelTabs.TabPages.ContainsKey(joinChannelForm.channel.ToLower()))
                {
                    // If the channel already exists, set the selected tab.
                    channelTabs.SelectedTab = channelTabs.TabPages[joinChannelForm.channel.ToLower()];
                    // Also set the join channel form to null.
                    joinChannelForm = null;
                    // Return false since it was unsuccessful.
                    return false;
                }
                else
                {
                    // Create a new tab with the channel name as its text.
                    channelTabs.TabPages.Add(joinChannelForm.channel.ToLower(), joinChannelForm.channel);
                    // Create the new chat tab.
                    ChannelTab newChatTab = new ChannelTab();
                    // Set the tab dock style to fill the tab.
                    newChatTab.Dock = DockStyle.Fill;
                    // Set the socket, user info form and join channel form.
                    newChatTab.joinChannelForm = joinChannelForm;
                    newChatTab.userInfo = userInfo;
                    newChatTab.mainSocket = mainSocket;
                    newChatTab.server = this;
                    // Clear the message recieved text box.
                    newChatTab.msgRecvBox.Text = "";

                    // Focus on the send message text box.
                    newChatTab.sendMsgBox.Focus();

                    // Add the tab to the list of chennels available.
                    channels.Add(joinChannelForm.channel.ToLower(), newChatTab);
                    // Set the current channel.
                    currentChannel = (ChannelTab)(channels[joinChannelForm.channel.ToLower()]);
                    // Set the tab name for the channel.
                    channelTabs.TabPages[channelTabs.TabPages.IndexOfKey(joinChannelForm.channel)].Controls.Add(newChatTab);

                    // Select the newly created channel.
                    channelTabs.SelectTab(joinChannelForm.channel);

                    // Send the command to join the desired channel.
                    mainSocket.Send(Encoding.UTF8.GetBytes("JOIN " + joinChannelForm.channel + "\r\n"));
                }
            }
            else
            {
                // If joining the channel was cancelled, set the join channel form to null.
                joinChannelForm = null;
                // Return false since it was unsuccessful.
                return false;
            }

            // Return true since it was successful.
            return true;
        }
コード例 #3
0
ファイル: ServerTab.cs プロジェクト: GEMISIS/IRC-Client
        /// <summary>
        /// Creates a new private chat with a specific user.
        /// </summary>
        /// <param name="userName">The user to start a private chat with.</param>
        public void newPrivateChat(string userName)
        {
            // Check if the tab already exists for this private chat.
            if (!channelTabs.TabPages.ContainsKey(userName.ToLower().TrimEnd(new char[] { ' ' })))
            {
                // Create a new tab with the channel name as its text.
                channelTabs.TabPages.Add(userName.ToLower().TrimEnd(new char[] { ' ' }), userName.TrimEnd(new char[] { ' ' }));
                // Create the new chat tab.
                ChannelTab newChatTab = new ChannelTab();
                // Set the tab dock style to fill the tab.
                newChatTab.Dock = DockStyle.Fill;
                // Set the socket, user info form and join channel form, but setting the channel to be
                // the other users' name.
                newChatTab.joinChannelForm = new JoinChannelForm();
                newChatTab.joinChannelForm.channel = userName.TrimEnd(new char[] { ' ' });
                newChatTab.userInfo = userInfo;
                newChatTab.mainSocket = mainSocket;
                // Set the new tab's server.
                newChatTab.server = this;
                // Clear the new tab's user list box.
                newChatTab.userListBox.Items.Clear();
                // Add the other user from the private chat to the user list box.
                newChatTab.userListBox.Items.Add(userName);
                // Clear the message recieved text box.
                newChatTab.msgRecvBox.Text = "";

                // Focus on the send message text box.
                newChatTab.sendMsgBox.Focus();

                // Add the tab to the list of chennels available.
                channels.Add(userName.ToLower().TrimEnd(new char[] { ' ' }), newChatTab);
                // Set the current channel.
                currentChannel = (ChannelTab)(channels[userName.ToLower().TrimEnd(new char[] { ' ' })]);
                // Set the tab name for the channel.
                channelTabs.TabPages[channelTabs.TabPages.IndexOfKey(userName.ToLower().TrimEnd(new char[] { ' ' }))].Controls.Add(newChatTab);

                // Select the newly created channel.
                channelTabs.SelectTab(userName.ToLower().TrimEnd(new char[] { ' ' }));
            }
        }
コード例 #4
0
ファイル: ServerTab.cs プロジェクト: GEMISIS/IRC-Client
        /// <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();
        }