void LoginHandler::handleLoginMessage(protocol::MessagePtr msg)
{
	if(msg->type() != protocol::MSG_LOGIN) {
		logger::error() << "login handler was passed a non-login message!";
		return;
	}

	QString message = msg.cast<protocol::Login>().message();

	if(_state == WAIT_FOR_SECURE) {
		// Secure mode: wait for STARTTLS before doing anything
		if(message == "STARTTLS") {
			handleStarttls();
		} else {
			send("ERROR MUSTSECURE");
			logger::notice() << _client << "Didn't secure connection!";
			_client->disconnectError("must secure connection first");
		}

	} else if(_state == WAIT_FOR_IDENT) {
		// Wait for user identification before moving on to session listing
		if(message == "STARTTLS") {
			handleStarttls();
		} else if(message.startsWith("IDENT ")) {
			handleIdentMessage(message);
		} else {
			logger::notice() << _client << "Invalid login message";
			_client->disconnectError("invalid message");
		}

	} else if(_state == WAIT_FOR_IDENTITYMANAGER_REPLY) {
		// Client shouldn't shouldn't send anything in this state
		logger::notice() << _client << "Got login message while waiting for identity manager reply";
		_client->disconnectError("unexpected message");

	} else {
		if(message.startsWith("HOST ")) {
			handleHostMessage(message);
		} else if(message.startsWith("JOIN ")) {
			handleJoinMessage(message);
		} else {
			logger::notice() << _client << "Got invalid login message";
			_client->disconnectError("invalid message");
		}
	}
}
void LoginHandler::handleLoginMessage(protocol::MessagePtr msg)
{
	if(msg->type() != protocol::MSG_COMMAND) {
		logger::error() << "login handler was passed a non-login message!";
		return;
	}

	protocol::ServerCommand cmd = msg.cast<protocol::Command>().cmd();

	if(m_state == WAIT_FOR_SECURE) {
		// Secure mode: wait for STARTTLS before doing anything
		if(cmd.cmd == "startTls") {
			handleStarttls();
		} else {
			logger::notice() << m_client << "Didn't secure connection!";
			sendError("tlsRequired", "TLS required");
		}

	} else if(m_state == WAIT_FOR_IDENT) {
		// Wait for user identification before moving on to session listing
		if(cmd.cmd == "startTls") {
			handleStarttls();
		} else if(cmd.cmd == "ident") {
			handleIdentMessage(cmd);
		} else {
			logger::notice() << m_client << "Invalid login message";
			m_client->disconnectError("invalid message");
		}

	} else if(m_state == WAIT_FOR_IDENTITYMANAGER_REPLY) {
		// Client shouldn't shouldn't send anything in this state
		logger::notice() << m_client << "Got login message while waiting for identity manager reply";
		m_client->disconnectError("unexpected message");

	} else {
		if(cmd.cmd == "host") {
			handleHostMessage(cmd);
		} else if(cmd.cmd == "join") {
			handleJoinMessage(cmd);
		} else {
			logger::notice() << m_client << "Got invalid login message";
			m_client->disconnectError("invalid message");
		}
	}
}