-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSharpClient.cs
376 lines (318 loc) · 10.6 KB
/
SharpClient.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using SharpIMClient.Collections;
namespace SharpIMClient
{
public class SharpClient
{
private readonly BuddyCollection _buddyList;
private bool _isConnected;
private NetworkStream _ns;
private string _password;
private StreamReader _sr;
private SslStream _ssl;
private StreamWriter _sw;
private TcpClient _tcp;
private string _username;
public SharpClient()
{
_buddyList = new BuddyCollection();
}
public string ScreenName
{
get { return _username; }
}
public string Password
{
get { return _password; }
}
public BuddyCollection Buddies
{
get { return _buddyList; }
}
public bool IsConnected
{
get { return _isConnected; }
}
public event UserMessageEventHandler OnUserMessage;
public event ServerMessageEventHandler OnServerMessage;
public event UserStatusEventHandler OnUserStatus;
public event BuddyListReceivedEventHandler OnBuddyListReceived;
public event GroupMessageEventHandler OnGroupMessage;
public event UserPartedGroupEventHandler OnUserPartedGroup;
public event UserJoinedGroupEventHandler OnUserJoinedGroup;
public event UserKickedUserEventHandler OnUserKickedUser;
public event UserBannedUserEventHandler OnUserBannedUser;
public void SendUserMessage(string user, string message)
{
_sw.WriteLine("MSG {0} {1}", user, message);
_sw.Flush();
}
public void SendGroupMessage(string group, string message)
{
_sw.WriteLine("MSG #{0} {1}", group, message);
_sw.Flush();
}
public void JoinGroup(string group)
{
_sw.WriteLine("JOIN {0}", group);
_sw.Flush();
}
public void PartGroup(string group)
{
_sw.WriteLine("PART {0}", group);
_sw.Flush();
}
public void GetBuddy(string user)
{
while (!_buddyList.Contains(user))
{
GetBuddySignOnTime(user);
GetBuddyStatus(user);
}
}
private void GetBuddyStatus(string user)
{
_sw.WriteLine("GET STATUS {0}", user);
_sw.Flush();
}
private void GetBuddySignOnTime(string user)
{
_sw.WriteLine("GET SIGNONTIME {0}", user);
_sw.Flush();
}
public void SetEmail(string email)
{
_sw.WriteLine("SET EMAIL {0}", email);
_sw.Flush();
}
public void GetBuddyList()
{
_sw.WriteLine("GET BUDDYLIST");
_sw.Flush();
}
public void AddBuddy(string buddy)
{
_sw.WriteLine("ADD BUDDY {0}", buddy);
_sw.Flush();
_buddyList.Add(new BuddyInfo(buddy));
GetBuddy(buddy);
}
public void RemoveBuddy(string buddy)
{
_sw.WriteLine("REMOVE BUDDY {0}", buddy);
_sw.Flush();
Buddies.Remove(_buddyList.GetBuddy(buddy));
}
public void GlobalMessage(string message)
{
_sw.WriteLine("GLOBALMSG {0}", message);
_sw.Flush();
}
private void UpdateBuddyStatus(string name, BuddyStatus status)
{
BuddyInfo budd = _buddyList.GetBuddy(name);
budd.Status = status;
}
private void UpdateBuddySignOnTime(string name, DateTime time)
{
BuddyInfo budd = _buddyList.GetBuddy(name);
budd.SignOnTime = time;
}
private void ReaderThread()
{
while (true)
{
string msg = _sr.ReadLine();
string[] msgarray = msg.Split(' ');
if (msgarray[0] == "MSG" && msgarray.Length >= 4)
{
if (msgarray[1].ToUpper() == "USER")
{
if (OnUserMessage != null)
OnUserMessage(this, msgarray[2], Utility.GetRestOfMessage(msgarray, 3));
}
else if (msgarray[1].ToUpper() == "GROUP")
{
if (OnGroupMessage != null)
OnGroupMessage(this, msgarray[2], msgarray[3], Utility.GetRestOfMessage(msgarray, 4));
}
}
else if (msgarray[0] == "JOINED")
{
if (OnUserJoinedGroup != null) OnUserJoinedGroup(this, msgarray[1], msgarray[2]);
}
else if (msgarray[0] == "PARTED")
{
if (OnUserPartedGroup != null) OnUserPartedGroup(this, msgarray[1], msgarray[2]);
}
else if (msgarray[0] == "KICKED")
{
if (OnUserKickedUser != null)
OnUserKickedUser(this, msgarray[2], msgarray[3], msgarray[1],
Utility.GetRestOfMessage(msgarray, 4));
}
else if (msgarray[0] == "BANNED")
{
if (OnUserBannedUser != null)
OnUserBannedUser(this, msgarray[2], msgarray[3], msgarray[1],
Utility.GetRestOfMessage(msgarray, 4));
}
else if (msgarray[0] == "TIME")
{
if (msgarray[1].ToUpper() == "USER")
{
DateTime time = DateTime.FromBinary(long.Parse(msgarray[3]));
UpdateBuddySignOnTime(msgarray[2], time);
}
}
else if (msgarray[0] == "STATUS")
{
if (msgarray[2].ToUpper() == "ONLINE")
{
UpdateBuddyStatus(msgarray[1], BuddyStatus.Online);
if (OnUserStatus != null) OnUserStatus(this, msgarray[1], BuddyStatus.Online);
}
else if (msgarray[2].ToUpper() == "AWAY")
{
UpdateBuddyStatus(msgarray[1], BuddyStatus.Away);
if (OnUserStatus != null) OnUserStatus(this, msgarray[1], BuddyStatus.Away);
}
else if (msgarray[2].ToUpper() == "OFFLINE")
{
UpdateBuddyStatus(msgarray[1], BuddyStatus.Offline);
if (OnUserStatus != null) OnUserStatus(this, msgarray[1], BuddyStatus.Offline);
}
}
else if (msgarray[0].ToUpper() == "BUDDYLIST")
{
string[] buddys = msgarray[1].Split(';');
foreach (string x in buddys)
{
if (x != string.Empty)
{
_buddyList.Add(new BuddyInfo(x));
GetBuddy(x);
}
}
Thread.Sleep(50*buddys.Length);
if (OnBuddyListReceived != null) OnBuddyListReceived(this);
}
else
{
if (OnServerMessage != null) OnServerMessage(this, msg);
}
}
}
public void RawMessage(string message)
{
_sw.WriteLine(message);
_sw.Flush();
}
public bool Register(string user, string pass)
{
_sw.WriteLine("REGISTER {0} {1}", user, pass);
_sw.Flush();
string resp = _sr.ReadLine();
if (resp.EndsWith("FAIL"))
{
return false;
}
else
{
_username = user;
_password = pass;
new Thread(ReaderThread).Start();
return true;
}
}
public bool Register(string user, byte[] passhash)
{
string pass = Encoding.ASCII.GetString(passhash);
_sw.WriteLine("REGISTER {0} {1}", user, pass);
_sw.Flush();
string resp = _sr.ReadLine();
if (resp.EndsWith("FAIL"))
{
return false;
}
else
{
_username = user;
_password = pass;
new Thread(ReaderThread).Start();
return true;
}
}
public void Connect(string host, int port)
{
_tcp = new TcpClient(host, port);
_ns = _tcp.GetStream();
_ssl = new SslStream(_ns);
_ssl.AuthenticateAsClient(host);
if (_ssl.IsAuthenticated)
{
_sw = new StreamWriter(_ssl);
_sr = new StreamReader(_ssl);
_isConnected = true;
}
}
public bool Login(string user, byte[] passhash)
{
string pass = Encoding.ASCII.GetString(passhash);
_sw.WriteLine("LOGIN {0} {1}", user, pass);
_sw.Flush();
string resp = _sr.ReadLine();
if (!resp.StartsWith("405"))
{
_username = user;
_password = pass;
new Thread(ReaderThread).Start();
return true;
}
else
{
return false;
}
}
public bool Login(string user, string pass)
{
_sw.WriteLine("LOGIN {0} {1}", user, pass);
_sw.Flush();
string resp = _sr.ReadLine();
if (!resp.StartsWith("405"))
{
_username = user;
_password = pass;
new Thread(ReaderThread).Start();
return true;
}
else
{
return false;
}
}
public void SaveData()
{
_sw.WriteLine("SAVE");
_sw.Flush();
}
public void Disconnect()
{
if (_isConnected)
{
_sw.WriteLine("QUIT");
_sw.Flush();
_sr.Close();
_sw.Close();
_ns.Close();
_tcp.Close();
_isConnected = false;
}
}
}
}