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
|
namespace Xmpp {
public class Jid : Object {
public string? localpart {
get {
if (level == Level.BARE) return part;
if (level == Level.FULL && parent.level == Level.BARE) return parent.part;
return null;
}
}
public string domainpart {
get {
if (level == Level.DOMAIN) return part;
return parent.domainpart;
}
}
public string? resourcepart {
get {
if (level == Level.FULL) return part;
return null;
}
}
public Jid bare_jid {
get {
if (level == Level.FULL) return parent;
return this;
}
}
public Jid domain_jid {
get {
if (level == Level.DOMAIN) return this;
return parent.domain_jid;
}
}
private static WeakMap<string, Jid> INTERNED = new WeakMap<string, Jid>();
enum Level {
DOMAIN,
BARE,
FULL
}
private Level level;
private Jid? parent;
private string part;
private string? jid;
private Jid() {}
private Jid.domain(string domainpart) throws InvalidJidError {
this.level = Level.DOMAIN;
this.part = domainpart;
this.jid = this.part;
}
private Jid.bare(string localpart, Jid parent, string jid) throws InvalidJidError {
this.level = Level.BARE;
this.parent = parent;
this.part = localpart;
this.jid = jid;
}
private Jid.full(Jid parent, string resourcepart, string jid) throws InvalidJidError {
this.level = Level.FULL;
this.parent = parent;
this.part = resourcepart;
this.jid = jid;
}
private static Jid get_interned(string jid) {
if (INTERNED == null) new Jid();
return INTERNED.get(jid);
}
private static Jid from_domain(string domainpart) throws InvalidJidError {
if (domainpart.length == 0) throw new InvalidJidError.EMPTY_DOMAIN("Domain is empty");
Jid result = get_interned(domainpart);
if (result != null) return result;
int domain_length = domainpart.length;
if (domainpart[domain_length - 1] == '.') domain_length--;
string prepared_domainpart = prepare(idna_decode(domainpart, domain_length), ICU.PrepType.RFC3491_NAMEPREP);
result = get_interned(prepared_domainpart);
if (result != null) {
INTERNED.set(domainpart, result);
return result;
}
idna_verify(prepared_domainpart);
result = new Jid.domain(prepared_domainpart);
INTERNED.set(prepared_domainpart, result);
INTERNED.set(domainpart, result);
return result;
}
private static Jid from_bare(string localpart, Jid parent) throws InvalidJidError {
if (localpart.length == 0) throw new InvalidJidError.EMPTY_LOCAL("Localpart is empty but non-null");
if (parent.level != Level.DOMAIN) throw new InvalidJidError.UNKNOWN("Parent of bare jid was not a domain");
string prepared_localpart = prepare(localpart, ICU.PrepType.RFC3920_NODEPREP);
string prepared_jid = "%s@%s".printf(prepared_localpart, parent.part);
Jid result = get_interned(prepared_jid);
if (result != null) return result;
result = new Jid.bare(prepared_localpart, parent, prepared_jid);
INTERNED.set(prepared_jid, result);
return result;
}
private static Jid from_full(Jid parent, string resourcepart) throws InvalidJidError {
if (resourcepart.length == 0) throw new InvalidJidError.EMPTY_RESOURCE("Resource is empty but non-null");
if (parent.level > Level.BARE) throw new InvalidJidError.UNKNOWN("Parent of full jid was not a bare jid or domain");
string prepared_resourcepart = prepare(resourcepart, ICU.PrepType.RFC3920_RESOURCEPREP);
string prepared_jid = "%s/%s".printf(parent.jid, prepared_resourcepart);
Jid result = get_interned(prepared_jid);
if (result != null) return result;
result = new Jid.full(parent, prepared_resourcepart, prepared_jid);
INTERNED.set(prepared_jid, result);
return result;
}
public static Jid from_string(string jid) throws InvalidJidError {
Jid result = get_interned(jid);
if (result != null) return result;
int slash_index = jid.index_of("/");
int at_index = jid.index_of("@");
if (at_index > slash_index && slash_index != -1) at_index = -1;
string resourcepart = slash_index < 0 ? null : jid.slice(slash_index + 1, jid.length);
string localpart = at_index < 0 ? null : jid.slice(0, at_index);
string domainpart;
if (at_index < 0) {
if (slash_index < 0) {
domainpart = jid;
} else {
domainpart = jid.slice(0, slash_index);
}
} else {
if (slash_index < 0) {
domainpart = jid.slice(at_index + 1, jid.length);
} else {
domainpart = jid.slice(at_index + 1, slash_index);
}
}
result = from_components(localpart, domainpart, resourcepart);
INTERNED.set(jid, result);
return result;
}
public static Jid from_components(string? localpart, string domainpart, string? resourcepart) throws InvalidJidError {
Jid jid = from_domain(domainpart);
if (localpart != null) {
jid = from_bare(localpart, jid);
}
if (resourcepart != null) {
jid = from_full(jid, resourcepart);
}
return jid;
}
private static string idna_decode(string src, int src_length = -1) throws InvalidJidError {
ICU.ErrorCode status = ICU.ErrorCode.ZERO_ERROR;
ICU.IDNAInfo info;
char[] dest = new char[src.length * 2];
ICU.IDNA.openUTS46(ICU.IDNAOptions.DEFAULT, ref status).nameToUnicodeUTF8(src, src_length, dest, out info, ref status);
if (status == ICU.ErrorCode.INVALID_CHAR_FOUND) {
throw new InvalidJidError.INVALID_CHAR("Found invalid character");
} else if (status.is_failure() || info.errors > 0) {
throw new InvalidJidError.UNKNOWN(@"Unknown error: $(status.errorName())");
}
return (string) dest;
}
private static void idna_verify(string src) throws InvalidJidError {
ICU.ErrorCode status = ICU.ErrorCode.ZERO_ERROR;
ICU.IDNAInfo info;
char[] dest = new char[src.length * 2];
ICU.IDNA.openUTS46(ICU.IDNAOptions.DEFAULT, ref status).nameToASCII_UTF8(src, -1, dest, out info, ref status);
if (status == ICU.ErrorCode.INVALID_CHAR_FOUND) {
throw new InvalidJidError.INVALID_CHAR("Found invalid character");
} else if (status.is_failure() || info.errors > 0) {
throw new InvalidJidError.UNKNOWN(@"Unknown error: $(status.errorName())");
}
}
private static string? prepare(string? src, ICU.PrepType type, bool strict = false) throws InvalidJidError {
if (src == null) return src;
try {
ICU.ParseError error;
ICU.ErrorCode status = ICU.ErrorCode.ZERO_ERROR;
ICU.PrepProfile profile = ICU.PrepProfile.openByType(type, ref status);
ICU.String src16 = ICU.String.from_string(src);
int32 dest16_capacity = src16.len() * 2 + 1;
ICU.String dest16 = ICU.String.alloc(dest16_capacity);
long dest16_length = profile.prepare(src16, src16.len(), dest16, dest16_capacity, strict ? ICU.PrepOptions.DEFAULT : ICU.PrepOptions.ALLOW_UNASSIGNED, out error, ref status);
if (status == ICU.ErrorCode.INVALID_CHAR_FOUND) {
throw new InvalidJidError.INVALID_CHAR("Found invalid character");
} else if (status == ICU.ErrorCode.STRINGPREP_PROHIBITED_ERROR) {
throw new InvalidJidError.INVALID_CHAR("Found prohibited character");
} else if (status != ICU.ErrorCode.ZERO_ERROR) {
throw new InvalidJidError.UNKNOWN(@"Unknown error: $(status.errorName())");
} else if (dest16_length < 0) {
throw new InvalidJidError.UNKNOWN("Unknown error");
}
return dest16.to_string();
} catch (ConvertError e) {
throw new InvalidJidError.INVALID_CHAR(@"Conversion error: $(e.message)");
}
}
public Jid with_resource(string? resourcepart) throws InvalidJidError {
if (resourcepart == null) return bare_jid;
return Jid.from_full(bare_jid, resourcepart);
}
public bool is_domain() {
return level == Level.DOMAIN;
}
public bool is_bare() {
return level <= Level.BARE;
}
public bool is_full() {
return level == Level.FULL;
}
public unowned string to_string() {
return jid;
}
public bool equals_bare(Jid? jid) {
return jid != null && equals_bare_func(this, jid);
}
public bool equals(Jid? jid) {
return jid != null && equals_func(this, jid);
}
public static new bool equals_bare_func(Jid jid1, Jid jid2) {
return equals_func(jid1.bare_jid, jid2.bare_jid);
}
public static bool equals_func(Jid? jid1, Jid? jid2) {
if (jid1 == null && jid2 == null) return true;
if (jid1 == null || jid2 == null) return false;
if (jid1 == jid2) return true;
bool res = jid1.level == jid2.level && jid1.part == jid2.part && equals_func(jid1.parent, jid2.parent);
if (res) warning("JIDs considered equal, but are not same instance");
return res;
}
public static new uint hash_bare_func(Jid jid) {
return jid.bare_jid.to_string().hash();
}
public static new uint hash_func(Jid jid) {
return jid.to_string().hash();
}
}
public errordomain InvalidJidError {
EMPTY_DOMAIN,
EMPTY_RESOURCE,
EMPTY_LOCAL,
INVALID_CHAR,
UNKNOWN
}
}
|