PageRenderTime 1350ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/MainWindow.cs

http://github.com/taftse/OpenIRCC
C# | 289 lines | 236 code | 35 blank | 18 comment | 21 complexity | 41f3ffe9db1acd0c2a9c1409c8216963 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Threading;
  11. using System.Net.IRC;
  12. using System.Net.IRC.Client;
  13. namespace TIRCClient
  14. {
  15. public partial class MainWindow : Form
  16. {
  17. //delegate void SetTextCallback(string text);
  18. delegate void setChannelCallback(Server server,Channel channel,string message);
  19. delegate void setServerCallback(Server server, string message);
  20. private Thread serverThread;
  21. public Server ircClient;
  22. public TreeNode selectedChannel = new TreeNode();
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. serverThread = new Thread(new ThreadStart(ClientConnection));
  27. serverThread.Start();
  28. }
  29. private void ClientConnection()
  30. {
  31. this.ircClient = new Server();
  32. this.ircClient.ServerCommandsSent += new ServerMessage(SentServerCommands);
  33. this.ircClient.ChannelReceivedMessages += new ChannelMessage(ReceivedChannelMessages);
  34. this.ircClient.ChannelSentMessages += new ChannelMessage(SentChannelMessages);
  35. this.ircClient.ChannelSentCommands += new ChannelMessage(SentChannelCommands);
  36. this.ircClient.ChannelReceivedCommands += new ChannelMessage(ReceivedChannelCommands);
  37. /*this.ircClient.UserJoinEvent += new ChannelMessage(ChannelEvent);
  38. this.ircClient.ServerEvent += new ServerMessage(ServerEvent);
  39. this.ircClient.SendEvent += new CommandSent(ServerEvent);
  40. this.ircClient.ChannelEvent += new ChannelMessage(ChannelEvent);*/
  41. this.ircClient.Connect("irc.freenode.net", 6667,"taftse taftse","Taftse|Test");
  42. }
  43. private void SentServerCommands(Server server, string message)
  44. {
  45. if (this.InvokeRequired)
  46. {
  47. setServerCallback s = new setServerCallback(SentServerCommands);
  48. this.Invoke(s, new object[] { server, message });
  49. }
  50. else
  51. {
  52. switch (message)
  53. {
  54. case "CONNECT": this.JoindServer(server); break;
  55. //default: this.PrintToChatWindow(server, message); break;
  56. }
  57. }
  58. }
  59. private void ReceivedChannelMessages(Server server, Channel channel, string message)
  60. {
  61. if (this.InvokeRequired)
  62. {
  63. setChannelCallback s = new setChannelCallback(ReceivedChannelMessages);
  64. this.Invoke(s, new object[] { server, channel, message });
  65. }
  66. else
  67. {
  68. this.PrintToChatWindow(server, message);
  69. }
  70. }
  71. private void SentChannelMessages(Server server, Channel channel, string message)
  72. {
  73. if (this.InvokeRequired)
  74. {
  75. setChannelCallback s = new setChannelCallback(SentChannelMessages);
  76. this.Invoke(s,new object[] {server,channel,message});
  77. }
  78. else
  79. {
  80. this.PrintToChatWindow(server, message);
  81. }
  82. }
  83. private void SentChannelCommands(Server server, Channel channel, string message)
  84. {
  85. if (this.InvokeRequired)
  86. {
  87. setChannelCallback s = new setChannelCallback(SentChannelCommands);
  88. this.Invoke(s, new object[] { server, channel, message });
  89. }
  90. else
  91. {
  92. switch (message)
  93. {
  94. case "JOIN": this.JoinedChannel(server, channel); break;
  95. case "PART": this.LeftChannel(server, channel); break;
  96. //default: this.ChatWindow.AppendText(message + " \n"); break;
  97. }
  98. }
  99. }
  100. private void ReceivedChannelCommands(Server server, Channel channel, string message)
  101. {
  102. if (this.InvokeRequired)
  103. {
  104. setChannelCallback s = new setChannelCallback(ReceivedChannelCommands);
  105. this.Invoke(s, new object[] { server, channel, message });
  106. }
  107. else
  108. {
  109. switch (message)
  110. {
  111. case "NAMES": this.UserBox.DataSource = channel.NickNames; break;
  112. case "JOIN": this.UserBox.DataSource = channel.NickNames; break;
  113. case "TOPIC": this.ChannelTitle.Text = channel.Title; break;
  114. }
  115. }
  116. }
  117. private void PrintToChatWindow(Server server, string message)
  118. {
  119. this.ChatWindow.AppendText("[" + DateTime.Now + "] " + message + "\r\n");
  120. }
  121. private void JoindServer(Server server)
  122. {
  123. TreeNode newServer = new TreeNode();
  124. newServer.Name = "serverNode_" + server.Name;
  125. newServer.Text = " " + server.Name;
  126. this.ChannelList.Nodes.Add(newServer);
  127. this.SelectChannelListNode(newServer);
  128. }
  129. private void JoinedChannel(Server server,Channel channel)
  130. {
  131. TreeNode newChannel = new TreeNode();
  132. newChannel.Name = "channelNode_"+ channel.Name;
  133. newChannel.Text = channel.Name;
  134. TreeNode[] serverNode = this.ChannelList.Nodes.Find("serverNode_" + server.Name, false);
  135. serverNode[0].Nodes.Add(newChannel);
  136. this.SelectChannelListNode(newChannel);
  137. }
  138. private void LeftChannel(Server server, Channel channel)
  139. {
  140. TreeNode[] serverNode = this.ChannelList.Nodes.Find("serverNode_" + server.Name, false);
  141. TreeNode[] channelNode = serverNode[0].Nodes.Find("channelNode_" + channel.Name, false);
  142. serverNode[0].Nodes.Remove(channelNode[0]);
  143. }
  144. private void Op_Click(object sender, EventArgs e)
  145. {
  146. }
  147. private void DeOp_Click(object sender, EventArgs e)
  148. {
  149. }
  150. private void Ban_Click(object sender, EventArgs e)
  151. {
  152. }
  153. private void Kick_Click(object sender, EventArgs e)
  154. {
  155. // sendChannelMessage("/KICK "+channelName+" "+NickName);
  156. }
  157. private void SendFile_Click(object sender, EventArgs e)
  158. {
  159. }
  160. private void Dialog_Click(object sender, EventArgs e)
  161. {
  162. }
  163. private void NickNameButton_Click(object sender, EventArgs e)
  164. {
  165. NickNameDialog nickDialog = new NickNameDialog(this);
  166. nickDialog.Show();
  167. }
  168. private void exitApplication_Click(object sender, EventArgs e)
  169. {
  170. serverThread.Abort();
  171. Application.Exit();
  172. }
  173. private void MesageWindow_KeyPress(object sender, KeyPressEventArgs e)
  174. {
  175. if (e.KeyChar == (char)Keys.Enter)
  176. {
  177. this.send_message();
  178. }
  179. }
  180. private void send_message()
  181. {
  182. /* string[] channelNameParts = new string[this.selectedChannel.Name.Split('_').Length];
  183. channelNameParts =this.selectedChannel.Name.Split('_');*/
  184. string channelName = this.ircClient.SelectedChannel;
  185. string value = MesageWindow.Text.ToString();
  186. //if (value.Split(' ').Length > 1)
  187. // {
  188. string[] valueParts = new string[value.Split(' ').Length];
  189. valueParts = value.Split(' ');
  190. switch (valueParts[0])
  191. {
  192. case "/action":
  193. value = value.Replace("/action", "ACTION");
  194. this.ircClient.SendCommand("PRIVMSG", ":\u0001" + value + "\u0001", channelName);
  195. break;
  196. case "/join": this.ircClient.JoinChannel(valueParts[1]); break;
  197. case "/leave":
  198. if (valueParts.Length > 1)
  199. {
  200. this.ircClient.LeaveChannel(valueParts[1]);
  201. }
  202. else
  203. {
  204. this.ircClient.LeaveChannel(channelName);
  205. }
  206. break;
  207. case "/quit": this.ircClient.SendCommand("QUIT",null); break;
  208. case "/msg": this.ircClient.SendCommand("PRIVMSG", ":" + valueParts[2], valueParts[1]); break;
  209. default: this.ircClient.SendCommand("PRIVMSG", ":" + value, channelName); break;
  210. }
  211. /* }
  212. else
  213. {
  214. this.ircClient.SendCommand("PRIVMSG",":"+ value, channelName);
  215. }*/
  216. MesageWindow.Text = null;
  217. }
  218. private void SelectChannelListNode(TreeNode channel)
  219. {
  220. //this.selectedChannel = channel;
  221. string[] channelNameParts = new string[channel.Name.Split('_').Length];
  222. channelNameParts = channel.Name.Split('_');
  223. if (channelNameParts[0] == "serverNode")
  224. {
  225. if (this.ircClient.SelectedChannel != channelNameParts[1])
  226. {
  227. this.ChannelList.SelectedNode = channel;
  228. this.ircClient.SelectedChannel = channelNameParts[1];
  229. Channel selectedServer = this.ircClient.getChannel(channelNameParts[1]);
  230. this.ChatWindow.Text = selectedServer.Messages;
  231. this.NickNameButton.Text = this.ircClient.NickName;
  232. this.ChannelTitle.Text = "";
  233. this.UserBox.DataSource = new List<string>();
  234. }
  235. }
  236. else if (channelNameParts[0] == "channelNode")
  237. {
  238. if (this.ircClient.SelectedChannel != channelNameParts[1])
  239. {
  240. this.ChannelList.SelectedNode = channel;
  241. this.ircClient.SelectedChannel = channelNameParts[1];
  242. Channel selectedChannel = this.ircClient.getChannel(channelNameParts[1]);
  243. this.ChatWindow.Text = selectedChannel.Messages;
  244. this.ChannelTitle.Text = selectedChannel.Title;
  245. this.UserBox.DataSource = selectedChannel.NickNames;
  246. }
  247. }
  248. }
  249. private void ChannelList_AfterSelect(object sender, TreeViewEventArgs e)
  250. {
  251. this.SelectChannelListNode(e.Node);
  252. }
  253. }
  254. }