summaryrefslogtreecommitdiffstats
blob: d2b33f3a428ada6768dc8c6db8c3a393c55a84d2 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/MUC/MUCImpl.h>

#include <memory>

#include <boost/bind.hpp>

#include <Swiften/Client/StanzaChannel.h>
#include <Swiften/Elements/CapsInfo.h>
#include <Swiften/Elements/Form.h>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Elements/MUCAdminPayload.h>
#include <Swiften/Elements/MUCDestroyPayload.h>
#include <Swiften/Elements/MUCInvitationPayload.h>
#include <Swiften/Elements/MUCPayload.h>
#include <Swiften/Elements/MUCUserPayload.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/MUC/MUCRegistry.h>
#include <Swiften/Presence/DirectedPresenceSender.h>
#include <Swiften/Queries/GenericRequest.h>
#include <Swiften/Queries/IQRouter.h>

namespace Swift {

typedef std::pair<std::string, MUCOccupant> StringMUCOccupantPair;

MUCImpl::MUCImpl(StanzaChannel* stanzaChannel, IQRouter* iqRouter, DirectedPresenceSender* presenceSender, const JID &muc, MUCRegistry* mucRegistry) : ownMUCJID(muc), stanzaChannel(stanzaChannel), iqRouter_(iqRouter), presenceSender(presenceSender), mucRegistry(mucRegistry) {
    scopedConnection_ = stanzaChannel->onPresenceReceived.connect(boost::bind(&MUCImpl::handleIncomingPresence, this, _1));
}

MUCImpl::~MUCImpl()
{
}

//FIXME: discover reserved nickname

/**
 * Join the MUC with default context.
 */
void MUCImpl::joinAs(const std::string &nick) {
    joinSince_ = boost::posix_time::not_a_date_time;
    internalJoin(nick);
}

/**
 * Set the password used for entering the room.
 */
void MUCImpl::setPassword(const boost::optional<std::string>& newPassword) {
    password = newPassword;
}

/**
 * Join the MUC with context since date.
 */
void MUCImpl::joinWithContextSince(const std::string &nick, const boost::posix_time::ptime& since) {
    joinSince_ = since;
    internalJoin(nick);
}

std::map<std::string, MUCOccupant> MUCImpl::getOccupants() const {
    return occupants;
}

bool MUCImpl::isEqualExceptID(const Presence& lhs, const Presence& rhs) {
    bool isEqual = false;
    if (lhs.getFrom() == rhs.getFrom() && lhs.getTo() == rhs.getTo() && lhs.getStatus() == rhs.getStatus() && lhs.getShow() == rhs.getShow()) {
        CapsInfo::ref lhsCaps = lhs.getPayload<CapsInfo>();
        CapsInfo::ref rhsCaps = rhs.getPayload<CapsInfo>();

        if (!!lhsCaps && !!rhsCaps) {
            isEqual = (*lhsCaps == *rhsCaps);
        }
        else {
            isEqual = (!lhsCaps && !rhsCaps);
        }
    }
    return isEqual;
}

void MUCImpl::internalJoin(const std::string &nick) {
    //TODO: history request
    joinComplete_ = false;
    joinSucceeded_ = false;

    mucRegistry->addMUC(getJID());

    ownMUCJID = JID(ownMUCJID.getNode(), ownMUCJID.getDomain(), nick);

    Presence::ref joinPresence = presenceSender->getLastSentUndirectedPresence() ? (*presenceSender->getLastSentUndirectedPresence())->clone() : std::make_shared<Presence>();
    assert(joinPresence->getType() == Presence::Available);
    joinPresence->setTo(ownMUCJID);
    MUCPayload::ref mucPayload = std::make_shared<MUCPayload>();
    if (joinSince_ != boost::posix_time::not_a_date_time) {
        mucPayload->setSince(joinSince_);
    }
    if (password) {
        mucPayload->setPassword(*password);
    }
    joinPresence->addPayload(mucPayload);
    joinRequestPresence_ = joinPresence;
    presenceSender->sendPresence(joinPresence);
}

void MUCImpl::changeNickname(const std::string& newNickname) {
    Presence::ref changeNicknamePresence = std::make_shared<Presence>();
    changeNicknamePresence->setTo(ownMUCJID.toBare().toString() + std::string("/") + newNickname);
    presenceSender->sendPresence(changeNicknamePresence);
}

void MUCImpl::part() {
    presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::AndSendPresence);
    mucRegistry->removeMUC(getJID());
}

