Client.cpp 9.1 KB

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