#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), myConnectionRetries(0), myOnServerFlag(false), myMutedFlag(false), myJoinMessageTimer(new QTimer()), myJoinMessagePrinted(false) { myEndFloodTime = QTime::currentTime(); myQWBroadcastFloodTime = myEndFloodTime; mySpamBroadcastFloodTime = myEndFloodTime; myJoinMessageTimer->setSingleShot(true); } Client::~Client() { delete myJoinMessageTimer; } void Client::say(const QString &msg) { QString cmd = "say " + msg; sendCmd(cmd.toAscii().data()); } void Client::sayTeam(const QString &msg) { QString cmd = "say_team " + 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 = 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; } void Client::retryConnection() { if(myConnectionRetries == ConnectionRetries) { print("Giving up!\n"); disconnect(); myOnServerFlag = false; return; } print("Reconnecting...\n"); reconnect(); myConnectionRetries++; } bool Client::isMuted() const { return myMutedFlag; } 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)) { 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|mute|unmute)\\s*(.+)$"); if(regex.indexIn(message) == -1) return; /* Flood prot */ QTime currentTime = QTime::currentTime(); int timeLeft = currentTime.secsTo(myEndFloodTime); if(timeLeft > 0) { if(!myFloodMsgPrinted) { say("> FloodProt: Not so fast, wait " + QString::number(timeLeft) + " sec(s)."); myFloodMsgPrinted = true; } return; } myEndFloodTime = QTime::currentTime().addSecs(Settings::globalInstance()->floodProtTime()); myFloodMsgPrinted = false; QString command = regex.capturedTexts().at(1); QString args = regex.capturedTexts().at(2); if(command == "help") { say("> Commands:"); say("> Broadcast message: .qw and .spam "); say("> Mute/Unmute the bot: .mute and .unmute"); say("> .help to show this help message."); return; } if(command == "qw" || command == "spam") { if(!args.trimmed().size()) { say("> The format is ." + command + " ."); return; } if(command == "qw") { timeLeft = currentTime.secsTo(myQWBroadcastFloodTime); if(timeLeft > 0) { say("> FloodProt: Wait " + QString::number(timeLeft) + " sec(s) before broadcasting a new .qw message."); return; } myQWBroadcastFloodTime = currentTime.addSecs(Settings::globalInstance()->qwFloodProtTime()); } else if(command == "spam") { timeLeft = currentTime.secsTo(mySpamBroadcastFloodTime); if(timeLeft > 0) { say("> FloodProt: Wait " + QString::number(timeLeft) + " sec(s) before broadcasting a new .spam message."); return; } mySpamBroadcastFloodTime = currentTime.addSecs(Settings::globalInstance()->spamFloodProtTime()); } QString server(QString(host()) + ":" + QString::number(port())); 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()); myApp->requestBroadcast("dev", nick, server, parsedMsg); //myApp->requestBroadcast(command, nick, server, args.trimmed()); return; } if(command == "mute") { say("> Muted!"); myMutedFlag = true; return; } if(command == "unmute") { say("> Unmuted!"); myMutedFlag = false; return; } } 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; myMutedFlag = 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; } 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 { myConnectionRetries = 0; myOnServerFlag = true; myJoinMessageTimer->start(Settings::globalInstance()->timeToSayHiAfterConnected()*1000); myJoinMessagePrinted = 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); }