Client.cpp 7.2 KB

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