|
@@ -4,28 +4,29 @@
|
|
#include <QTcpSocket>
|
|
#include <QTcpSocket>
|
|
#include <QStringList>
|
|
#include <QStringList>
|
|
#include <QTime>
|
|
#include <QTime>
|
|
|
|
+#include <QTimer>
|
|
#include "App.h"
|
|
#include "App.h"
|
|
#include "Settings.h"
|
|
#include "Settings.h"
|
|
|
|
|
|
-Client::Client(App *app):
|
|
|
|
|
|
+Client::Client(App *app, ActiveClient* ac):
|
|
QWClient(),
|
|
QWClient(),
|
|
|
|
+ myActiveClient(ac),
|
|
myApp(app),
|
|
myApp(app),
|
|
- mySocket(NULL),
|
|
|
|
- myEndFloodTimer(new QTime()),
|
|
|
|
|
|
+ myEndFloodTime(new QTime()),
|
|
myConnectionRetries(0),
|
|
myConnectionRetries(0),
|
|
- myOnServerFlag(false)
|
|
|
|
|
|
+ myOnServerFlag(false),
|
|
|
|
+ myMutedFlag(false),
|
|
|
|
+ myJoinMessageTimer(new QTimer()),
|
|
|
|
+ myJoinMessagePrinted(false)
|
|
{
|
|
{
|
|
- *myEndFloodTimer = QTime::currentTime();
|
|
|
|
|
|
+ *myEndFloodTime = QTime::currentTime();
|
|
|
|
+ myJoinMessageTimer->setSingleShot(true);
|
|
}
|
|
}
|
|
|
|
|
|
Client::~Client()
|
|
Client::~Client()
|
|
{
|
|
{
|
|
- delete myEndFloodTimer;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void Client::setSocket(QTcpSocket *socket)
|
|
|
|
-{
|
|
|
|
- mySocket = socket;
|
|
|
|
|
|
+ delete myEndFloodTime;
|
|
|
|
+ delete myJoinMessageTimer;
|
|
}
|
|
}
|
|
|
|
|
|
void Client::say(const QString &msg)
|
|
void Client::say(const QString &msg)
|
|
@@ -55,17 +56,17 @@ void Client::print(const QString &msg)
|
|
{
|
|
{
|
|
QString str;
|
|
QString str;
|
|
|
|
|
|
- str = QString(host()) + ":" + QString::number(port()) + "> " + msg;
|
|
|
|
|
|
+ str = QString(host()) + ":" + QString::number(port()) + "> " + msg;
|
|
QByteArray b = str.toAscii();
|
|
QByteArray b = str.toAscii();
|
|
Client::stripColor(b.data());
|
|
Client::stripColor(b.data());
|
|
str = QString::fromAscii(b.data());
|
|
str = QString::fromAscii(b.data());
|
|
|
|
|
|
- printf("%s", str.toAscii().data());
|
|
|
|
- if(mySocket)
|
|
|
|
- {
|
|
|
|
- mySocket->write(str.toAscii());
|
|
|
|
- mySocket->waitForBytesWritten();
|
|
|
|
- }
|
|
|
|
|
|
+ myApp->print(str);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Client::setPlayerList(PlayerList &playerList)
|
|
|
|
+{
|
|
|
|
+ myPlayerList = playerList;
|
|
}
|
|
}
|
|
|
|
|
|
void Client::onDisconnect()
|
|
void Client::onDisconnect()
|
|
@@ -89,15 +90,44 @@ void Client::retryConnection()
|
|
myConnectionRetries++;
|
|
myConnectionRetries++;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+bool Client::isMuted() const
|
|
|
|
+{
|
|
|
|
+ return myMutedFlag;
|
|
|
|
+}
|
|
|
|
+
|
|
void Client::parsePrintedLine()
|
|
void Client::parsePrintedLine()
|
|
{
|
|
{
|
|
- QRegExp regex("^(.+):\\s+\\.([A-Za-z]+)\\s*(.*)$");
|
|
|
|
|
|
+ Player player;
|
|
|
|
+ bool parsed = false;
|
|
|
|
+ quint16 lastMatchSize = 0;
|
|
|
|
|
|
- if(regex.indexIn(myPrintLine) == -1)
|
|
|
|
|
|
+ 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;
|
|
return;
|
|
|
|
|
|
|
|
+ QString nick(myPrintLine.left(lastMatchSize));
|
|
|
|
+ QString message(myPrintLine.right(myPrintLine.size() - lastMatchSize));
|
|
|
|
+ QRegExp regex("^:\\s+\\.([A-Za-z]+)\\s*(.+)$");
|
|
|
|
+ if(regex.indexIn(message) == -1)
|
|
|
|
+ return;
|
|
|
|
+
|
|
/* Flood prot */
|
|
/* Flood prot */
|
|
- int timeLeft = QTime::currentTime().secsTo(*myEndFloodTimer);
|
|
|
|
|
|
+ int timeLeft = QTime::currentTime().secsTo(*myEndFloodTime);
|
|
if(timeLeft > 0)
|
|
if(timeLeft > 0)
|
|
{
|
|
{
|
|
if(!myFloodMsgPrinted)
|
|
if(!myFloodMsgPrinted)
|
|
@@ -107,19 +137,17 @@ void Client::parsePrintedLine()
|
|
}
|
|
}
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- *myEndFloodTimer = QTime::currentTime().addSecs(Settings::globalInstance()->floodProtTime());
|
|
|
|
-
|
|
|
|
- QString nick = regex.capturedTexts().at(1);
|
|
|
|
- QString command = regex.capturedTexts().at(2);
|
|
|
|
- QString args = regex.capturedTexts().at(3);
|
|
|
|
|
|
+ *myEndFloodTime = QTime::currentTime().addSecs(Settings::globalInstance()->floodProtTime());
|
|
|
|
+ myFloodMsgPrinted = false;
|
|
|
|
|
|
- myFloodMsgPrinted = false;
|
|
|
|
|
|
+ QString command = regex.capturedTexts().at(1);
|
|
|
|
+ QString args = regex.capturedTexts().at(2);
|
|
|
|
|
|
if(command == "help")
|
|
if(command == "help")
|
|
{
|
|
{
|
|
- say("> commands:");
|
|
|
|
- say("> .qw <message>");
|
|
|
|
- say("> .spam <message>");
|
|
|
|
|
|
+ say("> Commands:");
|
|
|
|
+ say("> Broadcast message: .qw <message> and .spam <message>");
|
|
|
|
+ say("> Mute/Unmute the bot: .mute and .unmute");
|
|
say("> .help to show this help message.");
|
|
say("> .help to show this help message.");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -132,13 +160,11 @@ void Client::parsePrintedLine()
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- say("> Broadcasting...");
|
|
|
|
-
|
|
|
|
QString server(QString(host()) + ":" + QString::number(port()));
|
|
QString server(QString(host()) + ":" + QString::number(port()));
|
|
QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
|
|
QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
|
|
|
|
|
|
//-qw- Skillah - #crazy88 : 4on4 MIX qw.foppa.dk:27503 7/8
|
|
//-qw- Skillah - #crazy88 : 4on4 MIX qw.foppa.dk:27503 7/8
|
|
- myApp->broadcast(message);
|
|
|
|
|
|
+ myApp->broadcast(message, myActiveClient);
|
|
|
|
|
|
//myApp->requestBroadcast(command, nick, server, args.trimmed());
|
|
//myApp->requestBroadcast(command, nick, server, args.trimmed());
|
|
|
|
|
|
@@ -148,6 +174,20 @@ void Client::parsePrintedLine()
|
|
|
|
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if(command == "mute")
|
|
|
|
+ {
|
|
|
|
+ say("> Muted!");
|
|
|
|
+ myMutedFlag = true;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(command == "unmute")
|
|
|
|
+ {
|
|
|
|
+ say("> Unmuted!");
|
|
|
|
+ myMutedFlag = false;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
void Client::onPrint(int, const char *msg)
|
|
void Client::onPrint(int, const char *msg)
|
|
@@ -192,6 +232,7 @@ void Client::onLevelChanged(int, const char *levelName, float, float, float, flo
|
|
{
|
|
{
|
|
print(QString(levelName) + "\n");
|
|
print(QString(levelName) + "\n");
|
|
myDownloadProgressPrintedFlag = false;
|
|
myDownloadProgressPrintedFlag = false;
|
|
|
|
+ myMutedFlag = false;
|
|
}
|
|
}
|
|
|
|
|
|
void Client::onChallenge()
|
|
void Client::onChallenge()
|
|
@@ -214,6 +255,16 @@ void Client::onDownloadStarted(const char *fileName)
|
|
print("Download started " + QString(fileName) + "\n");
|
|
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)
|
|
void Client::onOOBPrint(const char *msg)
|
|
{
|
|
{
|
|
print(QString(msg));
|
|
print(QString(msg));
|
|
@@ -226,6 +277,8 @@ void Client::onStuffedCmd(const char *cmd)
|
|
{
|
|
{
|
|
myConnectionRetries = 0;
|
|
myConnectionRetries = 0;
|
|
myOnServerFlag = true;
|
|
myOnServerFlag = true;
|
|
|
|
+ myJoinMessageTimer->start(10000);
|
|
|
|
+ myJoinMessagePrinted = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|