void ConversationManager::handleMessageReceived(Swift::Message::ref message) {
// 	std::string name = message->getTo().getUnescapedNode();
// 	if (name.find_last_of("%") != std::string::npos) { // OK when commented
// 		name.replace(name.find_last_of("%"), 1, "@"); // OK when commented
// 	}
	std::string name = Buddy::JIDToLegacyName(message->getTo());
	if (name.empty()) {
		LOG4CXX_WARN(logger, m_user->getJID().toString() << ": Tried to create empty conversation");
		return;
	}

	// create conversation if it does not exist.
	if (!m_convs[name]) {
		Conversation *conv = m_component->getFactory()->createConversation(this, name);
		addConversation(conv);
	}
	// if it exists and it's MUC, but this message is PM, get PM conversation or create new one.
	else if (m_convs[name]->isMUC() && message->getType() != Swift::Message::Groupchat) {
		std::string room_name = name;
		name = room_name + "/" + message->getTo().getResource();
		if (m_convs.find(name) == m_convs.end()) {
			Conversation *conv = m_component->getFactory()->createConversation(this, message->getTo().getResource());
			conv->setRoom(room_name);
			conv->setNickname(name);
			addConversation(conv);
		}
	}

	// update resource and send the message
	m_convs[name]->setJID(message->getFrom());
	m_convs[name]->sendMessage(message);
}
void ConversationManager::handleMessageReceived(Swift::Message::ref message) {
	std::string name = message->getTo().getUnescapedNode();
	if (name.find_last_of("%") != std::string::npos) {
		name.replace(name.find_last_of("%"), 1, "@");
	}

	if (!m_convs[name]) {
		m_convs[name] = m_component->getFactory()->createConversation(this, name);
	}
	m_convs[name]->sendMessage(message);
}
void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
	if (!message->getTo().getNode().empty())
		return;

	if (message->getFrom().toBare().toString() != CONFIG_STRING(m_component->getConfig(), "service.admin_jid")) {
		LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().toBare().toString());
		return;
	}

	// Ignore empty messages
	if (message->getBody().empty()) {
		return;
	}

	LOG4CXX_INFO(logger, "Message from admin received");
	message->setTo(message->getFrom());
	message->setFrom(m_component->getJID());

	if (message->getBody() == "status") {
		int users = m_userManager->getUserCount();
		int backends = m_server->getBackendCount();
		message->setBody("Running (" + boost::lexical_cast<std::string>(users) + " users connected using " + boost::lexical_cast<std::string>(backends) + " backends)");
	}
	else if (message->getBody() == "online_users") {
		std::string lst;
		const std::map<std::string, User *> &users = m_userManager->getUsers();
		if (users.size() == 0)
			lst = "0";

		for (std::map<std::string, User *>::const_iterator it = users.begin(); it != users.end(); it ++) {
			lst += (*it).first + "\n";
		}

		message->setBody(lst);
	}
	else if (message->getBody() == "online_users_count") {
		int users = m_userManager->getUserCount();
		message->setBody(boost::lexical_cast<std::string>(users));
	}
	else if (message->getBody() == "reload") {
		bool done = m_component->getConfig()->reload();
		if (done) {
			message->setBody("Config reloaded");
		}
		else {
			message->setBody("Error during config reload");
		}
	}
	else if (message->getBody() == "online_users_per_backend") {
		std::string lst;
		int id = 1;

		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
		for (std::list <NetworkPluginServer::Backend *>::const_iterator b = backends.begin(); b != backends.end(); b++) {
			NetworkPluginServer::Backend *backend = *b;
			lst += "Backend " + boost::lexical_cast<std::string>(id) + " (ID=" + backend->id + ")";
			lst += backend->acceptUsers ? "" : " - not-accepting";
			lst += backend->longRun ? " - long-running" : "";
			lst += ":\n";
			if (backend->users.size() == 0) {
				lst += "   waiting for users\n";
			}
			else {
				time_t now = time(NULL);
				for (std::list<User *>::const_iterator u = backend->users.begin(); u != backend->users.end(); u++) {
					User *user = *u;
					lst += "   " + user->getJID().toBare().toString();
					lst += " - non-active for " + boost::lexical_cast<std::string>(now - user->getLastActivity()) + " seconds";
					lst += "\n";
				}
			}
			id++;
		}

		message->setBody(lst);
	}
	else if (message->getBody().find("has_online_user") == 0) {
		User *user = m_userManager->getUser(getArg(message->getBody()));
		std::cout << getArg(message->getBody()) << "\n";
		message->setBody(boost::lexical_cast<std::string>(user != NULL));
	}
	else if (message->getBody() == "backends_count") {
		int backends = m_server->getBackendCount();
		message->setBody(boost::lexical_cast<std::string>(backends));
	}
	else if (message->getBody() == "res_memory") {
		double shared = 0;
		double rss = 0;
#ifndef WIN32
		process_mem_usage(shared, rss);
#endif

		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
			rss += backend->res;
		}

		message->setBody(boost::lexical_cast<std::string>(rss));
	}