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;
}
Exemple #2
0
void TabLog::viewLogHistory_processResponse(const Response &resp)
{
    const Response_ViewLogHistory &response = resp.GetExtension(Response_ViewLogHistory::ext);
    if (resp.response_code() != Response::RespOk) {
        QMessageBox::critical(static_cast<QWidget *>(parent()), tr("Message History"), tr("Failed to collect message history information."));
        return;
    }

    if (response.log_message_size() == 0) {
        QMessageBox::information(static_cast<QWidget *>(parent()), tr("Message History"), tr("There are no messages for the selected filters."));
        return;
    }

    int roomCounter = 0, gameCounter = 0, chatCounter = 0;
    roomTable->setRowCount(roomCounter);
    gameTable->setRowCount(gameCounter);
    chatTable->setRowCount(chatCounter);

    for (int i = 0; i < response.log_message_size(); ++i)
    {
        ServerInfo_ChatMessage message = response.log_message(i);
        if (QString::fromStdString(message.target_type()) == "room") {
            roomTable->insertRow(roomCounter);
            roomTable->setItem(roomCounter, 0, new QTableWidgetItem(QString::fromStdString(message.time())));
            roomTable->setItem(roomCounter, 1, new QTableWidgetItem(QString::fromStdString(message.sender_name())));
            roomTable->setItem(roomCounter, 2, new QTableWidgetItem(QString::fromStdString(message.sender_ip())));
            roomTable->setItem(roomCounter, 3, new QTableWidgetItem(QString::fromStdString(message.message())));
            roomTable->setItem(roomCounter, 4, new QTableWidgetItem(QString::fromStdString(message.target_id())));
            roomTable->setItem(roomCounter, 5, new QTableWidgetItem(QString::fromStdString(message.target_name())));
            ++roomCounter;
        }

        if (QString::fromStdString(message.target_type()) == "game") {
            gameTable->insertRow(gameCounter);
            gameTable->setItem(gameCounter, 0, new QTableWidgetItem(QString::fromStdString(message.time())));
            gameTable->setItem(gameCounter, 1, new QTableWidgetItem(QString::fromStdString(message.sender_name())));
            gameTable->setItem(gameCounter, 2, new QTableWidgetItem(QString::fromStdString(message.sender_ip())));
            gameTable->setItem(gameCounter, 3, new QTableWidgetItem(QString::fromStdString(message.message())));
            gameTable->setItem(gameCounter, 4, new QTableWidgetItem(QString::fromStdString(message.target_id())));
            gameTable->setItem(gameCounter, 5, new QTableWidgetItem(QString::fromStdString(message.target_name())));
            ++gameCounter;
        }

        if (QString::fromStdString(message.target_type()) == "chat") {
            chatTable->insertRow(chatCounter);
            chatTable->setItem(chatCounter, 0, new QTableWidgetItem(QString::fromStdString(message.time())));
            chatTable->setItem(chatCounter, 1, new QTableWidgetItem(QString::fromStdString(message.sender_name())));
            chatTable->setItem(chatCounter, 2, new QTableWidgetItem(QString::fromStdString(message.sender_ip())));
            chatTable->setItem(chatCounter, 3, new QTableWidgetItem(QString::fromStdString(message.message())));
            chatTable->setItem(chatCounter, 4, new QTableWidgetItem(QString::fromStdString(message.target_id())));
            chatTable->setItem(chatCounter, 5, new QTableWidgetItem(QString::fromStdString(message.target_name())));
            ++chatCounter;
        }
    }

    if (roomCounter) {
        roomTable->show();
        roomTable->resizeColumnsToContents();
    } else {
        roomTable->hide();
    }

    if (gameCounter) {
        gameTable->resizeColumnsToContents();
        gameTable->show();
    } else {
        gameTable->hide();
    }

    if (chatCounter) {
        chatTable->resizeColumnsToContents();
        chatTable->show();
    } else {
        chatTable->hide();
    }
}