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