summaryrefslogtreecommitdiffstats
blob: 83009e98cbf6227e2e5318d6e201a66895af083f (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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Presence/SubscriptionManager.h>

#include <boost/bind.hpp>

#include <Swiften/Client/StanzaChannel.h>

namespace Swift {

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

SubscriptionManager::~SubscriptionManager() {
    stanzaChannel->onPresenceReceived.disconnect(boost::bind(&SubscriptionManager::handleIncomingPresence, this, _1));
}

void SubscriptionManager::cancelSubscription(const JID& jid) {
    Presence::ref stanza(new Presence());
    stanza->setType(Presence::Unsubscribed);
    stanza->setTo(jid);
    stanzaChannel->sendPresence(stanza);
}

void SubscriptionManager::confirmSubscription(const JID& jid) {
    Presence::ref stanza(new Presence());
    stanza->setType(Presence::Subscribed);
    stanza->setTo(jid);
    stanzaChannel->sendPresence(stanza);
}


void SubscriptionManager::requestSubscription(const JID& jid) {
    Presence::ref stanza(new Presence());
    stanza->setType(Presence::Subscribe);
    stanza->setTo(jid);
    stanzaChannel->sendPresence(stanza);
}

void SubscriptionManager::handleIncomingPresence(Presence::ref presence) {
    JID bareJID(presence->getFrom().toBare());
    if (presence->getType() == Presence::Subscribe) {
        onPresenceSubscriptionRequest(bareJID, presence->getStatus(), presence);
    }
    else if (presence->getType() == Presence::Unsubscribe) {
        onPresenceSubscriptionRevoked(bareJID, presence->getStatus());
    }
}


}