summaryrefslogtreecommitdiffstats
blob: 99892a9c317b6c660ba4030d9dc3f613f68eedab (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (c) 2013-2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Sluift/SluiftClient.h>

#include <boost/numeric/conversion/cast.hpp>

#include <Swiften/Client/Client.h>
#include <Swiften/Client/ClientBlockListManager.h>
#include <Swiften/Client/ClientXMLTracer.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Elements/PubSubEvent.h>
#include <Swiften/Queries/RawRequest.h>
#include <Swiften/Roster/XMPPRoster.h>

#include <Sluift/Helpers.h>
#include <Sluift/Lua/Exception.h>
#include <Sluift/SluiftGlobals.h>

using namespace Swift;

SluiftClient::SluiftClient(
        const JID& jid,
        const std::string& password,
        NetworkFactories* networkFactories,
        SimpleEventLoop* eventLoop) :
            networkFactories(networkFactories),
            eventLoop(eventLoop),
            tracer(nullptr) {
    client = new Client(jid, password, networkFactories);
    client->setAlwaysTrustCertificates();
    client->onDisconnected.connect(boost::bind(&SluiftClient::handleDisconnected, this, _1));
    client->onMessageReceived.connect(boost::bind(&SluiftClient::handleIncomingMessage, this, _1));
    client->onPresenceReceived.connect(boost::bind(&SluiftClient::handleIncomingPresence, this, _1));
    client->getPubSubManager()->onEvent.connect(boost::bind(&SluiftClient::handleIncomingPubSubEvent, this, _1, _2));
    client->getRoster()->onInitialRosterPopulated.connect(boost::bind(&SluiftClient::handleInitialRosterPopulated, this));
    client->getRoster()->onJIDAdded.connect(boost::bind(&SluiftClient::handleIncomingRosterAdd, this, _1));
    client->getRoster()->onJIDRemoved.connect(boost::bind(&SluiftClient::handleIncomingRosterRemove, this, _1));
    client->getRoster()->onJIDUpdated.connect(boost::bind(&SluiftClient::handleIncomingRosterUpdate, this, _1));
    client->getClientBlockListManager()->getBlockList()->onItemAdded.connect(boost::bind(&SluiftClient::handleIncomingBlockEvent, this, _1));
    client->getClientBlockListManager()->getBlockList()->onItemRemoved.connect(boost::bind(&SluiftClient::handleIncomingUnblockEvent, this, _1));
}

SluiftClient::~SluiftClient() {
    delete tracer;
    delete client;
}

void SluiftClient::connect() {
    if (client->isActive()) {
        throw Lua::Exception("Client is already connecting or connected");
    }
    rosterReceived = false;
    blockListReceived = false;
    disconnectedError = boost::optional<ClientError>();
    client->connect(options);
}

void SluiftClient::setTraceEnabled(bool b) {
    if (b && !tracer) {
        tracer = new ClientXMLTracer(client, options.boshURL.isEmpty()? false: true);
    }
    else if (!b && tracer) {
        delete tracer;
        tracer = nullptr;
    }
}

void SluiftClient::waitConnected(int timeout) {
    Watchdog watchdog(timeout, networkFactories->getTimerFactory());
    while (!watchdog.getTimedOut() && client->isActive() && !client->isAvailable()) {
        eventLoop->runUntilEvents();
    }
    if (watchdog.getTimedOut()) {
        client->disconnect();
        throw Lua::Exception("Timeout while connecting");
    }
    if (disconnectedError) {
        throw Lua::Exception(getErrorString(*disconnectedError));
    }
}

bool SluiftClient::isConnected() const {
    return client->isAvailable();
}

void SluiftClient::disconnect() {
    client->disconnect();
    while (client->isActive()) {
        eventLoop->runUntilEvents();
    }
}

void SluiftClient::setSoftwareVersion(const std::string& name, const std::string& version, const std::string& os) {
    client->setSoftwareVersion(name, version, os);
}

boost::optional<SluiftClient::Event> SluiftClient::getNextEvent(
        int timeout, boost::function<bool (const Event&)> condition) {
    Watchdog watchdog(timeout, networkFactories->getTimerFactory());
    size_t currentIndex = 0;
    while (true) {
        // Look for pending events in the queue
        while (currentIndex < pendingEvents.size()) {
            Event event = pendingEvents[currentIndex];
            if (!condition || condition(event)) {
                pendingEvents.erase(
                        pendingEvents.begin()
                        + boost::numeric_cast<int>(currentIndex));
                return event;
            }
            ++currentIndex;
        }

        // Wait for new events
        while (!watchdog.getTimedOut() && currentIndex >= pendingEvents.size() && client->isActive()) {
            eventLoop->runUntilEvents();
        }

        // Finish if we're disconnected or timed out
        if (watchdog.getTimedOut() || !client->isActive()) {
            return boost::optional<Event>();
        }
    }
}

std::vector<JID> SluiftClient::getBlockList(int timeout) {
    Watchdog watchdog(timeout, networkFactories->getTimerFactory());
    if (!blockListReceived) {
        // If we haven't requested it yet, request it for the first time
        client->getClientBlockListManager()->requestBlockList();

        // Wait for new events
        while (!watchdog.getTimedOut() && client->getClientBlockListManager()->getBlockList()->getState() != BlockList::Available) {
            eventLoop->runUntilEvents();
        }

        // Throw an error if we're timed out
        if (watchdog.getTimedOut()) {
            throw Lua::Exception("Timeout while requesting blocklist");
        }
    }
    blockListReceived = true;
    return client->getClientBlockListManager()->getBlockList()->getItems();
}

std::vector<XMPPRosterItem> SluiftClient::getRoster(int timeout) {
    Watchdog watchdog(timeout, networkFactories->getTimerFactory());
    if (!rosterReceived) {
        // If we haven't requested it yet, request it for the first time
        client->requestRoster();

        // Wait for new events
        while (!watchdog.getTimedOut() && !rosterReceived) {
            eventLoop->runUntilEvents();
        }

        // Throw an error if we're timed out
        if (watchdog.getTimedOut()) {
            throw Lua::Exception("Timeout while requesting roster");
        }
    }
    return client->getRoster()->getItems();
}

void SluiftClient::handleIncomingMessage(std::shared_ptr<Message> stanza) {
    if (stanza->getPayload<PubSubEvent>()) {
        // Already handled by pubsub manager
        return;
    }
    pendingEvents.push_back(Event(stanza));
}

void SluiftClient::handleIncomingPresence(std::shared_ptr<Presence> stanza) {
    pendingEvents.push_back(Event(stanza));
}

void SluiftClient::handleIncomingPubSubEvent(const JID& from, std::shared_ptr<PubSubEventPayload> event) {
    pendingEvents.push_back(Event(from, event));
}

void SluiftClient::handleIncomingBlockEvent(const JID& item) {
    pendingEvents.push_back(Event(item, Event::BlockEventType));
}

void SluiftClient::handleIncomingUnblockEvent(const JID& item) {
    pendingEvents.push_back(Event(item, Event::UnblockEventType));
}

void SluiftClient::handleInitialRosterPopulated() {
    rosterReceived = true;
}

void SluiftClient::handleIncomingRosterAdd(const JID& item) {
    pendingEvents.push_back(Event(item, Event::RosterAddType));
}

void SluiftClient::handleIncomingRosterRemove(const JID& item) {
    pendingEvents.push_back(Event(item, Event::RosterRemoveType));
}

void SluiftClient::handleIncomingRosterUpdate(const JID& item) {
    pendingEvents.push_back(Event(item, Event::RosterUpdateType));
}

void SluiftClient::handleRequestResponse(std::shared_ptr<Payload> response, std::shared_ptr<ErrorPayload> error) {
    requestResponse = response;
    requestError = error;
    requestResponseReceived = true;
}

void SluiftClient::handleDisconnected(const boost::optional<ClientError>& error) {
    disconnectedError = error;
}

Sluift::Response SluiftClient::doSendRequest(std::shared_ptr<Request> request, int timeout) {
    requestResponse.reset();
    requestError.reset();
    requestResponseReceived = false;
    request->send();

    Watchdog watchdog(timeout, networkFactories->getTimerFactory());
    while (!watchdog.getTimedOut() && !requestResponseReceived) {
        eventLoop->runUntilEvents();
    }
    return Sluift::Response(requestResponse, watchdog.getTimedOut() ?
            std::make_shared<ErrorPayload>(ErrorPayload::RemoteServerTimeout) : requestError);
}