privacymanager.cpp

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

Generated on Sat Nov 10 08:50:27 2007 for gloox by  doxygen 1.5.3-20071008