/* * Copyright (c) 2011-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include #include #include #include #include using namespace Swift; namespace { class BlockResponder : public SetResponder { public: BlockResponder(std::shared_ptr blockList, IQRouter* iqRouter) : SetResponder(iqRouter), blockList(blockList) { } virtual bool handleSetRequest(const JID& from, const JID&, const std::string& id, std::shared_ptr payload) { if (getIQRouter()->isAccountJID(from)) { if (payload) { blockList->addItems(payload->getItems()); } sendResponse(from, id, std::shared_ptr()); } else { sendError(from, id, ErrorPayload::NotAuthorized, ErrorPayload::Cancel); } return true; } private: std::shared_ptr blockList; }; class UnblockResponder : public SetResponder { public: UnblockResponder(std::shared_ptr blockList, IQRouter* iqRouter) : SetResponder(iqRouter), blockList(blockList) { } virtual bool handleSetRequest(const JID& from, const JID&, const std::string& id, std::shared_ptr payload) { if (getIQRouter()->isAccountJID(from)) { if (payload) { if (payload->getItems().empty()) { blockList->removeAllItems(); } else { blockList->removeItems(payload->getItems()); } } sendResponse(from, id, std::shared_ptr()); } else { sendError(from, id, ErrorPayload::NotAuthorized, ErrorPayload::Cancel); } return true; } private: std::shared_ptr blockList; }; } ClientBlockListManager::ClientBlockListManager(IQRouter* iqRouter) : iqRouter(iqRouter) { } ClientBlockListManager::~ClientBlockListManager() { if (blockList && blockList->getState() == BlockList::Available) { unblockResponder->stop(); blockResponder->stop(); } } std::shared_ptr ClientBlockListManager::getBlockList() { if (!blockList) { blockList = std::make_shared(); blockList->setState(BlockList::Init); } return blockList; } std::shared_ptr ClientBlockListManager::requestBlockList() { if (!blockList) { blockList = std::make_shared(); } blockList->setState(BlockList::Requesting); std::shared_ptr > getRequest = std::make_shared< GenericRequest >(IQ::Get, JID(), std::make_shared(), iqRouter); getRequest->onResponse.connect(boost::bind(&ClientBlockListManager::handleBlockListReceived, this, _1, _2)); getRequest->send(); return blockList; } GenericRequest::ref ClientBlockListManager::createBlockJIDRequest(const JID& jid) { return createBlockJIDsRequest(std::vector(1, jid)); } GenericRequest::ref ClientBlockListManager::createBlockJIDsRequest(const std::vector& jids) { std::shared_ptr payload = std::make_shared(jids); return std::make_shared< GenericRequest >(IQ::Set, JID(), payload, iqRouter); } GenericRequest::ref ClientBlockListManager::createUnblockJIDRequest(const JID& jid) { return createUnblockJIDsRequest(std::vector(1, jid)); } GenericRequest::ref ClientBlockListManager::createUnblockJIDsRequest(const std::vector& jids) { std::shared_ptr payload = std::make_shared(jids); return std::make_shared< GenericRequest >(IQ::Set, JID(), payload, iqRouter); } GenericRequest::ref ClientBlockListManager::createUnblockAllRequest() { return createUnblockJIDsRequest(std::vector()); } void ClientBlockListManager::handleBlockListReceived(std::shared_ptr payload, ErrorPayload::ref error) { if (error || !payload) { blockList->setState(BlockList::Error); } else { blockList->setItems(payload->getItems()); blockList->setState(BlockList::Available); if (!blockResponder) { blockResponder = std::make_shared(blockList, iqRouter); blockResponder->start(); } if (!unblockResponder) { unblockResponder = std::make_shared(blockList, iqRouter); unblockResponder->start(); } } }