forked from smiley22/S22.Xmpp
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Jid.cs
205 lines (191 loc) · 6.92 KB
/
Jid.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
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace Sharp.Xmpp
{
/// <summary>
/// Represents the address of an XMPP entity, also known as Jabber Identifier (JID).
/// </summary>
[Serializable]
public sealed class Jid
{
/// <summary>
/// The domain identifier of the JID.
/// </summary>
public string Domain
{
get;
private set;
}
/// <summary>
/// The node identifier of the JID. This may be null or empty.
/// </summary>
public string Node
{
get;
private set;
}
/// <summary>
/// The resource identifier of the JID. This may be null or empty.
/// </summary>
public string Resource
{
get;
private set;
}
/// <summary>
/// Determines whether the JID is a 'bare JID', i.e. a JID without resource
/// identifier.
/// </summary>
public bool IsBareJid
{
get
{
return !String.IsNullOrEmpty(Node) &&
!String.IsNullOrEmpty(Domain) && String.IsNullOrEmpty(Resource);
}
}
/// <summary>
/// Determines whether the JID is a 'full JID', i.e. a JID with both a node
/// and a resource identifier.
/// </summary>
public bool IsFullJid
{
get
{
return !String.IsNullOrEmpty(Node) &&
!String.IsNullOrEmpty(Domain) && !String.IsNullOrEmpty(Resource);
}
}
/// <summary>
/// Initializes a new instance of the JID class.
/// </summary>
/// <param name="jid">A string from which to construct the JID.</param>
/// <exception cref="ArgumentNullException">The jid parameter is
/// null.</exception>
/// <exception cref="ArgumentException">The jid parameter does not
/// represent a valid JID.</exception>
public Jid(string jid)
{
jid.ThrowIfNullOrEmpty("jid");
Match m = Regex.Match(jid,
"(?:(?<node>[^@]+)@)?(?<domain>[^/]+)(?:/(?<resource>.+))?");
if (!m.Success)
throw new ArgumentException("The argument is not a valid JID.");
Domain = m.Groups["domain"].Value;
Node = m.Groups["node"].Value;
if (Node == String.Empty)
Node = null;
Resource = m.Groups["resource"].Value;
if (Resource == String.Empty)
Resource = null;
}
/// <summary>
/// Initializes a new instance of the JID class using the specified domain,
/// node and optionally resource.
/// </summary>
/// <param name="domain">The domain of the JID.</param>
/// <param name="node">The node of the JID.</param>
/// <param name="resource">The resource of the JID. This may be omitted.</param>
/// <exception cref="ArgumentNullException">The domain parameter is null.</exception>
/// <exception cref="ArgumentException">The domain parameter is the
/// empty string.</exception>
public Jid(string domain, string node, string resource = null)
{
domain.ThrowIfNullOrEmpty("domain");
Domain = domain;
Node = node;
Resource = resource;
}
/// <summary>
/// Implicit conversion operator for type string to type Jid.
/// </summary>
/// <param name="jid">The string to convert into a Jid instance.</param>
/// <returns>A Jid instance created from the specified string.</returns>
public static implicit operator Jid(string jid)
{
return new Jid(jid);
}
/// <summary>
/// Returns a new JID instance representing the 'bare JID' constructd from
/// this JID.
/// </summary>
/// <returns>A bare JID constructed from this JID instance.</returns>
public Jid GetBareJid()
{
return new Jid(Domain, Node);
}
/// <summary>
/// Returns a textual representation of the JID.
/// </summary>
/// <returns>A textual representation of this JID instance.</returns>
public override string ToString()
{
StringBuilder b = new StringBuilder();
if (!String.IsNullOrEmpty(Node))
b.Append(Node + "@");
b.Append(Domain);
if (!String.IsNullOrEmpty(Resource))
b.Append("/" + Resource);
return b.ToString();
}
/// <summary>
/// Determines whether the specified object is equal to this Jid
/// instance.
/// </summary>
/// <param name="obj">The object to compare with the current object.</param>
/// <returns>True if the specified object is semantically equal to this
/// Jid instance; Otherwise false.</returns>
public override bool Equals(object obj)
{
if (obj == null)
return false;
Jid other = obj as Jid;
if (other == null)
return false;
return Node == other.Node && Domain == other.Domain &&
Resource == other.Resource;
}
/// <summary>
/// Returns the hash code of this instance.
/// </summary>
/// <returns>The hash code of this JID instance.</returns>
public override int GetHashCode()
{
int hash = 13;
if (Node != null)
hash = (hash * 7) + Node.GetHashCode();
hash = (hash * 7) + Domain.GetHashCode();
if (Resource != null)
hash = (hash * 7) + Resource.GetHashCode();
return hash;
}
/// <summary>
/// Determines whether the specified Jid objects are equal.
/// </summary>
/// <param name="a">The first object.</param>
/// <param name="b">The second object.</param>
/// <returns>True if the specified objects are semantically equal;
/// Otherwise false.</returns>
public static bool operator ==(Jid a, Jid b)
{
if (System.Object.ReferenceEquals(a, b))
return true;
if (((object)a == null) || ((object)b == null))
return false;
return a.Node == b.Node && a.Domain == b.Domain &&
a.Resource == b.Resource;
}
/// <summary>
/// Determines whether the specified Jid objects are unequal.
/// </summary>
/// <param name="a">The first object.</param>
/// <param name="b">The second object.</param>
/// <returns>True if the specified objects are not semantically equal;
/// Otherwise false.</returns>
public static bool operator !=(Jid a, Jid b)
{
return !(a == b);
}
}
}