XMPPMessage.m
6.75 KB
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
#import "XMPPMessage.h"
#import "XMPPJID.h"
#import "NSXMLElement+XMPP.h"
#import <objc/runtime.h>
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
@implementation XMPPMessage
#if DEBUG
+ (void)initialize
{
// We use the object_setClass method below to dynamically change the class from a standard NSXMLElement.
// The size of the two classes is expected to be the same.
//
// If a developer adds instance methods to this class, bad things happen at runtime that are very hard to debug.
// This check is here to aid future developers who may make this mistake.
//
// For Fearless And Experienced Objective-C Developers:
// It may be possible to support adding instance variables to this class if you seriously need it.
// To do so, try realloc'ing self after altering the class, and then initialize your variables.
size_t superSize = class_getInstanceSize([NSXMLElement class]);
size_t ourSize = class_getInstanceSize([XMPPMessage class]);
if (superSize != ourSize)
{
NSLog(@"Adding instance variables to XMPPMessage is not currently supported!");
exit(15);
}
}
#endif
+ (XMPPMessage *)messageFromElement:(NSXMLElement *)element
{
object_setClass(element, [XMPPMessage class]);
return (XMPPMessage *)element;
}
+ (XMPPMessage *)message
{
return [[XMPPMessage alloc] init];
}
+ (XMPPMessage *)messageWithType:(NSString *)type
{
return [[XMPPMessage alloc] initWithType:type to:nil];
}
+ (XMPPMessage *)messageWithType:(NSString *)type to:(XMPPJID *)to
{
return [[XMPPMessage alloc] initWithType:type to:to];
}
+ (XMPPMessage *)messageWithType:(NSString *)type to:(XMPPJID *)jid elementID:(NSString *)eid
{
return [[XMPPMessage alloc] initWithType:type to:jid elementID:eid];
}
+ (XMPPMessage *)messageWithType:(NSString *)type to:(XMPPJID *)jid elementID:(NSString *)eid child:(NSXMLElement *)childElement
{
return [[XMPPMessage alloc] initWithType:type to:jid elementID:eid child:childElement];
}
+ (XMPPMessage *)messageWithType:(NSString *)type elementID:(NSString *)eid
{
return [[XMPPMessage alloc] initWithType:type elementID:eid];
}
+ (XMPPMessage *)messageWithType:(NSString *)type elementID:(NSString *)eid child:(NSXMLElement *)childElement
{
return [[XMPPMessage alloc] initWithType:type elementID:eid child:childElement];
}
+ (XMPPMessage *)messageWithType:(NSString *)type child:(NSXMLElement *)childElement
{
return [[XMPPMessage alloc] initWithType:type child:childElement];
}
- (id)init
{
return [self initWithType:nil to:nil elementID:nil child:nil];
}
- (id)initWithType:(NSString *)type
{
return [self initWithType:type to:nil elementID:nil child:nil];
}
- (id)initWithType:(NSString *)type to:(XMPPJID *)jid
{
return [self initWithType:type to:jid elementID:nil child:nil];
}
- (id)initWithType:(NSString *)type to:(XMPPJID *)jid elementID:(NSString *)eid
{
return [self initWithType:type to:jid elementID:eid child:nil];
}
- (id)initWithType:(NSString *)type to:(XMPPJID *)jid elementID:(NSString *)eid child:(NSXMLElement *)childElement
{
if ((self = [super initWithName:@"message"]))
{
if (type)
[self addAttributeWithName:@"type" stringValue:type];
if (jid)
[self addAttributeWithName:@"to" stringValue:[jid full]];
if (eid)
[self addAttributeWithName:@"id" stringValue:eid];
if (childElement)
[self addChild:childElement];
}
return self;
}
- (id)initWithType:(NSString *)type elementID:(NSString *)eid
{
return [self initWithType:type to:nil elementID:eid child:nil];
}
- (id)initWithType:(NSString *)type elementID:(NSString *)eid child:(NSXMLElement *)childElement
{
return [self initWithType:type to:nil elementID:eid child:childElement];
}
- (id)initWithType:(NSString *)type child:(NSXMLElement *)childElement
{
return [self initWithType:type to:nil elementID:nil child:childElement];
}
- (id)initWithXMLString:(NSString *)string error:(NSError *__autoreleasing *)error
{
if((self = [super initWithXMLString:string error:error])){
self = [XMPPMessage messageFromElement:self];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
NSXMLElement *element = [super copyWithZone:zone];
return [XMPPMessage messageFromElement:element];
}
- (NSString *)type
{
return [[self attributeForName:@"type"] stringValue];
}
- (NSString *)subject
{
return [[self elementForName:@"subject"] stringValue];
}
- (NSString *)body
{
return [[self elementForName:@"body"] stringValue];
}
- (NSString *)bodyForLanguage:(NSString *)language
{
NSString *bodyForLanguage = nil;
for (NSXMLElement *bodyElement in [self elementsForName:@"body"])
{
NSString *lang = [[bodyElement attributeForName:@"xml:lang"] stringValue];
// Openfire strips off the xml prefix
if (lang == nil)
{
lang = [[bodyElement attributeForName:@"lang"] stringValue];
}
if ([language isEqualToString:lang] || ([language length] == 0 && [lang length] == 0))
{
bodyForLanguage = [bodyElement stringValue];
break;
}
}
return bodyForLanguage;
}
- (NSString *)thread
{
return [[self elementForName:@"thread"] stringValue];
}
- (void)addSubject:(NSString *)subject
{
NSXMLElement *subjectElement = [NSXMLElement elementWithName:@"subject" stringValue:subject];
[self addChild:subjectElement];
}
- (void)addBody:(NSString *)body
{
NSXMLElement *bodyElement = [NSXMLElement elementWithName:@"body" stringValue:body];
[self addChild:bodyElement];
}
- (void)addBody:(NSString *)body withLanguage:(NSString *)language
{
NSXMLElement *bodyElement = [NSXMLElement elementWithName:@"body" stringValue:body];
if ([language length])
{
[bodyElement addAttributeWithName:@"xml:lang" stringValue:language];
}
[self addChild:bodyElement];
}
- (void)addThread:(NSString *)thread
{
NSXMLElement *threadElement = [NSXMLElement elementWithName:@"thread" stringValue:thread];
[self addChild:threadElement];
}
- (BOOL)isChatMessage
{
return [[[self attributeForName:@"type"] stringValue] isEqualToString:@"chat"];
}
- (BOOL)isChatMessageWithBody
{
if ([self isChatMessage])
{
return [self isMessageWithBody];
}
return NO;
}
- (BOOL)isErrorMessage
{
return [[[self attributeForName:@"type"] stringValue] isEqualToString:@"error"];
}
- (NSError *)errorMessage
{
if (![self isErrorMessage]) {
return nil;
}
NSXMLElement *error = [self elementForName:@"error"];
return [NSError errorWithDomain:@"urn:ietf:params:xml:ns:xmpp-stanzas"
code:[error attributeIntValueForName:@"code"]
userInfo:@{NSLocalizedDescriptionKey : [error compactXMLString]}];
}
- (BOOL)isMessageWithBody
{
return ([self elementForName:@"body"] != nil);
}
@end