Client.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. say("> Last 5 messages: .lastmsgs");
  153. return;
  154. }
  155. if(command == "qw" || command == "spam")
  156. {
  157. if(!args.trimmed().size())
  158. {
  159. say("> The format is ." + command + " <message>.");
  160. return;
  161. }
  162. if(command == "qw")
  163. {
  164. timeLeft = currentTime.secsTo(myQWBroadcastFloodTime);
  165. if(timeLeft > 0)
  166. {
  167. say("> FloodProt: Wait " + QString::number(timeLeft) + " secs before new .qw");
  168. return;
  169. }
  170. myQWBroadcastFloodTime = currentTime.addSecs(Settings::globalInstance()->qwFloodProtTime());
  171. }
  172. else if(command == "spam")
  173. {
  174. timeLeft = currentTime.secsTo(mySpamBroadcastFloodTime);
  175. if(timeLeft > 0)
  176. {
  177. say("> FloodProt: Wait " + QString::number(timeLeft) + " secs before new .spam");
  178. return;
  179. }
  180. mySpamBroadcastFloodTime = currentTime.addSecs(Settings::globalInstance()->spamFloodProtTime());
  181. }
  182. QString server(QString(host()) + ":" + QString::number(port()) + " " + QString::number(playerCount()) + "/" + QString::number(myMaxClients));
  183. QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
  184. /* Broadcast within QW servers */
  185. myApp->broadcast(message, myActiveClient);
  186. /* Broadcast outside QW */
  187. nick = parseNameFun(nick); //for the irc message namefun must be removed.
  188. QString parsedMsg = parseNameFun(args.trimmed());
  189. if(!Settings::globalInstance()->developerMode())
  190. myApp->requestBroadcast(command, nick, server, args.trimmed());
  191. else
  192. myApp->requestBroadcast("dev", nick, server, parsedMsg);
  193. return;
  194. }
  195. if(command == "qw_mute")
  196. {
  197. say("> .qw Frequency Muted.");
  198. myQWMutedFlag = true;
  199. return;
  200. }
  201. if(command == "qw_unmute")
  202. {
  203. say("> .qw Frequency Unmuted.");
  204. myQWMutedFlag = false;
  205. return;
  206. }
  207. if(command == "spam_mute")
  208. {
  209. say("> .spam Frequency Muted.");
  210. mySpamMutedFlag = true;
  211. return;
  212. }
  213. if(command == "spam_unmute")
  214. {
  215. say("> .spam Frequency Unmuted.");
  216. mySpamMutedFlag = false;
  217. return;
  218. }
  219. if(command == "lastmsgs")
  220. {
  221. QString msg;
  222. int i = 0;
  223. foreach(msg, myApp->lastMessages())
  224. {
  225. if(++i > 5)
  226. break;
  227. say(msg);
  228. }
  229. return;
  230. }
  231. }
  232. int Client::playerCount() const
  233. {
  234. Player c;
  235. int pc = 0;
  236. foreach(c, myPlayerList)
  237. {
  238. if(c.spectator)
  239. continue;
  240. pc++;
  241. }
  242. return pc;
  243. }
  244. void Client::setMaxClients(int maxClients)
  245. {
  246. myMaxClients = maxClients;
  247. }
  248. int Client::maxClients() const
  249. {
  250. return myMaxClients;
  251. }
  252. void Client::onPrint(int, const char *msg)
  253. {
  254. if(!strlen(msg))
  255. return;
  256. QString text(msg);
  257. if(text.endsWith('\n'))
  258. {
  259. myPrintLine.append(text);
  260. parsePrintedLine();
  261. print(myPrintLine);
  262. myPrintLine.clear();
  263. }
  264. else
  265. {
  266. myPrintLine.append(text);
  267. }
  268. }
  269. bool Client::isOnServer() const
  270. {
  271. return myOnServerFlag;
  272. }
  273. void Client::onError(const char *description)
  274. {
  275. QString desc(description);
  276. if(desc == "Client Timed Out.")
  277. {
  278. print("Error (" + QString(description) + ")\n");
  279. }
  280. else
  281. {
  282. print("Error (" + QString(description) + ")\n");
  283. }
  284. myOnServerFlag = false;
  285. }
  286. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  287. {
  288. print(QString(levelName) + "\n");
  289. myDownloadProgressPrintedFlag = false;
  290. mySpamMutedFlag = false;
  291. myQWMutedFlag = false;
  292. }
  293. void Client::onChallenge()
  294. {
  295. print("challenge\n");
  296. }
  297. void Client::onConnection()
  298. {
  299. print("connection\n");
  300. }
  301. void Client::onConnected()
  302. {
  303. print("connected\n");
  304. }
  305. void Client::onDownloadStarted(const char *fileName)
  306. {
  307. print("Download started " + QString(fileName) + "\n");
  308. }
  309. void Client::run()
  310. {
  311. if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
  312. {
  313. say("Hi, I am QWNET's bot, type .help to see my commands.");
  314. myJoinMessagePrinted = true;
  315. }
  316. /* Keep nick... Simply set name again after 10 secs */
  317. if(!myKeepNickTimer->isActive())
  318. {
  319. setName(Settings::globalInstance()->botName().toAscii().data());
  320. myKeepNickTimer->start(30000);
  321. }
  322. /* Avoid wrap around of flood timers */
  323. QTime currentTime = QTime::currentTime();
  324. if(currentTime.secsTo(myEndFloodTime) < -16000)
  325. myEndFloodTime = currentTime;
  326. if(currentTime.secsTo(myQWBroadcastFloodTime) < -16000)
  327. myQWBroadcastFloodTime = currentTime;
  328. if(currentTime.secsTo(mySpamBroadcastFloodTime) < -16000)
  329. mySpamBroadcastFloodTime = currentTime;
  330. QWClient::run();
  331. }
  332. void Client::onOOBPrint(const char *msg)
  333. {
  334. print(QString(msg));
  335. }
  336. void Client::onStuffedCmd(const char *cmd)
  337. {
  338. QString strCmd(cmd);
  339. if(strCmd == "skins") //connection sequence complete
  340. {
  341. myConnectionRetries = 0;
  342. myOnServerFlag = true;
  343. /* Only say hi if hi is scheduled */
  344. if(myJoinMessageScheduled)
  345. {
  346. myJoinMessageTimer->start(Settings::globalInstance()->timeToSayHiAfterConnected()*1000);
  347. myJoinMessagePrinted = false;
  348. myJoinMessageScheduled = false;
  349. }
  350. }
  351. }
  352. void Client::onDownloadProgress(int percent)
  353. {
  354. if(!(percent % 10))
  355. {
  356. if(!myDownloadProgressPrintedFlag)
  357. {
  358. print("Download " + QString::number(percent) + "%\n");
  359. myDownloadProgressPrintedFlag = true;
  360. }
  361. }
  362. else
  363. {
  364. myDownloadProgressPrintedFlag = false;
  365. }
  366. }
  367. void Client::onDownloadFinished()
  368. {
  369. print("Download 100% finished.\n");
  370. }
  371. QString Client::parseNameFun(const QString &string)
  372. {
  373. QByteArray b(string.toAscii());
  374. QWClient::stripColor(b.data());
  375. return QString(b);
  376. }