Client.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. mySpamMutedFlag(false),
  17. myQWMutedFlag(false),
  18. myJoinMessageTimer(new QTimer()),
  19. myJoinMessagePrinted(false)
  20. {
  21. myEndFloodTime = QTime::currentTime();
  22. myQWBroadcastFloodTime = myEndFloodTime;
  23. mySpamBroadcastFloodTime = myEndFloodTime;
  24. myJoinMessageTimer->setSingleShot(true);
  25. }
  26. Client::~Client()
  27. {
  28. delete myJoinMessageTimer;
  29. }
  30. void Client::say(const QString &msg)
  31. {
  32. QString cmd = "say " + msg;
  33. sendCmd(cmd.toAscii().data());
  34. }
  35. void Client::sayTeam(const QString &msg)
  36. {
  37. QString cmd = "say_team " + msg;
  38. sendCmd(cmd.toAscii().data());
  39. }
  40. void Client::setTeam(const QString &msg)
  41. {
  42. sendCmd(QString("setinfo \"team\" \"" + msg + "\"").toAscii().data());
  43. }
  44. void Client::disconnect()
  45. {
  46. QWClient::disconnect();
  47. myOnServerFlag = false;
  48. }
  49. void Client::print(const QString &msg)
  50. {
  51. QString str;
  52. str = QString(host()) + ":" + QString::number(port()) + "> " + msg;
  53. QByteArray b = str.toAscii();
  54. Client::stripColor(b.data());
  55. str = QString::fromAscii(b.data());
  56. myApp->print(str);
  57. }
  58. void Client::setPlayerList(PlayerList &playerList)
  59. {
  60. myPlayerList = playerList;
  61. }
  62. void Client::onDisconnect()
  63. {
  64. print("Disconnected..\n");
  65. myOnServerFlag = false;
  66. }
  67. void Client::retryConnection()
  68. {
  69. if(myConnectionRetries == ConnectionRetries)
  70. {
  71. print("Giving up!\n");
  72. disconnect();
  73. myOnServerFlag = false;
  74. return;
  75. }
  76. print("Reconnecting...\n");
  77. reconnect();
  78. myConnectionRetries++;
  79. }
  80. bool Client::isQWMuted() const
  81. {
  82. return myQWMutedFlag;
  83. }
  84. bool Client::isSpamMuted() const
  85. {
  86. return mySpamMutedFlag;
  87. }
  88. void Client::parsePrintedLine()
  89. {
  90. Player player;
  91. bool parsed = false;
  92. quint16 lastMatchSize = 0;
  93. QByteArray playerName;
  94. QByteArray printLine(myPrintLine.toAscii());
  95. QWClient::stripColor(printLine.data());
  96. foreach(player, myPlayerList)
  97. {
  98. playerName = player.name.toAscii();
  99. QWClient::stripColor(playerName.data());
  100. if(printLine.startsWith(playerName) || (player.spectator && printLine.startsWith(QString("[SPEC] " + player.name).toAscii())))
  101. {
  102. if(player.spectator && printLine.startsWith("[SPEC] "))
  103. {
  104. if(lastMatchSize < (playerName.size() + 7))
  105. lastMatchSize = (playerName.size() + 7);
  106. }
  107. else if(lastMatchSize < playerName.size())
  108. {
  109. lastMatchSize = playerName.size();
  110. }
  111. parsed = true;
  112. }
  113. }
  114. if(!parsed)
  115. return;
  116. QString nick(myPrintLine.left(lastMatchSize));
  117. QString message(myPrintLine.right(myPrintLine.size() - lastMatchSize));
  118. QRegExp regex("^:\\s+\\.(spam|qw|help|qw_mute|qw_unmute|spam_mute|spam_unmute)\\s*(.+)$");
  119. if(regex.indexIn(message) == -1)
  120. return;
  121. /* Flood prot */
  122. QTime currentTime = QTime::currentTime();
  123. int timeLeft = currentTime.secsTo(myEndFloodTime);
  124. if(timeLeft > 0)
  125. {
  126. if(!myFloodMsgPrinted)
  127. {
  128. say("> FloodProt: Not so fast, wait " + QString::number(timeLeft) + " sec(s).");
  129. myFloodMsgPrinted = true;
  130. }
  131. return;
  132. }
  133. myEndFloodTime = QTime::currentTime().addSecs(Settings::globalInstance()->floodProtTime());
  134. myFloodMsgPrinted = false;
  135. QString command = regex.capturedTexts().at(1);
  136. QString args = regex.capturedTexts().at(2);
  137. if(command == "help")
  138. {
  139. say("> Commands:");
  140. say("> Broadcast a message: .qw <message> and .spam <message>, beware of the floodprot limits 600s for .qw and 300s for .spam");
  141. say("> Mute/Unmute: .qw_mute and .qw_unmute or .spam_mute or .spam_unmute");
  142. say("> .help to show this help message.");
  143. return;
  144. }
  145. if(command == "qw" || command == "spam")
  146. {
  147. if(!args.trimmed().size())
  148. {
  149. say("> The format is ." + command + " <message>.");
  150. return;
  151. }
  152. if(command == "qw")
  153. {
  154. timeLeft = currentTime.secsTo(myQWBroadcastFloodTime);
  155. if(timeLeft > 0)
  156. {
  157. say("> FloodProt: Wait " + QString::number(timeLeft) + " sec(s) before broadcasting a new .qw message.");
  158. return;
  159. }
  160. myQWBroadcastFloodTime = currentTime.addSecs(Settings::globalInstance()->qwFloodProtTime());
  161. }
  162. else if(command == "spam")
  163. {
  164. timeLeft = currentTime.secsTo(mySpamBroadcastFloodTime);
  165. if(timeLeft > 0)
  166. {
  167. say("> FloodProt: Wait " + QString::number(timeLeft) + " sec(s) before broadcasting a new .spam message.");
  168. return;
  169. }
  170. mySpamBroadcastFloodTime = currentTime.addSecs(Settings::globalInstance()->spamFloodProtTime());
  171. }
  172. QString server(QString(host()) + ":" + QString::number(port()));
  173. QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
  174. /* Broadcast within QW servers */
  175. myApp->broadcast(message, myActiveClient);
  176. /* Broadcast outside QW */
  177. nick = parseNameFun(nick); //for the irc message namefun must be removed.
  178. QString parsedMsg = parseNameFun(args.trimmed());
  179. myApp->requestBroadcast("dev", nick, server, parsedMsg);
  180. //myApp->requestBroadcast(command, nick, server, args.trimmed());
  181. return;
  182. }
  183. if(command == "qw_mute")
  184. {
  185. say("> .qw Frequency Muted.");
  186. myQWMutedFlag = true;
  187. return;
  188. }
  189. if(command == "qw_unmute")
  190. {
  191. say("> .qw Frequency Unmuted.");
  192. myQWMutedFlag = false;
  193. return;
  194. }
  195. if(command == "spam_mute")
  196. {
  197. say("> .spam Frequency Muted.");
  198. mySpamMutedFlag = true;
  199. return;
  200. }
  201. if(command == "spam_unmute")
  202. {
  203. say("> .spam Frequency Unmuted.");
  204. mySpamMutedFlag = false;
  205. return;
  206. }
  207. }
  208. void Client::onPrint(int, const char *msg)
  209. {
  210. if(!strlen(msg))
  211. return;
  212. QString text(msg);
  213. if(text.endsWith('\n'))
  214. {
  215. myPrintLine.append(text);
  216. parsePrintedLine();
  217. print(myPrintLine);
  218. myPrintLine.clear();
  219. }
  220. else
  221. {
  222. myPrintLine.append(text);
  223. }
  224. }
  225. bool Client::isOnServer() const
  226. {
  227. return myOnServerFlag;
  228. }
  229. void Client::onError(const char *description)
  230. {
  231. QString desc(description);
  232. if(desc == "Client Timed Out.")
  233. {
  234. print("Error (" + QString(description) + ")\n");
  235. }
  236. else
  237. {
  238. print("Error (" + QString(description) + ")\n");
  239. }
  240. myOnServerFlag = false;
  241. }
  242. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  243. {
  244. print(QString(levelName) + "\n");
  245. myDownloadProgressPrintedFlag = false;
  246. mySpamMutedFlag = false;
  247. myQWMutedFlag = false;
  248. }
  249. void Client::onChallenge()
  250. {
  251. print("challenge\n");
  252. }
  253. void Client::onConnection()
  254. {
  255. print("connection\n");
  256. }
  257. void Client::onConnected()
  258. {
  259. print("connected\n");
  260. }
  261. void Client::onDownloadStarted(const char *fileName)
  262. {
  263. print("Download started " + QString(fileName) + "\n");
  264. }
  265. void Client::run()
  266. {
  267. if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
  268. {
  269. say("Hi, I am QWNET's bot, type .help to see my commands.");
  270. myJoinMessagePrinted = true;
  271. }
  272. QWClient::run();
  273. }
  274. void Client::onOOBPrint(const char *msg)
  275. {
  276. print(QString(msg));
  277. }
  278. void Client::onStuffedCmd(const char *cmd)
  279. {
  280. QString strCmd(cmd);
  281. if(strCmd == "skins") //connection sequence complete
  282. {
  283. myConnectionRetries = 0;
  284. myOnServerFlag = true;
  285. myJoinMessageTimer->start(Settings::globalInstance()->timeToSayHiAfterConnected()*1000);
  286. myJoinMessagePrinted = false;
  287. }
  288. }
  289. void Client::onDownloadProgress(int percent)
  290. {
  291. if(!(percent % 10))
  292. {
  293. if(!myDownloadProgressPrintedFlag)
  294. {
  295. print("Download " + QString::number(percent) + "%\n");
  296. myDownloadProgressPrintedFlag = true;
  297. }
  298. }
  299. else
  300. {
  301. myDownloadProgressPrintedFlag = false;
  302. }
  303. }
  304. void Client::onDownloadFinished()
  305. {
  306. print("Download 100% finished.\n");
  307. }
  308. QString Client::parseNameFun(const QString &string)
  309. {
  310. QByteArray b(string.toAscii());
  311. QWClient::stripColor(b.data());
  312. return QString(b);
  313. }