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