-
Notifications
You must be signed in to change notification settings - Fork 6
/
TNStropheServerAdministration.j
389 lines (324 loc) · 13.9 KB
/
TNStropheServerAdministration.j
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* TNStropheServerAdministration.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@import <Foundation/Foundation.j>
@import "Resources/Strophe/strophe.js"
@import "TNStropheConnection.j"
@import "TNStropheJID.j"
TNStropheServerAdministrationGetConnectedUserNotification = @"TNStropheServerAdministrationGetConnectedUserNotification";
TNStropheServerAdministrationGetRegisteredUserNotification = @"TNStropheServerAdministrationGetRegisteredUserNotification";
TNStropheServerAdministrationRegisterUserNotification = @"TNStropheServerAdministrationRegisterUserNotification";
TNStropheServerAdministrationSendAnnouncementNotification = @"TNStropheServerAdministrationSendAnnouncementNotification";
TNStropheServerAdministrationSetUserEnabledNotification = @"TNStropheServerAdministrationSetUserEnabledNotification";
TNStropheServerAdministrationUnregisterUserNotification = @"TNStropheServerAdministrationUnregisterUserNotification";
/*! @ingroup strophecappuccino
This class allows to manage XMPP server using XMPP
*/
@implementation TNStropheServerAdministration : CPObject
{
TNStropheConnection _connection @accessors(property=connection);
TNStropheJID _server @accessors(property=server);
id _delegate @accessors(property=delegate);
}
#pragma mark -
#pragma mark Initialization
/*! initialize a new TNStropheServerAdminstration
@param aConnection a TNStropheConnection
@param aServer the target XMPP server
*/
- (id)initWithConnection:(TNStropheConnection)aConnection server:(TNStropheJID)aServer
{
if (self = [super init])
{
_connection = aConnection;
_server = aServer
}
return self;
}
#pragma mark -
#pragma mark Utilities
/*! factor sending basic commands
@param anAction the action to send
@param aSelector the selector to trigger
*/
- (void)sendAction:(CPString)anAction selector:(SEL)aSelector
{
var uid = [_connection getUniqueId],
stanza = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
params = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
[stanza setTo:_server];
[stanza addChildWithName:@"command" andAttributes:{
@"xmlns": Strophe.NS.COMMAND,
@"action": @"execute",
@"node": anAction}];
[_connection registerSelector:aSelector ofObject:self withDict:params];
[_connection send:stanza];
}
#pragma mark -
#pragma mark Announcement
/*! send a message to all connected users
@param anAnnouncement the body of the message
@param aSubject the subject of the message
*/
- (void)sendAnnouncement:(CPString)anAnnouncement subject:(CPString)aSubject
{
var uid = [_connection getUniqueId],
stanza = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
params = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
[stanza setTo:_server];
[stanza addChildWithName:@"command" andAttributes:{
@"xmlns": Strophe.NS.COMMAND,
@"node": @"http://jabber.org/protocol/admin#announce"}];
[stanza addChildWithName:@"x" andAttributes:{@"xmlns": @"jabber:x:data", @"type": @"submit"}];
[stanza addChildWithName:@"field" andAttributes:{@"type": @"hidden", @"var": @"FORM_TYPE"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:@"http://jabber.org/protocol/admin"];
[stanza up];
[stanza up];
if (aSubject)
{
[stanza addChildWithName:@"field" andAttributes:{@"var": @"subject"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:aSubject];
[stanza up];
[stanza up];
}
[stanza addChildWithName:@"field" andAttributes:{@"var": @"body"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:anAnnouncement];
[_connection registerSelector:@selector(_didSendAnnouncement:) ofObject:self withDict:params];
[_connection send:stanza];
}
/*! @ignore
*/
- (void)_didSendAnnouncement:(TNStropheStanza)aStanza
{
if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didSendAnnouncement:)])
[_delegate serverAdmin:self didSendAnnouncement:aStanza];
else
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationSendAnnouncementNotification object:self userInfo:aStanza];
}
#pragma mark -
#pragma mark Users
/*! get all the registred users
NOT IMPLEMENTED IN EJABBERD
*/
- (void)registeredUsers
{
[self sendAction:@"http://jabber.org/protocol/admin#get-registered-users-list" selector:@selector(_didGetRegisteredUsers:)]
}
/*! @ignore
*/
- (void)_didGetRegisteredUsers:(TNStropheStanza)aStanza
{
if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didGetRegisteredUsers:)])
[_delegate serverAdmin:self didGetRegisteredUsers:aStanza];
else
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationGetRegisteredUserNotification object:self userInfo:aStanza];
}
/*! get all the connected users
NOT IMPLEMENTED IN EJABBERD
*/
- (void)connectedUsers
{
[self sendAction:@"http://jabber.org/protocol/admin#get-online-users-list" selector:@selector(_didGetConnectedUsers:)]
}
/*! @ignore
*/
- (void)_didGetConnectedUsers:(TNStropheStanza)aStanza
{
if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didGetConnectedUsers:)])
[_delegate serverAdmin:self didGetConnectedUsers:aStanza];
else
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationGetConnectedUserNotification object:self userInfo:aStanza];
}
/*! add a user to the server
@param aJID the JID of the new user
@param aPassword the password
@param aName the given name
@param aSurname the surname
@param anEmail the email
*/
- (void)registerUser:(TNStropheJID)aJID password:(CPString)aPassword name:(CPString)aName surname:(CPString)aSurname email:(CPString)anEmail
{
var uid = [_connection getUniqueId],
stanza = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
params = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
[stanza setTo:_server];
[stanza addChildWithName:@"command" andAttributes:{
@"xmlns": Strophe.NS.COMMAND,
@"node": @"http://jabber.org/protocol/admin#add-user"}];
[stanza addChildWithName:@"x" andAttributes:{@"xmlns": @"jabber:x:data", @"type": @"submit"}];
[stanza addChildWithName:@"field" andAttributes:{@"type": @"hidden", @"var": @"FORM_TYPE"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:@"http://jabber.org/protocol/admin"];
[stanza up];
[stanza up];
[stanza addChildWithName:@"field" andAttributes:{@"var": @"accountjid"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:[aJID bare]];
[stanza up];
[stanza up];
[stanza addChildWithName:@"field" andAttributes:{@"var": @"password"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:aPassword];
[stanza up];
[stanza up];
[stanza addChildWithName:@"field" andAttributes:{@"var": @"password-verify"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:aPassword];
[stanza up];
[stanza up];
[stanza addChildWithName:@"field" andAttributes:{@"var": @"email"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:anEmail];
[stanza up];
[stanza up];
[stanza addChildWithName:@"field" andAttributes:{@"var": @"given_name"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:aName];
[stanza up];
[stanza up];
[stanza addChildWithName:@"field" andAttributes:{@"var": @"surname"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:aSurname];
[stanza up];
[stanza up];
[_connection registerSelector:@selector(_didRegisterUser:) ofObject:self withDict:params];
[_connection send:stanza];
}
/*! @ignore
*/
- (void)_didRegisterUser:(TNStropheStanza)aStanza
{
if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didRegisterUser:)])
[_delegate serverAdmin:self didRegisterUser:aStanza];
else
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationRegisterUserNotification object:self userInfo:aStanza];
}
/*! remove a user from the server
@param someJIDs array of TNStropheJID to remove
*/
- (void)unregisterUsers:(CPArray)someJIDs
{
var uid = [_connection getUniqueId],
stanza = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
params = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
[stanza setTo:_server];
[stanza addChildWithName:@"command" andAttributes:{
@"xmlns": Strophe.NS.COMMAND,
@"node": @"http://jabber.org/protocol/admin#delete-user"}];
[stanza addChildWithName:@"x" andAttributes:{@"xmlns": @"jabber:x:data", @"type": @"submit"}];
[stanza addChildWithName:@"field" andAttributes:{@"type": @"hidden", @"var": @"FORM_TYPE"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:@"http://jabber.org/protocol/admin"];
[stanza up];
[stanza up];
[stanza addChildWithName:@"field" andAttributes:{@"var": @"accountjids"}];
for (var i = 0; i < [someJIDs count]; i++)
{
[stanza addChildWithName:@"value"];
[stanza addTextNode:[[someJIDs objectAtIndex:i] bare]];
[stanza up];
}
[stanza up];
[_connection registerSelector:@selector(_didUnregisterUser:) ofObject:self withDict:params];
[_connection send:stanza];
}
/*! @ignore
*/
- (void)_didUnregisterUser:(TNStropheStanza)aStanza
{
if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didUnregisterUser:)])
[_delegate serverAdmin:self didUnregisterUser:aStanza];
else
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationUnregisterUserNotification object:self userInfo:aStanza];
}
/*! set if user's account should be enabled or disabled
@param someJIDs array of target TNStropheJID
@param shouldEnable enable or disable the accounts
*/
- (void)setUsers:(CPArray)someJIDs enabled:(BOOL)shouldEnable
{
var uid = [_connection getUniqueId],
stanza = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
params = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"],
node = (shouldEnable) ? @"http://jabber.org/protocol/admin#reenable-user" : @"http://jabber.org/protocol/admin#disable-user";
[stanza setTo:_server];
[stanza addChildWithName:@"command" andAttributes:{
@"xmlns": Strophe.NS.COMMAND,
@"node": node}];
[stanza addChildWithName:@"x" andAttributes:{@"xmlns": @"jabber:x:data", @"type": @"submit"}];
[stanza addChildWithName:@"field" andAttributes:{@"type": @"hidden", @"var": @"FORM_TYPE"}];
[stanza addChildWithName:@"value"];
[stanza addTextNode:@"http://jabber.org/protocol/admin"];
[stanza up];
[stanza up];
[stanza addChildWithName:@"field" andAttributes:{@"var": @"accountjids"}];
for (var i = 0; i < [someJIDs count]; i++)
{
[stanza addChildWithName:@"value"];
[stanza addTextNode:[[someJIDs objectAtIndex:i] bare]];
[stanza up];
}
[stanza up];
[_connection registerSelector:@selector(_didEnableUsers:) ofObject:self withDict:params];
[_connection send:stanza];
}
/*! @ignore
*/
- (void)_didEnableUsers:(TNStropheStanza)aStanza
{
if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didEnableUsers:)])
[_delegate serverAdmin:self didEnableUsers:aStanza];
else
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationSetUserEnabledNotification object:self userInfo:aStanza];
}
@end
/*! Subclass of TNStropheServerAdministration that handles
the ejabberd way to get registred users
*/
@implementation TNStropheEjabberdAdministration : TNStropheServerAdministration
/*! get all the rgistered users
ONLY WITH EJABBERD
*/
- (void)registredUsers
{
var uid = [_connection getUniqueId],
stanza = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"get"}],
params = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
[stanza setTo:_server];
[stanza addChildWithName:@"query" andAttributes:{
@"xmlns": @"http://jabber.org/protocol/disco#items",
@"node": @"all users"}];
[_connection registerSelector:@selector(_didGetRegisteredUsers:) ofObject:self withDict:params];
[_connection send:stanza];
}
- (void)_didGetRegisteredUsers:(TNStropheStanza)aStanza
{
if ([aStanza type] != @"result")
[CPException raise:@"error" reason:@"stanza error"];
var users = [CPArray array],
items = [aStanza childrenWithName:@"item"];
for (var i = 0; i < [items count]; i++)
[users addObject:[TNStropheJID stropheJIDWithString:[[items objectAtIndex:i] valueForAttribute:@"jid"]]];
if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didGetRegisteredUsers:)])
[_delegate serverAdmin:self didGetRegisteredUsers:users];
else
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationGetRegisteredUserNotification object:self userInfo:users];
}
@end