#include "Client.h" #include #include #include #include #include #include #include "App.h" #include "Settings.h" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Client::Client(App *app, ActiveClient* ac): QWClient(), myActiveClient(ac), myApp(app), myOnServerFlag(false), mySpamMutedFlag(false), myQWMutedFlag(false), myJoinMessageTimer(new QTimer()), myKeepNickTimer(new QTimer()), myFloodTimer(new QTimer()), myQWBroadcastFloodTimer(new QTimer()), mySpamBroadcastFloodTimer(new QTimer()), myJoinMessagePrinted(false), myJoinMessageScheduled(false), myMaxClients(0) { myJoinMessageTimer->setSingleShot(true); myKeepNickTimer->setSingleShot(true); myFloodTimer->setSingleShot(true); myQWBroadcastFloodTimer->setSingleShot(true); mySpamBroadcastFloodTimer->setSingleShot(true); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Client::~Client() { delete myJoinMessageTimer; delete myKeepNickTimer; delete myFloodTimer; delete myQWBroadcastFloodTimer; delete mySpamBroadcastFloodTimer; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::connect(const char *host, quint16 port) { myJoinMessageScheduled = true; //Hi message only scheduled at bot connection QWClient::connect(host, port); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::say(const QString &msg) { QString cmd = "say " + msg; sendCmd(cmd.toAscii().data()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::setTeam(const QString &msg) { sendCmd(QString("setinfo \"team\" \"" + msg + "\"").toAscii().data()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::disconnect() { QWClient::disconnect(); myOnServerFlag = false; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::print(const QString &msg) { QString str; str = "[" + QTime::currentTime().toString(Qt::ISODate) + "] " + QString(host()) + ":" + QString::number(port()) + "> " + msg; QByteArray b = str.toAscii(); Client::stripColor(b.data()); str = QString::fromAscii(b.data()); myApp->print(str); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::setPlayerList(PlayerList &playerList) { myPlayerList = playerList; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onDisconnect() { print("Disconnected..\n"); myOnServerFlag = false; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Client::isQWMuted() const { return myQWMutedFlag; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Client::isSpamMuted() const { return mySpamMutedFlag; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::parsePrintedLine() { Player player; bool parsed = false; quint16 lastMatchSize = 0; QByteArray playerName; QByteArray printLine(myPrintLine.toAscii()); QWClient::stripColor(printLine.data()); foreach(player, myPlayerList) { playerName = player.name.toAscii(); QWClient::stripColor(playerName.data()); if(printLine.startsWith(playerName) || (player.spectator && printLine.startsWith(QString("[SPEC] " + player.name).toAscii()))) { if(player.spectator && printLine.startsWith("[SPEC] ")) { if(lastMatchSize < (playerName.size() + 7)) lastMatchSize = (playerName.size() + 7); } else if(lastMatchSize < playerName.size()) { lastMatchSize = playerName.size(); } parsed = true; } } if(!parsed) return; QString nick(myPrintLine.left(lastMatchSize)); QString message(myPrintLine.right(myPrintLine.size() - lastMatchSize)); QRegExp regex("^:\\s+\\.(spam|qw|help|qw_mute|qw_unmute|spam_mute|spam_unmute|lastmsgs)\\s*(.+)$"); if(regex.indexIn(message) == -1) return; /* Flood prot */ QTime currentTime = QTime::currentTime(); int floodProtTime = Settings::globalInstance()->floodProtTime(); if(myFloodTimer->isActive()) { if(!myFloodMsgPrinted) { say("> FloodProt: Not so fast, wait " + QString::number(floodProtTime + currentTime.secsTo(myFloodTimerStart)) + " sec(s)."); myFloodMsgPrinted = true; } return; } myFloodTimerStart = currentTime; myFloodTimer->start(floodProtTime*1000); myFloodMsgPrinted = false; QString command = regex.capturedTexts().at(1); QString args = regex.capturedTexts().at(2); if(command == "help") { //say("> Commands:"); say("> Broadcast a message: .qw and .spam "); //, beware of the floodprot limits 600s for .qw and 300s for .spam"); say("> (Un)Mute: .qw_mute .qw_unmute or .spam_mute .spam_unmute"); say("> Last 5 messages: .lastmsgs"); return; } if(command == "qw" || command == "spam") { if(!args.trimmed().size()) { say("> The format is ." + command + " ."); return; } /* Floodprot for broadcasting commands */ if(command == "qw") { int qwFloodProtTime = Settings::globalInstance()->qwFloodProtTime(); if(myQWBroadcastFloodTimer->isActive()) { say("> FloodProt: Wait " + QString::number(qwFloodProtTime + currentTime.secsTo(myQWBroadcastFloodTimerStart)) + " secs before new .qw"); return; } myQWBroadcastFloodTimerStart = currentTime; myQWBroadcastFloodTimer->start(qwFloodProtTime*1000); } else if(command == "spam") { int spamFloodProtTime = Settings::globalInstance()->spamFloodProtTime(); if(mySpamBroadcastFloodTimer->isActive()) { say("> FloodProt: Wait " + QString::number(spamFloodProtTime + currentTime.secsTo(mySpamBroadcastFloodTimerStart)) + " secs before new .spam"); return; } mySpamBroadcastFloodTimerStart = currentTime; mySpamBroadcastFloodTimer->start(spamFloodProtTime*1000); } QString server(QString(host()) + ":" + QString::number(port()) + " " + QString::number(playerCount()) + "/" + QString::number(myMaxClients)); QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed()); /* Broadcast within QW servers */ myApp->broadcast(message, myActiveClient); /* Broadcast outside QW */ nick = parseNameFun(nick); //for the irc message namefun must be removed. QString parsedMsg = parseNameFun(args.trimmed()); if(!Settings::globalInstance()->developerMode()) myApp->requestBroadcast(command, nick, server, args.trimmed()); else myApp->requestBroadcast("dev", nick, server, parsedMsg); return; } if(command == "qw_mute") { say("> .qw Frequency Muted."); myQWMutedFlag = true; return; } if(command == "qw_unmute") { say("> .qw Frequency Unmuted."); myQWMutedFlag = false; return; } if(command == "spam_mute") { say("> .spam Frequency Muted."); mySpamMutedFlag = true; return; } if(command == "spam_unmute") { say("> .spam Frequency Unmuted."); mySpamMutedFlag = false; return; } if(command == "lastmsgs") { QStringList messages = myApp->lastMessages(); if(!messages.size()) { say("None"); return; } QString msg; int i = 0; foreach(msg, messages) { if(++i > 5) break; say(msg); } return; } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int Client::playerCount() const { Player c; int pc = 0; foreach(c, myPlayerList) { if(c.spectator) continue; pc++; } return pc; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::setMaxClients(int maxClients) { myMaxClients = maxClients; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int Client::maxClients() const { return myMaxClients; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onPrint(int, const char *msg) { if(!strlen(msg)) return; QString text(msg); if(text.endsWith('\n')) { myPrintLine.append(text); parsePrintedLine(); print(myPrintLine); myPrintLine.clear(); } else { myPrintLine.append(text); } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Client::isOnServer() const { return myOnServerFlag; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onError(const char *description) { QString desc(description); if(desc == "Client Timed Out.") { print("Error (" + QString(description) + ")\n"); } else { print("Error (" + QString(description) + ")\n"); } myOnServerFlag = false; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float) { print(QString(levelName) + "\n"); myDownloadProgressPrintedFlag = false; mySpamMutedFlag = false; myQWMutedFlag = false; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onChallenge() { print("challenge\n"); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onConnection() { print("connection\n"); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onConnected() { print("connected\n"); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onDownloadStarted(const char *fileName) { print("Download started " + QString(fileName) + "\n"); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::run() { if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted) { say("Hi, I am QWNET's bot, type .help to see my commands."); myJoinMessagePrinted = true; } /* Keep nick... Simply set name again after 10 secs */ if(!myKeepNickTimer->isActive()) { setName(Settings::globalInstance()->botName().toAscii().data()); myKeepNickTimer->start(30000); } QWClient::run(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onOOBPrint(const char *msg) { print(QString(msg)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onStuffedCmd(const char *cmd) { QString strCmd(cmd); if(strCmd == "skins") //connection sequence complete { myOnServerFlag = true; /* Only say hi if hi is scheduled */ if(myJoinMessageScheduled) { myJoinMessageTimer->start(Settings::globalInstance()->timeToSayHiAfterConnected()*1000); myJoinMessagePrinted = false; myJoinMessageScheduled = false; } } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onDownloadProgress(int percent) { if(!(percent % 10)) { if(!myDownloadProgressPrintedFlag) { print("Download " + QString::number(percent) + "%\n"); myDownloadProgressPrintedFlag = true; } } else { myDownloadProgressPrintedFlag = false; } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Client::onDownloadFinished() { print("Download 100% finished.\n"); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - QString Client::parseNameFun(const QString &string) { QByteArray b(string.toAscii()); QWClient::stripColor(b.data()); return QString(b); }