-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.cs
526 lines (496 loc) · 15 KB
/
Channel.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) version 3.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
using System;
using System.Collections.Generic;
namespace libirc
{
/// <summary>
/// This is a global interface for channel modes with parameters
/// </summary>
[Serializable]
public class ChannelParameterMode
{
/// <summary>
/// Target of a mode
/// </summary>
public string Target = null;
/// <summary>
/// Time when it was set
/// </summary>
public string Time = null;
/// <summary>
/// User who set the ban / invite
/// </summary>
public string User = null;
}
/// <summary>
/// Invite
/// </summary>
[Serializable]
public class ChannelInvite : ChannelParameterMode
{
/// <summary>
/// Creates a new instance of invite
/// </summary>
public ChannelInvite()
{
// This empty constructor is here so that we can serialize this
}
/// <summary>
/// Creates a new instance of invite
/// </summary>
/// <param name="user">User</param>
/// <param name="target">Target</param>
/// <param name="time">Time</param>
public ChannelInvite(string user, string target, string time)
{
User = user;
Target = target;
Time = time;
}
}
/// <summary>
/// Exception
/// </summary>
[Serializable]
public class ChannelBanException : ChannelParameterMode
{
/// <summary>
/// Creates a new instance of channel ban exception (xml constructor only)
/// </summary>
public ChannelBanException()
{
// This empty constructor is here so that we can serialize this
}
}
/// <summary>
/// Simplest ban
/// </summary>
[Serializable]
public class ChannelBan : ChannelParameterMode
{
/// <summary>
/// Creates a new instance of simple ban (xml constructor only)
/// </summary>
public ChannelBan()
{
// This empty constructor is here so that we can serialize this
}
/// <summary>
/// Creates a new instance of simple ban
/// </summary>
/// <param name="user">Person who set a ban</param>
/// <param name="target">Who is target</param>
/// <param name="time">Unix date when it was set</param>
public ChannelBan(string user, string target, string time)
{
Target = target;
User = user;
Time = time;
}
}
/// <summary>
/// Channel object
/// </summary>
[Serializable]
public class Channel : Target
{
/// <summary>
/// Name of a channel including the special prefix, if it's unknown this variable is null
/// </summary>
public string Name
{
set
{
this.lname = null;
this.name = value;
}
get
{
return this.name;
}
}
private string name = null;
public override string TargetName
{
get
{
return this.Name;
}
}
/// <summary>
/// Lower case of this channel that is used frequently, we cache it here so that we
/// don't need to use expensive string functions to make it so often
/// </summary>
[Obsolete ("Use LowerName instead of this")]
public string lName = null;
private string lname = null;
public string LowerName
{
get
{
if (this.Name == null)
return null;
if (this.lname == null)
lname = this.Name.ToLower();
return lname;
}
}
/// <summary>
/// Network the channel belongs to
/// </summary>
[NonSerialized]
public Network _Network = null;
/// <summary>
/// List of all users in current channel
/// </summary>
[NonSerialized]
protected Dictionary<string, User> UserList = new Dictionary<string, User>();
/// <summary>
/// Topic, if it's unknown this variable is null
/// </summary>
public string Topic = null;
/// <summary>
/// User who set a topic
/// </summary>
public string TopicUser = "<Unknown user>";
/// <summary>
/// Date when a topic was set
/// </summary>
public int TopicDate = 0;
public string Password = null;
/// <summary>
/// Invites
/// </summary>
public List<ChannelInvite> Invites = null;
/// <summary>
/// List of bans set
/// </summary>
public List<ChannelBan> Bans = null;
/// <summary>
/// Exception list
/// </summary>
public List<ChannelBanException> Exceptions = null;
public DateTime CreationTime;
public string Website = null;
/// <summary>
/// If channel output is temporarily hidden
/// </summary>
public bool TemporarilyHidden = false;
/// <summary>
/// If true the channel is processing the /who data
/// </summary>
public bool IsParsingWhoData = false;
/// <summary>
/// If true the channel is processing invites
/// </summary>
public bool IsParsingInviteData = false;
/// <summary>
/// If true the channel is processing ban data
/// </summary>
public bool IsParsingBanData = false;
/// <summary>
/// If true the channel is processing exception data
/// </summary>
public bool IsParsingExceptionData = false;
/// <summary>
/// If true the channel is processing whois data
/// </summary>
public bool IsParsingWhoisData = false;
/// <summary>
/// Channel mode
/// </summary>
[NonSerialized]
public NetworkMode ChannelMode = null;
/// <summary>
/// If true the window is considered usable, in case it's false, the window is flagged as parted channel
/// </summary>
public bool ChannelWork = false;
/// <summary>
/// Whether part from this channel was requested, this is used to detect if part was forced or not
/// </summary>
public bool PartRequested = false;
[Obsolete ("no longer used")]
public bool dispose = false;
/// <summary>
/// If this is false the channel is not being used / you aren't in it or you can't access it
/// </summary>
public virtual bool IsAlive
{
get
{
if (!ChannelWork)
{
return false;
}
if (_Network != null)
{
return _Network.IsConnected;
}
return false;
}
}
public virtual int UserCount
{
get
{
return this.UserList.Count;
}
}
/// <summary>
/// Constructor (simple)
/// </summary>
public Channel()
{
ChannelWork = true;
ChannelMode = new NetworkMode(NetworkMode.ModeType.Channel, _Network);
Topic = "";
TopicUser = "";
}
/// <summary>
/// Constructor (normal)
/// </summary>
public Channel(Network network)
{
_Network = network;
ChannelWork = true;
ChannelMode = new NetworkMode(NetworkMode.ModeType.Channel, _Network);
Topic = "";
TopicUser = "";
}
/// <summary>
/// Renew bans
/// </summary>
public void ReloadBans()
{
IsParsingBanData = true;
if (Bans == null)
{
Bans = new List<ChannelBan>();
}
else
{
lock (Bans)
{
Bans.Clear();
}
}
_Network.Transfer("MODE " + Name + " +b");
}
public void ReloadInvites()
{
IsParsingExceptionData = true;
if (Invites == null)
{
Invites = new List<ChannelInvite>();
}
else
{
lock (Invites)
{
Invites.Clear();
}
}
_Network.Transfer("MODE " + Name + " +I");
}
public void ReloadExceptions()
{
IsParsingExceptionData = true;
if (Exceptions == null)
{
Exceptions = new List<ChannelBanException>();
}
else
{
lock (Exceptions)
{
Exceptions.Clear();
}
}
_Network.Transfer("MODE " + Name + " +e");
}
/// <summary>
/// Return true if channel contains the given user name
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public bool ContainsUser(string user)
{
if (UserFromName(user) != null)
{
return true;
}
return false;
}
/// <summary>
/// Part this channel
/// </summary>
public virtual void Part()
{
if (IsAlive && _Network != null)
{
_Network.Part(this);
PartRequested = true;
}
}
public virtual void ClearUsers()
{
this.UserList.Clear();
}
/// <summary>
/// Return true if a channel is matching ban (exact, not a mask)
/// </summary>
/// <param name="host"></param>
/// <returns></returns>
public virtual bool ContainsBan(string host)
{
if (Bans == null)
{
return false;
}
lock (Bans)
{
foreach (ChannelBan name in Bans)
{
if (name.Target == host)
{
return true;
}
}
}
return false;
}
public virtual Dictionary<string, User> RetrieveUL()
{
lock (this.UserList)
{
return new Dictionary<string, User>(this.UserList);
}
}
/// <summary>
/// This function returns a special user mode for a user that should be in user list (for example % for halfop or @ for operator)
/// </summary>
/// <param name="nick"></param>
/// <returns></returns>
protected static string uchr(User nick)
{
return nick.ChannelPrefix;
}
public virtual void InsertUser(User user)
{
lock (this.UserList)
{
// don't use LowNick directly it can change between these 2 instructions
string ln = user.LowerNick;
user.Channel = this;
if (!this.UserList.ContainsKey(ln))
{
this.UserList.Add(ln, user);
}
}
}
/// <summary>
/// Insert ban to a ban list, this will not set a ban to channel, this will only set it into memory of pidgeon
/// </summary>
/// <param name="ban">Host</param>
/// <param name="user">User who set</param>
/// <param name="time">Time when it was set</param>
public virtual void InsertBan(string ban, string user, string time = "0")
{
if (Bans == null)
{
Bans = new List<ChannelBan>();
}
ChannelBan br = new ChannelBan(user, ban, time);
lock (Bans)
{
Bans.Add(br);
}
}
/// <summary>
/// Removes a ban where target is matching "ban" this needs to be perfect match (a != A and x* != xX) you can't use mask
/// </summary>
/// <param name="ban"></param>
/// <returns></returns>
public virtual bool RemoveBan(string ban)
{
if (this.Bans == null)
{
return false;
}
ChannelBan br = null;
lock (Bans)
{
foreach (ChannelBan xx in Bans)
{
if (xx.Target == ban)
{
br = xx;
break;
}
}
if (br != null)
{
Bans.Remove(br);
return true;
}
}
return false;
}
public virtual void RemoveUser(User user)
{
lock (this.UserList)
{
string name = user.Nick.ToLower ();
if (this.UserList.ContainsKey (name))
{
this.UserList.Remove (name);
}
}
}
public virtual void RemoveUser(string nick)
{
nick = nick.ToLower();
lock (this.UserList)
{
if (this.UserList.ContainsKey(nick))
{
this.UserList.Remove(nick);
}
}
}
public virtual User GetSelf()
{
if (this._Network != null)
{
return this.UserFromName(this._Network.Nickname);
}
return null;
}
/// <summary>
/// Return user object if specified user exist
/// </summary>
/// <param name="name">User name</param>
/// <returns></returns>
public virtual User UserFromName(string name)
{
if (name == null)
{
throw new Exception("User name can't be null");
}
User user = null;
this.UserList.TryGetValue (name.ToLower (System.Globalization.CultureInfo.CurrentUICulture), out user);
return user;
}
}
}