void MUCImpl::handleUserLeft(LeavingType type) {
    std::map<std::string,MUCOccupant>::iterator i = occupants.find(ownMUCJID.getResource());
    if (i != occupants.end()) {
        MUCOccupant me = i->second;
        occupants.erase(i);
        onOccupantLeft(me, type, "");
    }
    occupants.clear();
    joinComplete_ = false;
    joinSucceeded_ = false;
    isUnlocked_ = false;
    presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
}

void MUCImpl::handleIncomingPresence(Presence::ref presence) {
    if (!isFromMUC(presence->getFrom())) {
        return;
    }

    MUCUserPayload::ref mucPayload;
    for (MUCUserPayload::ref payload : presence->getPayloads<MUCUserPayload>()) {
        if (!payload->getItems().empty() || !payload->getStatusCodes().empty()) {
            mucPayload = payload;
        }
    }

    // On the first incoming presence, check if our join has succeeded
    // (i.e. we start getting non-error presence from the MUC) or not
    if (!joinSucceeded_) {
        if (presence->getType() == Presence::Error) {
            onJoinFailed(presence->getPayload<ErrorPayload>());
            return;
        }
        else if (presence->getType() == Presence::Available) {
            joinSucceeded_ = true;
            presenceSender->addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
            if (presenceSender->getLastSentUndirectedPresence() && !isEqualExceptID(**(presenceSender->getLastSentUndirectedPresence()), *joinRequestPresence_)) {
                // our presence changed between join request and join complete, send current presence to MUC
                Presence::ref latestPresence = std::make_shared<Presence>(**presenceSender->getLastSentUndirectedPresence());
                latestPresence->setTo(ownMUCJID);
                presenceSender->sendPresence(latestPresence);
            }
        }
        else if (presence->getType() == Presence::Unavailable) {
            onJoinFailed(std::shared_ptr<ErrorPayload>());
            return;
        }
    }

    std::string nick = presence->getFrom().getResource();
    if (nick.empty()) {
        return;
    }
    MUCOccupant::Role role(MUCOccupant::NoRole);
    MUCOccupant::Affiliation affiliation(MUCOccupant::NoAffiliation);
    boost::optional<JID> realJID;
    if (mucPayload && mucPayload->getItems().size() > 0) {
        role = mucPayload->getItems()[0].role ? mucPayload->getItems()[0].role.get() : MUCOccupant::NoRole;
        affiliation = mucPayload->getItems()[0].affiliation ? mucPayload->getItems()[0].affiliation.get() : MUCOccupant::NoAffiliation;
        realJID = mucPayload->getItems()[0].realJID;
    }

    //100 is non-anonymous
    //TODO: 100 may also be specified in a <message/>
    //170 is room logging to http
    //TODO: Nick changes
    if (presence->getType() == Presence::Unavailable) {
        LeavingType type = LeavePart;
        boost::optional<std::string> newNickname;
        if (mucPayload) {
            if (std::dynamic_pointer_cast<MUCDestroyPayload>(mucPayload->getPayload())) {
                type = LeaveDestroy;
            }
            else {
                for (MUCUserPayload::StatusCode status : mucPayload->getStatusCodes()) {
                    if (status.code == 307) {
                        type = LeaveKick;
                    }
                    else if (status.code == 301) {
                        type = LeaveBan;
                    }
                    else if (status.code == 321) {
                        type = LeaveNotMember;
                    }
                    else if (status.code == 303) {
                        if (mucPayload->getItems().size() == 1) {
                            newNickname = mucPayload->getItems()[0].nick;
                        }
                    }
                }
            }
        }
        if (newNickname) {
            std::map<std::string,MUCOccupant>::iterator i = occupants.find(nick);
            if (i != occupants.end()) {
                MUCOccupant occupant = i->second;
                occupants.erase(i);
                occupant.setNick(newNickname.get());
                occupants.insert(std::make_pair(newNickname.get(), occupant));
                onOccupantNicknameChanged(nick, newNickname.get());
            }
        }
        else {
            if (presence->getFrom() == ownMUCJID) {
                handleUserLeft(type);
                return;
            }
            else {
                std::map<std::string,MUCOccupant>::iterator i = occupants.find(nick);
                if (i != occupants.end()) {
                    //TODO: part type
                    MUCOccupant occupant = i->second;
                    occupants.erase(i);
                    onOccupantLeft(occupant, type, "");
                }
            }
        }
    }
    else if (presence->getType() == Presence::Available) {
        std::map<std::string, MUCOccupant>::iterator it = occupants.find(nick);
        MUCOccupant occupant(nick, role, affiliation);
        bool isJoin = true;
        if (realJID) {
            occupant.setRealJID(realJID.get());
        }
        if (it != occupants.end()) {
            isJoin = false;
            MUCOccupant oldOccupant = it->second;
            if (oldOccupant.getRole() != role) {
                onOccupantRoleChanged(nick, occupant, oldOccupant.getRole());
            }
            if (oldOccupant.getAffiliation() != affiliation) {
                onOccupantAffiliationChanged(nick, affiliation, oldOccupant.getAffiliation());
            }
            occupants.erase(it);
        }
        std::pair<std::map<std::string, MUCOccupant>::iterator, bool> result = occupants.insert(std::make_pair(nick, occupant));
        if (isJoin) {
            onOccupantJoined(result.first->second);
        }
        onOccupantPresenceChange(presence);

        if (mucPayload && !joinComplete_) {
            bool isLocked = false;
            for (MUCUserPayload::StatusCode status : mucPayload->getStatusCodes()) {
                if (status.code == 110) {
                    /* Simply knowing this is your presence is enough, 210 doesn't seem to be necessary. */
                    joinComplete_ = true;
                    if (ownMUCJID != presence->getFrom()) {
                        presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
                        ownMUCJID = presence->getFrom();
                        presenceSender->addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::AndSendPresence);
                    }
                }
                // MUC status 201: a new room has been created
                if (status.code == 201) {
                    isLocked = true;
                    /* Room is created and locked */
                    /* Currently deal with this by making an instant room */
                    if (ownMUCJID != presence->getFrom()) {
                        presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
                        ownMUCJID = presence->getFrom();
                        presenceSender->addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::AndSendPresence);
                    }
                    if (createAsReservedIfNew) {
                        unlocking = true;
                        requestConfigurationForm();
                    }
                    else {
                        // Accept default room configuration and create an instant room http://xmpp.org/extensions/xep-0045.html#createroom-instant
                        MUCOwnerPayload::ref mucPayload(new MUCOwnerPayload());
                        presenceSender->addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
                        mucPayload->setPayload(std::make_shared<Form>(Form::SubmitType));
                        std::shared_ptr< GenericRequest<MUCOwnerPayload> > request = std::make_shared< GenericRequest<MUCOwnerPayload> >(IQ::Set, getJID(), mucPayload, iqRouter_);
                        request->onResponse.connect(boost::bind(&MUCImpl::handleCreationConfigResponse, this, _1, _2));
                        request->send();
                    }
                }
            }

            if (joinComplete_ && !isLocked) {
                assert(hasOccupant(getOwnNick()));
                onJoinComplete(getOwnNick());
            }
            if (!isLocked && !isUnlocked_ && (presence->getFrom() == ownMUCJID)) {
                isUnlocked_ = true;
                onUnlocked();
            }
        }
    }
}

