Client.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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|lastmsgs)\\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. if(command == "lastmsgs")
  219. {
  220. QString msg;
  221. int i = 0;
  222. foreach(msg, myApp->lastMessages())
  223. {
  224. if(++i > 5)
  225. break;
  226. say(msg);
  227. }
  228. return;
  229. }
  230. }
  231. int Client::playerCount() const
  232. {
  233. Player c;
  234. int pc = 0;
  235. foreach(c, myPlayerList)
  236. {
  237. if(c.spectator)
  238. continue;
  239. pc++;
  240. }
  241. return pc;
  242. }
  243. void Client::setMaxClients(int maxClients)
  244. {
  245. myMaxClients = maxClients;
  246. }
  247. int Client::maxClients() const
  248. {
  249. return myMaxClients;
  250. }
  251. void Client::onPrint(int, const char *msg)
  252. {
  253. if(!strlen(msg))
  254. return;
  255. QString text(msg);
  256. if(text.endsWith('\n'))
  257. {
  258. myPrintLine.append(text);
  259. parsePrintedLine();
  260. print(myPrintLine);
  261. myPrintLine.clear();
  262. }
  263. else
  264. {
  265. myPrintLine.append(text);
  266. }
  267. }
  268. bool Client::isOnServer() const
  269. {
  270. return myOnServerFlag;
  271. }
  272. void Client::onError(const char *description)
  273. {
  274. QString desc(description);
  275. if(desc == "Client Timed Out.")
  276. {
  277. print("Error (" + QString(description) + ")\n");
  278. }
  279. else
  280. {
  281. print("Error (" + QString(description) + ")\n");
  282. }
  283. myOnServerFlag = false;
  284. }
  285. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  286. {
  287. print(QString(levelName) + "\n");
  288. myDownloadProgressPrintedFlag = false;
  289. mySpamMutedFlag = false;
  290. myQWMutedFlag = false;
  291. }
  292. void Client::onChallenge()
  293. {
  294. print("challenge\n");
  295. }
  296. void Client::onConnection()
  297. {
  298. print("connection\n");
  299. }
  300. void Client::onConnected()
  301. {
  302. print("connected\n");
  303. }
  304. void Client::onDownloadStarted(const char *fileName)
  305. {
  306. print("Download started " + QString(fileName) + "\n");
  307. }
  308. void Client::run()
  309. {
  310. if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
  311. {
  312. say("Hi, I am QWNET's bot, type .help to see my commands.");
  313. myJoinMessagePrinted = true;
  314. }
  315. /* Keep nick... Simply set name again after 10 secs */
  316. if(!myKeepNickTimer->isActive())
  317. {
  318. setName(Settings::globalInstance()->botName().toAscii().data());
  319. myKeepNickTimer->start(30000);
  320. }
  321. /* Avoid wrap around of flood timers */
  322. QTime currentTime = QTime::currentTime();
  323. if(currentTime.secsTo(myEndFloodTime) < -16000)
  324. myEndFloodTime = currentTime;
  325. if(currentTime.secsTo(myQWBroadcastFloodTime) < -16000)
  326. myQWBroadcastFloodTime = currentTime;
  327. if(currentTime.secsTo(mySpamBroadcastFloodTime) < -16000)
  328. mySpamBroadcastFloodTime = currentTime;
  329. QWClient::run();
  330. }
  331. void Client::onOOBPrint(const char *msg)
  332. {
  333. print(QString(msg));
  334. }
  335. void Client::onStuffedCmd(const char *cmd)
  336. {
  337. QString strCmd(cmd);
  338. if(strCmd == "skins") //connection sequence complete
  339. {
  340. myConnectionRetries = 0;
  341. myOnServerFlag = true;
  342. /* Only say hi if hi is scheduled */
  343. if(myJoinMessageScheduled)
  344. {
  345. myJoinMessageTimer->start(Settings::globalInstance()->timeToSayHiAfterConnected()*1000);
  346. myJoinMessagePrinted = false;
  347. myJoinMessageScheduled = false;
  348. }
  349. }
  350. }
  351. void Client::onDownloadProgress(int percent)
  352. {
  353. if(!(percent % 10))
  354. {
  355. if(!myDownloadProgressPrintedFlag)
  356. {
  357. print("Download " + QString::number(percent) + "%\n");
  358. myDownloadProgressPrintedFlag = true;
  359. }
  360. }
  361. else
  362. {
  363. myDownloadProgressPrintedFlag = false;
  364. }
  365. }
  366. void Client::onDownloadFinished()
  367. {
  368. print("Download 100% finished.\n");
  369. }
  370. QString Client::parseNameFun(const QString &string)
  371. {
  372. QByteArray b(string.toAscii());
  373. QWClient::stripColor(b.data());
  374. return QString(b);
  375. }