diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Events/MessageEvent.h | 4 | ||||
-rw-r--r-- | Swiften/Events/StanzaEvent.h (renamed from Swiften/Events/Event.h) | 6 | ||||
-rw-r--r-- | Swiften/Events/SubscriptionRequestEvent.h | 36 | ||||
-rw-r--r-- | Swiften/Presence/PresenceOracle.cpp | 24 | ||||
-rw-r--r-- | Swiften/Presence/UnitTest/PresenceOracleTest.cpp | 58 |
5 files changed, 99 insertions, 29 deletions
diff --git a/Swiften/Events/MessageEvent.h b/Swiften/Events/MessageEvent.h index 0adfa82..1f71493 100644 --- a/Swiften/Events/MessageEvent.h +++ b/Swiften/Events/MessageEvent.h @@ -6,11 +6,11 @@ #include <boost/signals.hpp> #include <boost/shared_ptr.hpp> -#include "Swiften/Events/Event.h" +#include "Swiften/Events/StanzaEvent.h" #include "Swiften/Elements/Message.h" namespace Swift { - class MessageEvent : public Event { + class MessageEvent : public StanzaEvent { public: MessageEvent(boost::shared_ptr<Message> stanza) : stanza_(stanza){}; virtual ~MessageEvent(){}; diff --git a/Swiften/Events/Event.h b/Swiften/Events/StanzaEvent.h index b4a71f6..adef112 100644 --- a/Swiften/Events/Event.h +++ b/Swiften/Events/StanzaEvent.h @@ -4,10 +4,10 @@ #include <boost/shared_ptr.hpp> namespace Swift { - class Event { + class StanzaEvent { public: - Event(){}; - virtual ~Event() {}; + StanzaEvent(){}; + virtual ~StanzaEvent() {}; boost::signal<void()> onConclusion; }; } diff --git a/Swiften/Events/SubscriptionRequestEvent.h b/Swiften/Events/SubscriptionRequestEvent.h new file mode 100644 index 0000000..fe41df7 --- /dev/null +++ b/Swiften/Events/SubscriptionRequestEvent.h @@ -0,0 +1,36 @@ +#pragma once + +#include <cassert> + +#include <boost/signals.hpp> +#include <boost/shared_ptr.hpp> + +#include "Swiften/Events/StanzaEvent.h" +#include "Swiften/Base/String.h" +#include "Swiften/JID/JID.h" + +namespace Swift { + class SubscriptionRequestEvent : public StanzaEvent { + public: + SubscriptionRequestEvent(const JID& jid, const String& reason) : jid_(jid), reason_(reason){}; + virtual ~SubscriptionRequestEvent(){}; + const JID& getJID() const {return jid_;}; + const String& getReason() const {return reason_;}; + boost::signal<void()> onAccept; + boost::signal<void()> onDecline; + void accept() { + onAccept(); + onConclusion(); + } + + void decline() { + onDecline(); + onConclusion(); + } + + private: + JID jid_; + String reason_; + }; +} + diff --git a/Swiften/Presence/PresenceOracle.cpp b/Swiften/Presence/PresenceOracle.cpp index af98510..f1e6adb 100644 --- a/Swiften/Presence/PresenceOracle.cpp +++ b/Swiften/Presence/PresenceOracle.cpp @@ -13,17 +13,23 @@ PresenceOracle::PresenceOracle(StanzaChannel* stanzaChannel) { void PresenceOracle::handleIncomingPresence(boost::shared_ptr<Presence> presence) { JID bareJID = JID(presence->getFrom().toBare()); - 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; + + if (presence->getType() == Presence::Subscribe) { + fprintf(stderr, "rarr, sub\n"); + 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); } - jidMap[presence->getFrom()] = presence; - entries_[bareJID] = jidMap; - onPresenceChange(presence, last); } } diff --git a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp index e6f56b2..2c9c526 100644 --- a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp +++ b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp @@ -15,11 +15,18 @@ class PresencePointerPair { boost::shared_ptr<Presence> two; }; +class SubscriptionRequestInfo { + public: + boost::optional<JID> jid; + boost::optional<String> reason; +}; + class PresenceOracleTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(PresenceOracleTest); CPPUNIT_TEST(testFirstPresence); CPPUNIT_TEST(testSecondPresence); + CPPUNIT_TEST(testSubscriptionRequest); CPPUNIT_TEST_SUITE_END(); private: @@ -37,11 +44,11 @@ class PresenceOracleTest : public CppUnit::TestFixture CPPUNIT_ASSERT_EQUAL(newPresence, out->one); } - void handlePresenceSubscriptionRequest(const JID& jid, const String& reason, boost::optional<JID>& outJID, boost::optional<String>& outReason) { - CPPUNIT_ASSERT(!outJID); - CPPUNIT_ASSERT(!outReason); - outJID = jid; - outReason = reason; + void handlePresenceSubscriptionRequest(const JID& jid, const String& reason, SubscriptionRequestInfo* info) { + CPPUNIT_ASSERT(!info->jid); + CPPUNIT_ASSERT(!info->reason); + info->jid = jid; + info->reason = reason; } void setUp() { @@ -58,15 +65,14 @@ class PresenceOracleTest : public CppUnit::TestFixture PresencePointerPair out; oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out)); - boost::optional<JID> jid; - boost::optional<String> reason; - oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, jid, reason)); + SubscriptionRequestInfo info; + oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info)); boost::shared_ptr<Presence> sentPresence(new Presence("blarb")); stanzaChannel_->onPresenceReceived(sentPresence); - CPPUNIT_ASSERT(!jid); - CPPUNIT_ASSERT(!reason); + CPPUNIT_ASSERT(!info.jid); + CPPUNIT_ASSERT(!info.reason); CPPUNIT_ASSERT(out.two.get() == NULL); CPPUNIT_ASSERT_EQUAL(sentPresence, out.one); } @@ -80,19 +86,41 @@ class PresenceOracleTest : public CppUnit::TestFixture CPPUNIT_ASSERT_EQUAL(sentPresence1, out.one); out.one = boost::shared_ptr<Presence>(); - boost::optional<JID> jid; - boost::optional<String> reason; - oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, jid, reason)); + SubscriptionRequestInfo info; + oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info)); boost::shared_ptr<Presence> sentPresence2(new Presence("test2")); stanzaChannel_->onPresenceReceived(sentPresence2); - CPPUNIT_ASSERT(!jid); - CPPUNIT_ASSERT(!reason); + CPPUNIT_ASSERT(!info.jid); + CPPUNIT_ASSERT(!info.reason); CPPUNIT_ASSERT_EQUAL(sentPresence1, out.two); CPPUNIT_ASSERT_EQUAL(sentPresence2, out.one); } + void testSubscriptionRequest() { + PresencePointerPair out; + oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out)); + SubscriptionRequestInfo info; + oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info)); + + String reasonText = "Because I want to"; + JID sentJID = JID("me@example.com"); + + boost::shared_ptr<Presence> sentPresence(new Presence()); + sentPresence->setType(Presence::Subscribe); + sentPresence->setFrom(sentJID); + sentPresence->setStatus(reasonText); + stanzaChannel_->onPresenceReceived(sentPresence); + + CPPUNIT_ASSERT(info.jid); + CPPUNIT_ASSERT(info.reason); + CPPUNIT_ASSERT_EQUAL(sentJID, info.jid.get()); + CPPUNIT_ASSERT_EQUAL(reasonText, info.reason.get()); + + CPPUNIT_ASSERT(!out.two); + CPPUNIT_ASSERT(!out.one); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(PresenceOracleTest); |