-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.cs
529 lines (502 loc) · 15 KB
/
User.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
527
528
529
// 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;
using System.Text;
namespace libirc
{
/// <summary>
/// User, Every user on irc has instance of this class for every channel they are in
/// </summary>
[Serializable]
public class User : Target, IComparable
{
/// <summary>
/// Host name
/// </summary>
public string Host = null;
/// <summary>
/// Network this user belongs to
/// </summary>
[NonSerialized]
public Network _Network = null;
/// <summary>
/// Identifier
/// </summary>
public string Ident = null;
/// <summary>
/// Channel mode (it could be +o as well as +ov so if you want to retrieve current channel status of user, look for highest level)
/// </summary>
public NetworkMode ChannelMode = new NetworkMode();
/// <summary>
/// Status
/// </summary>
public ChannelStatus Status = ChannelStatus.Regular;
protected string nick = null;
protected string lnick = null;
public override string TargetName
{
get
{
return this.nick;
}
}
/// <summary>
/// Nick
/// </summary>
public virtual string Nick
{
set
{
if (this.Nick != value)
{
if (this.Channel != null)
{
// rename the key in dictionary
this.Channel.RemoveUser(this);
// store again this exactly same user
this.lnick = value.ToLower();
this.nick = value;
this.Channel.InsertUser(this);
return;
}
this.lnick = value.ToLower();
this.nick = value;
}
}
get
{
return nick;
}
}
/// <summary>
/// Return a lowercase nickname of this user, this function is likely faster than string.ToLower() because it uses caching so if you need to get lowercase
/// nick very often, you should use this
/// </summary>
public virtual string LowerNick
{
get
{
if (this.lnick == null)
{
this.lnick = this.Nick.ToLower();
}
return this.lnick;
}
}
[Obsolete ("Renamed to LowerNick")]
public virtual string LowNick
{
get
{
return this.LowerNick;
}
}
/// <summary>
/// Name
/// </summary>
public string RealName = null;
/// <summary>
/// Server
/// </summary>
public string Server = null;
/// <summary>
/// Away message
/// </summary>
public string AwayMessage = null;
/// <summary>
/// User away
/// </summary>
public bool Away = false;
/// <summary>
/// Check
/// </summary>
public DateTime LastAwayCheck;
/// <summary>
/// Primary chan
/// </summary>
public Channel Channel = null;
/// <summary>
/// Return true if user is owner of a channel
/// </summary>
public virtual bool IsOwner
{
get
{
if (ChannelMode._Mode.Contains("q"))
{
return true;
}
return false;
}
}
/// <summary>
/// Return true if user is admin of a channel
/// </summary>
public virtual bool IsAdmin
{
get
{
if (ChannelMode._Mode.Contains("a"))
{
return true;
}
return false;
}
}
/// <summary>
/// Return true if user is op of a channel
/// </summary>
public virtual bool IsOp
{
get
{
if (ChannelMode._Mode.Contains("o"))
{
return true;
}
return false;
}
}
/// <summary>
/// Return true if user is half_operator of a channel
/// </summary>
public virtual bool IsHalfop
{
get
{
if (ChannelMode._Mode.Contains("h"))
{
return true;
}
return false;
}
}
/// <summary>
/// Return true if user is voiced in this channel
/// </summary>
public virtual bool IsVoiced
{
get
{
if (ChannelMode._Mode.Contains("v"))
{
return true;
}
return false;
}
}
/// <summary>
/// Away time
/// </summary>
public DateTime AwayTime;
private bool destroyed = false;
/// <summary>
/// This will return true in case object was requested to be disposed
/// you should never work with objects that return true here
/// </summary>
public virtual bool IsDestroyed
{
get
{
return destroyed;
}
}
/// <summary>
/// This return true if we are looking at current user
/// </summary>
public virtual bool IsPidgeon
{
get
{
return (Nick.ToLower() == _Network.Nickname.ToLower());
}
}
/// <summary>
/// Get a list of all channels this user is in
/// </summary>
public virtual List<Channel> ChannelList
{
get
{
List<Channel> List = new List<Channel>();
if (_Network == null)
{
return null;
}
lock (_Network.Channels)
{
foreach (Channel xx in _Network.Channels.Values)
{
if (xx.ContainsUser(Nick))
{
List.Add(xx);
}
}
}
return List;
}
}
protected char ChannelSymbol = '\0';
/// <summary>
/// This is a symbol that user has before his name in a channel (for example voiced user would have + on most networks)
/// </summary>
public virtual char ChannelPrefix_Char
{
get
{
if (ChannelSymbol != '\0')
{
return ChannelSymbol;
}
if (ChannelMode._Mode.Count > 0)
{
int i = 100;
char c = '0';
lock (_Network.CUModes)
{
foreach (char mode in _Network.CUModes)
{
if (ChannelMode._Mode.Contains(mode.ToString()))
{
if (_Network.CUModes.IndexOf(mode) < i)
{
i = _Network.CUModes.IndexOf(mode);
c = mode;
}
}
}
}
if (c != '0')
{
ChannelSymbol = _Network.UChars[i];
return ChannelSymbol;
}
}
return '\0';
}
set
{
if (_Network.UChars.Contains(value))
{
SymbolMode(value);
this.ChannelSymbol = value;
}
}
}
/// <summary>
/// This is a symbol that user has before his name in a channel (for example voiced user would have + on most networks)
/// </summary>
public virtual string ChannelPrefix
{
get
{
char symbol = ChannelPrefix_Char;
if (symbol == '\0')
{
return "";
}
return symbol.ToString();
}
}
/// <summary>
/// Creates a new user
/// </summary>
/// <param name="user">user!ident@hostname</param>
/// <param name="network">Network this class belongs to</param>
public User(string source, Network network)
{
UserInfo info = new UserInfo(source);
MakeUser(info.Nick, info.Host, network, info.Ident);
}
public User(UserInfo info, Network network)
{
MakeUser(info.Nick, info.Host, network, info.Ident);
}
/// <summary>
/// Creates a new user
/// </summary>
/// <param name="nick"></param>
/// <param name="host"></param>
/// <param name="network"></param>
/// <param name="ident"></param>
public User(string nick, string host, string ident, Network network)
{
if (network == null)
{
throw new Exception("Network can't be null in here");
}
MakeUser(nick, host, network, ident);
}
/// <summary>
/// Creates a new user
/// </summary>
/// <param name="nick">Nick</param>
/// <param name="host">Host</param>
/// <param name="network">Network</param>
/// <param name="ident">Ident</param>
/// <param name="server">Server</param>
/// <param name="channel">Channel</param>
public User(string nick, string host, string ident, Network network, Channel channel)
{
this.Channel = channel;
MakeUser(nick, host, network, ident);
}
public User(string source)
{
if (source.Contains("!"))
{
this.nick = source.Substring(0, source.IndexOf("!", StringComparison.Ordinal));
this.Ident = source.Substring(source.IndexOf("!") + 1);
if (this.Ident.Contains("@"))
{
this.Host = this.Ident.Substring(this.Ident.IndexOf("@") + 1);
this.Ident = this.Ident.Substring(this.Ident.IndexOf("@"));
}
}
else
{
this.nick = source;
}
}
/// <summary>
/// Reset the user mode back to none
/// </summary>
public virtual void ResetMode()
{
ChannelSymbol = '\0';
}
/// <summary>
/// Change a user level according to symbol
/// </summary>
/// <param name="symbol"></param>
public virtual void SymbolMode(char symbol)
{
if (_Network == null)
{
return;
}
if (symbol == '\0')
{
return;
}
if (_Network.UChars.Contains(symbol))
{
char mode = _Network.CUModes[_Network.UChars.IndexOf(symbol)];
ChannelMode.ChangeMode("+" + mode.ToString());
ResetMode();
}
}
protected virtual void MakeUser(string nick, string host, Network network, string ident)
{
_Network = network;
if (!string.IsNullOrEmpty(nick))
{
char prefix = nick[0];
if (network.UChars.Contains(prefix))
{
SymbolMode(prefix);
nick = nick.Substring(1);
}
}
this.nick = nick;
this.Ident = ident;
this.Host = host;
this.Server = network.ServerName;
}
/// <summary>
/// Destroy
/// </summary>
public virtual void Destroy()
{
if (IsDestroyed)
{
return;
}
Channel = null;
_Network = null;
destroyed = true;
}
/// <summary>
/// Converts a user object to string
/// </summary>
/// <returns>[nick!ident@host]</returns>
public override string ToString()
{
return Nick + "!" + Ident + "@" + Host;
}
/// <summary>
/// Generate full string
/// </summary>
/// <returns></returns>
public virtual string ConvertToInfoString()
{
if (!string.IsNullOrEmpty(RealName))
{
return RealName + "\n" + ToString();
}
return ToString();
}
/// <summary>
/// Returns a new instance of UserInfo that is filled up
/// </summary>
/// <returns>The user info.</returns>
public virtual UserInfo ToUserInfo()
{
return new UserInfo(this.Nick, this.Ident, this.Host);
}
/// <summary>
/// Internal function
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public virtual int CompareTo(object obj)
{
if (obj is User)
{
return this.Nick.CompareTo((obj as User).Nick);
}
return 0;
}
/// <summary>
/// Channel status
/// </summary>
public enum ChannelStatus
{
/// <summary>
/// Owner
/// </summary>
Owner,
/// <summary>
/// Admin
/// </summary>
Admin,
/// <summary>
/// Operator
/// </summary>
Op,
/// <summary>
/// Halfop
/// </summary>
Halfop,
/// <summary>
/// Voice
/// </summary>
Voice,
/// <summary>
/// Normal user
/// </summary>
Regular,
}
}
}