void MUCImpl::handleCreationConfigResponse(MUCOwnerPayload::ref /*unused*/, ErrorPayload::ref error) {
    unlocking = false;
    if (error) {
        presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::AndSendPresence);
        onJoinFailed(error);
    } else {
        onJoinComplete(getOwnNick()); /* Previously, this wasn't needed here, as the presence duplication bug caused an emit elsewhere. */
        isUnlocked_ = true;
        onUnlocked();
    }
}

bool MUCImpl::hasOccupant(const std::string& nick) {
    return occupants.find(nick) != occupants.end();
}

const MUCOccupant& MUCImpl::getOccupant(const std::string& nick) {
    return occupants.find(nick)->second;
}

void MUCImpl::kickOccupant(const JID& jid) {
    changeOccupantRole(jid, MUCOccupant::NoRole);
}

/**
 * Call with the room JID, not the real JID.
 */
void MUCImpl::changeOccupantRole(const JID& jid, MUCOccupant::Role role) {
    MUCAdminPayload::ref mucPayload = std::make_shared<MUCAdminPayload>();
    MUCItem item;
    item.role = role;
    item.nick = jid.getResource();
    mucPayload->addItem(item);
    std::shared_ptr<GenericRequest<MUCAdminPayload> > request = std::make_shared<GenericRequest<MUCAdminPayload> >(IQ::Set, getJID(), mucPayload, iqRouter_);
    request->onResponse.connect(boost::bind(&MUCImpl::handleOccupantRoleChangeResponse, this, _1, _2, jid, role));
    request->send();

}

