-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTNStropheJID.j
290 lines (239 loc) · 7.42 KB
/
TNStropheJID.j
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
/*
* TNStropheJID.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
*
* This library 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 3.0 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
TNStropheJIDExceptionJID = @"TNStropheJIDExceptionJID";
/*! @ingroup strophecappuccino
This class represents a XMPP JID
*/
@implementation TNStropheJID : CPObject
{
BOOL _isServer @accessors(getter=isServer);
CPString _domain @accessors(property=domain);
CPString _node @accessors(property=node);
CPString _resource @accessors(property=resource);
}
#pragma mark -
#pragma mark Class methods
+ (void)initialize
{
[self exposeBinding:@"domain"];
[self exposeBinding:@"node"];
[self exposeBinding:@"resource"];
}
/*! return a new TNStropheJID
@param aNode the node part of the JID
@param aDomain the domain part of the JID
@param aResource the resource part of the JID
*/
+ (TNStropheJID)stropheJIDWithNode:(CPString)aNode domain:(CPString)aDomain resource:(CPString)aResource
{
return [[TNStropheJID alloc] initWithNode:aNode domain:aDomain resource:aResource];
}
/*! return a new TNStropheJID
@param aNode the node part of the JID
@param aDomain the domain part of the JID
@param aResource the resource part of the JID
*/
+ (TNStropheJID)stropheJIDWithNode:(CPString)aNode domain:(CPString)aDomain
{
return [[TNStropheJID alloc] initWithNode:aNode domain:aDomain];
}
/*! return a new TNStropheJID
@param aNode the node part of the JID
@param aDomain the domain part of the JID
@param aResource the resource part of the JID
*/
+ (TNStropheJID)stropheJIDWithString:(CPString)aStringJID
{
return [[TNStropheJID alloc] initWithString:aStringJID];
}
#pragma mark -
#pragma mark Initialization
/*! return a new TNStropheJID
@param aNode the node part of the JID
@param aDomain the domain part of the JID
@param aResource the resource part of the JID
*/
- (TNStropheJID)initWithNode:(CPString)aNode domain:(CPString)aDomain resource:(CPString)aResource
{
if (self = [super init])
{
[self setNode:aNode];
[self setDomain:aDomain];
[self setResource:aResource];
_isServer = (!aDomain && !aResource);
if ((_node && _node.indexOf(" ") != -1) || (_domain && _domain.indexOf(" ") != -1) || (_resource && _resource.indexOf(" ") != -1))
[CPException raise:TNStropheJIDExceptionJID reason:@"Given information are not a valid JID."];
}
return self;
}
/*! return a new TNStropheJID
@param aNode the node part of the JID
@param aDomain the domain part of the JID
*/
- (TNStropheJID)initWithNode:(CPString)aNode domain:(CPString)aDomain
{
return [self initWithNode:aNode domain:aDomain resource:nil];
}
/*! return a new TNStropheJID
@param aNode the node part of the JID
@param aDomain the domain part of the JID
*/
- (TNStropheJID)initWithString:(CPString)aStringJID
{
if (!aStringJID)
return;
var node = aStringJID.split("@")[0],
domain,
resource;
if (aStringJID.indexOf("@") != -1) //this is a server
{
domain = aStringJID.split("@")[1].split("/")[0],
resource = aStringJID.split("/")[1];
}
if (!node)
[CPException raise:TNStropheJIDExceptionJID reason:aStringJID + @" is not a valid JID"];
return [self initWithNode:node domain:domain resource:resource];
}
#pragma mark -
#pragma mark Setters and Getters
/*! return the bare JID (node@domain)
@return CPString containing the bare JID
*/
- (CPString)bare
{
if (_domain)
return _node + @"@" + _domain;
else
return _node;
}
/*! set the bare JID from the string.
resource, if any remains the same
@param aBareJID CPString containing the bare JID
*/
- (void)setBare:(CPString)aBareJID
{
var node = aBareJID.split("@")[0],
domain = aBareJID.split("@")[1].split("/")[0];
if (!node || !domain)
[CPException raise:TNStropheJIDExceptionJID reason:aBareJID + @" is not a valid JID"];
[self setNode:node];
[self setDomain:domain];
}
/*! return the full JID (node@domain/resource)
@return CPString containing the full JID
*/
- (CPString)full
{
if (_resource)
return [self bare] + "/" + _resource;
else
return [self bare];
}
/*! set the full JID from the string.
@param aFullJID CPString containing the full JID
*/
- (void)setFull:(CPString)aFullJID
{
[self setBare:aFullJID];
var resource = aFullJID.split("/")[1];
if (!resource)
[CPException raise:TNStropheJIDExceptionJID reason:aFullJID + @" is not a valid JID"];
[self setResource:resource]
}
/*! description method
@return CPString containing the full JID
*/
- (CPString)description
{
return [self stringValue];
}
/*! string value method
@return CPString containing the full JID
*/
- (CPString)stringValue
{
return [self full];
}
/*! convenient method
*/
- (CPString)uppercaseString
{
return [[self stringValue] uppercaseString];
}
/*! convenient method
*/
- (CPString)lowercaseString
{
return [[self stringValue] lowercaseString];
}
#pragma mark -
#pragma mark Operations
/*! check if given TNStropheJID is equals to self
@param anotherJID the TNStropheJID to compare
*/
- (BOOL)equals:(TNStropheJID)anotherJID
{
return [self fullEquals:anotherJID];
}
/*! check if given TNStropheJID fullJID is equals to self
@param anotherJID the TNStropheJID to compare
*/
- (BOOL)fullEquals:(TNStropheJID)anotherJID
{
return ([self bareEquals:anotherJID] && ([[anotherJID resource] uppercaseString] === [[self resource] uppercaseString]))
}
/*! check if given TNStropheJID's node and domain is equals to self
@param anotherJID the TNStropheJID to compare
*/
- (BOOL)bareEquals:(TNStropheJID)anotherJID
{
return (([[anotherJID node] uppercaseString] === [[self node] uppercaseString]) && ([[anotherJID domain] uppercaseString] === [[self domain] uppercaseString]));
}
/*! compare rthe JID with another (using the full JID)
@param another the JID to compare with
*/
- (CPComparisonResult)compare:(TNStropheJID)anotherJID
{
var stringRepA = [self stringValue],
stringRepB = [anotherJID stringValue];
return [stringRepA compare:stringRepB];
}
@end
@implementation TNStropheJID (CPCodingCompliance)
- (id)initWithCoder:(CPCoder)aCoder
{
if (self = [super init])
{
_node = [aCoder decodeObjectForKey:@"_node"];
_domain = [aCoder decodeObjectForKey:@"_domain"];
_resource = [aCoder decodeObjectForKey:@"_resource"];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:_node forKey:@"_node"];
if (_domain)
[aCoder encodeObject:_domain forKey:@"_domain"];
if (_resource)
[aCoder encodeObject:_resource forKey:@"_resource"];
}
@end