Changeset - 17fe52b61f81
[Not reviewed]
0 4 0
Jan Kaluza - 10 years ago 2016-01-20 07:36:05
jkaluza@redhat.com
Fix #98 - Leave the room internally when disconnecting user from room because of error.
4 files changed with 47 insertions and 13 deletions:
0 comments (0 inline, 0 general)
include/transport/User.h
Show inline comments
 
@@ -117,48 +117,50 @@ class User {
 
		}
 

	
 
		int getResourceCount() {
 
			return m_resources;
 
		}
 

	
 
		void addUserSetting(const std::string &key, const std::string &value) {
 
			m_settings[key] = value;
 
		}
 

	
 
		const std::string &getUserSetting(const std::string &key) {
 
			return m_settings[key];
 
		}
 

	
 
		void setCacheMessages(bool cacheMessages);
 

	
 
		bool shouldCacheMessages() {
 
			return m_cacheMessages;
 
		}
 

	
 
		void setReconnectLimit(int limit) {
 
			m_reconnectLimit = limit;
 
		}
 

	
 
		void leaveRoom(const std::string &room);
 

	
 
		boost::signal<void ()> onReadyToConnect;
 
		boost::signal<void (Swift::Presence::ref presence)> onPresenceChanged;
 
		boost::signal<void (Swift::Presence::ref presence)> onRawPresenceReceived;
 
		boost::signal<void (const Swift::JID &who, const std::string &room, const std::string &nickname, const std::string &password)> onRoomJoined;
 
		boost::signal<void (const std::string &room)> onRoomLeft;
 
		boost::signal<void ()> onDisconnected;
 

	
 
	private:
 
		void onConnectingTimeout();
 

	
 
		Swift::JID m_jid;
 
		Component *m_component;
 
		RosterManager *m_rosterManager;
 
		UserManager *m_userManager;
 
		ConversationManager *m_conversationManager;
 
		PresenceOracle *m_presenceOracle;
 
		UserInfo m_userInfo;
 
		void *m_data;
 
		bool m_connected;
 
		bool m_readyForConnect;
 
		bool m_ignoreDisconnect;
 
		Swift::Timer::ref m_reconnectTimer;
 
		boost::shared_ptr<Swift::Connection> connection;
 
		time_t m_lastActivity;
libtransport/Conversation.cpp
Show inline comments
 
@@ -342,27 +342,34 @@ void Conversation::handleRawPresence(Swift::Presence::ref presence) {
 

	
 
void Conversation::handleParticipantChanged(const std::string &nick, Conversation::ParticipantFlag flag, int status, const std::string &statusMessage, const std::string &newname) {
 
	Swift::Presence::ref presence = generatePresence(nick, flag, status, statusMessage, newname);
 

	
 
	if (presence->getType() == Swift::Presence::Unavailable) {
 
		m_participants.erase(nick);
 
	}
 
	else {
 
		m_participants[nick] = presence;
 
	}
 

	
 

	
 
	BOOST_FOREACH(const Swift::JID &jid, m_jids) {
 
		presence->setTo(jid);
 
		m_conversationManager->getComponent()->getFrontend()->sendPresence(presence);
 
	}
 
	if (!newname.empty()) {
 
		handleParticipantChanged(newname, flag, status, statusMessage);
 
	}
 

	
 
	if (m_sentInitialPresence && m_subject) {
 
		m_conversationManager->getComponent()->getFrontend()->sendMessage(m_subject);
 
		m_subject.reset();
 
	}
 

	
 
	// We send error presences only to inform user that he is disconnected
 
	// from the room. This code must be extended in case we start sending error
 
	// presences in other situations.
 
	if (presence->getType() == Swift::Presence::Error) {
 
		m_conversationManager->getUser()->leaveRoom(m_legacyName);
 
	}
 
}
 

	
 
}
libtransport/User.cpp
Show inline comments
 
@@ -130,106 +130,111 @@ void User::sendCurrentPresence() {
 
}
 

	
 
void User::setConnected(bool connected) {
 
	m_connected = connected;
 
	m_reconnectCounter = 0;
 
	setIgnoreDisconnect(false);
 
	updateLastActivity();
 

	
 
	sendCurrentPresence();
 

	
 
	if (m_connected) {
 
		BOOST_FOREACH(Swift::Presence::ref &presence, m_joinedRooms) {
 
			handlePresence(presence, true);
 
		}
 
	}
 
}
 

	
 
void User::setCacheMessages(bool cacheMessages) {
 
	if (m_component->inServerMode() && !m_cacheMessages && cacheMessages) {
 
		m_conversationManager->sendCachedChatMessages();
 
	}
 
	m_cacheMessages = cacheMessages;
 
}
 

	
 
void User::leaveRoom(const std::string &room) {
 
	onRoomLeft(room);
 

	
 
	BOOST_FOREACH(Swift::Presence::ref &p, m_joinedRooms) {
 
		if (Buddy::JIDToLegacyName(p->getTo()) == room) {
 
			m_joinedRooms.remove(p);
 
			break;
 
		}
 
	}
 

	
 
	Conversation *conv = m_conversationManager->getConversation(room);
 
	if (conv) {
 
		m_conversationManager->removeConversation(conv);
 
		delete conv;
 
	}
 
}
 

	
 
