Changeset - c80c02ccb158
[Not reviewed]
0 4 0
Jan Kaluza - 9 years ago 2016-02-10 17:02:06
jkaluza@redhat.com
Libtransport: Rename user when he tries to connect to room from second client with different nickname than he used with first client
4 files changed with 79 insertions and 33 deletions:
0 comments (0 inline, 0 general)
include/transport/Conversation.h
Show inline comments
 
@@ -136,13 +136,13 @@ class Conversation {
 
			return m_room;
 
		}
 

	
 
		void destroyRoom();
 

	
 
		std::string getParticipants();
 
		void sendParticipants(const Swift::JID &to);
 
		void sendParticipants(const Swift::JID &to, const std::string &nickname);
 

	
 
		void sendCachedMessages(const Swift::JID &to = Swift::JID());
 

	
 
		void setMUCEscaping(bool mucEscaping);
 

	
 
	private:
 
@@ -157,12 +157,13 @@ class Conversation {
 
		bool m_muc;
 
		Swift::JID m_jid;
 
		std::list<Swift::JID> m_jids;
 
		bool m_sentInitialPresence;
 
		bool m_nicknameChanged;
 
		bool m_mucEscaping;
 
		bool m_sentInitialSubject;
 

	
 
		// TODO: Move this to some extra class to cache the most used
 
		// rooms across different accounts. Just now if we have 10 users
 
		// connected to single room, we store all those things 10 times.
 
		// It would be also great to store last 100 messages per room
 
		// every time, so we can get history messages for IRC for example.
libtransport/Conversation.cpp
Show inline comments
 
@@ -44,12 +44,13 @@ Conversation::Conversation(ConversationManager *conversationManager, const std::
 
	m_legacyName = legacyName;
 
	m_muc = isMUC;
 
	m_jid = m_conversationManager->getUser()->getJID().toBare();
 
	m_sentInitialPresence = false;
 
	m_nicknameChanged = false;
 
	m_mucEscaping = false;
 
	m_sentInitialSubject = false;
 

	
 
	if (CONFIG_BOOL_DEFAULTED(conversationManager->getComponent()->getConfig(), "features.rawxml", false)) {
 
		m_sentInitialPresence = true;
 
	}
 
}
 

	
 
@@ -125,15 +126,17 @@ void Conversation::handleRawMessage(boost::shared_ptr<Swift::Message> &message)
 
			cacheMessage(message);
 
		}
 
		else {
 
			BOOST_FOREACH(const Swift::JID &jid, m_jids) {
 
				message->setTo(jid);
 
				// Subject has to be sent after our own presence (the one with code 110)
 
				if (!message->getSubject().empty() && m_sentInitialPresence == false) {
 
				if (!message->getSubject().empty()) {
 
					m_subject = message;
 
					return;
 
					if (m_sentInitialPresence == false) {
 
						return;
 
					}
 
				}
 
				m_conversationManager->getComponent()->getFrontend()->sendMessage(message);
 
			}
 
		}
 
	}
 
}
 
@@ -224,13 +227,39 @@ std::string Conversation::getParticipants() {
 
	for (std::map<std::string, Participant>::iterator it = m_participants.begin(); it != m_participants.end(); it++) {
 
		ret += (*it).second.presence->getFrom().getResource() + ", ";
 
	}
 
	return ret;
 
}
 

	
 
