Client.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "Client.h"
  2. #include <QString>
  3. #include <stdio.h>
  4. #include <QTcpSocket>
  5. #include <QStringList>
  6. #include <QTime>
  7. #include <QTimer>
  8. #include "App.h"
  9. #include "Settings.h"
  10. Client::Client(App *app, ActiveClient* ac):
  11. QWClient(),
  12. myActiveClient(ac),
  13. myApp(app),
  14. myEndFloodTime(new QTime()),
  15. myConnectionRetries(0),
  16. myOnServerFlag(false),
  17. myMutedFlag(false),
  18. myJoinMessageTimer(new QTimer()),
  19. myJoinMessagePrinted(false)
  20. {
  21. *myEndFloodTime = QTime::currentTime();
  22. myJoinMessageTimer->setSingleShot(true);
  23. }
  24. Client::~Client()
  25. {
  26. delete myEndFloodTime;
  27. delete myJoinMessageTimer;
  28. }
  29. void Client::say(const QString &msg)
  30. {
  31. QString cmd = "say " + msg;
  32. sendCmd(cmd.toAscii().data());
  33. }
  34. void Client::sayTeam(const QString &msg)
  35. {
  36. QString cmd = "say_team " + msg;
  37. sendCmd(cmd.toAscii().data());
  38. }
  39. void Client::setTeam(const QString &msg)
  40. {
  41. sendCmd(QString("setinfo \"team\" \"" + msg + "\"").toAscii().data());
  42. }
  43. void Client::disconnect()
  44. {
  45. QWClient::disconnect();
  46. myOnServerFlag = false;
  47. }
  48. void Client::print(const QString &msg)
  49. {
  50. QString str;
  51. str = QString(host()) + ":" + QString::number(port()) + "> " + msg;
  52. QByteArray b = str.toAscii();
  53. Client::stripColor(b.data());
  54. str = QString::fromAscii(b.data());
  55. myApp->print(str);
  56. }
  57. void Client::setPlayerList(PlayerList &playerList)
  58. {
  59. myPlayerList = playerList;
  60. }
  61. void Client::onDisconnect()
  62. {
  63. print("Disconnected..\n");
  64. myOnServerFlag = false;
  65. }
  66. void Client::retryConnection()
  67. {
  68. if(myConnectionRetries == ConnectionRetries)
  69. {
  70. print("Giving up!\n");
  71. disconnect();
  72. myOnServerFlag = false;
  73. return;
  74. }
  75. print("Reconnecting...\n");
  76. reconnect();
  77. myConnectionRetries++;
  78. }
  79. bool Client::isMuted() const
  80. {
  81. return myMutedFlag;
  82. }
  83. void Client::parsePrintedLine()
  84. {
  85. Player player;
  86. bool parsed = false;
  87. quint16 lastMatchSize = 0;
  88. QByteArray playerName;
  89. QByteArray printLine(myPrintLine.toAscii());
  90. QWClient::stripColor(printLine.data());
  91. foreach(player, myPlayerList)
  92. {
  93. playerName = player.name.toAscii();
  94. QWClient::stripColor(playerName.data());
  95. if(printLine.startsWith(playerName))
  96. {
  97. if(lastMatchSize < playerName.size())
  98. lastMatchSize = playerName.size();
  99. parsed = true;
  100. }
  101. }
  102. if(!parsed)
  103. return;
  104. QString nick(myPrintLine.left(lastMatchSize));
  105. QString message(myPrintLine.right(myPrintLine.size() - lastMatchSize));
  106. QRegExp regex("^:\\s+\\.([A-Za-z]+)\\s*(.+)$");
  107. if(regex.indexIn(message) == -1)
  108. return;
  109. /* Flood prot */
  110. int timeLeft = QTime::currentTime().secsTo(*myEndFloodTime);
  111. if(timeLeft > 0)
  112. {
  113. if(!myFloodMsgPrinted)
  114. {
  115. say("> Wait " + QString::number(timeLeft) + " second(s) before issuing a new command.");
  116. myFloodMsgPrinted = true;
  117. }
  118. return;
  119. }
  120. *myEndFloodTime = QTime::currentTime().addSecs(Settings::globalInstance()->floodProtTime());
  121. myFloodMsgPrinted = false;
  122. QString command = regex.capturedTexts().at(1);
  123. QString args = regex.capturedTexts().at(2);
  124. if(command == "help")
  125. {
  126. say("> Commands:");
  127. say("> Broadcast message: .qw <message> and .spam <message>");
  128. say("> Mute/Unmute the bot: .mute and .unmute");
  129. say("> .help to show this help message.");
  130. return;
  131. }
  132. if(command == "qw" || command == "spam")
  133. {
  134. if(!args.trimmed().size())
  135. {
  136. say("> The format is ." + command + " <message>.");
  137. return;
  138. }
  139. QString server(QString(host()) + ":" + QString::number(port()));
  140. QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
  141. //-qw- Skillah - #crazy88 : 4on4 MIX qw.foppa.dk:27503 7/8
  142. myApp->broadcast(message, myActiveClient);
  143. //myApp->requestBroadcast(command, nick, server, args.trimmed());
  144. nick = parseNameFun(nick); //for the irc message namefun must be removed.
  145. QString parsedMsg = parseNameFun(args.trimmed());
  146. myApp->requestBroadcast("dev", nick, server, parsedMsg);
  147. return;
  148. }
  149. if(command == "mute")
  150. {
  151. say("> Muted!");
  152. myMutedFlag = true;
  153. return;
  154. }
  155. if(command == "unmute")
  156. {
  157. say("> Unmuted!");
  158. myMutedFlag = false;
  159. return;
  160. }
  161. }
  162. void Client::onPrint(int, const char *msg)
  163. {
  164. if(!strlen(msg))
  165. return;
  166. QString text(msg);
  167. if(text.endsWith('\n'))
  168. {
  169. myPrintLine.append(text);
  170. parsePrintedLine();
  171. print(myPrintLine);
  172. myPrintLine.clear();
  173. }
  174. else
  175. {
  176. myPrintLine.append(text);
  177. }
  178. }
  179. bool Client::isOnServer() const
  180. {
  181. return myOnServerFlag;
  182. }
  183. void Client::onError(const char *description)
  184. {
  185. QString desc(description);
  186. if(desc == "Client Timed Out.")
  187. {
  188. print("Error (" + QString(description) + ")\n");
  189. }
  190. else
  191. {
  192. print("Error (" + QString(description) + ")\n");
  193. }
  194. myOnServerFlag = false;
  195. }
  196. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  197. {
  198. print(QString(levelName) + "\n");
  199. myDownloadProgressPrintedFlag = false;
  200. myMutedFlag = false;
  201. }
  202. void Client::onChallenge()
  203. {
  204. print("challenge\n");
  205. }
  206. void Client::onConnection()
  207. {
  208. print("connection\n");
  209. }
  210. void Client::onConnected()
  211. {
  212. print("connected\n");
  213. }
  214. void Client::onDownloadStarted(const char *fileName)
  215. {
  216. print("Download started " + QString(fileName) + "\n");
  217. }
  218. void Client::run()
  219. {
  220. if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
  221. {
  222. say("Hi, I am QWNET's bot, type .help to see my commands.");
  223. myJoinMessagePrinted = true;
  224. }
  225. QWClient::run();
  226. }
  227. void Client::onOOBPrint(const char *msg)
  228. {
  229. print(QString(msg));
  230. }
  231. void Client::onStuffedCmd(const char *cmd)
  232. {
  233. QString strCmd(cmd);
  234. if(strCmd == "skins") //connection sequence complete
  235. {
  236. myConnectionRetries = 0;
  237. myOnServerFlag = true;
  238. myJoinMessageTimer->start(10000);
  239. myJoinMessagePrinted = false;
  240. }
  241. }
  242. void Client::onDownloadProgress(int percent)
  243. {
  244. if(!(percent % 10))
  245. {
  246. if(!myDownloadProgressPrintedFlag)
  247. {
  248. print("Download " + QString::number(percent) + "%\n");
  249. myDownloadProgressPrintedFlag = true;
  250. }
  251. }
  252. else
  253. {
  254. myDownloadProgressPrintedFlag = false;
  255. }
  256. }
  257. void Client::onDownloadFinished()
  258. {
  259. print("Download 100% finished.\n");
  260. }
  261. QString Client::parseNameFun(const QString &string)
  262. {
  263. QByteArray b(string.toAscii());
  264. QWClient::stripColor(b.data());
  265. return QString(b);
  266. }