summaryrefslogtreecommitdiffstats
blob: 4390e9bdd995634b0cf7c8d4521eb068d5bfb496 (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * 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/PresenceSender.h"
#include "Swiften/Client/StanzaChannel.h"
#include "Swiften/Elements/IQ.h"
#include "Swiften/Elements/MUCPayload.h"

namespace Swift {

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

MUC::MUC(StanzaChannel* stanzaChannel, PresenceSender* presenceSender, const JID &muc) : ownMUCJID(muc), stanzaChannel(stanzaChannel), presenceSender(presenceSender) {
	scopedConnection_ = stanzaChannel->onPresenceReceived.connect(boost::bind(&MUC::handleIncomingPresence, this, _1));
}

void MUC::joinAs(const String &nick) {
	firstPresenceSeen = false;

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

	boost::shared_ptr<Presence> joinPresence(new Presence());
	joinPresence->setTo(ownMUCJID);
	joinPresence->addPayload(boost::shared_ptr<Payload>(new MUCPayload()));
	presenceSender->sendPresence(joinPresence);
}

void MUC::part() {
	presenceSender->removeDirectedPresenceReceiver(ownMUCJID);
}

void MUC::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
	if (!isFromMUC(presence->getFrom())) {
		return;
	}

	if (!firstPresenceSeen) {
		if (presence->getType() == Presence::Error) {
			onJoinComplete(JoinFailed);
			return;
		}
		firstPresenceSeen = true;
		onJoinComplete(JoinSucceeded);
		presenceSender->addDirectedPresenceReceiver(ownMUCJID);
	}

	String nick = presence->getFrom().getResource();
	if (nick.isEmpty()) {
		return;
	}
	if (presence->getType() == Presence::Unavailable) {
		std::map<String,MUCOccupant>::iterator i = occupants.find(nick);
		if (i != occupants.end()) {
			onOccupantLeft(i->second, Part, "");
			occupants.erase(i);
		}
	}
	else if (presence->getType() == Presence::Available) {
		std::pair<std::map<String,MUCOccupant>::iterator, bool> result = occupants.insert(std::make_pair(nick, MUCOccupant(nick)));
		if (result.second) {
			onOccupantJoined(result.first->second);
		}
		onOccupantPresenceChange(presence);
	}
}


}