Changeset - 4717bd007185
[Not reviewed]
0 5 0
HanzZ - 13 years ago 2012-09-21 13:56:17
hanzz.k@gmail.com
Send unavailable presence when server goes down. Use LOG4CXX macros in irc backend
5 files changed with 59 insertions and 11 deletions:
0 comments (0 inline, 0 general)
backends/libcommuni/ircnetworkplugin.cpp
Show inline comments
 
#include "ircnetworkplugin.h"
 
#include <IrcCommand>
 
#include <IrcMessage>
 
#include "transport/logging.h"
 

	
 
DEFINE_LOGGER(logger, "IRCNetworkPlugin");
 

	
 
#define FROM_UTF8(WHAT) QString::fromUtf8((WHAT).c_str(), (WHAT).size())
 
#define TO_UTF8(WHAT) std::string((WHAT).toUtf8().data(), (WHAT).toUtf8().size())
 
@@ -17,7 +20,6 @@ void IRCNetworkPlugin::readData() {
 
	if (availableBytes == 0)
 
		return;
 

	
 
	std::cout << "READ\n";
 
	std::string d = std::string(m_socket->readAll().data(), availableBytes);
 
	handleDataRead(d);
 
}
 
@@ -27,7 +29,8 @@ void IRCNetworkPlugin::sendData(const std::string &string) {
 
}
 

	
 
void IRCNetworkPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
	// Server is in server-mode, so user is JID of server when we want to connect
 
	// In server mode, hostname of the server we want to connect to is stored in "user" JID.
 
	// In component mode we will connect user to the IRC network once he joins the room.
 
	if (CONFIG_BOOL(config, "service.server_mode")) {
 
		MyIrcSession *session = new MyIrcSession(user, this);
 
		std::string h = user.substr(0, user.find("@"));
 
@@ -35,10 +38,11 @@ void IRCNetworkPlugin::handleLoginRequest(const std::string &user, const std::st
 
		session->setHost(FROM_UTF8(h.substr(h.find("%") + 1)));
 
		session->setPort(6667);
 
		session->open();
 
		std::cout << "CONNECTING IRC NETWORK " << h.substr(h.find("%") + 1) << "\n";
 
		LOG4CXX_INFO(logger, user << ": Connecting IRC network " << h.substr(h.find("%") + 1));
 
		m_sessions[user] = session;
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, user << ": Ready for connections");
 
		handleConnected(user);
 
	}
 
}
 
@@ -53,7 +57,6 @@ void IRCNetworkPlugin::handleLogoutRequest(const std::string &user, const std::s
 

	
 
void IRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &/*xhtml*/) {
 
	std::string u = user;
 
	std::cout << "AAAAA " << legacyName << "\n";
 
	if (!CONFIG_BOOL(config, "service.server_mode")) {
 
		u = user + legacyName.substr(legacyName.find("@") + 1);
 
		if (u.find("/") != std::string::npos) {
 
@@ -61,7 +64,7 @@ void IRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const s
 
		}
 
	}
 
	if (m_sessions[u] == NULL) {
 
		std::cout << "No session for " << u << "\n";
 
		LOG4CXX_WARN(logger, user << ": Session name: " << u << ", No session for user");
 
		return;
 
	}
 

	
 
@@ -74,19 +77,19 @@ void IRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const s
 
			r = legacyName.substr(legacyName.find("/") + 1);
 
		}
 
	}
 
	std::cout << "MESSAGE " << u << " " << r << "\n";
 
	LOG4CXX_INFO(logger, user << ": Session name: " << u << ", message to " << r);
 
	m_sessions[u]->sendCommand(IrcCommand::createMessage(FROM_UTF8(r), FROM_UTF8(message)));
 
	std::cout << "SENT\n";
 
}
 

	
 
