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
|
/*
* Copyright (c) 2010 Kevin Smith
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "Swiften/MUC/MUC.h"
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include "Swiften/Presence/DirectedPresenceSender.h"
#include "Swiften/Client/StanzaChannel.h"
#include "Swiften/Queries/IQRouter.h"
#include "Swiften/Elements/Form.h"
#include "Swiften/Elements/IQ.h"
#include "Swiften/Elements/MUCUserPayload.h"
#include "Swiften/Elements/MUCPayload.h"
#include "Swiften/MUC/MUCRegistry.h"
#include "Swiften/Queries/GenericRequest.h"
namespace Swift {
typedef std::pair<String, MUCOccupant> StringMUCOccupantPair;
MUC::MUC(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(&MUC::handleIncomingPresence, this, _1));
mucRegistry->addMUC(getJID());
}
//FIXME: discover reserved nickname
/**
* Join the MUC with default context.
*/
void MUC::joinAs(const String &nick) {
joinSince_ = boost::posix_time::not_a_date_time;
internalJoin(nick);
}
void MUC::internalJoin(const String &nick) {
//TODO: password
//TODO: history request
joinComplete_ = false;
ownMUCJID = JID(ownMUCJID.getNode(), ownMUCJID.getDomain(), nick);
boost::shared_ptr<Presence> joinPresence(presenceSender->getLastSentUndirectedPresence());
assert(joinPresence->getType() == Presence::Available);
joinPresence->setTo(ownMUCJID);
boost::shared_ptr<MUCPayload> mucPayload(new MUCPayload());
if (joinSince_ != boost::posix_time::not_a_date_time) {
mucPayload->setSince(joinSince_);
}
joinPresence->addPayload(mucPayload);
presenceSender->sendPresence(joinPresence);
}
/**
* Join the MUC with context since date.
*/
void MUC::joinWithContextSince(const String &nick, const boost::posix_time::ptime& since) {
joinSince_ = since;
internalJoin(nick);
}
void MUC::part() {
presenceSender->removeDirectedPresenceReceiver(ownMUCJID);
mucRegistry->removeMUC(getJID());
}
void MUC::handleUserLeft(LeavingType type) {
std::map<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;
presenceSender->removeDirectedPresenceReceiver(ownMUCJID);
}
void MUC::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
if (!isFromMUC(presence->getFrom())) {
return;
}
boost::shared_ptr<MUCUserPayload> mucPayload;
foreach (boost::shared_ptr<MUCUserPayload> payload, presence->getPayloads<MUCUserPayload>()) {
if (payload->getItems().size() > 0 || payload->getStatusCodes().size() > 0) {
mucPayload = payload;
}
}
if (!joinComplete_) {
if (presence->getType() == Presence::Error) {
String reason;
onJoinFailed(presence->getPayload<ErrorPayload>());
return;
}
}
String nick = presence->getFrom().getResource();
if (nick.isEmpty()) {
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;
affiliation = mucPayload->getItems()[0].affiliation;
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) {
if (presence->getFrom() == ownMUCJID) {
handleUserLeft(Part);
return;
} else {
std::map<String,MUCOccupant>::iterator i = occupants.find(nick);
if (i != occupants.end()) {
//TODO: part type
onOccupantLeft(i->second, Part, "");
occupants.erase(i);
}
}
} else if (presence->getType() == Presence::Available) {
std::map<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<String, MUCOccupant>::iterator, bool> result = occupants.insert(std::make_pair(nick, occupant));
if (isJoin) {
onOccupantJoined(result.first->second);
}
onOccupantPresenceChange(presence);
}
if (mucPayload && !joinComplete_) {
foreach (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;
ownMUCJID = presence->getFrom();
onJoinComplete(getOwnNick());
presenceSender->addDirectedPresenceReceiver(ownMUCJID);
}
if (status.code == 201) {
/* Room is created and locked */
/* Currently deal with this by making an instant room */
ownMUCJID = presence->getFrom();
boost::shared_ptr<MUCOwnerPayload> mucPayload(new MUCOwnerPayload());
mucPayload->setPayload(boost::shared_ptr<Payload>(new Form(Form::SubmitType)));
GenericRequest<MUCOwnerPayload>* request = new GenericRequest<MUCOwnerPayload>(IQ::Set, getJID(), mucPayload, iqRouter_);
request->onResponse.connect(boost::bind(&MUC::handleCreationConfigResponse, this, _1, _2));
request->send();
}
}
}
}
void MUC::handleCreationConfigResponse(boost::shared_ptr<MUCOwnerPayload> /*unused*/, ErrorPayload::ref error) {
if (error) {
onJoinFailed(error);
} else {
/* onJoinComplete(getOwnNick()); isn't needed here, the presence will cause an emit elsewhere. */
presenceSender->addDirectedPresenceReceiver(ownMUCJID);
}
}
//FIXME: Recognise Topic changes
//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
}
|