StropheCappuccino API 1.0.0
/tmp/tempDoc.doc/TNStropheServerAdministration.j
Go to the documentation of this file.
00001 /*
00002  * TNStropheServerAdministration.j
00003  *
00004  * Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU Affero General Public License as
00007  * published by the Free Software Foundation, either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU Affero General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Affero General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 
00020 
00021 TNStropheServerAdministrationGetConnectedUserNotification       = @"TNStropheServerAdministrationGetConnectedUserNotification";
00022 TNStropheServerAdministrationGetRegisteredUserNotification      = @"TNStropheServerAdministrationGetRegisteredUserNotification";
00023 TNStropheServerAdministrationRegisterUserNotification           = @"TNStropheServerAdministrationRegisterUserNotification";
00024 TNStropheServerAdministrationSendAnnouncementNotification       = @"TNStropheServerAdministrationSendAnnouncementNotification";
00025 TNStropheServerAdministrationSetUserEnabledNotification         = @"TNStropheServerAdministrationSetUserEnabledNotification";
00026 TNStropheServerAdministrationUnregisterUserNotification         = @"TNStropheServerAdministrationUnregisterUserNotification";
00027 
00028 
00032 @implementation TNStropheServerAdministration : CPObject
00033 {
00034     TNStropheConnection     _connection;
00035     TNStropheJID            _server;
00036     id                      _delegate;
00037 }
00038 
00039 #pragma mark -
00040 #pragma mark Initialization
00041 
00046 - (id)initWithConnection:(TNStropheConnection)aConnection server:(TNStropheJID)aServer
00047 {
00048     if (self = [super init])
00049     {
00050         _connection = aConnection;
00051         _server     = aServer
00052     }
00053 
00054     return self;
00055 }
00056 
00057 
00058 #pragma mark -
00059 #pragma mark Utilities
00060 
00065 - (void)sendAction:(CPString)anAction selector:(SEL)aSelector
00066 {
00067     var uid         = [_connection getUniqueId],
00068         stanza      = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
00069         params      = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
00070 
00071     [stanza setTo:_server];
00072     [stanza addChildWithName:@"command" andAttributes:{
00073         @"xmlns": Strophe.NS.COMMAND,
00074         @"action": @"execute",
00075         @"node": anAction}];
00076 
00077     [_connection registerSelector:aSelector ofObject:self withDict:params];
00078     [_connection send:stanza];
00079 }
00080 
00081 
00082 #pragma mark -
00083 #pragma mark Announcement
00084 
00089 - (void)sendAnnouncement:(CPString)anAnnouncement subject:(CPString)aSubject
00090 {
00091     var uid         = [_connection getUniqueId],
00092         stanza      = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
00093         params      = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
00094 
00095     [stanza setTo:_server];
00096     [stanza addChildWithName:@"command" andAttributes:{
00097         @"xmlns": Strophe.NS.COMMAND,
00098         @"node": @"http://jabber.org/protocol/admin#announce"}];
00099 
00100     [stanza addChildWithName:@"x" andAttributes:{@"xmlns": @"jabber:x:data", @"type": @"submit"}];
00101 
00102     [stanza addChildWithName:@"field" andAttributes:{@"type": @"hidden", @"var": @"FORM_TYPE"}];
00103     [stanza addChildWithName:@"value"];
00104     [stanza addTextNode:@"http://jabber.org/protocol/admin"];
00105     [stanza up];
00106     [stanza up];
00107     if (aSubject)
00108     {
00109         [stanza addChildWithName:@"field" andAttributes:{@"var": @"subject"}];
00110         [stanza addChildWithName:@"value"];
00111         [stanza addTextNode:aSubject];
00112         [stanza up];
00113         [stanza up];
00114     }
00115     [stanza addChildWithName:@"field" andAttributes:{@"var": @"body"}];
00116     [stanza addChildWithName:@"value"];
00117     [stanza addTextNode:anAnnouncement];
00118 
00119     [_connection registerSelector:@selector(_didSendAnnouncement:) ofObject:self withDict:params];
00120     [_connection send:stanza];
00121 }
00122 
00125 - (void)_didSendAnnouncement:(TNStropheStanza)aStanza
00126 {
00127     if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didSendAnnouncement:)])
00128         [_delegate serverAdmin:self didSendAnnouncement:aStanza];
00129     else
00130         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationSendAnnouncementNotification object:self userInfo:aStanza];
00131 }
00132 
00133 
00134 #pragma mark -
00135 #pragma mark Users
00136 
00140 - (void)registeredUsers
00141 {
00142     [self sendAction:@"http://jabber.org/protocol/admin#get-registered-users-list" selector:@selector(_didGetRegisteredUsers:)]
00143 }
00144 
00147 - (void)_didGetRegisteredUsers:(TNStropheStanza)aStanza
00148 {
00149     if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didGetRegisteredUsers:)])
00150         [_delegate serverAdmin:self didGetRegisteredUsers:aStanza];
00151     else
00152         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationGetRegisteredUserNotification object:self userInfo:aStanza];
00153 }
00154 
00158 - (void)connectedUsers
00159 {
00160     [self sendAction:@"http://jabber.org/protocol/admin#get-online-users-list" selector:@selector(_didGetConnectedUsers:)]
00161 }
00162 
00165 - (void)_didGetConnectedUsers:(TNStropheStanza)aStanza
00166 {
00167     if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didGetConnectedUsers:)])
00168         [_delegate serverAdmin:self didGetConnectedUsers:aStanza];
00169     else
00170         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationGetConnectedUserNotification object:self userInfo:aStanza];
00171 }
00172 
00180 - (void)registerUser:(TNStropheJID)aJID  password:(CPString)aPassword name:(CPString)aName surname:(CPString)aSurname email:(CPString)anEmail
00181 {
00182     var uid         = [_connection getUniqueId],
00183         stanza      = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
00184         params      = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
00185 
00186     [stanza setTo:_server];
00187     [stanza addChildWithName:@"command" andAttributes:{
00188         @"xmlns": Strophe.NS.COMMAND,
00189         @"node": @"http://jabber.org/protocol/admin#add-user"}];
00190 
00191     [stanza addChildWithName:@"x" andAttributes:{@"xmlns": @"jabber:x:data", @"type": @"submit"}];
00192 
00193     [stanza addChildWithName:@"field" andAttributes:{@"type": @"hidden", @"var": @"FORM_TYPE"}];
00194     [stanza addChildWithName:@"value"];
00195     [stanza addTextNode:@"http://jabber.org/protocol/admin"];
00196     [stanza up];
00197     [stanza up];
00198 
00199     [stanza addChildWithName:@"field" andAttributes:{@"var": @"accountjid"}];
00200     [stanza addChildWithName:@"value"];
00201     [stanza addTextNode:[aJID bare]];
00202     [stanza up];
00203     [stanza up];
00204 
00205     [stanza addChildWithName:@"field" andAttributes:{@"var": @"password"}];
00206     [stanza addChildWithName:@"value"];
00207     [stanza addTextNode:aPassword];
00208     [stanza up];
00209     [stanza up];
00210 
00211     [stanza addChildWithName:@"field" andAttributes:{@"var": @"password-verify"}];
00212     [stanza addChildWithName:@"value"];
00213     [stanza addTextNode:aPassword];
00214     [stanza up];
00215     [stanza up];
00216 
00217     [stanza addChildWithName:@"field" andAttributes:{@"var": @"email"}];
00218     [stanza addChildWithName:@"value"];
00219     [stanza addTextNode:anEmail];
00220     [stanza up];
00221     [stanza up];
00222 
00223     [stanza addChildWithName:@"field" andAttributes:{@"var": @"given_name"}];
00224     [stanza addChildWithName:@"value"];
00225     [stanza addTextNode:aName];
00226     [stanza up];
00227     [stanza up];
00228 
00229     [stanza addChildWithName:@"field" andAttributes:{@"var": @"surname"}];
00230     [stanza addChildWithName:@"value"];
00231     [stanza addTextNode:aSurname];
00232     [stanza up];
00233     [stanza up];
00234 
00235     [_connection registerSelector:@selector(_didRegisterUser:) ofObject:self withDict:params];
00236     [_connection send:stanza];
00237 }
00238 
00241 - (void)_didRegisterUser:(TNStropheStanza)aStanza
00242 {
00243     if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didRegisterUser:)])
00244         [_delegate serverAdmin:self didRegisterUser:aStanza];
00245     else
00246         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationRegisterUserNotification object:self userInfo:aStanza];
00247 }
00248 
00252 - (void)unregisterUsers:(CPArray)someJIDs
00253 {
00254     var uid         = [_connection getUniqueId],
00255         stanza      = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
00256         params      = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
00257 
00258     [stanza setTo:_server];
00259     [stanza addChildWithName:@"command" andAttributes:{
00260         @"xmlns": Strophe.NS.COMMAND,
00261         @"node": @"http://jabber.org/protocol/admin#delete-user"}];
00262 
00263     [stanza addChildWithName:@"x" andAttributes:{@"xmlns": @"jabber:x:data", @"type": @"submit"}];
00264 
00265     [stanza addChildWithName:@"field" andAttributes:{@"type": @"hidden", @"var": @"FORM_TYPE"}];
00266     [stanza addChildWithName:@"value"];
00267     [stanza addTextNode:@"http://jabber.org/protocol/admin"];
00268     [stanza up];
00269     [stanza up];
00270 
00271     [stanza addChildWithName:@"field" andAttributes:{@"var": @"accountjids"}];
00272     for (var i = 0; i < [someJIDs count]; i++)
00273     {
00274         [stanza addChildWithName:@"value"];
00275         [stanza addTextNode:[[someJIDs objectAtIndex:i] bare]];
00276         [stanza up];
00277     }
00278     [stanza up];
00279 
00280     [_connection registerSelector:@selector(_didUnregisterUser:) ofObject:self withDict:params];
00281     [_connection send:stanza];
00282 }
00283 
00286 - (void)_didUnregisterUser:(TNStropheStanza)aStanza
00287 {
00288     if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didUnregisterUser:)])
00289         [_delegate serverAdmin:self didUnregisterUser:aStanza];
00290     else
00291         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationUnregisterUserNotification object:self userInfo:aStanza];
00292 }
00293 
00298 - (void)setUsers:(CPArray)someJIDs enabled:(BOOL)shouldEnable
00299 {
00300     var uid         = [_connection getUniqueId],
00301         stanza      = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
00302         params      = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"],
00303         node        = (shouldEnable) ? @"http://jabber.org/protocol/admin#reenable-user" : @"http://jabber.org/protocol/admin#disable-user";
00304 
00305     [stanza setTo:_server];
00306     [stanza addChildWithName:@"command" andAttributes:{
00307         @"xmlns": Strophe.NS.COMMAND,
00308         @"node": node}];
00309 
00310     [stanza addChildWithName:@"x" andAttributes:{@"xmlns": @"jabber:x:data", @"type": @"submit"}];
00311 
00312     [stanza addChildWithName:@"field" andAttributes:{@"type": @"hidden", @"var": @"FORM_TYPE"}];
00313     [stanza addChildWithName:@"value"];
00314     [stanza addTextNode:@"http://jabber.org/protocol/admin"];
00315     [stanza up];
00316     [stanza up];
00317 
00318     [stanza addChildWithName:@"field" andAttributes:{@"var": @"accountjids"}];
00319     for (var i = 0; i < [someJIDs count]; i++)
00320     {
00321         [stanza addChildWithName:@"value"];
00322         [stanza addTextNode:[[someJIDs objectAtIndex:i] bare]];
00323         [stanza up];
00324     }
00325     [stanza up];
00326 
00327     [_connection registerSelector:@selector(_didEnableUsers:) ofObject:self withDict:params];
00328     [_connection send:stanza];
00329 }
00330 
00333 - (void)_didEnableUsers:(TNStropheStanza)aStanza
00334 {
00335     if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didEnableUsers:)])
00336         [_delegate serverAdmin:self didEnableUsers:aStanza];
00337     else
00338         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationSetUserEnabledNotification object:self userInfo:aStanza];
00339 }
00340 
00341 @end
00342 
00343 
00347 @implementation TNStropheEjabberdAdministration : TNStropheServerAdministration
00348 
00352 - (void)registredUsers
00353 {
00354     var uid         = [_connection getUniqueId],
00355         stanza      = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"get"}],
00356         params      = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
00357 
00358     [stanza setTo:_server];
00359     [stanza addChildWithName:@"query" andAttributes:{
00360         @"xmlns": @"http://jabber.org/protocol/disco#items",
00361         @"node": @"all users"}];
00362 
00363     [_connection registerSelector:@selector(_didGetRegisteredUsers:) ofObject:self withDict:params];
00364     [_connection send:stanza];
00365 }
00366 
00367 - (void)_didGetRegisteredUsers:(TNStropheStanza)aStanza
00368 {
00369     if ([aStanza type] != @"result")
00370         [CPException raise:@"error" reason:@"stanza error"];
00371 
00372     var users = [CPArray array],
00373         items = [aStanza childrenWithName:@"item"];
00374 
00375     for (var i = 0; i < [items count]; i++)
00376         [users addObject:[TNStropheJID stropheJIDWithString:[[items objectAtIndex:i] valueForAttribute:@"jid"]]];
00377 
00378     if (_delegate && [_delegate respondsToSelector:@selector(serverAdmin:didGetRegisteredUsers:)])
00379         [_delegate serverAdmin:self didGetRegisteredUsers:users];
00380     else
00381         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheServerAdministrationGetRegisteredUserNotification object:self userInfo:users];
00382 }
00383 
00384 
00385 @end
00386 
00387 @implementation TNStropheServerAdministration (CPSynthesizedAccessors)
00388 
00392 - (TNStropheConnection)connection
00393 {
00394     return _connection;
00395 }
00396 
00400 - (void)setConnection:(TNStropheConnection)aValue
00401 {
00402     _connection = aValue;
00403 }
00404 
00408 - (TNStropheJID)server
00409 {
00410     return _server;
00411 }
00412 
00416 - (void)setServer:(TNStropheJID)aValue
00417 {
00418     _server = aValue;
00419 }
00420 
00424 - (id)delegate
00425 {
00426     return _delegate;
00427 }
00428 
00432 - (void)setDelegate:(id)aValue
00433 {
00434     _delegate = aValue;
00435 }
00436 
00437 @end
 All Classes Namespaces Files Functions Variables