예제 #1
0
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc)
{
	if (authState == NotLoggedIn)
		return Response::RespLoginNeeded;
	
	if (rooms.contains(cmd.room_id()))
		return Response::RespContextError;
	
	QReadLocker serverLocker(&server->roomsLock);
	Server_Room *r = server->getRooms().value(cmd.room_id(), 0);
	if (!r)
		return Response::RespNameNotFound;
	
	r->addClient(this);
	rooms.insert(r->getId(), r);
	
	Event_RoomSay joinMessageEvent;
	joinMessageEvent.set_message(r->getJoinMessage().toStdString());
	rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent));
	
	Response_JoinRoom *re = new Response_JoinRoom;
	r->getInfo(*re->mutable_room_info(), true);
	
	rc.setResponseExtension(re);
	return Response::RespOk;
}
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc)
{
    if (authState == NotLoggedIn)
        return Response::RespLoginNeeded;

    if (rooms.contains(cmd.room_id()))
        return Response::RespContextError;

    QReadLocker serverLocker(&server->roomsLock);
    Server_Room *r = server->getRooms().value(cmd.room_id(), 0);
    if (!r)
        return Response::RespNameNotFound;

    QString roomPermission = r->getRoomPermission().toLower();
    if (roomPermission != "none"){
        if (roomPermission == "registered") {
            if (!(userInfo->user_level() & ServerInfo_User::IsRegistered))
                return Response::RespUserLevelTooLow;
        }

        if (roomPermission == "moderator"){
            if (!(userInfo->user_level() & ServerInfo_User::IsModerator))
                return Response::RespUserLevelTooLow;
        }

        if (roomPermission == "administrator"){
            if (!(userInfo->user_level() & ServerInfo_User::IsAdmin))
                return Response::RespUserLevelTooLow;
        }
    }

    r->addClient(this);
    rooms.insert(r->getId(), r);

    Event_RoomSay joinMessageEvent;
    joinMessageEvent.set_message(r->getJoinMessage().toStdString());
    joinMessageEvent.set_message_type(Event_RoomSay::Welcome);
    rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent));

    QReadLocker chatHistoryLocker(&r->historyLock);
    QList<ServerInfo_ChatMessage> chatHistory = r->getChatHistory();
    ServerInfo_ChatMessage chatMessage;
    for (int i = 0; i < chatHistory.size(); ++i) {
        chatMessage = chatHistory.at(i);
        qDebug() << QString::fromStdString(chatMessage.message()).simplified();
        Event_RoomSay roomChatHistory;
        roomChatHistory.set_message(chatMessage.sender_name() + ": " + chatMessage.message());
        roomChatHistory.set_message_type(Event_RoomSay::ChatHistory);
        roomChatHistory.set_time_of(QDateTime::fromString(QString::fromStdString(chatMessage.time())).toMSecsSinceEpoch());
        rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(roomChatHistory));
    }

    Response_JoinRoom *re = new Response_JoinRoom;
    r->getInfo(*re->mutable_room_info(), true);

    rc.setResponseExtension(re);
    return Response::RespOk;
}
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc)
{
    if (authState == NotLoggedIn)
        return Response::RespLoginNeeded;

    if (rooms.contains(cmd.room_id()))
        return Response::RespContextError;

    QReadLocker serverLocker(&server->roomsLock);
    Server_Room *r = server->getRooms().value(cmd.room_id(), 0);
    if (!r)
        return Response::RespNameNotFound;

    QString roomPermission = r->getRoomPermission().toLower();
    if (roomPermission != "none"){
        if (roomPermission == "registered") {
            if (!(userInfo->user_level() & ServerInfo_User::IsRegistered))
                return Response::RespUserLevelTooLow;
        }

        if (roomPermission == "moderator"){
            if (!(userInfo->user_level() & ServerInfo_User::IsModerator))
                return Response::RespUserLevelTooLow;
        }

        if (roomPermission == "administrator"){
            if (!(userInfo->user_level() & ServerInfo_User::IsAdmin))
                return Response::RespUserLevelTooLow;
        }
    }

    r->addClient(this);
    rooms.insert(r->getId(), r);

    Event_RoomSay joinMessageEvent;
    joinMessageEvent.set_message(r->getJoinMessage().toStdString());
    rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent));

    Response_JoinRoom *re = new Response_JoinRoom;
    r->getInfo(*re->mutable_room_info(), true);

    rc.setResponseExtension(re);
    return Response::RespOk;
}