void MUCImpl::handleOccupantRoleChangeResponse(MUCAdminPayload::ref /*unused*/, ErrorPayload::ref error, const JID& jid, MUCOccupant::Role role) {
    if (error) {
        onRoleChangeFailed(error, jid, role);
    }
}

void MUCImpl::requestAffiliationList(MUCOccupant::Affiliation affiliation) {
    MUCAdminPayload::ref mucPayload = std::make_shared<MUCAdminPayload>();
    MUCItem item;
    item.affiliation = affiliation;
    mucPayload->addItem(item);
    std::shared_ptr<GenericRequest<MUCAdminPayload> > request = std::make_shared< GenericRequest<MUCAdminPayload> >(IQ::Get, getJID(), mucPayload, iqRouter_);
    request->onResponse.connect(boost::bind(&MUCImpl::handleAffiliationListResponse, this, _1, _2, affiliation));
    request->send();
}

/**
 * Must be called with the real JID, not the room JID.
 */
void MUCImpl::changeAffiliation(const JID& jid, MUCOccupant::Affiliation affiliation) {
    MUCAdminPayload::ref mucPayload = std::make_shared<MUCAdminPayload>();
    MUCItem item;
    item.affiliation = affiliation;
    item.realJID = jid;
    mucPayload->addItem(item);
    std::shared_ptr<GenericRequest<MUCAdminPayload> > request = std::make_shared<GenericRequest<MUCAdminPayload> >(IQ::Set, getJID(), mucPayload, iqRouter_);
    request->onResponse.connect(boost::bind(&MUCImpl::handleAffiliationChangeResponse, this, _1, _2, jid, affiliation));
    request->send();
}

void MUCImpl::handleAffiliationListResponse(MUCAdminPayload::ref payload, ErrorPayload::ref error, MUCOccupant::Affiliation affiliation) {
    if (error) {
        onAffiliationListFailed(error);
    }
    else {
        std::vector<JID> jids;
        for (MUCItem item : payload->getItems()) {
            if (item.realJID) {
                jids.push_back(*item.realJID);
            }
        }
        onAffiliationListReceived(affiliation, jids);
    }
}

void MUCImpl::handleAffiliationChangeResponse(MUCAdminPayload::ref /*unused*/, ErrorPayload::ref error, const JID& jid, MUCOccupant::Affiliation affiliation) {
    if (error) {
        onAffiliationChangeFailed(error, jid, affiliation);
    }
}

