-
Notifications
You must be signed in to change notification settings - Fork 0
/
IrcUser.cs
197 lines (183 loc) · 5.9 KB
/
IrcUser.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
using System;
using System.Linq;
using System.Collections.Generic;
namespace ChatSharp
{
/// <summary>
/// A user connected to IRC.
/// </summary>
public class IrcUser : IEquatable<IrcUser>
{
internal IrcUser()
{
Channels = new ChannelCollection();
ChannelModes = new Dictionary<IrcChannel, char?>();
}
/// <summary>
/// Constructs an IrcUser given a hostmask or nick.
/// </summary>
public IrcUser(string host) : this()
{
if (!host.Contains("@") && !host.Contains("!"))
Nick = host;
else
{
string[] mask = host.Split('@', '!');
Nick = mask[0];
User = mask[1];
if (mask.Length <= 2)
{
Hostname = "";
}
else
{
Hostname = mask[2];
}
}
}
/// <summary>
/// Constructs an IrcUser given a nick and user.
/// </summary>
public IrcUser(string nick, string user) : this()
{
Nick = nick;
User = user;
RealName = User;
Mode = string.Empty;
}
/// <summary>
/// Constructs an IRC user given a nick, user, and password.
/// </summary>
public IrcUser(string nick, string user, string password) : this(nick, user)
{
Password = password;
}
/// <summary>
/// Constructs an IRC user given a nick, user, password, and real name.
/// </summary>
public IrcUser(string nick, string user, string password, string realName) : this(nick, user, password)
{
RealName = realName;
}
/// <summary>
/// The user's nick.
/// </summary>
public string Nick { get; internal set; }
/// <summary>
/// The user's user (an IRC construct, a string that identifies your username).
/// </summary>
public string User { get; internal set; }
/// <summary>
/// The user's password. Will not be set on anyone but your own user.
/// </summary>
public string Password { get; internal set; }
/// <summary>
/// The user's mode.
/// </summary>
/// <value>The mode.</value>
public string Mode { get; internal set; }
/// <summary>
/// The user's real name.
/// </summary>
/// <value>The name of the real.</value>
public string RealName { get; internal set; }
/// <summary>
/// The user's hostname.
/// </summary>
public string Hostname { get; internal set; }
/// <summary>
/// Channels this user is present in. Note that this only includes channels you are
/// also present in, even after a successful WHOIS.
/// </summary>
/// <value>The channels.</value>
public ChannelCollection Channels { get; set; }
internal Dictionary<IrcChannel, char?> ChannelModes { get; set; }
/// <summary>
/// This user's hostmask (nick!user@host).
/// </summary>
public string Hostmask
{
get
{
return Nick + "!" + User + "@" + Hostname;
}
}
/// <summary>
/// Returns true if the user matches the given mask. Can be used to check if a ban applies
/// to this user, for example.
/// </summary>
public bool Match(string mask)
{
if (mask.Contains("!") && mask.Contains("@"))
{
if (mask.Contains('$'))
mask = mask.Remove(mask.IndexOf('$')); // Extra fluff on some networks
var parts = mask.Split('!', '@');
if (Match(parts[0], Nick) && Match(parts[1], User) && Match(parts[2], Hostname))
return true;
}
return false;
}
/// <summary>
/// Checks if the given hostmask matches the given mask.
/// </summary>
public static bool Match(string mask, string value)
{
if (value == null)
value = string.Empty;
int i = 0;
int j = 0;
for (; j < value.Length && i < mask.Length; j++)
{
if (mask[i] == '?')
i++;
else if (mask[i] == '*')
{
i++;
if (i >= mask.Length)
return true;
while (++j < value.Length && value[j] != mask[i]) ;
if (j-- == value.Length)
return false;
}
else
{
if (char.ToUpper(mask[i]) != char.ToUpper(value[j]))
return false;
i++;
}
}
return i == mask.Length && j == value.Length;
}
/// <summary>
/// True if this user is equal to another (compares hostmasks).
/// </summary>
public bool Equals(IrcUser other)
{
return other.Hostmask == Hostmask;
}
/// <summary>
/// True if this user is equal to another (compares hostmasks).
/// </summary>
public override bool Equals(object obj)
{
if (obj is IrcUser)
return Equals((IrcUser)obj);
return false;
}
/// <summary>
/// Returns the hash code of the user's hostmask.
/// </summary>
public override int GetHashCode()
{
return Hostmask.GetHashCode();
}
/// <summary>
/// Returns the user's hostmask.
/// </summary>
public override string ToString()
{
return Hostmask;
}
}
}