void Conversation::sendParticipants(const Swift::JID &to) {
 
void Conversation::sendParticipants(const Swift::JID &to, const std::string &nickname) {
 
	// When user tries to join this room from another resource using
 
	// different nickname than the original one has, we have to rename
 
	// him.
 
	if (m_nickname != nickname && !nickname.empty()) {
 
		Swift::Presence::ref presence;
 
		std::string tmp = m_nickname;
 

	
 
		// At first connect the user.
 
		m_nickname = nickname;
 
		presence = generatePresence(nickname, 0, (int) Swift::StatusShow::Online, "", "", "");
 
		presence->setTo(to);
 
		m_conversationManager->getComponent()->getFrontend()->sendPresence(presence);
 

	
 
		// Now change his nickname to the right one.
 
		m_nicknameChanged = true;
 
		presence = generatePresence(nickname, 0, (int) Swift::StatusShow::Online, "", tmp, "");
 
		presence->setTo(to);
 
		m_conversationManager->getComponent()->getFrontend()->sendPresence(presence);
 

	
 
		// And send the presence from as new user
 
		m_nickname = tmp;
 
		presence = generatePresence(m_nickname, 0, (int) Swift::StatusShow::Online, "", "", "");
 
		presence->setTo(to);
 
		m_conversationManager->getComponent()->getFrontend()->sendPresence(presence);
 
	}
 

	
 
	for (std::map<std::string, Participant>::iterator it = m_participants.begin(); it != m_participants.end(); it++) {
 
		(*it).second.presence->setTo(to);
 
		m_conversationManager->getComponent()->getFrontend()->sendPresence((*it).second.presence);
 
	}
 
}
 

	
 
@@ -382,15 +411,15 @@ void Conversation::handleParticipantChanged(const std::string &nick, Conversatio
 
		m_conversationManager->getComponent()->getFrontend()->sendPresence(presence);
 
	}
 
	if (!newname.empty()) {
 
		handleParticipantChanged(newname, flag, status, statusMessage, "", iconhash);
 
	}
 

	
 
	if (m_sentInitialPresence && m_subject) {
 
	if (m_sentInitialPresence && !m_sentInitialSubject && m_subject) {
 
		m_sentInitialSubject = true;
 
		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) {
libtransport/User.cpp
Show inline comments
 
@@ -256,13 +256,13 @@ void User::handlePresence(Swift::Presence::ref presence, bool forceJoin) {
 
			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 {
 
					conv->addJID(presence->getFrom());
 
					conv->sendParticipants(presence->getFrom());
 
					conv->sendParticipants(presence->getFrom(), presence->getTo().getResource());
 
					conv->sendCachedMessages(presence->getFrom());
 
				}
 

	
 
				if (forceJoin) {
 
					onRawPresenceReceived(presence);
 
					onRoomJoined(presence->getFrom(), room, presence->getTo().getResource(), password);
tests/libtransport/conversationmanager.cpp
Show inline comments
 
@@ -119,13 +119,13 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
 
		// subject before this one.
 
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
 

	
 
		// this user presence - status code 110
 
		conv->handleParticipantChanged("nickname", Conversation::PARTICIPANT_FLAG_MODERATOR, Swift::StatusShow::Away, "my status message");
 
		loop->processEvents();
 
		
 

	
 
		CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[1])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("subject"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getSubject());
 
		received.clear();
 

	
 
		// send response
 
@@ -407,27 +407,35 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
 
		Swift::MUCPayload *payload = new Swift::MUCPayload();
 
		payload->setPassword("password");
 
		response->addPayload(boost::shared_ptr<Swift::Payload>(payload));
 
		injectPresence(response);
 
		loop->processEvents();
 

	
 
		CPPUNIT_ASSERT_EQUAL(4, (int) received.size());
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[1])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getBody());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getFrom().toString());
 

	
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[2])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("hi there2!"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getBody());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getFrom().toString());
 

	
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[3])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("subject"), dynamic_cast<Swift::Message *>(getStanza(received[3]))->getSubject());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[3]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[3]))->getFrom().toString());
 
		CPPUNIT_ASSERT_EQUAL(7, (int) received.size());
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[2])));
 
		CPPUNIT_ASSERT_EQUAL(Swift::Presence::Unavailable, dynamic_cast<Swift::Presence *>(getStanza(received[2]))->getType());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Presence *>(getStanza(received[2]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/hanzz"), dynamic_cast<Swift::Presence *>(getStanza(received[2]))->getFrom().toString());
 
		CPPUNIT_ASSERT(getStanza(received[2])->getPayload<Swift::MUCUserPayload>());
 
		CPPUNIT_ASSERT_EQUAL(std::string("nickname"), *getStanza(received[2])->getPayload<Swift::MUCUserPayload>()->getItems()[0].nick);
 
		CPPUNIT_ASSERT_EQUAL(303, getStanza(received[2])->getPayload<Swift::MUCUserPayload>()->getStatusCodes()[2].code);
 

	
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[4])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[4]))->getBody());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[4]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[4]))->getFrom().toString());
 

	
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[5])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("hi there2!"), dynamic_cast<Swift::Message *>(getStanza(received[5]))->getBody());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[5]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[5]))->getFrom().toString());
 

	
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[6])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("subject"), dynamic_cast<Swift::Message *>(getStanza(received[6]))->getSubject());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[6]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[6]))->getFrom().toString());
 
	}
 

	
 
	void handleGroupchatMessagesBouncerLeave() {
 
		User *user = userManager->getUser("user@localhost");
 
		user->addUserSetting("stay_connected", "1");
 
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
 
@@ -476,22 +484,30 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
 
		Swift::MUCPayload *payload = new Swift::MUCPayload();
 
		payload->setPassword("password");
 
		response->addPayload(boost::shared_ptr<Swift::Payload>(payload));
 
		injectPresence(response);
 
		loop->processEvents();
 

	
 
		CPPUNIT_ASSERT_EQUAL(3, (int) received.size());
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[1])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getBody());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getFrom().toString());
 

	
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[2])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("hi there2!"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getBody());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getFrom().toString());
 
		CPPUNIT_ASSERT_EQUAL(6, (int) received.size());
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[2])));
 
		CPPUNIT_ASSERT_EQUAL(Swift::Presence::Unavailable, dynamic_cast<Swift::Presence *>(getStanza(received[2]))->getType());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Presence *>(getStanza(received[2]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/hanzz"), dynamic_cast<Swift::Presence *>(getStanza(received[2]))->getFrom().toString());
 
		CPPUNIT_ASSERT(getStanza(received[2])->getPayload<Swift::MUCUserPayload>());
 
		CPPUNIT_ASSERT_EQUAL(std::string("nickname"), *getStanza(received[2])->getPayload<Swift::MUCUserPayload>()->getItems()[0].nick);
 
		CPPUNIT_ASSERT_EQUAL(303, getStanza(received[2])->getPayload<Swift::MUCUserPayload>()->getStatusCodes()[2].code);
 

	
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[4])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[4]))->getBody());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[4]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[4]))->getFrom().toString());
 

	
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[5])));
 
		CPPUNIT_ASSERT_EQUAL(std::string("hi there2!"), dynamic_cast<Swift::Message *>(getStanza(received[5]))->getBody());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[5]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[5]))->getFrom().toString());
 

	
 
	}
 

	
 
	void handleGroupchatMessagesTwoResources() {
 
		connectSecondResource();
 
		received2.clear();
0 comments (0 inline, 0 general)