void MUCImpl::changeSubject(const std::string& subject) {
    Message::ref message = std::make_shared<Message>();
    message->setSubject(subject);
    message->setType(Message::Groupchat);
    message->setTo(ownMUCJID.toBare());
    stanzaChannel->sendMessage(message);
}

void MUCImpl::requestConfigurationForm() {
    MUCOwnerPayload::ref mucPayload(new MUCOwnerPayload());
    std::shared_ptr<GenericRequest<MUCOwnerPayload> > request = std::make_shared<GenericRequest<MUCOwnerPayload> >(IQ::Get, getJID(), mucPayload, iqRouter_);
    request->onResponse.connect(boost::bind(&MUCImpl::handleConfigurationFormReceived, this, _1, _2));
    request->send();
}

void MUCImpl::cancelConfigureRoom() {
    MUCOwnerPayload::ref mucPayload(new MUCOwnerPayload());
    mucPayload->setPayload(std::make_shared<Form>(Form::CancelType));
    std::shared_ptr<GenericRequest<MUCOwnerPayload> > request = std::make_shared<GenericRequest<MUCOwnerPayload> >(IQ::Set, getJID(), mucPayload, iqRouter_);
    request->send();
}

void MUCImpl::handleConfigurationFormReceived(MUCOwnerPayload::ref payload, ErrorPayload::ref error) {
    Form::ref form;
    if (payload) {
        form = payload->getForm();
    }
    if (error || !form) {
        onConfigurationFailed(error);
    } else {
        onConfigurationFormReceived(form);
    }
}

void MUCImpl::handleConfigurationResultReceived(MUCOwnerPayload::ref /*payload*/, ErrorPayload::ref error) {
    if (error) {
        onConfigurationFailed(error);
    }
}

void MUCImpl::configureRoom(Form::ref form) {
    MUCOwnerPayload::ref mucPayload(new MUCOwnerPayload());
    mucPayload->setPayload(form);
    std::shared_ptr<GenericRequest<MUCOwnerPayload> > request = std::make_shared<GenericRequest<MUCOwnerPayload> >(IQ::Set, getJID(), mucPayload, iqRouter_);
    if (unlocking) {
        request->onResponse.connect(boost::bind(&MUCImpl::handleCreationConfigResponse, this, _1, _2));
    }
    else {
        request->onResponse.connect(boost::bind(&MUCImpl::handleConfigurationResultReceived, this, _1, _2));
    }
    request->send();
}

void MUCImpl::destroyRoom() {
    MUCOwnerPayload::ref mucPayload = std::make_shared<MUCOwnerPayload>();
    MUCDestroyPayload::ref mucDestroyPayload = std::make_shared<MUCDestroyPayload>();
    mucPayload->setPayload(mucDestroyPayload);
    std::shared_ptr< GenericRequest<MUCOwnerPayload> > request = std::make_shared<GenericRequest<MUCOwnerPayload> >(IQ::Set, getJID(), mucPayload, iqRouter_);
    request->onResponse.connect(boost::bind(&MUCImpl::handleConfigurationResultReceived, this, _1, _2));
    request->send();
}

void MUCImpl::invitePerson(const JID& person, const std::string& reason, bool isImpromptu, bool isReuseChat) {
    Message::ref message = std::make_shared<Message>();
    message->setTo(person);
    message->setType(Message::Normal);
    MUCInvitationPayload::ref invite = std::make_shared<MUCInvitationPayload>();
    invite->setReason(reason);
    invite->setJID(ownMUCJID.toBare());
    invite->setIsImpromptu(isImpromptu);
    invite->setIsContinuation(isReuseChat);
    message->addPayload(invite);
    stanzaChannel->sendMessage(message);
}

//TODO: Invites(direct/mediated)

//TODO: requesting membership

//TODO: get member list

//TODO: request voice

//TODO: moderator use cases

//TODO: Admin use cases

//TODO: Owner use cases

}