// APPConfiguration.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using ProjectName.Base; namespace ProjectName.Application.Configuration { public class AppConfiguration { #region Public Static Members /// /// server /// public static string server { get; set; } /// /// nickname /// public static string nickName { get; set; } /// /// username /// public static string userName { get; set; } /// /// port /// public static int port { get; set; } #endregion Public Static Members #region Constructor public static void initConfiguration() { if (!ConfigurationSettings.AppSettings["ProjectName.Application.Configuration.IRCConnection.server"].Equals(null)) AppConfiguration.server = ConfigurationSettings.AppSettings["ProjectName.Application.Configuration.IRCConnection.server"]; if (!ConfigurationSettings.AppSettings["ProjectName.Application.Configuration.IRCConnection.userName"].Equals(null)) AppConfiguration.userName = ConfigurationSettings.AppSettings["ProjectName.Application.Configuration.IRCConnection.userName"]; if (!ConfigurationSettings.AppSettings["ProjectName.Application.Configuration.IRCConnection.nickName"].Equals(null)) AppConfiguration.nickName = ConfigurationSettings.AppSettings["ProjectName.Application.Configuration.IRCConnection.nickName"]; if (!ConfigurationSettings.AppSettings["ProjectName.Application.Configuration.IRCConnection.port"].Equals(null)) AppConfiguration.port = NullCheck.GetInt32Value(ConfigurationSettings.AppSettings["ProjectName.Application.Configuration.IRCConnection.port"], 0); } #endregion Constructor } } // CONNECTION STATUS.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectName.IRC { public enum CONNECTION_STATUS { Connecting = 0, Connected = 1, Disconnected = 2 } } // IRCChannel.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectName.IRC { public class IRCChannel { #region Public Members /// /// Channel name /// public string name { get; set; } #endregion Public Members } } // IRCConnection.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; using System.Threading; using ProjectName.Base; using ProjectName.IRC; namespace ProjectName.IRC.Connection { public class IRCConnection { #region Private Members /// /// net stream /// private NetworkStream m_stream; /// /// reader /// private StreamReader m_reader; /// /// writer /// private StreamWriter m_writer; /// /// Thread IRC Handler /// private Thread m_threadIRCHandler; /// /// Handler message /// private IRCHandlerPrivmsg m_ircHandlerPrivmsg; #endregion Private Members #region Public Members /// /// server /// public string server { get; set; } /// /// port /// public int port { get; set; } /// /// username /// public string userName { get; set; } /// /// nickname /// public string nickName { get; set; } /// /// Connection Status /// private CONNECTION_STATUS m_connectionStatus = CONNECTION_STATUS.Disconnected; public CONNECTION_STATUS connectionStatus { get { return this.m_connectionStatus; } set { this.m_connectionStatus = value; } } /// /// Connection Instance /// public IRCConnectionInstance currentInstance { get; set; } #endregion Public Members #region Public Methods /// /// Connection /// public void Launch() { if (string.IsNullOrEmpty(this.server) || string.IsNullOrEmpty(this.userName) || string.IsNullOrEmpty(this.nickName) || port.Equals(null)) throw new SystemException("ERROR : you must configure the irc client correctly"); this.currentInstance = new IRCConnectionInstance(); // Connection TcpClient client = new TcpClient(this.server, this.port); this.connectionStatus = CONNECTION_STATUS.Connecting; this.m_stream = client.GetStream(); this.m_reader = new StreamReader(this.m_stream); this.m_writer = new StreamWriter(this.m_stream); this.m_writer.WriteLine("USER " + this.userName + " 8 * :" + this.userName); this.m_writer.Flush(); this.m_writer.WriteLine("NICK " + this.nickName); this.m_writer.Flush(); this.m_threadIRCHandler = new Thread(new ThreadStart(this.handleConnection)); this.m_threadIRCHandler.Start(); } /// /// Disconnection /// public void Disconnect() { this.m_reader.Close(); this.m_writer.Close(); this.m_stream.Close(); this.m_threadIRCHandler.Abort(); this.m_connectionStatus = CONNECTION_STATUS.Disconnected; Console.WriteLine("The connection has been closed"); Console.ReadLine(); } /// /// Write into IRC Stream /// /// public void writeToIRC(string text) { this.m_writer.WriteLine(text); this.m_writer.Flush(); } #endregion Public Methods #region Public Events /// /// Privmsg Handler Delegate /// /// /// public delegate void PrivmsgHandler(object sender, string text); /// /// OnPrivmsg Event /// public event PrivmsgHandler Privmsg; #endregion Public Events #region Private Methods /// /// Connection Handler /// private void handleConnection() { this.m_ircHandlerPrivmsg = new IRCHandlerPrivmsg(this); Privmsg += new PrivmsgHandler(OnPrivmsg); string line; while (true) { while ((line = this.m_reader.ReadLine()) != null) { Console.WriteLine(line); StringBuilder sb = new StringBuilder(); string[] sline = line.Split(' '); string sender = sline[0].Substring(1, sline[0].Length - 1); #region Connection // Connected if (sline[1].Equals("001")) { this.connectionStatus = CONNECTION_STATUS.Connected; } // Server infos if (sline[1].Equals("002")) { this.currentInstance.currentServer = sline[6].Substring(0, sline[6].Length - 1); } // PING if (sline[0] == "PING") { string replyNumber = sline[1]; Console.WriteLine("PONG " + replyNumber); this.writeToIRC("PONG " + replyNumber); } #endregion Connection // PRIVMSG if (sline[1].Equals("PRIVMSG")) { sb.Append(sline[3].Substring(1, sline[3].Length - 1)); sb.AppendFormat(" {0}", StringHelper.getTextByRange(sline, 4, null, ' ')); if (Privmsg != null) OnPrivmsg(sender, sb.ToString()); } // JOIN if (sline[1].Equals("JOIN")) { if (!sender.Equals(this.currentInstance.currentServer, StringComparison.InvariantCultureIgnoreCase)) continue; IRCChannel channel = new IRCChannel(); channel.name = sline[2]; this.currentInstance.currentChannels.Add(channel); } // PART if (sline[1].Equals("PART")) { if (!sender.Equals(this.currentInstance.currentServer, StringComparison.InvariantCultureIgnoreCase)) continue; IRCChannel channel = this.currentInstance.getCurrentIRCChannelByName(sline[2]); if (channel.Equals(null)) return; this.currentInstance.currentChannels.Remove(channel); } } } } #endregion Private Methods #region Events private void OnPrivmsg(string sender, string text) { this.m_ircHandlerPrivmsg.handlePrivmsg(sender, text); } #endregion Events } } // IRC ConnectionInstance.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using ProjectName.IRC; namespace ProjectName.IRC.Connection { public class IRCConnectionInstance { #region Public Members /// /// Current Server /// public string currentServer { get; set; } /// /// Current Nickname /// public string currentNickname { get; set; } /// /// Current Channels /// private List m_currentChannels = new List(); public List currentChannels { get { return this.m_currentChannels; } } #endregion Public Members #region Public Methods /// /// Get the IRC Channel by its name /// /// /// public IRCChannel getCurrentIRCChannelByName(string name) { foreach (IRCChannel ircChannel in this.currentChannels) { if (ircChannel.name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) return ircChannel; } return null; } #endregion Public Methods } } // IRCHandlerPrivmsg.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using ProjectName.Base; using ProjectName.IRC.Connection; namespace ProjectName.IRC { public class IRCHandlerPrivmsg { #region Private Class /// /// PrivmsgEventArgs /// public class PrivmsgEventArgs : EventArgs { #region Public Members public string ircChannel { get; set; } public string message { get; set; } public bool isPrivateMessage { get; set; } #endregion Public Members } #endregion Private Class #region Private Members /// /// Delegate /// /// /// private delegate void OnPrivmsg(string sender, PrivmsgEventArgs e); /// /// IRC Connection /// private IRCConnection m_ircConnection; /// /// List of events /// private Dictionary m_privmsgEventsList; #endregion Private Members #region Public Constructor public IRCHandlerPrivmsg(IRCConnection ircConnection) { this.m_ircConnection = ircConnection; this.m_privmsgEventsList = new Dictionary(); this.m_privmsgEventsList.Add(".join", new OnPrivmsg(join)); this.m_privmsgEventsList.Add(".part", new OnPrivmsg(part)); } #endregion Public Constructor #region Public Methods /// /// Privmsg Handler /// /// /// public void handlePrivmsg(string sender, string text) { string[] stext = text.Split(' '); if (!this.m_privmsgEventsList.ContainsKey(stext[0].Trim())) return; // StringHelper.getTextByRange(stext, 1, null, ' ') this.m_privmsgEventsList[stext[0].Trim()](sender, new PrivmsgEventArgs()); } #endregion Public Methods #region Private Methods /// /// Join /// /// /// private void join(string sender, PrivmsgEventArgs e) { this.m_ircConnection.writeToIRC(string.Format("JOIN {0}", e.message)); } /// /// Part /// /// /// private void part(string sender, PrivmsgEventArgs e) { this.m_ircConnection.writeToIRC(string.Format("PART {0}", e.message)); } #endregion Private Methods } } Program.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; using ProjectName.IRC.Connection; using ProjectName.Application.Configuration; namespace ProjectName { class sharp { static void Main(string[] args) { AppConfiguration.initConfiguration(); IRCConnection ircConnection = new IRCConnection(); ircConnection.server = AppConfiguration.server; ircConnection.port = AppConfiguration.port; ircConnection.nickName = AppConfiguration.nickName; ircConnection.userName = AppConfiguration.userName; ircConnection.Launch(); } } } /////////////////////////////////////// PROJET BASE LINKER AU PROJET using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectName.Base { public class NullCheck { #region Public Methods /// /// Try to convert value into int /// /// /// /// public static int GetInt32Value(object value, int defaultValue) { try { return Convert.ToInt32(value); } catch { return defaultValue; } } #endregion Public Methods } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectName.Base { public class StringHelper { #region Public Static Methods /// /// Returns a text by a range of splitted args /// /// /// /// /// /// /// public static string getTextByRange(string text, int startIndex, int? endIndex, char separator, char? concatChar) { string[] stext = text.Split(separator); StringBuilder sb = new StringBuilder(); for (int i = startIndex; ((i <= endIndex || endIndex.Equals(null)) && (i <= stext.Count() - 1)); i++) sb.AppendFormat("{0}{1}", stext[i], !concatChar.Equals(null) ? concatChar.ToString() : string.Empty); return sb.ToString(); } /// /// Returns a text by a range of splitted args /// /// /// /// /// /// /// /// public static string getTextByRange(string[] stext, int startIndex, int? endIndex, char? concatChar) { StringBuilder sb = new StringBuilder(); for (int i = startIndex; ((i <= endIndex || endIndex.Equals(null)) && (i <= stext.Count() - 1)); i++) sb.AppendFormat("{0}{1}", stext[i], !concatChar.Equals(null) ? concatChar.ToString() : string.Empty); return sb.ToString(); } #endregion Public Static Methods } }