-
Notifications
You must be signed in to change notification settings - Fork 2
/
XMPPServiceDiscovery.m
221 lines (212 loc) · 6.25 KB
/
XMPPServiceDiscovery.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
//
// XMPPServiceDiscovery.m
// Jabber
//
// Created by David Chisnall on 18/02/2005.
// Copyright 2005 __MyCompanyName__. All rights reserved.
//
#import "XMPPServiceDiscovery.h"
#import "XMPPAccount.h"
#import "XMPPDiscoInfo.h"
#import "XMPPDiscoItems.h"
#import <EtoileXML/ETXMLWriter.h>
static NSString * xmlnsXMPPDiscoInfo = @"http://jabber.org/protocol/disco#info";
static NSString * xmlnsXMPPDiscoItems = @"http://jabber.org/protocol/disco#items";
@implementation XMPPServiceDiscovery
- (XMPPServiceDiscovery*) initWithAccount:(XMPPAccount*)account
{
SELFINIT;
features = [[NSMutableDictionary alloc] init];
myFeatures = [[NSMutableSet alloc] init];
//Feature must be supported
[myFeatures addObject:@"http://jabber.org/protocol/disco#info"];
//TODO: Put these initialisations somewhere sensible.
[myFeatures addObject:@"http://jabber.org/protocol/xhtml-im"];
children = [[NSMutableDictionary alloc] init];
capabilitiesPerJID = [[NSMutableDictionary alloc] init];
featuresForCapabilities = [[NSMutableDictionary alloc] init];
dispatcher = [[account roster] dispatcher];
connection = [account connection];
[dispatcher addInfoQueryHandler:self forNamespace:xmlnsXMPPDiscoInfo];
[dispatcher addInfoQueryHandler:self forNamespace:xmlnsXMPPDiscoItems];
return self;
}
- (void) setCapabilities:(NSString*)caps forJID:(JID*)aJid
{
NSString * jid = [aJid jidString];
[capabilitiesPerJID setObject:caps forKey:jid];
if([featuresForCapabilities objectForKey:caps] == nil)
{
[self featuresForJID:aJid node:nil];
}
}
- (void) sendQueryToJID:(const NSString*)jid node:(const NSString*)node inNamespace:(const NSString*)xmlns
{
ETXMLWriter *xmlWriter = [connection xmlWriter];
NSString * iqID = [connection nextMessageID];
[xmlWriter startElement: @"iq"
attributes: D(@"get", @"type",
jid, @"to",
iqID, @"id")];
if(node != nil) /*it was ==, breaking the execution on OS X*/
{
[xmlWriter startAndEndElement: @"query"
attributes: D(xmlns, @"xmlns",
node, @"node")];
}
else
{
[xmlWriter startAndEndElement: @"query"
attributes: D(xmlns, @"xmlns")];
}
[xmlWriter endElement]; //</iq>
[dispatcher addInfoQueryResultHandler: self forID: iqID];
}
- (NSDictionary*) infoForJID:(JID*)aJid node:(NSString*)aNode
{
NSString * jid = [aJid jidString];
NSString * node = aNode;
if(aNode == nil)
{
node = @"";
}
NSDictionary * result = [[features objectForKey:jid] objectForKey:node];
//If we haven't already got these values, request them
if(result == nil)
{
NSString * caps = [capabilitiesPerJID objectForKey:jid];
if(caps != nil)
{
result = [featuresForCapabilities objectForKey:caps];
}
else
{
[self sendQueryToJID:jid node:node inNamespace:xmlnsXMPPDiscoInfo];
}
}
return result;
}
- (NSArray*) identitiesForJID:(JID*)aJid node:(NSString*)aNode
{
NSDictionary * infoDictionary = [self infoForJID:aJid node:aNode];
return [infoDictionary objectForKey:@"identities"];
}
- (NSArray*) featuresForJID:(JID*)aJid node:(NSString*)aNode
{
NSDictionary * infoDictionary = [self infoForJID:aJid node:aNode];
return [infoDictionary objectForKey:@"features"];
}
- (NSArray*) itemsForJID:(JID*)aJid node:(NSString*)aNode
{
NSString * jid = [aJid jidString];
NSString * node = aNode;
if(aNode == nil)
{
node = @"";
}
NSArray * result = [[children objectForKey:jid] objectForKey:node];
//If we haven't already got these values, request them
if(result == nil)
{
[self sendQueryToJID:jid node:node inNamespace:xmlnsXMPPDiscoItems];
}
return result;
}
- (void) handleInfoQuery:(XMPPInfoQueryStanza*)anIQ
{
NSString * jid = [[anIQ jid] jidString];
switch([anIQ type])
{
case IQ_TYPE_GET:
{
if([[anIQ queryNamespace] isEqualToString:xmlnsXMPPDiscoInfo])
{
ETXMLWriter *xmlWriter = [connection xmlWriter];
[xmlWriter startElement: @"iq"
attributes: D(@"result", @"type",
jid, @"to",
[anIQ sequenceID], @"id")];
[xmlWriter startElement: @"query"
attributes: D(xmlnsXMPPDiscoInfo, @"xmlns")];
//TODO: Make the type configurable
[xmlWriter startElement: @"identity"
attributes: D(@"client", @"category",
@"pc", @"type")];
FOREACH(myFeatures, feature, NSString*)
{
[xmlWriter startAndEndElement: @"feature"
attributes: D(feature, @"var")];
}
[xmlWriter endElement]; // </identity>
[xmlWriter endElement]; // </query>
[xmlWriter endElement]; // </iq>
}
break;
}
case IQ_TYPE_RESULT:
{
info = [[anIQ children] objectForKey:@"XMPPDiscoInfo"];
items = [[anIQ children] objectForKey:@"XMPPDiscoItems"];
if(info != nil)
{
NSDictionary * nodeInfo = D([info identities], @"identities",
[info features], @"features");
NSString * node = [info node];
if(node == nil)
{
node = @"";
}
NSMutableDictionary * nodes = [features objectForKey:jid];
if(nodes == nil)
{
nodes = [NSMutableDictionary dictionary];
[features setObject:nodes forKey:jid];
}
[nodes setObject:nodeInfo forKey:node];
NSString * caps = [capabilitiesPerJID objectForKey:jid];
if(caps != nil)
{
[featuresForCapabilities setObject:nodeInfo forKey:caps];
}
[[NSNotificationCenter defaultCenter]
postNotificationName: @"XMPPDiscoFeaturesFound"
object: self
userInfo: D(jid, @"jid")];
}
if(items != nil)
{
NSArray * nodeItems = [items items];
NSString * node = [items node];
if(node == nil)
{
node = @"";
}
NSMutableDictionary * nodes = [children objectForKey:jid];
if(nodes == nil)
{
nodes = [NSMutableDictionary dictionary];
[children setObject:nodes forKey:jid];
}
[nodes setObject:nodeItems forKey:node];
[[NSNotificationCenter defaultCenter]
postNotificationName: @"XMPPDiscoItemsFound"
object: self
userInfo: D(jid, @"jid")];
}
}
default: {}
}
}
- (void) addFeature:(NSString*)aFeature
{
[myFeatures addObject:aFeature];
}
- (XMPPDiscoInfo*) info
{
return info;
}
- (XMPPDiscoItems*) items
{
return items;
}
@end