summaryrefslogtreecommitdiffstats
blob: bfdec30b328cd4f36e193a2b21c8d5ed005f2c27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (c) 2011-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Client/ClientBlockListManager.h>

#include <cassert>
#include <memory>

#include <boost/bind.hpp>

#include <Swiften/Client/BlockListImpl.h>

using namespace Swift;

namespace {
    class BlockResponder : public SetResponder<BlockPayload> {
        public:
            BlockResponder(std::shared_ptr<BlockListImpl> blockList, IQRouter* iqRouter) : SetResponder<BlockPayload>(iqRouter), blockList(blockList) {
            }

            virtual bool handleSetRequest(const JID& from, const JID&, const std::string& id, std::shared_ptr<BlockPayload> payload) {
                if (getIQRouter()->isAccountJID(from)) {
                        if (payload) {
                            blockList->addItems(payload->getItems());
                        }
                        sendResponse(from, id, std::shared_ptr<BlockPayload>());
                }
                else {
                    sendError(from, id, ErrorPayload::NotAuthorized, ErrorPayload::Cancel);
                }
                return true;
            }

        private:
            std::shared_ptr<BlockListImpl> blockList;
    };

    class UnblockResponder : public SetResponder<UnblockPayload> {
        public:
            UnblockResponder(std::shared_ptr<BlockListImpl> blockList, IQRouter* iqRouter) : SetResponder<UnblockPayload>(iqRouter), blockList(blockList) {
            }

            virtual bool handleSetRequest(const JID& from, const JID&, const std::string& id, std::shared_ptr<UnblockPayload> payload) {
                if (getIQRouter()->isAccountJID(from)) {
                    if (payload) {
                        if (payload->getItems().empty()) {
                            blockList->removeAllItems();
                        }
                        else {
                            blockList->removeItems(payload->getItems());
                        }
                    }
                    sendResponse(from, id, std::shared_ptr<UnblockPayload>());
                }
                else {
                    sendError(from, id, ErrorPayload::NotAuthorized, ErrorPayload::Cancel);
                }
                return true;
            }

        private:
            std::shared_ptr<BlockListImpl> blockList;
    };
}

ClientBlockListManager::ClientBlockListManager(IQRouter* iqRouter) : iqRouter(iqRouter) {

}

ClientBlockListManager::~ClientBlockListManager() {
    if (blockList && blockList->getState() == BlockList::Available) {
        unblockResponder->stop();
        blockResponder->stop();
    }
}

std::shared_ptr<BlockList> ClientBlockListManager::getBlockList() {
    if (!blockList) {
        blockList = std::make_shared<BlockListImpl>();
        blockList->setState(BlockList::Init);
    }
    return blockList;
}

std::shared_ptr<BlockList> ClientBlockListManager::requestBlockList() {
    if (!blockList) {
        blockList = std::make_shared<BlockListImpl>();
    }
    blockList->setState(BlockList::Requesting);
    std::shared_ptr<GenericRequest<BlockListPayload> > getRequest = std::make_shared< GenericRequest<BlockListPayload> >(IQ::Get, JID(), std::make_shared<BlockListPayload>(), iqRouter);
    getRequest->onResponse.connect(boost::bind(&ClientBlockListManager::handleBlockListReceived, this, _1, _2));
    getRequest->send();
    return blockList;
}

GenericRequest<BlockPayload>::ref ClientBlockListManager::createBlockJIDRequest(const JID& jid) {
    return createBlockJIDsRequest(std::vector<JID>(1, jid));
}

GenericRequest<BlockPayload>::ref ClientBlockListManager::createBlockJIDsRequest(const std::vector<JID>& jids) {
    std::shared_ptr<BlockPayload> payload = std::make_shared<BlockPayload>(jids);
    return std::make_shared< GenericRequest<BlockPayload> >(IQ::Set, JID(), payload, iqRouter);
}

GenericRequest<UnblockPayload>::ref ClientBlockListManager::createUnblockJIDRequest(const JID& jid) {
    return createUnblockJIDsRequest(std::vector<JID>(1, jid));
}

GenericRequest<UnblockPayload>::ref ClientBlockListManager::createUnblockJIDsRequest(const std::vector<JID>& jids) {
    std::shared_ptr<UnblockPayload> payload = std::make_shared<UnblockPayload>(jids);
    return std::make_shared< GenericRequest<UnblockPayload> >(IQ::Set, JID(), payload, iqRouter);
}

GenericRequest<UnblockPayload>::ref ClientBlockListManager::createUnblockAllRequest() {
    return createUnblockJIDsRequest(std::vector<JID>());
}


void ClientBlockListManager::handleBlockListReceived(std::shared_ptr<BlockListPayload> 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<BlockResponder>(blockList, iqRouter);
            blockResponder->start();
        }
        if (!unblockResponder) {
            unblockResponder = std::make_shared<UnblockResponder>(blockList, iqRouter);
            unblockResponder->start();
        }
    }
}