void User::handlePresence(Swift::Presence::ref presence, bool forceJoin) {
 
	LOG4CXX_INFO(logger, "PRESENCE " << presence->getFrom().toString() << " " << presence->getTo().toString());
 
	if (!m_connected) {
 
		// we are not connected to legacy network, so we should do it when disco#info arrive :)
 
		if (m_readyForConnect == false) {
 
			boost::shared_ptr<Swift::CapsInfo> capsInfo = presence->getPayload<Swift::CapsInfo>();
 
			if (capsInfo && capsInfo->getHash() == "sha-1") {
 
				if (m_component->getFrontend()->sendCapabilitiesRequest(presence->getFrom()) != Swift::DiscoInfo::ref()) {
 
					LOG4CXX_INFO(logger, m_jid.toString() << ": Ready to be connected to legacy network");
 
					m_readyForConnect = true;
 
					onReadyToConnect();
 
				}
 
				else {
 
					m_reconnectTimer->start();
 
				}
 
			}
 
			else if (m_component->inServerMode()) {
 
				LOG4CXX_INFO(logger, m_jid.toString() << ": Ready to be connected to legacy network");
 
				m_readyForConnect = true;
 
				onReadyToConnect();
 
			}
 
			else {
 
				m_reconnectTimer->start();
 
			}
 
		}
 
	}
 

	
 
	if (!presence->getTo().getNode().empty()) {
 
		bool isMUC = presence->getPayload<Swift::MUCPayload>() != NULL || *presence->getTo().getNode().c_str() == '#';
 
		if (presence->getType() == Swift::Presence::Unavailable) {
 
			std::string room = Buddy::JIDToLegacyName(presence->getTo());
 
			Conversation *conv = m_conversationManager->getConversation(room);
 
			if (conv) {
 
				conv->removeJID(presence->getFrom());
 
				if (!conv->getJIDs().empty()) {
 
					return;
 
				}
 
			}
 
			else {
 
				return;
 
			}
 

	
 
			if (getUserSetting("stay_connected") != "1") {
 
				LOG4CXX_INFO(logger, m_jid.toString() << ": Going to left room " << room);
 
				onRawPresenceReceived(presence);
 
				onRoomLeft(room);
 

	
 
				BOOST_FOREACH(Swift::Presence::ref &p, m_joinedRooms) {
 
					if (p->getTo() == presence->getTo()) {
 
						m_joinedRooms.remove(p);
 
						break;
 
					}
 
				}
 

	
 
				if (conv) {
 
					m_conversationManager->removeConversation(conv);
 
					delete conv;
 
				}
 
				leaveRoom(room);
 
			}
 

	
 
			return;
 
		}
 
		else if (isMUC) {
 
			// force connection to legacy network to let backend to handle auto-join on connect.
 
			if (!m_readyForConnect) {
 
				LOG4CXX_INFO(logger, m_jid.toString() << ": Ready to be connected to legacy network");
 
				m_readyForConnect = true;
 
				onReadyToConnect();
 
			}
 

	
 
			std::string room = Buddy::JIDToLegacyName(presence->getTo());
 
			std::string password = "";
 
			if (presence->getPayload<Swift::MUCPayload>() != NULL) {
 
				password = presence->getPayload<Swift::MUCPayload>()->getPassword() ? *presence->getPayload<Swift::MUCPayload>()->getPassword() : "";
 
			}
 

	
 
			Conversation *conv = m_conversationManager->getConversation(room);
 
			if (conv != NULL) {
 
				if (std::find(conv->getJIDs().begin(), conv->getJIDs().end(), presence->getFrom()) != conv->getJIDs().end()) {
 
					LOG4CXX_INFO(logger, m_jid.toString() << ": User has already tried to join room " << room << " as " << presence->getTo().getResource());
 
				}
 
				else {
tests/libtransport/user.cpp
Show inline comments
 
@@ -8,48 +8,49 @@
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Server/ServerFromClientSession.h"
 
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
 
#include "basictest.h"
 

	
 
using namespace Transport;
 

	
 
class UserTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
 
	CPPUNIT_TEST_SUITE(UserTest);
 
	CPPUNIT_TEST(sendCurrentPresence);
 
    CPPUNIT_TEST(handlePresence);
 
	CPPUNIT_TEST(handlePresenceJoinRoom);
 
	CPPUNIT_TEST(handlePresenceJoinRoomTwoResources);
 
	CPPUNIT_TEST(handlePresenceLeaveRoom);
 
	CPPUNIT_TEST(handlePresenceLeaveRoomTwoResources);
 
	CPPUNIT_TEST(handlePresenceLeaveRoomTwoResourcesOneDisconnects);
 
	CPPUNIT_TEST(handlePresenceLeaveRoomBouncer);
 
	CPPUNIT_TEST(handlePresenceLeaveRoomTwoResourcesBouncer);
 
	CPPUNIT_TEST(handlePresenceLeaveRoomTwoResourcesOneDisconnectsBouncer);
 
	CPPUNIT_TEST(leaveJoinedRoom);
 
	CPPUNIT_TEST(joinRoomBeforeConnected);
 
	CPPUNIT_TEST(handleDisconnected);
 
	CPPUNIT_TEST(handleDisconnectedReconnect);
 
	CPPUNIT_TEST(joinRoomHandleDisconnectedRejoin);
 
	CPPUNIT_TEST(joinRoomAfterFlagNotAuthorized);
 
	CPPUNIT_TEST_SUITE_END();
 

	
 
	public:
 
		std::string room;
 
		std::string roomNickname;
 
		std::string roomPassword;
 
		bool readyToConnect;
 
		bool disconnected;
 
		Swift::Presence::ref changedPresence;
 

	
 
		void setUp (void) {
 
			disconnected = false;
 
			readyToConnect = false;
 
			changedPresence = Swift::Presence::ref();
 
			room = "";
 
			roomNickname = "";
 
			roomPassword = "";
 

	
 
			setMeUp();
 
			userManager->onUserCreated.connect(boost::bind(&UserTest::handleUserCreated, this, _1));
 
			connectUser();
 
			received.clear();
 
		}
 

	
 
@@ -117,48 +118,49 @@ class UserTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
 
		User *user = userManager->getUser("user@localhost");
 

	
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setTo("room@localhost/hanzz");
 
		response->setFrom("user@localhost/resource");
 

	
 
		Swift::MUCPayload *payload = new Swift::MUCPayload();
 
		payload->setPassword("password");
 
		response->addPayload(boost::shared_ptr<Swift::Payload>(payload));
 
		injectPresence(response);
 
		loop->processEvents();
 

	
 
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
 
		CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
 
		CPPUNIT_ASSERT_EQUAL(std::string("hanzz"), roomNickname);
 
		CPPUNIT_ASSERT_EQUAL(std::string("password"), roomPassword);
 

	
 
		room = "";
 
		roomNickname = "";
 
		roomPassword = "";
 

	
 
		// simulate that backend joined the room
 
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "room", true);
 
		conv->addJID("user@localhost/resource");
 
		conv->setNickname("hanzz");
 
		user->getConversationManager()->addConversation(conv);
 

	
 
		received.clear();
 
		injectPresence(response);
 
		loop->processEvents();
 

	
 
		// no presence received in server mode, just disco#info
 
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
 

	
 
		CPPUNIT_ASSERT_EQUAL(std::string(""), room);
 
		CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
 
		CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
 
	}
 

	
 
	void handlePresenceJoinRoomTwoResources() {
 
		handlePresenceJoinRoom();
 
		User *user = userManager->getUser("user@localhost");
 

	
 
		// Add 1 participant
 
		Conversation *conv = user->getConversationManager()->getConversation("room");
 
		conv->handleParticipantChanged("anotheruser", Conversation::PARTICIPANT_FLAG_NONE, Swift::StatusShow::Away, "my status message");
 

	
 
		// Connect 2nd resource
 
		connectSecondResource();
 
@@ -431,27 +433,45 @@ class UserTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
 
		CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
 

	
 
		user->setConnected(true);
 
		CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
 
		CPPUNIT_ASSERT_EQUAL(std::string("hanzz"), roomNickname);
 
		CPPUNIT_ASSERT_EQUAL(std::string("password"), roomPassword);
 
	}
 

	
 
	void joinRoomHandleDisconnectedRejoin() {
 
		User *user = userManager->getUser("user@localhost");
 
		handlePresenceJoinRoom();
 
		handleDisconnectedReconnect();
 
		room = "";
 
		roomNickname = "";
 
		roomPassword = "";
 
		received.clear();
 
		user->setConnected(true);
 

	
 
		CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
 
		CPPUNIT_ASSERT_EQUAL(std::string("hanzz"), roomNickname);
 
		CPPUNIT_ASSERT_EQUAL(std::string("password"), roomPassword);
 
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
 
	}
 

	
 
	void joinRoomAfterFlagNotAuthorized() {
 
		User *user = userManager->getUser("user@localhost");
 
		handlePresenceJoinRoom();
 

	
 
		Conversation *conv = user->getConversationManager()->getConversation("room");
 
		conv->handleParticipantChanged("hanzz", Conversation::PARTICIPANT_FLAG_NOT_AUTHORIZED, Swift::StatusShow::Away, "my status message");
 
		loop->processEvents();
 

	
 
		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
 
		CPPUNIT_ASSERT_EQUAL(Swift::Presence::Error, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
 
		CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::ErrorPayload>());
 
		CPPUNIT_ASSERT_EQUAL(Swift::ErrorPayload::NotAuthorized, getStanza(received[0])->getPayload<Swift::ErrorPayload>()->getCondition());
 

	
 
		received.clear();
 
		handlePresenceJoinRoom();
 
	}
 

	
 
};
 

	
 
CPPUNIT_TEST_SUITE_REGISTRATION (UserTest);
0 comments (0 inline, 0 general)