Esempio n. 1
0
void ChatServer::processMessage(ClientStatusChanged *msg)
{
    QStringList list = m_clientList.getChannelsForClient(msg->username).keys();
    QStringList::iterator itr = list.begin();
    ChannelSystemMessage *system = new ChannelSystemMessage();
    ChatClient client = m_clientList.getClient(msg->username);
    if(msg->status.isEmpty())
    {
        system->message = msg->username + " returned to original state";
        client.setUserState("Online");
    }
    else
    {
        client.setUserState(msg->status);
        system->message = QString("%1 changed his state to \"%2\"")
                                  .arg(msg->username)
                                  .arg(msg->status);
    }
    m_clientList.updateClient(client);
    emit updateTable("clients");
    for(; itr != list.end(); ++itr)
    {
        system->channelName = *itr;
        sendMessageToChannel(*itr, msg);
        sendMessageToChannel(*itr, system);
        emit channelLog(*itr, system->message);
    }
    delete system;
}
Esempio n. 2
0
void ChatServer::processMessage(AuthorizationRequest *msg, QTcpSocket *socket)
//processing authorization request
//here we need to check whether user exists in table or not
//YOBA ETO YA, PSHH-PSSHHH
//and we need to form authorization answer and send it to client
{
    if (!msg)
    {
        QString log = "Error processing authprization request- message is empty";
        emit serverLog(esMinor, log);
        return;
    }
    AuthorizationAnswer *answer = new AuthorizationAnswer();
    switch (m_clientList.authorize(msg->username, msg->password, socket))
    {
    case GeneralClientList::arAllreadyAuthorized:
        {
            answer->authorizationResult = false;
            answer->denialReason = "User allready authorized";
            break;
        }
    case GeneralClientList::arWrongAuthData:
        {
            answer->authorizationResult = false;
            answer->denialReason = "Wrong username or password";
            break;
        }
    case GeneralClientList::arAuthSuccess:
        {
            answer->authorizationResult = true;
            break;
        }
    }
    sendMessageToClient(socket, answer);
    if (answer->authorizationResult)
    {
        ChatClient client = m_clientList.getClient(msg->username);
        client.setUserState("Online");
        m_clientList.updateClient(client);
        emit updateTable("clients");
        QStringList channels = m_clientList.getChannelsForClient(msg->username).keys();
        ChannelSystemMessage *informMsg = new ChannelSystemMessage();
        ChannelUserList *updateListMsg = new ChannelUserList();
        informMsg->message = msg->username + " entered chat.";
        for (int i = 0; i < channels.count(); ++i)
        {
            informMsg->channelName = channels[i];
            updateListMsg->channelName = channels[i];
            ChatChannel channel = m_clientList.getChannel(channels[i]);
            for(int k = 0; k < channel.userList.count(); k++)
            {
                //FIXME: GOVNOKOD
                updateListMsg->userList.insert(channel.userList[k], getSendableState(channel.userList[k]));
            }
            for (int j = 0; j < channel.userList.count(); j++)
            {                
                QString username = channel.userList[j];
                if (username != msg->username)
                {
                    sendMessageToClient(username, informMsg);
                    sendMessageToClient(username, updateListMsg);
                }
            }
        }
        delete updateListMsg;
        delete informMsg;
    }
    delete answer;
}