-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRCClient.cs
286 lines (242 loc) · 8.94 KB
/
IRCClient.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
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace System.Net.IRC.Client
{
/*public delegate void SystemMessage(string SystemMessage);
public delegate void CommandReceived(string IrcCommand);
// public delegate void CommandSent(string IrcCommand);
// public delegate void ServerMessage(string ServerMessage);
public class IRCClient
{
public event SystemMessage eventSystemMessage;
public event CommandReceived eventReceiving;
public event CommandSent eventSent;
public event ServerMessage eventServerMessage;
public event SystemMessage serverConnect;
public event SystemMessage joinChannel;
private string host;
private int port;
private string nick;
private string user;
private string password;
private string realname = "Taftse's Client";
private bool isInvisible;
private Hashtable joinedChannels;
private string[] hostnameParts;
private Server thisServer;
private TcpClient connection;
private StreamReader reader;
private StreamWriter writer;
public string Host
{
get { return this.host; }
}
public int Port
{
get { return this.port; }
}
public string UserName
{
get { return this.user; }
}
public string Password
{
get { return this.password; }
}
public string NickName
{
get { return this.nick; }
set { this.ChangeNick(value); }
}
public bool IsInvisble
{
get { return this.isInvisible; }
set { this.isInvisible = value; }
}
public IRCClient()
{
}
public void Connect(string host, int port)
{
this.Connect(host, port, null, null, null);
}
public void Connect(string host, int port, string nickname)
{
this.Connect(host, port, null, null, nickname);
}
public void Connect(string host, int port, string username, string password, string nickname)
{
hostnameParts = new string[host.Split('.').Length];
hostnameParts = host.Split('.');
this.host = host;
this.port = port;
this.user = username;
this.password = password;
this.nick = nickname;
this.thisServer = new Server();
this.thisServer.Connect(this.host, this.port, this.nick, this.user, this.password);
this.connection = new TcpClient(this.host, this.port);
this.SystemMessage("Connected To: " + this.host);
if (this.serverConnect != null) { this.serverConnect(hostnameParts[1]); }
this.reader = new StreamReader(this.connection.GetStream());
this.writer = new StreamWriter(this.connection.GetStream());
this.authenticate("TaftseTest","taftse", "taftse taftse");
this.SendCommand("JOIN", "#codeigniter");
this.Listen();
}
public void authenticate(string nickName)
{
this.authenticate(nickName, this.user, this.password);
}
public void authenticate(string nickName ,string username)
{
this.authenticate(nickName,username,this.password);
}
public void authenticate(string nickName,string username,string password)
{
if (password != null)
{
this.SendCommand("PASS",password,false);
this.password = password;
}
this.SendCommand("NICK", nickName);
this.nick = nickName;
string isVisable = this.IsInvisble? "8" :"0";
if (username != null)
{
this.SendCommand("USER",username + " " + isVisable + " " + this.realname);
this.user = username;
}
}
private void Listen()
{
while (true)
{
string command;
while ((command = this.reader.ReadLine()) != null)
{
if (eventReceiving != null)
{
this.eventReceiving(command);
}
string[] commandParts = new string[command.Split(' ').Length];
commandParts = command.Split(' ');
if (commandParts[0].Substring(0, 1) == ":")
{
commandParts[0] = commandParts[0].Remove(0, 1);
}
string[] commandhostParts = new string[commandParts[0].Split('.').Length];
commandhostParts = commandParts[0].Split('.');
if ((commandParts[0] == this.host) ||(commandhostParts.Length > 1 &&commandhostParts[1] == this.hostnameParts[1] && commandhostParts[2] == this.hostnameParts[2]))
{
switch (commandParts[1])
{
case "332": break;
case "353": this.setNamesList(commandParts); break;
case "451": authenticate(this.nick); break;
default: this.ServerMessage(commandParts); break;
}
}
else if (commandParts[0] == "PING")
{
this.Ping(commandParts);
}
else
{
string commandAction = commandParts[1];
switch (commandAction)
{
case "JOIN": break;
case "PART": break;
case "MODE": break;
case "NICK": break;
case "KICK": break;
case "QUIT": break;
}
}
}
this.writer.Close();
this.reader.Close();
this.connection.Close();
}
}
private void Ping(string[] command)
{
string PingHash = "";
for (int intI = 1; intI < command.Length; intI++)
{
PingHash += command[intI] + " ";
}
this.SendCommand("PONG",PingHash);
}
public void SendCommand(string Command, string value)
{
this.SendCommand(Command, value, true);
}
public void SendCommand(string Command, string value,bool print)
{
this.SendCommand(Command, value, null,print);
}
public void SendCommand(string Command, string value,string channel)
{
this.SendCommand(Command, value, channel, true);
}
public void SendCommand(string Command,string value,string channel,bool print)
{
if (channel != null)
{
if (print == true)
{
this.eventSent(Command + " " + channel + " " + value);
}
this.writer.WriteLine(Command + " " + channel + " " + value);
}
else
{
if (print == true)
{
this.eventSent(Command + " " + value);
}
this.writer.WriteLine(Command + " " + value);
}
this.writer.Flush();
}
private void SystemMessage(string message)
{
this.eventSystemMessage(message);
}
private void ServerMessage(string[] command)
{
string ServerMessage = "";
for (int intI = 1; intI < command.Length; intI++)
{
ServerMessage += command[intI] + " ";
}
if (eventServerMessage != null) { this.eventServerMessage(ServerMessage.Trim()); }
}
public void ChangeNick(string newNick)
{
if (this.nick != newNick)
{
SendCommand("NICK", newNick);
this.nick = newNick;
}
}
public void setNamesList(string[]commandParts)
{
}
/*public void getAllChannels()
{
}
public void JoinChannel(string channelName)
{
this.writer.WriteLine("JOIN "+channelName);
this.writer.Flush();
this.channels.
}*/
/*}*/
}