00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "privacymanager.h"
00016 #include "clientbase.h"
00017
00018 #include <sstream>
00019
00020 namespace gloox
00021 {
00022
00023 PrivacyManager::PrivacyManager( ClientBase *parent )
00024 : m_parent( parent ), m_privacyListHandler( 0 )
00025 {
00026 if( m_parent )
00027 m_parent->registerIqHandler( this, XMLNS_PRIVACY );
00028 }
00029
00030 PrivacyManager::~PrivacyManager()
00031 {
00032 if( m_parent )
00033 m_parent->removeIqHandler( XMLNS_PRIVACY );
00034 }
00035
00036 std::string PrivacyManager::requestListNames()
00037 {
00038 std::string id = m_parent->getID();
00039
00040 Tag *iq = new Tag( "iq" );
00041 iq->addAttrib( "type", "get" );
00042 iq->addAttrib( "id", id );
00043 Tag *q = new Tag( "query" );
00044 q->addAttrib( "xmlns", XMLNS_PRIVACY );
00045 iq->addChild( q );
00046
00047 m_parent->trackID( this, id, PL_REQUEST_NAMES );
00048 m_parent->send( iq );
00049 return id;
00050 }
00051
00052 std::string PrivacyManager::requestList( const std::string& name )
00053 {
00054 std::string id = m_parent->getID();
00055
00056 Tag *iq = new Tag( "iq" );
00057 iq->addAttrib( "type", "get" );
00058 iq->addAttrib( "id", id );
00059 Tag *q = new Tag( "query" );
00060 q->addAttrib( "xmlns", XMLNS_PRIVACY );
00061 Tag *l = new Tag( "list" );
00062 l->addAttrib( "name", name );
00063 q->addChild( l );
00064 iq->addChild( q );
00065
00066 m_parent->trackID( this, id, PL_REQUEST_LIST );
00067 m_parent->send( iq );
00068 return id;
00069 }
00070
00071 std::string PrivacyManager::removeList( const std::string& name )
00072 {
00073 std::string id = m_parent->getID();
00074
00075 Tag *iq = new Tag( "iq" );
00076 iq->addAttrib( "type", "set" );
00077 iq->addAttrib( "id", id );
00078 Tag *q = new Tag( "query" );
00079 q->addAttrib( "xmlns", XMLNS_PRIVACY );
00080 Tag *l = new Tag( "list" );
00081 l->addAttrib( "name", name );
00082 q->addChild( l );
00083 iq->addChild( q );
00084
00085 m_parent->trackID( this, id, PL_REMOVE );
00086 m_parent->send( iq );
00087 return id;
00088 }
00089
00090 std::string PrivacyManager::setDefault( const std::string& name )
00091 {
00092 std::string id = m_parent->getID();
00093
00094 Tag *iq = new Tag( "iq" );
00095 iq->addAttrib( "type", "set" );
00096 iq->addAttrib( "id", id );
00097 Tag *q = new Tag( "query" );
00098 q->addAttrib( "xmlns", XMLNS_PRIVACY );
00099 Tag *d = new Tag( "default" );
00100 d->addAttrib( "name", name );
00101 q->addChild( d );
00102 iq->addChild( q );
00103
00104 m_parent->trackID( this, id, PL_DEFAULT );
00105 m_parent->send( iq );
00106 return id;
00107 }
00108
00109 std::string PrivacyManager::unsetDefault()
00110 {
00111 std::string id = m_parent->getID();
00112
00113 Tag *iq = new Tag( "iq" );
00114 iq->addAttrib( "type", "set" );
00115 iq->addAttrib( "id", id );
00116 Tag *q = new Tag( "query" );
00117 q->addAttrib( "xmlns", XMLNS_PRIVACY );
00118 Tag *d = new Tag( "default" );
00119 q->addChild( d );
00120 iq->addChild( q );
00121
00122 m_parent->trackID( this, id, PL_UNSET_DEFAULT );
00123 m_parent->send( iq );
00124 return id;
00125 }
00126
00127 std::string PrivacyManager::setActive( const std::string& name )
00128 {
00129 std::string id = m_parent->getID();
00130
00131 Tag *iq = new Tag( "iq" );
00132 iq->addAttrib( "type", "set" );
00133 iq->addAttrib( "id", id );
00134 Tag *q = new Tag( "query" );
00135 q->addAttrib( "xmlns", XMLNS_PRIVACY );
00136 Tag *a = new Tag( "active" );
00137 a->addAttrib( "name", name );
00138 q->addChild( a );
00139 iq->addChild( q );
00140
00141 m_parent->trackID( this, id, PL_ACTIVATE );
00142 m_parent->send( iq );
00143 return id;
00144 }
00145
00146 std::string PrivacyManager::unsetActive()
00147 {
00148 std::string id = m_parent->getID();
00149
00150 Tag *iq = new Tag( "iq" );
00151 iq->addAttrib( "type", "set" );
00152 iq->addAttrib( "id", id );
00153 Tag *q = new Tag( "query" );
00154 q->addAttrib( "xmlns", XMLNS_PRIVACY );
00155 Tag *a = new Tag( "active" );
00156 q->addChild( a );
00157 iq->addChild( q );
00158
00159 m_parent->trackID( this, id, PL_UNSET_ACTIVATE );
00160 m_parent->send( iq );
00161 return id;
00162 }
00163
00164 std::string PrivacyManager::store( const std::string& name, PrivacyListHandler::PrivacyList& list )
00165 {
00166 std::string id = m_parent->getID();
00167
00168 Tag *iq = new Tag( "iq" );
00169 iq->addAttrib( "type", "set" );
00170 iq->addAttrib( "id", id );
00171 Tag *q = new Tag( "query" );
00172 q->addAttrib( "xmlns", XMLNS_PRIVACY );
00173 Tag *l = new Tag( "list" );
00174 l->addAttrib( "name", name );
00175
00176 int count = 0;
00177 PrivacyListHandler::PrivacyList::iterator it = list.begin();
00178 for( ; it != list.end(); ++it )
00179 {
00180 Tag *i = new Tag( "item" );
00181 l->addChild( i );
00182 switch( (*it).type() )
00183 {
00184 case PrivacyItem::TYPE_JID:
00185 i->addAttrib( "type", "jid" );
00186 break;
00187 case PrivacyItem::TYPE_GROUP:
00188 i->addAttrib( "type", "group" );
00189 break;
00190 case PrivacyItem::TYPE_SUBSCRIPTION:
00191 i->addAttrib( "type", "subscription" );
00192 break;
00193 default:
00194 break;
00195 }
00196
00197 switch( (*it).action() )
00198 {
00199 case PrivacyItem::ACTION_ALLOW:
00200 i->addAttrib( "action", "allow" );
00201 break;
00202 case PrivacyItem::ACTION_DENY:
00203 i->addAttrib( "action", "deny" );
00204 break;
00205 }
00206
00207 int pType = (*it).packetType();
00208 if( pType != 15 )
00209 {
00210 if( pType & PrivacyItem::PACKET_MESSAGE )
00211 i->addChild( new Tag( "message" ) );
00212 if( pType & PrivacyItem::PACKET_PRESENCE_IN )
00213 i->addChild( new Tag( "presence-in" ) );
00214 if( pType & PrivacyItem::PACKET_PRESENCE_OUT )
00215 i->addChild( new Tag( "presence-out" ) );
00216 if( pType & PrivacyItem::PACKET_IQ )
00217 i->addChild( new Tag( "iq" ) );
00218 }
00219
00220 i->addAttrib( "value", (*it).value() );
00221
00222 std::ostringstream oss;
00223 oss << ++count;
00224 i->addAttrib( "order", oss.str() );
00225 }
00226
00227 q->addChild( l );
00228 iq->addChild( q );
00229
00230 m_parent->trackID( this, id, PL_STORE );
00231 m_parent->send( iq );
00232 return id;
00233 }
00234
00235 bool PrivacyManager::handleIq( Stanza *stanza )
00236 {
00237 if( stanza->subtype() != STANZA_IQ_SET || !m_privacyListHandler )
00238 return false;
00239
00240 Tag *l = stanza->findChild( "query" )->findChild( "list" );
00241 if( l->hasAttribute( "name" ) )
00242 {
00243 std::string name = l->findAttribute( "name" );
00244 m_privacyListHandler->handlePrivacyListChanged( name );
00245
00246 Tag *iq = new Tag( "iq" );
00247 iq->addAttrib( "type", "result" );
00248 iq->addAttrib( "id", stanza->id() );
00249 m_parent->send( iq );
00250 return true;
00251 }
00252
00253 return false;
00254 }
00255
00256 bool PrivacyManager::handleIqID( Stanza *stanza, int context )
00257 {
00258 if( stanza->subtype() != STANZA_IQ_RESULT || !m_privacyListHandler )
00259 return false;
00260
00261 switch( stanza->subtype() )
00262 {
00263 case STANZA_IQ_RESULT:
00264 switch( context )
00265 {
00266 case PL_STORE:
00267 m_privacyListHandler->handlePrivacyListResult( stanza->id(),
00268 PrivacyListHandler::RESULT_STORE_SUCCESS );
00269 break;
00270 case PL_ACTIVATE:
00271 m_privacyListHandler->handlePrivacyListResult( stanza->id(),
00272 PrivacyListHandler::RESULT_ACTIVATE_SUCCESS );
00273 break;
00274 case PL_DEFAULT:
00275 m_privacyListHandler->handlePrivacyListResult( stanza->id(),
00276 PrivacyListHandler::RESULT_DEFAULT_SUCCESS );
00277 break;
00278 case PL_REMOVE:
00279 m_privacyListHandler->handlePrivacyListResult( stanza->id(),
00280 PrivacyListHandler::RESULT_REMOVE_SUCCESS );
00281 break;
00282 case PL_REQUEST_NAMES:
00283 {
00284 StringList lists;
00285 std::string def;
00286 std::string active;
00287 Tag *q = stanza->findChild( "query" );
00288 Tag::TagList l = q->children();
00289 Tag::TagList::const_iterator it = l.begin();
00290 for( ; it != l.end(); ++it )
00291 {
00292 if( (*it)->name() == "default" )
00293 def = (*it)->findAttribute( "name" );
00294 if( (*it)->name() == "active" )
00295 def = (*it)->findAttribute( "name" );
00296 if( (*it)->name() == "list" )
00297 {
00298 const std::string name = (*it)->findAttribute( "name" );
00299 lists.push_back( name );
00300 }
00301 }
00302
00303 m_privacyListHandler->handlePrivacyListNames( def, active, lists );
00304 break;
00305 }
00306 case PL_REQUEST_LIST:
00307 {
00308 PrivacyListHandler::PrivacyList items;
00309
00310 Tag *list = stanza->findChild( "query" )->findChild( "list" );
00311 const std::string name = list->name();
00312 Tag::TagList l = list->children();
00313 Tag::TagList::iterator it = l.begin();
00314 for( ; it != l.end(); ++it )
00315 {
00316 PrivacyItem::ItemType type;
00317 PrivacyItem::ItemAction action;
00318 int packetType = 0;
00319
00320 const std::string t = (*it)->findAttribute( "type" );
00321 if( t == "jid" )
00322 type = PrivacyItem::TYPE_JID;
00323 else if( t == "group" )
00324 type = PrivacyItem::TYPE_GROUP;
00325 else if( t == "subscription" )
00326 type = PrivacyItem::TYPE_SUBSCRIPTION;
00327 else
00328 type = PrivacyItem::TYPE_UNDEFINED;
00329
00330 const std::string a = (*it)->findAttribute( "action" );
00331 if( a == "allow" )
00332 action = PrivacyItem::ACTION_ALLOW;
00333 else if( a == "deny" )
00334 action = PrivacyItem::ACTION_DENY;
00335 else
00336 action = PrivacyItem::ACTION_ALLOW;
00337
00338 std::string value = (*it)->findAttribute( "value" );
00339
00340 Tag::TagList c = (*it)->children();
00341 Tag::TagList::const_iterator it_c = c.begin();
00342 for( ; it_c != c.end(); ++it_c )
00343 {
00344 if( (*it_c)->name() == "iq" )
00345 packetType |= PrivacyItem::PACKET_IQ;
00346 else if( (*it_c)->name() == "presence-out" )
00347 packetType |= PrivacyItem::PACKET_PRESENCE_OUT;
00348 else if( (*it_c)->name() == "presence-in" )
00349 packetType |= PrivacyItem::PACKET_PRESENCE_IN;
00350 else if( (*it_c)->name() == "message" )
00351 packetType |= PrivacyItem::PACKET_MESSAGE;
00352 }
00353
00354 PrivacyItem item( type, action, packetType, value );
00355 items.push_back( item );
00356 }
00357 m_privacyListHandler->handlePrivacyList( name, items );
00358 break;
00359 }
00360 }
00361 break;
00362
00363 case STANZA_IQ_ERROR:
00364 {
00365 Tag *e = stanza->findChild( "error" );
00366 if( e->hasChild( "conflict" ) )
00367 m_privacyListHandler->handlePrivacyListResult( stanza->id(),
00368 PrivacyListHandler::RESULT_CONFLICT );
00369 else if( e->hasChild( "item-not-found" ) )
00370 m_privacyListHandler->handlePrivacyListResult( stanza->id(),
00371 PrivacyListHandler::RESULT_ITEM_NOT_FOUND );
00372 else if( e->hasChild( "bad-request" ) )
00373 m_privacyListHandler->handlePrivacyListResult( stanza->id(),
00374 PrivacyListHandler::RESULT_BAD_REQUEST );
00375 break;
00376 }
00377
00378 default:
00379 break;
00380 }
00381 return false;
00382 }
00383
00384 void PrivacyManager::registerPrivacyListHandler( PrivacyListHandler *plh )
00385 {
00386 m_privacyListHandler = plh;
00387 }
00388
00389 void PrivacyManager::removePrivacyListHandler()
00390 {
00391 m_privacyListHandler = 0;
00392 }
00393
00394 }