void IRCNetworkPlugin::handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password) {
 
	std::cout << "JOIN\n";
 
	std::string r = room;
 
	std::string u = user;
 
	if (!CONFIG_BOOL(config, "service.server_mode")) {
 
		u = user + room.substr(room.find("@") + 1);
 
		r = room.substr(0, room.find("@"));
 
	}
 

	
 
	LOG4CXX_INFO(logger, user << ": Session name: " << u << ", Joining room " << r);
 
	if (m_sessions[u] == NULL) {
 
		// in gateway mode we want to login this user to network according to legacyName
 
		if (room.find("@") != std::string::npos) {
 
@@ -96,15 +99,15 @@ void IRCNetworkPlugin::handleJoinRoomRequest(const std::string &user, const std:
 
			session->setHost(FROM_UTF8(room.substr(room.find("@") + 1)));
 
			session->setPort(6667);
 
			session->open();
 
			std::cout << "CONNECTING IRC NETWORK " << room.substr(room.find("@") + 1) << "\n";
 
			std::cout << "SUFFIX " << room.substr(room.find("@")) << "\n";
 
			LOG4CXX_INFO(logger, user << ": Connecting IRC network " << room.substr(room.find("@") + 1));
 
			m_sessions[u] = session;
 
		}
 
		else {
 
			LOG4CXX_WARN(logger, user << ": There's no proper server defined in room to which this user wants to join: " << room);
 
			return;
 
		}
 
	}
 
	std::cout << "JOINING " << r << "\n";
 

	
 
	m_sessions[u]->addAutoJoinChannel(r, password);
 
	m_sessions[u]->sendCommand(IrcCommand::createJoin(FROM_UTF8(r), FROM_UTF8(password)));
 
	m_sessions[u]->rooms += 1;
 
@@ -123,11 +126,14 @@ void IRCNetworkPlugin::handleLeaveRoomRequest(const std::string &user, const std
 
	if (m_sessions[u] == NULL)
 
		return;
 

	
 
	LOG4CXX_INFO(logger, user << ": Session name: " << u << ", Leaving room " << r);
 

	
 
	m_sessions[u]->sendCommand(IrcCommand::createPart(FROM_UTF8(r)));
 
	m_sessions[u]->removeAutoJoinChannel(r);
 
	m_sessions[u]->rooms -= 1;
 

	
 
	if (m_sessions[u]->rooms <= 0) {
 
		LOG4CXX_INFO(logger, user << ": Session name: " << u << ", User is not in room, disconnecting from network");
 
		m_sessions[u]->close();
 
		m_sessions[u]->deleteLater();
 
		m_sessions.erase(u);
include/transport/conversation.h
Show inline comments
 
@@ -114,6 +114,8 @@ class Conversation {
 
			return m_room;
 
		}
 

	
 
		void destroyRoom();
 

	
 
	private:
 
		ConversationManager *m_conversationManager;
 
		std::string m_legacyName;
src/conversation.cpp
Show inline comments
 
@@ -38,6 +38,28 @@ Conversation::Conversation(ConversationManager *conversationManager, const std::
 
Conversation::~Conversation() {
 
}
 

	
 
void Conversation::destroyRoom() {
 
	if (m_muc) {
 
		Swift::Presence::ref presence = Swift::Presence::create();
 
		std::string legacyName = m_legacyName;
 
		if (legacyName.find_last_of("@") != std::string::npos) {
 
			legacyName.replace(legacyName.find_last_of("@"), 1, "%"); // OK
 
		}
 
		presence->setFrom(Swift::JID(legacyName, m_conversationManager->getComponent()->getJID().toBare(), m_nickname));
 
		presence->setTo(m_jid);
 
		presence->setType(Swift::Presence::Unavailable);
 

	
 
		Swift::MUCItem item;
 
		item.affiliation = Swift::MUCOccupant::NoAffiliation;
 
		item.role = Swift::MUCOccupant::NoRole;
 
		Swift::MUCUserPayload *p = new Swift::MUCUserPayload ();
 
		p->addItem(item);
 

	
 
		presence->addPayload(boost::shared_ptr<Swift::Payload>(p));
 
		m_conversationManager->getComponent()->getStanzaChannel()->sendPresence(presence);
 
	}
 
}
 

	
 
void Conversation::setRoom(const std::string &room) {
 
	m_room = room;
 
	m_legacyName = m_room + "/" + m_legacyName;
src/conversationmanager.cpp
Show inline comments
 
@@ -41,6 +41,7 @@ ConversationManager::ConversationManager(User *user, Component *component){
 
ConversationManager::~ConversationManager() {
 
	while(!m_convs.empty()) {
 
		LOG4CXX_INFO(logger, m_user->getJID().toString() << ": Removing conversation " << (*m_convs.begin()).first);
 
		(*m_convs.begin()).second->destroyRoom();
 
		delete (*m_convs.begin()).second;
 
		m_convs.erase(m_convs.begin());
 
	}
src/tests/conversationmanager.cpp
Show inline comments
 
@@ -28,6 +28,7 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
 
	CPPUNIT_TEST(handleChatstateMessages);
 
	CPPUNIT_TEST(handleParticipantChanged);
 
	CPPUNIT_TEST(handlePMFromXMPP);
 
	CPPUNIT_TEST(handleGroupchatRemoved);
 
	CPPUNIT_TEST_SUITE_END();
 

	
 
	public:
 
@@ -278,6 +279,22 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
 
		pmconv->handleMessage(msg2);
 
	}
 

	
 
	void handleGroupchatRemoved() {
 
		User *user = userManager->getUser("user@localhost");
 
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
 
		conv->setNickname("nickname");
 
		conv->setJID("user@localhost/resource");
 
		received.clear();
 
		conv->destroyRoom();
 
		delete conv;
 

	
 
		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
 
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
 
		CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::None, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getShow());
 
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getTo().toString());
 
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/nickname"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getFrom().toString());
 
	}
 

	
 
};
 

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