Client.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. myConnectionRetries(0),
  15. myOnServerFlag(false),
  16. myMutedFlag(false),
  17. myJoinMessageTimer(new QTimer()),
  18. myJoinMessagePrinted(false)
  19. {
  20. myEndFloodTime = QTime::currentTime();
  21. myQWBroadcastFloodTime = myEndFloodTime;
  22. mySpamBroadcastFloodTime = myEndFloodTime;
  23. myJoinMessageTimer->setSingleShot(true);
  24. }
  25. Client::~Client()
  26. {
  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+\\.(spam|qw|help|mute|unmute)\\s*(.+)$");
  107. if(regex.indexIn(message) == -1)
  108. return;
  109. /* Flood prot */
  110. QTime currentTime = QTime::currentTime();
  111. int timeLeft = currentTime.secsTo(myEndFloodTime);
  112. if(timeLeft > 0)
  113. {
  114. if(!myFloodMsgPrinted)
  115. {
  116. say("> FloodProt: Not so fast, wait " + QString::number(timeLeft) + " sec(s).");
  117. myFloodMsgPrinted = true;
  118. }
  119. return;
  120. }
  121. myEndFloodTime = QTime::currentTime().addSecs(Settings::globalInstance()->floodProtTime());
  122. myFloodMsgPrinted = false;
  123. QString command = regex.capturedTexts().at(1);
  124. QString args = regex.capturedTexts().at(2);
  125. if(command == "help")
  126. {
  127. say("> Commands:");
  128. say("> Broadcast message: .qw <message> and .spam <message>");
  129. say("> Mute/Unmute the bot: .mute and .unmute");
  130. say("> .help to show this help message.");
  131. return;
  132. }
  133. if(command == "qw" || command == "spam")
  134. {
  135. if(!args.trimmed().size())
  136. {
  137. say("> The format is ." + command + " <message>.");
  138. return;
  139. }
  140. if(command == "qw")
  141. {
  142. timeLeft = currentTime.secsTo(myQWBroadcastFloodTime);
  143. if(timeLeft > 0)
  144. {
  145. say("> FloodProt: Wait " + QString::number(timeLeft) + " sec(s) before broadcasting a new .qw message.");
  146. return;
  147. }
  148. myQWBroadcastFloodTime = currentTime.addSecs(Settings::globalInstance()->qwFloodProtTime());
  149. }
  150. else if(command == "spam")
  151. {
  152. timeLeft = currentTime.secsTo(mySpamBroadcastFloodTime);
  153. if(timeLeft > 0)
  154. {
  155. say("> FloodProt: Wait " + QString::number(timeLeft) + " sec(s) before broadcasting a new .spam message.");
  156. return;
  157. }
  158. mySpamBroadcastFloodTime = currentTime.addSecs(Settings::globalInstance()->spamFloodProtTime());
  159. }
  160. QString server(QString(host()) + ":" + QString::number(port()));
  161. QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
  162. /* Broadcast within QW servers */
  163. myApp->broadcast(message, myActiveClient);
  164. /* Broadcast outside QW */
  165. nick = parseNameFun(nick); //for the irc message namefun must be removed.
  166. QString parsedMsg = parseNameFun(args.trimmed());
  167. myApp->requestBroadcast("dev", nick, server, parsedMsg);
  168. //myApp->requestBroadcast(command, nick, server, args.trimmed());
  169. return;
  170. }
  171. if(command == "mute")
  172. {
  173. say("> Muted!");
  174. myMutedFlag = true;
  175. return;
  176. }
  177. if(command == "unmute")
  178. {
  179. say("> Unmuted!");
  180. myMutedFlag = false;
  181. return;
  182. }
  183. }
  184. void Client::onPrint(int, const char *msg)
  185. {
  186. if(!strlen(msg))
  187. return;
  188. QString text(msg);
  189. if(text.endsWith('\n'))
  190. {
  191. myPrintLine.append(text);
  192. parsePrintedLine();
  193. print(myPrintLine);
  194. myPrintLine.clear();
  195. }
  196. else
  197. {
  198. myPrintLine.append(text);
  199. }
  200. }
  201. bool Client::isOnServer() const
  202. {
  203. return myOnServerFlag;
  204. }
  205. void Client::onError(const char *description)
  206. {
  207. QString desc(description);
  208. if(desc == "Client Timed Out.")
  209. {
  210. print("Error (" + QString(description) + ")\n");
  211. }
  212. else
  213. {
  214. print("Error (" + QString(description) + ")\n");
  215. }
  216. myOnServerFlag = false;
  217. }
  218. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  219. {
  220. print(QString(levelName) + "\n");
  221. myDownloadProgressPrintedFlag = false;
  222. myMutedFlag = false;
  223. }
  224. void Client::onChallenge()
  225. {
  226. print("challenge\n");
  227. }
  228. void Client::onConnection()
  229. {
  230. print("connection\n");
  231. }
  232. void Client::onConnected()
  233. {
  234. print("connected\n");
  235. }
  236. void Client::onDownloadStarted(const char *fileName)
  237. {
  238. print("Download started " + QString(fileName) + "\n");
  239. }
  240. void Client::run()
  241. {
  242. if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
  243. {
  244. say("Hi, I am QWNET's bot, type .help to see my commands.");
  245. myJoinMessagePrinted = true;
  246. }
  247. QWClient::run();
  248. }
  249. void Client::onOOBPrint(const char *msg)
  250. {
  251. print(QString(msg));
  252. }
  253. void Client::onStuffedCmd(const char *cmd)
  254. {
  255. QString strCmd(cmd);
  256. if(strCmd == "skins") //connection sequence complete
  257. {
  258. myConnectionRetries = 0;
  259. myOnServerFlag = true;
  260. myJoinMessageTimer->start(Settings::globalInstance()->timeToSayHiAfterConnected()*1000);
  261. myJoinMessagePrinted = false;
  262. }
  263. }
  264. void Client::onDownloadProgress(int percent)
  265. {
  266. if(!(percent % 10))
  267. {
  268. if(!myDownloadProgressPrintedFlag)
  269. {
  270. print("Download " + QString::number(percent) + "%\n");
  271. myDownloadProgressPrintedFlag = true;
  272. }
  273. }
  274. else
  275. {
  276. myDownloadProgressPrintedFlag = false;
  277. }
  278. }
  279. void Client::onDownloadFinished()
  280. {
  281. print("Download 100% finished.\n");
  282. }
  283. QString Client::parseNameFun(const QString &string)
  284. {
  285. QByteArray b(string.toAscii());
  286. QWClient::stripColor(b.data());
  287. return QString(b);
  288. }