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