summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2011-01-12 20:15:56 (GMT)
committerRemko Tronçon <git@el-tramo.be>2011-01-12 20:15:56 (GMT)
commit694ff33145467a9d8a0f87317124bc5c4251d18d (patch)
tree5d509c082417791add4356e4d512c7526adeeebc /Swiften/MUC
parent52cc4918673a80739ae67c99943273339e848458 (diff)
downloadswift-694ff33145467a9d8a0f87317124bc5c4251d18d.zip
swift-694ff33145467a9d8a0f87317124bc5c4251d18d.tar.bz2
Refactored MUC code a bit.
Diffstat (limited to 'Swiften/MUC')
-rw-r--r--Swiften/MUC/MUC.cpp9
-rw-r--r--Swiften/MUC/MUC.h2
-rw-r--r--Swiften/MUC/MUCOccupant.cpp46
-rw-r--r--Swiften/MUC/MUCOccupant.h41
-rw-r--r--Swiften/MUC/UnitTest/MUCTest.cpp116
5 files changed, 124 insertions, 90 deletions
diff --git a/Swiften/MUC/MUC.cpp b/Swiften/MUC/MUC.cpp
index 309107c..15a9a4f 100644
--- a/Swiften/MUC/MUC.cpp
+++ b/Swiften/MUC/MUC.cpp
@@ -43,6 +43,7 @@ void MUC::internalJoin(const String &nick) {
mucRegistry->addMUC(getJID());
joinComplete_ = false;
ownMUCJID = JID(ownMUCJID.getNode(), ownMUCJID.getDomain(), nick);
+ presenceSender->addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
boost::shared_ptr<Presence> joinPresence(presenceSender->getLastSentUndirectedPresence());
assert(joinPresence->getType() == Presence::Available);
joinPresence->setTo(ownMUCJID);
@@ -93,6 +94,7 @@ void MUC::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
if (!joinComplete_) {
if (presence->getType() == Presence::Error) {
String reason;
+ presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::AndSendPresence);
onJoinFailed(presence->getPayload<ErrorPayload>());
return;
}
@@ -156,15 +158,18 @@ void MUC::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
if (status.code == 110) {
/* Simply knowing this is your presence is enough, 210 doesn't seem to be necessary. */
joinComplete_ = true;
+ presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
ownMUCJID = presence->getFrom();
- onJoinComplete(getOwnNick());
presenceSender->addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
+ onJoinComplete(getOwnNick());
}
if (status.code == 201) {
/* Room is created and locked */
/* Currently deal with this by making an instant room */
+ presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
ownMUCJID = presence->getFrom();
boost::shared_ptr<MUCOwnerPayload> mucPayload(new MUCOwnerPayload());
+ presenceSender->addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
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));
@@ -177,10 +182,10 @@ void MUC::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
void MUC::handleCreationConfigResponse(boost::shared_ptr<MUCOwnerPayload> /*unused*/, ErrorPayload::ref error) {
if (error) {
+ presenceSender->removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::AndSendPresence);
onJoinFailed(error);
} else {
onJoinComplete(getOwnNick()); /* Previously, this wasn't needed here, as the presence duplication bug caused an emit elsewhere. */
- presenceSender->addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender::DontSendPresence);
}
}
diff --git a/Swiften/MUC/MUC.h b/Swiften/MUC/MUC.h
index 56cd392..44759d5 100644
--- a/Swiften/MUC/MUC.h
+++ b/Swiften/MUC/MUC.h
@@ -10,7 +10,7 @@
#include "Swiften/Base/String.h"
#include "Swiften/Elements/Message.h"
#include "Swiften/Elements/Presence.h"
-#include "Swiften/MUC/MUCOccupant.h"
+#include "Swiften/Elements/MUCOccupant.h"
#include "Swiften/MUC/MUCRegistry.h"
#include "Swiften/Elements/MUCOwnerPayload.h"
diff --git a/Swiften/MUC/MUCOccupant.cpp b/Swiften/MUC/MUCOccupant.cpp
deleted file mode 100644
index 33a5fcc..0000000
--- a/Swiften/MUC/MUCOccupant.cpp
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2010 Kevin Smith
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
- */
-
-#include "Swiften/MUC/MUCOccupant.h"
-
-namespace Swift {
-
-MUCOccupant::MUCOccupant(const String &nick, Role role, Affiliation affiliation) : nick_(nick), role_(role), affiliation_(affiliation) {
-}
-
-MUCOccupant::~MUCOccupant() {
-}
-
-MUCOccupant::MUCOccupant(const MUCOccupant& other) : nick_(other.getNick()), role_(other.getRole()), affiliation_(other.getAffiliation()), realJID_(other.getRealJID()) {
-
-}
-
-String MUCOccupant::getNick() const {
- return nick_;
-}
-
-MUCOccupant::Role MUCOccupant::getRole() const {
- return role_;
-}
-
-MUCOccupant::Affiliation MUCOccupant::getAffiliation() const {
- return affiliation_;
-}
-
-void MUCOccupant::setRealJID(const JID& realJID) {
- realJID_ = realJID;
-}
-
-void MUCOccupant::setNick(const String& nick) {
- nick_ = nick;
-}
-
-
-boost::optional<JID> MUCOccupant::getRealJID() const {
- return realJID_;
-}
-
-}
diff --git a/Swiften/MUC/MUCOccupant.h b/Swiften/MUC/MUCOccupant.h
deleted file mode 100644
index 96ac5ad..0000000
--- a/Swiften/MUC/MUCOccupant.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2010 Kevin Smith
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
- */
-
-#pragma once
-
-#include <boost/optional.hpp>
-
-#include "Swiften/Base/String.h"
-#include "Swiften/JID/JID.h"
-
-namespace Swift {
- class Client;
-
- class MUCOccupant {
- public:
- enum Role {Moderator, Participant, Visitor, NoRole};
- enum Affiliation {Owner, Admin, Member, Outcast, NoAffiliation};
-
- MUCOccupant(const String &nick, Role role, Affiliation affiliation);
- MUCOccupant(const MUCOccupant& other);
- ~MUCOccupant();
-
- String getNick() const;
- Role getRole() const;
- Affiliation getAffiliation() const;
- boost::optional<JID> getRealJID() const;
- void setRealJID(const JID& jid);
- void setNick(const String& nick);
-
- private:
- String nick_;
- Role role_;
- Affiliation affiliation_;
- boost::optional<JID> realJID_;
- /* If you add a field, remember to update the const copy constructor */
- };
-}
-
diff --git a/Swiften/MUC/UnitTest/MUCTest.cpp b/Swiften/MUC/UnitTest/MUCTest.cpp
new file mode 100644
index 0000000..1e3582b
--- /dev/null
+++ b/Swiften/MUC/UnitTest/MUCTest.cpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <boost/shared_ptr.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+#include <boost/bind.hpp>
+
+#include <Swiften/MUC/MUC.h>
+#include <Swiften/Client/DummyStanzaChannel.h>
+#include <Swiften/Presence/StanzaChannelPresenceSender.h>
+#include <Swiften/Presence/DirectedPresenceSender.h>
+#include <Swiften/Queries/IQRouter.h>
+
+using namespace Swift;
+
+class MUCTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(MUCTest);
+ CPPUNIT_TEST(testJoin);
+ CPPUNIT_TEST(testJoin_ChangePresenceDuringJoin);
+ /*CPPUNIT_TEST(testJoin_Success);
+ CPPUNIT_TEST(testJoin_Fail);*/
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ channel = new DummyStanzaChannel();
+ router = new IQRouter(channel);
+ mucRegistry = new MUCRegistry();
+ stanzaChannelPresenceSender = new StanzaChannelPresenceSender(channel);
+ presenceSender = new DirectedPresenceSender(stanzaChannelPresenceSender);
+ }
+
+ void tearDown() {
+ delete presenceSender;
+ delete stanzaChannelPresenceSender;
+ delete mucRegistry;
+ delete router;
+ delete channel;
+ }
+
+ void testJoin() {
+ MUC::ref testling = createMUC(JID("foo@bar.com"));
+ testling->joinAs("Alice");
+
+ CPPUNIT_ASSERT(mucRegistry->isMUC(JID("foo@bar.com")));
+ Presence::ref p = channel->getStanzaAtIndex<Presence>(0);
+ CPPUNIT_ASSERT(p);
+ CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/Alice"), p->getTo());
+ }
+
+ void testJoin_ChangePresenceDuringJoin() {
+ MUC::ref testling = createMUC(JID("foo@bar.com"));
+ testling->joinAs("Alice");
+
+ presenceSender->sendPresence(Presence::create("Test"));
+ CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(channel->sentStanzas.size()));
+ Presence::ref p = channel->getStanzaAtIndex<Presence>(2);
+ CPPUNIT_ASSERT(p);
+ CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/Alice"), p->getTo());
+ }
+
+ /*void testJoin_Success() {
+ MUC::ref testling = createMUC(JID("foo@bar.com"));
+ testling->onJoinFinished.connect(boost::bind(&MUCTest::handleJoinFinished, this, _1, _2));
+ testling->joinAs("Alice");
+ receivePresence(JID("foo@bar.com/Rabbit"), "Here");
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(joinResults.size()));
+ CPPUNIT_ASSERT_EQUAL(String("Alice"), joinResults[0].nick);
+ CPPUNIT_ASSERT(joinResults[0].error);
+ }
+
+ void testJoin_Fail() {
+ //CPPUNIT_ASSERT(!mucRegistry->isMUC(JID("foo@bar.com")));
+ }*/
+
+ private:
+ MUC::ref createMUC(const JID& jid) {
+ return boost::make_shared<MUC>(channel, router, presenceSender, jid, mucRegistry);
+ }
+
+ void handleJoinFinished(const String& nick, ErrorPayload::ref error) {
+ JoinResult r;
+ r.nick = nick;
+ r.error = error;
+ joinResults.push_back(r);
+ }
+
+ /*void receivePresence(const JID& jid, const String& status, const MUCUserPayload::Item& item) {
+ Presence::ref p = Presence::create(status);
+ p->setFrom(jid);
+ MUCUserPayload::ref mucUserPayload = boost::make_shared<MUCUserPayload>();
+ mucUserPayload->addItem(item);
+ p->addPayload(mucUserPayload);
+ channel->onPresenceReceived(p);
+ }*/
+
+ private:
+ DummyStanzaChannel* channel;
+ IQRouter* router;
+ MUCRegistry* mucRegistry;
+ StanzaChannelPresenceSender* stanzaChannelPresenceSender;
+ DirectedPresenceSender* presenceSender;
+ struct JoinResult {
+ String nick;
+ ErrorPayload::ref error;
+ };
+ std::vector<JoinResult> joinResults;
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(MUCTest);