-
Notifications
You must be signed in to change notification settings - Fork 2
/
XMPPPresence.m
248 lines (227 loc) · 4.69 KB
/
XMPPPresence.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
//
// XMPPPresence.m
// Jabber
//
// Created by David Chisnall on Sun Apr 25 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
#import <EtoileXML/ETXMLNode.h>
#import <EtoileXML/ETXMLParser.h>
#import <EtoileFoundation/EtoileCompatibility.h>
#import "XMPPPresence.h"
#import "XMPPPresenceStanzaFactory.h"
int PRESENCE_ICONS[] = {
0x25C9,
0x25C9,
0x25C8,
0x25C8,
0x25A3,
0x25CE,
0x25CE
};
@implementation XMPPPresence
+ (NSString*) displayStringForPresence:(unsigned char)_presence
{
switch(_presence)
{
case PRESENCE_CHAT:
return @"Free For Chat";
case PRESENCE_ONLINE:
return @"Online";
case PRESENCE_AWAY:
return @"Away";
case PRESENCE_XA:
return @"Extended Away";
case PRESENCE_DND:
return @"Do Not Disturb";
case PRESENCE_OFFLINE:
return @"Offline";
}
return @"Unknown";
}
+ (NSString*) xmppStringForPresence:(unsigned char)_presence
{
switch(_presence)
{
case PRESENCE_CHAT:
return @"chat";
case PRESENCE_ONLINE:
return @"online";
case PRESENCE_AWAY:
return @"away";
case PRESENCE_XA:
return @"xa";
case PRESENCE_DND:
return @"dnd";
}
return @"unknown";
}
+ (unsigned char) presenceForXMPPString:(NSString*)_presence
{
if([_presence rangeOfString:@"online"].location != NSNotFound)
{
return PRESENCE_ONLINE;
}
if([_presence rangeOfString:@"away"].location != NSNotFound)
{
return PRESENCE_AWAY;
}
if([_presence rangeOfString:@"xa"].location != NSNotFound)
{
return PRESENCE_XA;
}
if([_presence rangeOfString:@"dnd"].location != NSNotFound)
{
return PRESENCE_DND;
}
if([_presence rangeOfString:@"offline"].location != NSNotFound)
{
return PRESENCE_OFFLINE;
}
if([_presence rangeOfString:@"chat"].location != NSNotFound)
{
return PRESENCE_CHAT;
}
return PRESENCE_UNKNOWN;
}
- (void)startElement:(NSString *)aName
attributes:(NSDictionary *)attributes
{
if ([aName isEqualToString:@"presence"])
{
depth++;
from = [JID jidWithString:[attributes objectForKey:@"from"]];
NSString * presenceType = [attributes objectForKey:@"type"];
priority = 0;
onlineStatus = PRESENCE_UNKNOWN;
#ifndef DNDEBUG
ETLog(@"Presence from %@: %@", [attributes objectForKey:@"from"], presenceType);
#endif
if(presenceType == nil || [presenceType isEqualToString:@"online"])
{
#ifndef DNDEBUG
ETLog(@"Online");
#endif
onlineStatus = PRESENCE_ONLINE;
type = online;
}
else if([presenceType isEqualToString:@"unavailable"])
{
type = unavailable;
onlineStatus = PRESENCE_OFFLINE;
}
else if([presenceType isEqualToString:@"subscribe"])
{
type = subscribe;
}
else if([presenceType isEqualToString:@"unsubscribe"])
{
type = unsubscribe;
}
else if([presenceType isEqualToString:@"subscribed"])
{
type = subscribed;
}
else if([presenceType isEqualToString:@"unsubscribed"])
{
type = unsubscribed;
}
else
{
//ETLog(@"Presence stanza with unknown type received.");
}
}
else if([aName isEqualToString:@"c"])
{
caps = [attributes objectForKey:@"ver"];
depth++;
}
else
{
XMPPPresenceStanzaFactory * factory = [XMPPPresenceStanzaFactory 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];
}
}
// Sub-stanza KVC handlers
- (void) addshow:(NSString*)show
{
onlineStatus = [XMPPPresence presenceForXMPPString:show];
}
- (void) addstatus:(NSString*)status
{
message = status;
}
- (void) setpriority:(NSString*)aPriority
{
priority = [aPriority intValue];
}
- (void) addnickname:(NSString*)aNickname
{
nickname = aNickname;
}
- (id) initWithJID:(JID*)_jid
{
from = _jid;
onlineStatus = PRESENCE_UNKNOWN;
message = @"Status Unknown";
return [super init];
}
- (id) initWithXMLParser: (ETXMLParser*)aParser
key: (id) aKey
{
self = [super initWithXMLParser: aParser
key: aKey];
if (nil == self)
{
return nil;
}
from = [[JID alloc] init];
onlineStatus = PRESENCE_UNKNOWN;
message = nil;
return self;
}
- (unsigned char) show
{
return onlineStatus;
}
- (NSString*) status
{
return message;
}
- (NSString*) nickname
{
return nickname;
}
- (JID*) jid
{
return from;
}
- (int) priority
{
return priority;
}
- (PresenceType) type
{
return type;
}
- (NSString*) caps
{
return caps;
}
- (NSComparisonResult) compare:(XMPPPresence*)_otherPresence
{
if(onlineStatus < [_otherPresence show])
{
return NSOrderedAscending;
}
if(onlineStatus > [_otherPresence show])
{
return NSOrderedDescending;
}
return NSOrderedSame;
}
@end