Ejemplo n.º 1
0
void IRCClient::readAndProcess(){
    while(socket.canReadLine()){
        QString line = socket.readLine();
        QRegularExpressionMatch parsedLine = parser.match(line);
        if(parsedLine.hasMatch()){
            QString prefix = parsedLine.captured(1);
            QString nick = parsedLine.captured(2);
            QString username = parsedLine.captured(3);
            QString hostname = parsedLine.captured(4);
            QString command = parsedLine.captured(5);
            QStringList params = parsedLine.captured(6).split(" ");
            params.takeFirst();
            if(!parsedLine.captured(7).isEmpty())
                params.append(parsedLine.captured(7).trimmed());
            params.last() = params.last().trimmed();
            if(command.compare("PING", Qt::CaseInsensitive) == 0)
                socket.write(("PONG " + params.last() + "\r\n").toUtf8());
            else if(command.compare("JOIN", Qt::CaseInsensitive) == 0){
                if(nick.compare(this->nickname, Qt::CaseSensitive) == 0)
                    emit joinedChannel(params.first());
                else{
                    if(!userTable.contains(params.first()))
                        userTable[params.first()] = QStringList(nick);
                    else
                        userTable[params.first()].append(nick);
                    emit updateUserList(params.first(), userTable[params.first()]);
                    emit textChanged(nick + " has joined " + params.first() + "\r\n", params.first());
                }
            }
            else if(command.compare("353") == 0){
                if(!userTable.contains(params.at(2)))
                    userTable[params.at(2)] = params.last().split(" ");
                else
                    userTable[params.at(2)].append(params.last().split(" "));
                emit updateUserList(params.at(2), userTable[params.at(2)]);
            }
            else if(command.compare("NOTICE", Qt::CaseInsensitive) == 0){
                emit textChanged("[" + nick + "] " + params.last() + "\r\n");
            }
            else if(command.compare("PRIVMSG", Qt::CaseInsensitive) == 0){
                if(params.first().compare(this->nickname, Qt::CaseSensitive) == 0)
                    emit textChanged("<" + nick + " -> " + params.first() + "> " + params.last() + "\r\n");
                else
                    emit textChanged("<" + nick + "> " + params.last() + "\r\n", params.first());
            }
            else if(command.compare("QUIT", Qt::CaseInsensitive) == 0){
                if(params.isEmpty())
                    emit textChanged(nick + " has disconnected from the server\r\n");
                else
                    emit textChanged(nick + " has disconnected from the server\r\n");
            }
            else
                emit textChanged(line);
        }
        else
            qDebug() << line;
    }
}
clientController::clientController(QObject *parent) : QObject(parent)
{
    m_w = new clientConnectWindow();
    m_userWindow = new userListWindow();

    connect(m_w, SIGNAL(tryConnect(QString,QString,QString)), this, SLOT(on_tryConnect(QString,QString,QString)));


    connect(this, SIGNAL(userListChanged(QStringList)), m_userWindow, SLOT(updateUserList(QStringList)));
    connect(m_userWindow, SIGNAL(tryChatConnect(QString)), this, SLOT(on_tryStartChat(QString)));
    connect(this, SIGNAL(newMessage(QString,QString)), this, SLOT(on_newMessage(QString,QString)));
    connect(this, SIGNAL(rcvDisconnect(QString, QString)), this, SLOT(on_rcvDisconnect(QString,QString)));

    m_w->show();
}