Client.cpp 8.7 KB

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