ObjXMPP  Diff

Differences From Artifact [357151968c]:

To Artifact [c1681b6fa8]:


1
2

3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
10


+







/*
 * Copyright (c) 2011, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2012, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://webkeks.org/hg/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
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
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







+
+











-
+





+
+











-




-
-
-
-
-
-
-
-
-
-
-
-
-
-
-










-
-
-
-

-
+


-
+
+
+


+
-
+



-
+
-
-
-







-
-
-
+
+
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-

-
-
+
+

-
+




-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
-
-
-
+
-
-
-
-
-
-
-
-







 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define XMPP_ROSTER_M

#include <assert.h>

#import "XMPPRoster.h"
#import "XMPPRosterItem.h"
#import "XMPPConnection.h"
#import "XMPPIQ.h"
#import "XMPPJID.h"
#import "XMPPMulticastDelegate.h"
#import "namespaces.h"

@implementation XMPPRoster
- initWithConnection: (XMPPConnection*)conn
- initWithConnection: (XMPPConnection*)connection_
{
	self = [super init];

	@try {
		rosterItems = [[OFMutableDictionary alloc] init];
		connection = connection_;
		[connection addDelegate: self];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[rosterItems release];
	[rosterID release];

	[super dealloc];
}

- (void)XMPP_addRosterItem: (XMPPRosterItem*)rosterItem
{
	return [self XMPP_updateRosterItem: rosterItem];
}

- (void)XMPP_updateRosterItem: (XMPPRosterItem*)rosterItem
{
	[rosterItems setObject: rosterItem
			forKey: [[rosterItem JID] bareJID]];
}

- (void)XMPP_deleteRosterItem: (XMPPRosterItem*)rosterItem
{
	[rosterItems removeObjectForKey: [[rosterItem JID] bareJID]];
}

- (OFDictionary*)rosterItems
{
	return [[rosterItems copy] autorelease];
}

- (void)requestRoster
{
	XMPPIQ *iq;

	if (rosterID != nil)
		assert(0);

	rosterID = [[connection generateStanzaID] retain];
	iq = [XMPPIQ IQWithType: @"get"
			     ID: rosterID];
			     ID: [connection generateStanzaID]];
	[iq addChild: [OFXMLElement elementWithName: @"query"
					  namespace: XMPP_NS_ROSTER]];
	[connection sendStanza: iq];
	[connection     sendIQ: iq
	    withCallbackObject: self
		      selector: @selector(XMPP_handleInitialRoster:)];
}

- (BOOL)connection: (XMPPConnection*)connection_
- (BOOL)handleIQ: (XMPPIQ*)iq
      didReceiveIQ: (XMPPIQ*)iq
{
	OFXMLElement *rosterElement;
	OFXMLElement *element;
	XMPPRosterItem *rosterItem = nil;
	XMPPRosterItem *rosterItem;
	OFString *subscription;
	OFEnumerator *enumerator;
	BOOL isPush = ![[iq ID] isEqual: rosterID];

	rosterElement = [iq elementForName: @"query"
				 namespace: XMPP_NS_ROSTER];

	if (rosterElement == nil)
		return NO;

	if (isPush) {
		if (![[iq type] isEqual: @"set"])
			return NO;
	if (![[iq type] isEqual: @"set"])
		return NO;
	} else {
		if (![[iq type] isEqual: @"result"])
			return NO;
	}


	element = [rosterElement elementForName: @"item"
	enumerator = [[rosterElement children] objectEnumerator];
	while ((element = [enumerator nextObject]) != nil) {
		OFMutableArray *groups = [OFMutableArray array];
		OFEnumerator *groupEnumerator;
		OFXMLElement *groupElement;

		if (![[element name] isEqual: @"item"] ||
		    ![[element namespace] isEqual: XMPP_NS_ROSTER])
			continue;

		rosterItem = [XMPPRosterItem rosterItem];
		[rosterItem setJID: [XMPPJID JIDWithString:
		    [[element attributeForName: @"jid"] stringValue]]];
		[rosterItem setName:
		    [[element attributeForName: @"name"] stringValue]];

		subscription = [[element attributeForName:
		    @"subscription"] stringValue];

		if (![subscription isEqual: @"none"] &&
		    ![subscription isEqual: @"to"] &&
		    ![subscription isEqual: @"from"] &&
		    ![subscription isEqual: @"both"] &&
		    (![subscription isEqual: @"remove"] || !isPush))
			subscription = @"none";

		[rosterItem setSubscription: subscription];

		groupEnumerator = [[element
		    elementsForName: @"group"
			  namespace: XMPP_NS_ROSTER] objectEnumerator];
				      namespace: XMPP_NS_ROSTER];
		while ((groupElement = [groupEnumerator nextObject]) != nil)
			[groups addObject: [groupElement stringValue]];

		if ([groups count] > 0)
			[rosterItem setGroups: groups];
	if (element != nil) {
		rosterItem = [self XMPP_rosterItemWithXMLElement: element];

		if ([subscription isEqual: @"remove"])
		if ([[rosterItem subscription] isEqual: @"remove"])
			[self XMPP_deleteRosterItem: rosterItem];
		else
			[self XMPP_addRosterItem: rosterItem];

		if (isPush) {
			SEL sel = @selector(connection:didReceiveRosterItem:);

		if ([delegate respondsToSelector:
		    @selector(roster:didReceiveRosterItem:)])
			[delegate         roster: self
			[[connection XMPP_delegates]
			    broadcastSelector: sel
				forConnection: connection
				   withObject: rosterItem];
		}
	}
			    didReceiveRosterItem: rosterItem];
	}


	if (isPush) {
		[connection sendStanza: [iq resultIQ]];
	[connection_ sendStanza: [iq resultIQ]];
	} else {
		[[connection XMPP_delegates]
		    broadcastSelector: @selector(connectionDidReceiveRoster:)
			forConnection: connection];

		[rosterID release];
		rosterID = nil;
	}

	return YES;
}

- (void)addRosterItem: (XMPPRosterItem*)rosterItem
{
	[self updateRosterItem: rosterItem];
231
232
233
234
235
236
237


























































































238











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
271
272
273
274
275







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
		       stringValue: @"remove"];

	[query addChild: item];
	[iq addChild: query];

	[connection sendStanza: iq];
}

- (void)setDelegate: (id <XMPPRosterDelegate>)delegate_
{
	delegate = (id <XMPPRosterDelegate, OFObject>)delegate_;
}

- (id <XMPPRosterDelegate>)delegate
{
	return delegate;
}

- (void)XMPP_addRosterItem: (XMPPRosterItem*)rosterItem
{
	return [self XMPP_updateRosterItem: rosterItem];
}

- (void)XMPP_updateRosterItem: (XMPPRosterItem*)rosterItem
{
	[rosterItems setObject: rosterItem
			forKey: [[rosterItem JID] bareJID]];
}

- (void)XMPP_deleteRosterItem: (XMPPRosterItem*)rosterItem
{
	[rosterItems removeObjectForKey: [[rosterItem JID] bareJID]];
}

- (XMPPRosterItem*)XMPP_rosterItemWithXMLElement: (OFXMLElement*)element
{
	OFString *subscription;
	OFEnumerator *groupEnumerator;
	OFXMLElement *groupElement;
	OFMutableArray *groups = [OFMutableArray array];
	XMPPRosterItem *rosterItem = [XMPPRosterItem rosterItem];
	[rosterItem setJID: [XMPPJID JIDWithString:
	    [[element attributeForName: @"jid"] stringValue]]];
	[rosterItem setName:
	    [[element attributeForName: @"name"] stringValue]];

	subscription = [[element attributeForName:
	    @"subscription"] stringValue];

	if (![subscription isEqual: @"none"] &&
	    ![subscription isEqual: @"to"] &&
	    ![subscription isEqual: @"from"] &&
	    ![subscription isEqual: @"both"] &&
	    ![subscription isEqual: @"remove"])
		subscription = @"none";

	[rosterItem setSubscription: subscription];

	groupEnumerator = [[element
	    elementsForName: @"group"
		  namespace: XMPP_NS_ROSTER] objectEnumerator];
	while ((groupElement = [groupEnumerator nextObject]) != nil)
		[groups addObject: [groupElement stringValue]];

	if ([groups count] > 0)
		[rosterItem setGroups: groups];

	return rosterItem;
}

- (void)XMPP_handleInitialRoster: (XMPPIQ*)iq
{
	OFXMLElement *rosterElement;
	OFEnumerator *enumerator;
	OFXMLElement *element;
	XMPPRosterItem *rosterItem = nil;

	rosterElement = [iq elementForName: @"query"
				 namespace: XMPP_NS_ROSTER];

	enumerator = [[rosterElement children] objectEnumerator];
	while ((element = [enumerator nextObject]) != nil) {
		if (![[element name] isEqual: @"item"] ||
		    ![[element namespace] isEqual: XMPP_NS_ROSTER])
			continue;

		rosterItem = [self XMPP_rosterItemWithXMLElement: element];

		if ([[rosterItem subscription] isEqual: @"remove"])
			[self XMPP_deleteRosterItem: rosterItem];
		else
			[self XMPP_addRosterItem: rosterItem];
	}

	if ([delegate respondsToSelector: @selector(rosterWasReceived:)])
		[delegate rosterWasReceived: self];
}
@end

@implementation OFObject (XMPPRosterDelegate)
- (void)rosterWasReceived: (XMPPRoster*)roster
{
}

-         (void)roster: (XMPPRoster*)roster
  didReceiveRosterItem: (XMPPRosterItem*)rosterItem
{
}
@end