00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "privacymanager.h"
00016 #include "clientbase.h"
00017 #include "error.h"
00018
00019 #ifndef _WIN32_WCE
00020 # include <sstream>
00021 #endif
00022
00023 namespace gloox
00024 {
00025
00026 PrivacyManager::PrivacyManager( ClientBase* parent )
00027 : m_parent( parent ), m_privacyListHandler( 0 )
00028 {
00029 if( m_parent )
00030 m_parent->registerIqHandler( this, XMLNS_PRIVACY );
00031 }
00032
00033 PrivacyManager::~PrivacyManager()
00034 {
00035 if( m_parent )
00036 {
00037 m_parent->removeIqHandler( this, XMLNS_PRIVACY );
00038 m_parent->removeIDHandler( this );
00039 }
00040 }
00041
00042 std::string PrivacyManager::operation( int context, const std::string& name )
00043 {
00044 const std::string& id = m_parent->getID();
00045 IQ::IqType iqType = IQ::Set;
00046 if( context == PLRequestNames || context == PLRequestList )
00047 iqType = IQ::Get;
00048 IQ iq( iqType, JID(), id, XMLNS_PRIVACY );
00049 if( context != PLRequestNames )
00050 {
00051 std::string child;
00052 switch( context )
00053 {
00054 case PLRequestList:
00055 case PLRemove:
00056 child = "list";
00057 break;
00058 case PLDefault:
00059 case PLUnsetDefault:
00060 child = "default";
00061 break;
00062 case PLActivate:
00063 case PLUnsetActivate:
00064 child = "active";
00065 break;
00066 }
00067 Tag* l = new Tag( iq.query(), child );
00068 if( !name.empty() )
00069 l->addAttribute( "name", name );
00070 }
00071 m_parent->send( iq, this, context );
00072 return id;
00073 }
00074
00075 std::string PrivacyManager::store( const std::string& name, const PrivacyListHandler::PrivacyList& list )
00076 {
00077 if( list.empty() )
00078 return EmptyString;
00079
00080 const std::string& id = m_parent->getID();
00081
00082 IQ iq( IQ::Set, JID(), id, XMLNS_PRIVACY );
00083 Tag* l = new Tag( iq.query(), "list" );
00084 l->addAttribute( "name", name );
00085
00086 int count = 0;
00087 PrivacyListHandler::PrivacyList::const_iterator it = list.begin();
00088 for( ; it != list.end(); ++it )
00089 {
00090 Tag* i = new Tag( l, "item" );
00091
00092 switch( (*it).type() )
00093 {
00094 case PrivacyItem::TypeJid:
00095 i->addAttribute( TYPE, "jid" );
00096 break;
00097 case PrivacyItem::TypeGroup:
00098 i->addAttribute( TYPE, "group" );
00099 break;
00100 case PrivacyItem::TypeSubscription:
00101 i->addAttribute( TYPE, "subscription" );
00102 break;
00103 default:
00104 break;
00105 }
00106
00107 switch( (*it).action() )
00108 {
00109 case PrivacyItem::ActionAllow:
00110 i->addAttribute( "action", "allow" );
00111 break;
00112 case PrivacyItem::ActionDeny:
00113 i->addAttribute( "action", "deny" );
00114 break;
00115 }
00116
00117 int pType = (*it).packetType();
00118 if( pType != 15 )
00119 {
00120 if( pType & PrivacyItem::PacketMessage )
00121 new Tag( i, "message" );
00122 if( pType & PrivacyItem::PacketPresenceIn )
00123 new Tag( i, "presence-in" );
00124 if( pType & PrivacyItem::PacketPresenceOut )
00125 new Tag( i, "presence-out" );
00126 if( pType & PrivacyItem::PacketIq )
00127 new Tag( i, "iq" );
00128 }
00129
00130 i->addAttribute( "value", (*it).value() );
00131 i->addAttribute( "order", ++count );
00132 }
00133
00134 m_parent->send( iq, this, PLStore );
00135 return id;
00136 }
00137
00138 bool PrivacyManager::handleIq( const IQ& iq )
00139 {
00140 if( iq.subtype() != IQ::Set || !m_privacyListHandler )
00141 return false;
00142
00143 Tag* l = iq.query()->findChild( "list" );
00144 if( l->hasAttribute( "name" ) )
00145 {
00146 const std::string& name = l->findAttribute( "name" );
00147 m_privacyListHandler->handlePrivacyListChanged( name );
00148
00149 IQ re( IQ::Result, JID(), iq.id() );
00150 m_parent->send( re );
00151 return true;
00152 }
00153
00154 return false;
00155 }
00156
00157 void PrivacyManager::handleIqID( const IQ& iq, int context )
00158 {
00159 if( !m_privacyListHandler )
00160 return;
00161
00162 switch( iq.subtype() )
00163 {
00164 case IQ::Result:
00165 switch( context )
00166 {
00167 case PLStore:
00168 m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultStoreSuccess );
00169 break;
00170 case PLActivate:
00171 m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultActivateSuccess );
00172 break;
00173 case PLDefault:
00174 m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultDefaultSuccess );
00175 break;
00176 case PLRemove:
00177 m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultRemoveSuccess );
00178 break;
00179 case PLRequestNames:
00180 {
00181 StringList lists;
00182 std::string def;
00183 std::string active;
00184 Tag* q = iq.query();
00185 const TagList& l = q->children();
00186 TagList::const_iterator it = l.begin();
00187 for( ; it != l.end(); ++it )
00188 {
00189 const std::string& name = (*it)->findAttribute( "name" );
00190 if( (*it)->name() == "default" )
00191 def = name;
00192 else if( (*it)->name() == "active" )
00193 active = name;
00194 else if( (*it)->name() == "list" )
00195 lists.push_back( name );
00196 }
00197
00198 m_privacyListHandler->handlePrivacyListNames( def, active, lists );
00199 break;
00200 }
00201 case PLRequestList:
00202 {
00203 PrivacyListHandler::PrivacyList items;
00204
00205 Tag* list = iq.query()->findChild( "list" );
00206 const std::string& name = list->findAttribute( "name" );
00207 const TagList& l = list->children();
00208 TagList::const_iterator it = l.begin();
00209 for( ; it != l.end(); ++it )
00210 {
00211 PrivacyItem::ItemType type;
00212 PrivacyItem::ItemAction action;
00213 int packetType = 0;
00214
00215 const std::string& t = (*it)->findAttribute( TYPE );
00216 if( t == "jid" )
00217 type = PrivacyItem::TypeJid;
00218 else if( t == "group" )
00219 type = PrivacyItem::TypeGroup;
00220 else if( t == "subscription" )
00221 type = PrivacyItem::TypeSubscription;
00222 else
00223 type = PrivacyItem::TypeUndefined;
00224
00225 const std::string& a = (*it)->findAttribute( "action" );
00226 if( a == "allow" )
00227 action = PrivacyItem::ActionAllow;
00228 else if( a == "deny" )
00229 action = PrivacyItem::ActionDeny;
00230 else
00231 action = PrivacyItem::ActionAllow;
00232
00233 const std::string& value = (*it)->findAttribute( "value" );
00234
00235 const TagList& c = (*it)->children();
00236 TagList::const_iterator it_c = c.begin();
00237 for( ; it_c != c.end(); ++it_c )
00238 {
00239 if( (*it_c)->name() == "iq" )
00240 packetType |= PrivacyItem::PacketIq;
00241 else if( (*it_c)->name() == "presence-out" )
00242 packetType |= PrivacyItem::PacketPresenceOut;
00243 else if( (*it_c)->name() == "presence-in" )
00244 packetType |= PrivacyItem::PacketPresenceIn;
00245 else if( (*it_c)->name() == "message" )
00246 packetType |= PrivacyItem::PacketMessage;
00247 }
00248
00249 PrivacyItem item( type, action, packetType, value );
00250 items.push_back( item );
00251 }
00252 m_privacyListHandler->handlePrivacyList( name, items );
00253 break;
00254 }
00255 }
00256 break;
00257
00258 case IQ::Error:
00259 {
00260 switch( iq.error()->error() )
00261 {
00262 case StanzaErrorConflict:
00263 m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultConflict );
00264 break;
00265 case StanzaErrorItemNotFound:
00266 m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultItemNotFound );
00267 break;
00268 case StanzaErrorBadRequest:
00269 m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultBadRequest );
00270 break;
00271 default:
00272 m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultUnknownError );
00273 break;
00274 }
00275 break;
00276 }
00277
00278 default:
00279 break;
00280 }
00281 }
00282
00283 }