-
Notifications
You must be signed in to change notification settings - Fork 2
/
JID.m
191 lines (169 loc) · 3.46 KB
/
JID.m
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
//
// JID.m
// Jabber
//
// Created by David Chisnall on Sun Apr 25 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
#import "JID.h"
@implementation JID
+ (id) jidWithString:(NSString*)aJid
{
return [[JID alloc] initWithString:aJid];
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (NSString*) getJIDString
{
switch(type)
{
case serverJID:
return [NSString stringWithString:server];
case serverResourceJID:
return [NSString stringWithFormat:@"%@/%@",server,resource];
case userJID:
return [NSString stringWithFormat:@"%@@%@",user,server];
case resourceJID:
return [NSString stringWithFormat:@"%@@%@/%@",user,server,resource];
case invalidJID:
return nil;
}
return nil;
}
- (NSString*) getJIDStringWithNoResource
{
switch(type)
{
case serverJID:
case serverResourceJID:
return [NSString stringWithString:server];
case userJID:
case resourceJID:
return [NSString stringWithFormat:@"%@@%@",user,server];
case invalidJID:
return nil;
}
return nil;
}
- (id) init
{
self = [super init];
if(self == nil)
{
return nil;
}
user = nil;
server = nil;
resource = nil;
stringRepresentation = nil;
stringRepresentationWithNoResource = nil;
type = invalidJID;
return self;
}
- (id) initWithString:(NSString*)aJid
{
if (!(self = [self init])) return nil;
//JID's are not case sensitive. This is irritating, but what can you do?
aJid = [aJid lowercaseString];
NSRange at = [aJid rangeOfString:@"@"];
NSRange slash = [aJid rangeOfString:@"/"];
if(at.location == NSNotFound)
{
type = serverJID;
if(slash.location == NSNotFound)
{
type = serverJID;
server = aJid;
}
else
{
type = serverResourceJID;
server = [aJid substringToIndex:slash.location];
resource = [aJid substringFromIndex:slash.location + 1];
}
}
else
{
user = [aJid substringToIndex:at.location];
if(slash.location == NSNotFound)
{
type = userJID;
server = [aJid substringFromIndex:at.location + 1];
}
else
{
type = resourceJID;
at.location++;
at.length = slash.location - at.location;
server = [aJid substringWithRange:at];
resource = [aJid substringFromIndex:slash.location + 1];
}
}
stringRepresentation = [self getJIDString];
stringRepresentationWithNoResource = [self getJIDStringWithNoResource];
return self;
}
- (JIDType) type
{
return type;
}
- (NSComparisonResult) compare:(JID*)anAnotherJid
{
return [stringRepresentation compare:[anAnotherJid getJIDString]];
}
- (BOOL) isEqual:(id)anObject
{
if([anObject isKindOfClass:[NSString class]])
{
return [stringRepresentation isEqualToString:(NSString*)anObject];
}
if([anObject isKindOfClass:[JID class]])
{
return [self isEqualToJID:(JID*)anObject];
}
return NO;
}
- (NSUInteger) hash
{
return [stringRepresentation hash];
}
- (BOOL) isEqualToJID:(JID*)aJID
{
//This ought not to work.
if(type != aJID->type)
{
return NO;
}
return [stringRepresentation isEqualToString:aJID->stringRepresentation];
}
- (JID*) rootJID
{
return [JID jidWithString:stringRepresentationWithNoResource];
}
- (NSComparisonResult) compareWithNoResource:(JID*)anAnotherJid
{
return [stringRepresentationWithNoResource compare:[anAnotherJid getJIDStringWithNoResource]];
}
- (NSString*) jidString
{
return stringRepresentation;
}
- (NSString*) jidStringWithNoResource
{
return stringRepresentationWithNoResource;
}
- (NSString*) node
{
return user;
}
- (NSString*) domain
{
return server;
}
- (NSString*) resource
{
return resource;
}
@end