-
Notifications
You must be signed in to change notification settings - Fork 2
/
XMPPMessage.m
270 lines (245 loc) · 5.78 KB
/
XMPPMessage.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
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
//
// XMPPMessage.m
// Jabber
//
// Created by David Chisnall on 20/08/2004.
// Copyright 2004 __MyCompanyName__. All rights reserved.
//
#import "XMPPMessage.h"
#include <wchar.h>
#include <wctype.h>
#import <AppKit/AppKit.h>
#import <EtoileXML/ETXMLParser.h>
#import <EtoileXML/ETXMLString.h>
#import <EtoileXML/ETXMLWriter.h>
#import "XMPPMessageStanzaFactory.h"
#import "XMPPError.h"
#import "NSAttributedString+HTML-IM.h"
#import <EtoileFoundation/EtoileFoundation.h>
NSDictionary * MESSAGE_TYPES;
@implementation XMPPMessage
+ (void) initialize
{
MESSAGE_TYPES = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:MESSAGE_TYPE_CHAT], @"chat",
[NSNumber numberWithInt:MESSAGE_TYPE_MESSAGE], @"message",
[NSNumber numberWithInt:MESSAGE_TYPE_ERROR], @"error",
[NSNumber numberWithInt:MESSAGE_TYPE_GROUPCHAT], @"groupchat",
nil];
}
+ (XMPPMessage*) messageWithBody:(id)aBody for:(JID*)aRecipient withSubject:(NSString*)aSubject type:(message_type_t)aType
{
return [[XMPPMessage alloc] initWithBody:aBody for:aRecipient withSubject:aSubject type:aType];
}
- (XMPPMessage*) initWithBody:(id)aBody for:(JID*)aRecipient withSubject:(NSString*)aSubject type:(message_type_t)aType
{
if((self = [super init])==nil){
return nil;
}
#ifndef DNDEBUG
ETLog(@"Body (%@) %@", [aBody class], aBody);
#endif
if([aBody isKindOfClass:[NSString class]])
{
body = [aBody stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
else if([aBody isKindOfClass:[NSAttributedString class]])
{
body = [[aBody stringValueWithExpandedLinks] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
html = aBody;
}
correspondent = aRecipient;
subject = aSubject;
type = aType;
direction = out;
return self;
}
- (id) initWithXMLParser: (ETXMLParser*)aParser
key: (id) aKey
{
self = [super initWithXMLParser: aParser
key: aKey];
if (nil == self)
{
return nil;
}
unknownAttributes = [[NSMutableDictionary alloc] init];
timestamps = [[NSMutableArray alloc] init];
body = @"";
shouldDisplay = YES;
return self;
}
- (void) beginWritingToXMLWriter: (ETXMLWriter*)xmlWriter
{
NSMutableDictionary * attributes = [[NSMutableDictionary alloc] init];
switch(type)
{
case MESSAGE_TYPE_CHAT:
[attributes setValue:@"chat" forKey:@"type"];
break;
case MESSAGE_TYPE_ERROR:
[attributes setValue:@"error" forKey:@"type"];
break;
case MESSAGE_TYPE_GROUPCHAT:
[attributes setValue:@"groupchat" forKey:@"type"];
case MESSAGE_TYPE_MESSAGE:
case MESSAGE_TYPE_SPECIAL:
break;
}
if(direction == out)
{
[attributes setValue:[correspondent jidString] forKey:@"to"];
}
else
{
[attributes setValue:[correspondent jidString] forKey:@"from"];
}
[xmlWriter startElement: @"message" attributes: attributes];
if(subject != nil)
{
[xmlWriter startElement: @"subject"];
[xmlWriter characters: subject];
[xmlWriter endElement];
}
if(body != nil)
{
[xmlWriter startElement: @"body"];
[xmlWriter characters: body];
[xmlWriter endElement];
}
if(html != nil)
{
[html writeXHTMLIMToXMLWriter: xmlWriter];
}
}
- (void) finishWritingToXMLWriter: (ETXMLWriter*) xmlWriter
{
[xmlWriter endElement];
}
- (void)writeToXMLWriter: (ETXMLWriter*)xmlWriter
{
[self beginWritingToXMLWriter: (ETXMLWriter*) xmlWriter];
[self finishWritingToXMLWriter: (ETXMLWriter*) xmlWriter];
}
- (JID*) correspondent
{
return correspondent;
}
- (NSString*) subject
{
return subject;
}
- (void) setSubject:(NSString*)aSubject
{
subject = aSubject;
}
- (NSString*) body
{
return body;
}
- (void) setBody:(NSString*)aBody
{
body = aBody;
}
- (NSAttributedString*) HTMLBody
{
if(html != nil)
{
return html;
}
return [[NSAttributedString alloc] initWithString:body];
}
- (BOOL) in
{
if(direction == in)
{
return YES;
}
return NO;
}
- (message_type_t) type
{
return type;
}
- (XMPPTimestamp*) timestamp
{
return [timestamps lastObject];
}
- (XMPPError*) error
{
return error;
}
- (NSComparisonResult) compareByTimestamp:(XMPPMessage*)_other
{
return [[self timestamp] compare:[_other timestamp]];
}
- (void)characters:(NSString *)_chars
{
//Ignore cdata
}
- (void)startElement:(NSString *)aName
attributes:(NSDictionary*)attributes
{
if([aName isEqualToString:@"message"])
{
correspondent = [JID jidWithString:[attributes objectForKey:@"from"]];
direction = in;
type = [[MESSAGE_TYPES objectForKey:[attributes objectForKey:@"type"]] intValue];
}
else
{
XMPPMessageStanzaFactory * factory = [XMPPMessageStanzaFactory sharedStazaFactory];
NSString * xmlns = [attributes objectForKey:@"xmlns"];
Class handler = [factory handlerForTag:aName inNamespace:xmlns];
NSString * elementKey = [factory valueForTag:aName inNamespace:xmlns];
[[[handler alloc] initWithXMLParser:parser
key:elementKey] startElement:aName
attributes:attributes];
}
}
- (void)endElement:(NSString *)aName
{
if([aName isEqualToString:@"message"])
{
[[parser parentHandler] addChild:self forKey:key];
[parser popContentHandler];
}
else
{
ETLog(@"End of %@ tag received while parsing a message. This probably indicates a bug.", aName);
}
}
- (void) setParser:(id) XMLParser
{
parser = XMLParser;
}
- (void) addbody:(NSString*)aBody
{
body = [aBody stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void) addsubject:(NSString*)aSubject
{
subject = aSubject;
}
- (void) addtimestamp:(XMPPTimestamp*)aTimestamp
{
[timestamps addObject:aTimestamp];
[timestamps sortUsingSelector:@selector(compare:)];
}
- (void) addhtml:(NSAttributedString*)anAttributedString
{
html = anAttributedString;
}
- (void) adderror:(XMPPError*)anError
{
error = anError;
}
- (void) setShouldDisplay:(BOOL)aFlag
{
shouldDisplay = aFlag;
}
- (BOOL) shouldDisplay
{
return shouldDisplay;
}
@end