privacymanager.cpp

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

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1