summaryrefslogtreecommitdiffstats
blob: 988fc1046cc06e03fc5dd14db2bff9f419998e21 (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
#include "PresenceOracle.h"

#include <boost/bind.hpp>
#include "Swiften/Client/StanzaChannel.h"
namespace Swift {

typedef std::pair<JID, std::map<JID, boost::shared_ptr<Presence> > > JIDMapPair;
typedef std::pair<JID, boost::shared_ptr<Presence> > JIDPresencePair;

PresenceOracle::PresenceOracle(StanzaChannel* stanzaChannel) {
	stanzaChannel_ = stanzaChannel;
	stanzaChannel_->onPresenceReceived.connect(boost::bind(&PresenceOracle::handleIncomingPresence, this, _1));
}

void PresenceOracle::cancelSubscription(const JID& jid) {
	boost::shared_ptr<Presence> stanza(new Presence());
	stanza->setType(Presence::Unsubscribed);
	stanza->setTo(jid);
	stanzaChannel_->sendPresence(stanza);
}

void PresenceOracle::confirmSubscription(const JID& jid) {
	boost::shared_ptr<Presence> stanza(new Presence());
	stanza->setType(Presence::Subscribed);
	stanza->setTo(jid);
	stanzaChannel_->sendPresence(stanza);
}


void PresenceOracle::requestSubscription(const JID& jid) {
	boost::shared_ptr<Presence> stanza(new Presence());
	stanza->setType(Presence::Subscribe);
	stanza->setTo(jid);
	stanzaChannel_->sendPresence(stanza);
}


void PresenceOracle::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
	JID bareJID = JID(presence->getFrom().toBare());

	if (presence->getType() == Presence::Subscribe) {
		onPresenceSubscriptionRequest(bareJID, presence->getStatus());
	} else {
		std::map<JID, boost::shared_ptr<Presence> > jidMap = entries_[bareJID];
		boost::shared_ptr<Presence> last;
		foreach(JIDPresencePair pair, jidMap) {
			if (pair.first == presence->getFrom()) {
				last = pair.second;
				break;
			}
		}
		jidMap[presence->getFrom()] = presence;
		entries_[bareJID] = jidMap;
		onPresenceChange(presence, last);
	}
}

}