File indexing completed on 2024-04-28 07:54:11

0001 /*
0002  * privacymanager.cpp
0003  * Copyright (C) 2006  Remko Troncon
0004  *
0005  * This program is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU General Public License
0007  * as published by the Free Software Foundation; either version 2
0008  * of the License, or (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this library; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0018  * 02110-1301, USA
0019  *
0020  */
0021 #include "xmpp_xmlcommon.h"
0022 #include "xmpp_jid.h"
0023 #include "privacymanager.h"
0024 
0025 #include "jabber_protocol_debug.h"
0026 // #include "jabberprotocol.h"
0027 
0028 #define PRIVACY_NS "jabber:iq:privacy"
0029 
0030 using namespace XMPP;
0031 
0032 // -----------------------------------------------------------------------------
0033 //
0034 
0035 PrivacyListListener::PrivacyListListener ( Task* parent ) : Task ( parent ) {
0036 }
0037 
0038 bool PrivacyListListener::take ( const QDomElement &e ) {
0039     if ( e.tagName() != "iq" || e.attribute ( "type" ) != "set" )
0040         return false;
0041 
0042     QString ns = queryNS ( e );
0043     if ( ns == "jabber:iq:privacy" ) {
0044         // TODO: Do something with update
0045 
0046         // Confirm receipt
0047         QDomElement iq = createIQ ( doc(), "result", e.attribute ( "from" ), e.attribute ( "id" ) );
0048         send ( iq );
0049         return true;
0050     }
0051 
0052     return false;
0053 }
0054 
0055 // -----------------------------------------------------------------------------
0056 
0057 GetPrivacyListsTask::GetPrivacyListsTask ( Task* parent ) : Task ( parent ) {
0058     iq_ = createIQ ( doc(), "get", "", id() );
0059     QDomElement query = doc()->createElement ( "query" );
0060     query.setAttribute ( "xmlns",PRIVACY_NS );
0061     iq_.appendChild ( query );
0062 }
0063 
0064 void GetPrivacyListsTask::onGo() {
0065     send ( iq_ );
0066 }
0067 
0068 bool GetPrivacyListsTask::take ( const QDomElement &x ) {
0069     if ( !iqVerify ( x, "", id() ) )
0070         return false;
0071 
0072     //qCDebug(JABBER_PROTOCOL_LOG) << "Got reply for privacy lists.";
0073     if ( x.attribute ( "type" ) == "result" ) {
0074         QDomElement tag, q = queryTag ( x );
0075 
0076         for ( QDomNode n = q.firstChild(); !n.isNull(); n = n.nextSibling() ) {
0077             QDomElement e = n.toElement();
0078             if ( e.tagName() == "active" )
0079                 active_ = e.attribute ( "name" );
0080             else if ( e.tagName() == "default" )
0081                 default_ = e.attribute ( "name" );
0082             else if ( e.tagName() == "list" )
0083                 lists_.append ( e.attribute ( "name" ) );
0084             else
0085                 qCWarning(JABBER_PROTOCOL_LOG) << "Unknown tag in privacy lists.";
0086 
0087         }
0088         setSuccess();
0089     }
0090     else {
0091         setError ( x );
0092     }
0093     return true;
0094 }
0095 
0096 const QStringList& GetPrivacyListsTask::lists() {
0097     return lists_;
0098 }
0099 
0100 const QString& GetPrivacyListsTask::defaultList() {
0101     return default_;
0102 }
0103 
0104 const QString& GetPrivacyListsTask::activeList() {
0105     return active_;
0106 }
0107 
0108 // -----------------------------------------------------------------------------
0109 
0110 
0111 SetPrivacyListsTask::SetPrivacyListsTask ( Task* parent ) : Task ( parent ), changeDefault_ ( false ), changeActive_ ( false ), changeList_ ( false ), list_ ( "" ) {
0112 }
0113 
0114 void SetPrivacyListsTask::onGo() {
0115     QDomElement iq_ = createIQ ( doc(), "set", "", id() );
0116     QDomElement query = doc()->createElement ( "query" );
0117     query.setAttribute ( "xmlns",PRIVACY_NS );
0118     iq_.appendChild ( query );
0119 
0120     QDomElement e;
0121     if ( changeDefault_ ) {
0122         //qCDebug(JABBER_PROTOCOL_LOG) << "Changing default privacy list.";
0123         e = doc()->createElement ( "default" );
0124         if ( !value_.isEmpty() )
0125             e.setAttribute ( "name",value_ );
0126     }
0127     else if ( changeActive_ ) {
0128         //qCDebug(JABBER_PROTOCOL_LOG) << "Changing active privacy list.";
0129         e = doc()->createElement ( "active" );
0130         if ( !value_.isEmpty() )
0131             e.setAttribute ( "name",value_ );
0132     }
0133     else if ( changeList_ ) {
0134         //qCDebug(JABBER_PROTOCOL_LOG) << "Changing privacy list.";
0135         e = list_.toXml ( *doc() );
0136     }
0137     else {
0138         qCWarning(JABBER_PROTOCOL_LOG) << "Empty active/default list change request.";
0139         return;
0140     }
0141 
0142     query.appendChild ( e );
0143     send ( iq_ );
0144 }
0145 
0146 void SetPrivacyListsTask::setActive ( const QString& active ) {
0147     value_ = active;
0148     changeDefault_ = false;
0149     changeActive_ = true;
0150     changeList_ = false;
0151 }
0152 
0153 void SetPrivacyListsTask::setDefault ( const QString& d ) {
0154     value_ = d;
0155     changeDefault_ = true;
0156     changeActive_ = false;
0157     changeList_ = true;
0158 }
0159 
0160 void SetPrivacyListsTask::setList ( const PrivacyList& list ) {
0161     //qCDebug(JABBER_PROTOCOL_LOG) << "setList: " << list.toString();
0162     list_ = list;
0163     changeDefault_ = false;
0164     changeActive_ = false;
0165     changeList_ = true;
0166 }
0167 
0168 bool SetPrivacyListsTask::take ( const QDomElement &x ) {
0169     if ( !iqVerify ( x, "", id() ) )
0170         return false;
0171 
0172     if ( x.attribute ( "type" ) == "result" ) {
0173         //qCDebug(JABBER_PROTOCOL_LOG) << "Got successful reply for list change.";
0174         setSuccess();
0175     }
0176     else {
0177         qCWarning(JABBER_PROTOCOL_LOG) << "Got error reply for list change.";
0178         setError ( x );
0179     }
0180     return true;
0181 }
0182 
0183 
0184 // -----------------------------------------------------------------------------
0185 
0186 GetPrivacyListTask::GetPrivacyListTask ( Task* parent, const QString& name ) : Task ( parent ), name_ ( name ), list_ ( PrivacyList ( "" ) ) {
0187     iq_ = createIQ ( doc(), "get", "", id() );
0188     QDomElement query = doc()->createElement ( "query" );
0189     query.setAttribute ( "xmlns",PRIVACY_NS );
0190     iq_.appendChild ( query );
0191     QDomElement list = doc()->createElement ( "list" );
0192     list.setAttribute ( "name",name );
0193     query.appendChild ( list );
0194 }
0195 
0196 void GetPrivacyListTask::onGo() {
0197     //qCDebug(JABBER_PROTOCOL_LOG) << "privacy.cpp: Requesting privacy list %1." << name_;
0198     send ( iq_ );
0199 }
0200 
0201 bool GetPrivacyListTask::take ( const QDomElement &x ) {
0202     if ( !iqVerify ( x, "", id() ) )
0203         return false;
0204 
0205     //qCDebug(JABBER_PROTOCOL_LOG) << qPrintable (QString("Got privacy list %1 reply.").arg(name_));
0206     if ( x.attribute ( "type" ) == "result" ) {
0207         QDomElement q = queryTag ( x );
0208         bool found;
0209         QDomElement listTag = findSubTag ( q,"list",&found );
0210         if ( found ) {
0211             list_ = PrivacyList ( listTag );
0212         }
0213         else {
0214             qCWarning(JABBER_PROTOCOL_LOG) << "No valid list found.";
0215         }
0216         setSuccess();
0217     }
0218     else {
0219         setError ( x );
0220     }
0221     return true;
0222 }
0223 
0224 const PrivacyList& GetPrivacyListTask::list() {
0225     return list_;
0226 }
0227 
0228 
0229 // -----------------------------------------------------------------------------
0230 
0231 PrivacyManager::PrivacyManager ( XMPP::Task* rootTask ) : rootTask_ ( rootTask ), getDefault_waiting_ ( false ), block_waiting_ ( false )
0232 {
0233     listener_ = new PrivacyListListener ( rootTask_ );
0234 }
0235 
0236 PrivacyManager::~PrivacyManager()
0237 {
0238     delete listener_;
0239 }
0240 
0241 void PrivacyManager::requestListNames()
0242 {
0243     GetPrivacyListsTask* t = new GetPrivacyListsTask ( rootTask_ );
0244     connect ( t,&Task::finished,this, &PrivacyManager::receiveLists );
0245     t->go ( true );
0246 }
0247 
0248 void PrivacyManager::requestList ( const QString& name )
0249 {
0250     GetPrivacyListTask* t = new GetPrivacyListTask ( rootTask_, name );
0251     connect ( t,&Task::finished,this, &PrivacyManager::receiveList );
0252     t->go ( true );
0253 }
0254 
0255 void PrivacyManager::block ( const QString& target )
0256 {
0257     block_targets_.push_back ( target );
0258     if ( !block_waiting_ ) {
0259         block_waiting_ = true;
0260         connect ( this,&PrivacyManager::defaultListAvailable,this, &PrivacyManager::block_getDefaultList_success );
0261         connect ( this,&PrivacyManager::defaultListError,this, &PrivacyManager::block_getDefaultList_error );
0262         getDefaultList();
0263     }
0264 }
0265 
0266 void PrivacyManager::block_getDefaultList_success ( const PrivacyList& l_ )
0267 {
0268     PrivacyList l = l_;
0269     disconnect(this, &PrivacyManager::defaultListAvailable, this, &PrivacyManager::block_getDefaultList_success);
0270     disconnect(this, &PrivacyManager::defaultListError, this, &PrivacyManager::block_getDefaultList_error);
0271     block_waiting_ = false;
0272     while ( !block_targets_.isEmpty() )
0273         l.insertItem ( 0,PrivacyListItem::blockItem ( block_targets_.takeFirst() ) );
0274     changeList ( l );
0275 }
0276 
0277 void PrivacyManager::block_getDefaultList_error()
0278 {
0279     disconnect(this, &PrivacyManager::defaultListAvailable,this, &PrivacyManager::block_getDefaultList_success);
0280     disconnect(this, &PrivacyManager::defaultListError, this, &PrivacyManager::block_getDefaultList_error);
0281     block_waiting_ = false;
0282     block_targets_.clear();
0283 }
0284 
0285 void PrivacyManager::getDefaultList()
0286 {
0287     connect ( this,&PrivacyManager::listsReceived,this, &PrivacyManager::getDefault_listsReceived );
0288     connect ( this,&PrivacyManager::listsError,this, &PrivacyManager::getDefault_listsError );
0289     requestListNames();
0290 }
0291 
0292 void PrivacyManager::getDefault_listsReceived ( const QString& defaultList, const QString&, const QStringList& )
0293 {
0294     disconnect ( this,&PrivacyManager::listsReceived,this,&PrivacyManager::getDefault_listsReceived );
0295     disconnect ( this,&PrivacyManager::listsError,this,&PrivacyManager::getDefault_listsError );
0296 
0297     getDefault_default_ = defaultList;
0298     if ( !defaultList.isEmpty() ) {
0299         getDefault_waiting_ = true;
0300         connect ( this,&PrivacyManager::listReceived,this, &PrivacyManager::getDefault_listReceived );
0301         connect ( this,&PrivacyManager::listError,this, &PrivacyManager::getDefault_listError );
0302         requestList ( defaultList );
0303     }
0304     else {
0305         emit defaultListAvailable ( PrivacyList ( "" ) );
0306     }
0307 }
0308 
0309 void PrivacyManager::getDefault_listsError()
0310 {
0311     disconnect ( this,&PrivacyManager::listsReceived,this,&PrivacyManager::getDefault_listsReceived );
0312     disconnect ( this,&PrivacyManager::listsError,this,&PrivacyManager::getDefault_listsError );
0313     emit defaultListError();
0314 }
0315 
0316 void PrivacyManager::getDefault_listReceived ( const PrivacyList& l )
0317 {
0318     if ( l.name() == getDefault_default_ && getDefault_waiting_ ) {
0319         disconnect ( this,&PrivacyManager::listReceived,this,&PrivacyManager::getDefault_listReceived );
0320         disconnect ( this,&PrivacyManager::listError,this,&PrivacyManager::getDefault_listError );
0321         getDefault_waiting_ = false;
0322         emit defaultListAvailable ( l );
0323     }
0324 }
0325 
0326 void PrivacyManager::getDefault_listError()
0327 {
0328     emit defaultListError();
0329 }
0330 
0331 void PrivacyManager::changeDefaultList ( const QString& name )
0332 {
0333     SetPrivacyListsTask* t = new SetPrivacyListsTask ( rootTask_ );
0334     t->setDefault ( name );
0335     connect ( t,&Task::finished,this, &PrivacyManager::changeDefaultList_finished );
0336     t->go ( true );
0337 }
0338 
0339 void PrivacyManager::changeDefaultList_finished()
0340 {
0341     SetPrivacyListsTask *t = ( SetPrivacyListsTask* ) sender();
0342     if ( !t ) {
0343         qCWarning(JABBER_PROTOCOL_LOG) << "Unexpected sender.";
0344         return;
0345     }
0346 
0347     if ( t->success() ) {
0348         emit changeDefaultList_success();
0349     }
0350     else {
0351         emit changeDefaultList_error();
0352     }
0353 }
0354 
0355 void PrivacyManager::changeActiveList ( const QString& name )
0356 {
0357     SetPrivacyListsTask* t = new SetPrivacyListsTask ( rootTask_ );
0358     t->setActive ( name );
0359     connect ( t,&Task::finished,this, &PrivacyManager::changeActiveList_finished );
0360     t->go ( true );
0361 }
0362 
0363 void PrivacyManager::changeActiveList_finished()
0364 {
0365     SetPrivacyListsTask *t = ( SetPrivacyListsTask* ) sender();
0366     if ( !t ) {
0367         qCWarning(JABBER_PROTOCOL_LOG) << "Unexpected sender.";
0368         return;
0369     }
0370 
0371     if ( t->success() ) {
0372         emit changeActiveList_success();
0373     }
0374     else {
0375         emit changeActiveList_error();
0376     }
0377 }
0378 
0379 void PrivacyManager::changeList ( const PrivacyList& list )
0380 {
0381     SetPrivacyListsTask* t = new SetPrivacyListsTask ( rootTask_ );
0382     t->setList ( list );
0383     connect ( t,&Task::finished,this, &PrivacyManager::changeList_finished );
0384     t->go ( true );
0385 }
0386 
0387 void PrivacyManager::changeList_finished()
0388 {
0389     SetPrivacyListsTask *t = ( SetPrivacyListsTask* ) sender();
0390     if ( !t ) {
0391         qCWarning(JABBER_PROTOCOL_LOG) << "Unexpected sender.";
0392         return;
0393     }
0394 
0395     if ( t->success() ) {
0396         emit changeList_success();
0397     }
0398     else {
0399         emit changeList_error();
0400     }
0401 }
0402 
0403 void PrivacyManager::receiveLists()
0404 {
0405     GetPrivacyListsTask *t = ( GetPrivacyListsTask* ) sender();
0406     if ( !t ) {
0407         qCWarning(JABBER_PROTOCOL_LOG) << "Unexpected sender.";
0408         return;
0409     }
0410 
0411     if ( t->success() ) {
0412         emit listsReceived ( t->defaultList(),t->activeList(),t->lists() );
0413     }
0414     else {
0415         qCDebug(JABBER_PROTOCOL_LOG) << "Error in lists receiving.";
0416         emit listsError();
0417     }
0418 }
0419 
0420 void PrivacyManager::receiveList()
0421 {
0422     GetPrivacyListTask *t = ( GetPrivacyListTask* ) sender();
0423     if ( !t ) {
0424         qCDebug(JABBER_PROTOCOL_LOG) << "Unexpected sender.";
0425         return;
0426     }
0427 
0428     if ( t->success() ) {
0429         emit listReceived ( t->list() );
0430     }
0431     else {
0432         qCDebug(JABBER_PROTOCOL_LOG) << "Error in list receiving.";
0433         emit listError();
0434     }
0435 }
0436 
0437 #include "moc_privacymanager.cpp"