Client.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #include "Client.h"
  16. #include <QString>
  17. #include <stdio.h>
  18. #include <QTcpSocket>
  19. #include <QStringList>
  20. #include <QTime>
  21. #include <QTimer>
  22. #include "App.h"
  23. #include "Settings.h"
  24. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  25. Client::Client(App *app, ActiveClient* ac, bool supportsSendPrivate):
  26. QWClient(),
  27. myActiveClient(ac),
  28. myApp(app),
  29. myOnServerFlag(false),
  30. mySpamMutedFlag(false),
  31. myQWMutedFlag(false),
  32. myJoinMessageTimer(new QTimer()),
  33. myKeepNickTimer(new QTimer()),
  34. myFloodTimer(new QTimer()),
  35. myQWBroadcastFloodTimer(new QTimer()),
  36. mySpamBroadcastFloodTimer(new QTimer()),
  37. myJoinMessagePrinted(false),
  38. myJoinMessageScheduled(false),
  39. mySupportsSendPrivate(supportsSendPrivate),
  40. myMaxClients(0)
  41. {
  42. myJoinMessageTimer->setSingleShot(true);
  43. myKeepNickTimer->setSingleShot(true);
  44. myFloodTimer->setSingleShot(true);
  45. myQWBroadcastFloodTimer->setSingleShot(true);
  46. mySpamBroadcastFloodTimer->setSingleShot(true);
  47. }
  48. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  49. Client::~Client()
  50. {
  51. delete myJoinMessageTimer;
  52. delete myKeepNickTimer;
  53. delete myFloodTimer;
  54. delete myQWBroadcastFloodTimer;
  55. delete mySpamBroadcastFloodTimer;
  56. }
  57. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  58. void Client::connect(const char *host, quint16 port)
  59. {
  60. myJoinMessageScheduled = true; //Hi message only scheduled at bot connection
  61. setPing(13);
  62. QWClient::connect(host, port);
  63. }
  64. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  65. void Client::say(const QString &msg, const QString &nickName)
  66. {
  67. QString cmd("say ");
  68. if(!nickName.isEmpty() && mySupportsSendPrivate)
  69. cmd.append("s-p " + nickName + " ");
  70. cmd.append(msg);
  71. sendCmd(cmd.toAscii().data());
  72. }
  73. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  74. void Client::setTeam(const QString &msg)
  75. {
  76. sendCmd(QString("setinfo \"team\" \"" + msg + "\"").toAscii().data());
  77. }
  78. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  79. void Client::disconnect()
  80. {
  81. QWClient::disconnect();
  82. myOnServerFlag = false;
  83. }
  84. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  85. void Client::print(const QString &msg)
  86. {
  87. QString str;
  88. str = "[" + QTime::currentTime().toString(Qt::ISODate) + "] " + QString(host()) + ":" + QString::number(port()) + "> " + msg;
  89. QByteArray b = str.toAscii();
  90. Client::stripColor(b.data());
  91. str = QString::fromAscii(b.data());
  92. myApp->print(str);
  93. }
  94. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  95. void Client::setPlayerList(PlayerList &playerList)
  96. {
  97. myPlayerList = playerList;
  98. }
  99. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  100. void Client::onDisconnect()
  101. {
  102. print("Disconnected..\n");
  103. myOnServerFlag = false;
  104. }
  105. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  106. bool Client::isQWMuted() const
  107. {
  108. return myQWMutedFlag;
  109. }
  110. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  111. bool Client::isSpamMuted() const
  112. {
  113. return mySpamMutedFlag;
  114. }
  115. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  116. void Client::parsePrintedLine()
  117. {
  118. // Find the player that printed this line of text
  119. Player player, bestPlayer;
  120. quint16 lastMatchSize = 0;
  121. foreach(player, myPlayerList)
  122. {
  123. if(player.spectator && myPrintLine.startsWith("[SPEC] " + player.name))
  124. {
  125. if(lastMatchSize < (player.name.size() + 7))
  126. {
  127. lastMatchSize = (player.name.size() + 7);
  128. bestPlayer = player;
  129. }
  130. continue;
  131. }
  132. if(myPrintLine.startsWith(player.name))
  133. {
  134. if(lastMatchSize < player.name.size())
  135. {
  136. lastMatchSize = player.name.size();
  137. bestPlayer = player;
  138. }
  139. continue;
  140. }
  141. }
  142. if(!lastMatchSize)
  143. return;
  144. QString nick(bestPlayer.name);
  145. if(bestPlayer.spectator)
  146. nick.prepend("(spec) ");
  147. QString message(myPrintLine.right(myPrintLine.size() - lastMatchSize));
  148. QRegExp regex("^:\\s+\\.(spam|qw|help|qw_mute|qw_unmute|spam_mute|spam_unmute|lm)\\s*(.+)$");
  149. if(regex.indexIn(message) == -1)
  150. return;
  151. /* Flood prot */
  152. QTime currentTime = QTime::currentTime();
  153. int floodProtTime = Settings::globalInstance()->floodProtTime();
  154. if(myFloodTimer->isActive())
  155. {
  156. if(!myFloodMsgPrinted)
  157. {
  158. say("FloodProt: Not so fast, wait " + QString::number(floodProtTime + currentTime.secsTo(myFloodTimerStart)) + " sec(s).", bestPlayer.name);
  159. myFloodMsgPrinted = true;
  160. }
  161. return;
  162. }
  163. myFloodTimerStart = currentTime;
  164. myFloodTimer->start(floodProtTime*1000);
  165. myFloodMsgPrinted = false;
  166. QString command = regex.capturedTexts().at(1);
  167. QString args = regex.capturedTexts().at(2);
  168. if(command == "help")
  169. {
  170. say("Broadcast a message: .qw <message> and .spam <message>", bestPlayer.name);
  171. say("(Un)Mute: .qw_mute .qw_unmute or .spam_mute .spam_unmute", bestPlayer.name);
  172. say("Last 5 messages: .lm", bestPlayer.name);
  173. return;
  174. }
  175. if(command == "qw" || command == "spam")
  176. {
  177. if(!args.trimmed().size())
  178. {
  179. say("The format is ." + command + " <message>.", bestPlayer.name);
  180. return;
  181. }
  182. /* Floodprot for broadcasting commands */
  183. if(command == "qw")
  184. {
  185. int qwFloodProtTime = Settings::globalInstance()->qwFloodProtTime();
  186. if(myQWBroadcastFloodTimer->isActive())
  187. {
  188. say("FloodProt: Wait " + QString::number(qwFloodProtTime + currentTime.secsTo(myQWBroadcastFloodTimerStart)) + " secs before new .qw", bestPlayer.name);
  189. return;
  190. }
  191. myQWBroadcastFloodTimerStart = currentTime;
  192. myQWBroadcastFloodTimer->start(qwFloodProtTime*1000);
  193. }
  194. else if(command == "spam")
  195. {
  196. int spamFloodProtTime = Settings::globalInstance()->spamFloodProtTime();
  197. if(mySpamBroadcastFloodTimer->isActive())
  198. {
  199. say("FloodProt: Wait " + QString::number(spamFloodProtTime + currentTime.secsTo(mySpamBroadcastFloodTimerStart)) + " secs before new .spam", bestPlayer.name);
  200. return;
  201. }
  202. mySpamBroadcastFloodTimerStart = currentTime;
  203. mySpamBroadcastFloodTimer->start(spamFloodProtTime*1000);
  204. }
  205. // Prepare all strings to be broadcasted
  206. QString server(QString(host()) + ":" + QString::number(port()));
  207. // Tries to find the hostname for this ip in the cache
  208. QString resolvedServer(myApp->serverHostName(server));
  209. if(!resolvedServer.isEmpty())
  210. resolvedServer.append(":" + QString::number(port()));
  211. else
  212. resolvedServer = server;
  213. // Internal message to be broadcast within our network of bots (don't need to have namefun parsed)
  214. // The internal message uses the resolvedServer, because this message doesn't pass tru central, thus
  215. // the server ip isn't replaced by a hostname automatically
  216. QString internalMessage("-" + command + "- " + nick + " - " + resolvedServer + " " + QString::number(playerCount()) + "/" + QString::number(myMaxClients) + " : " + args.trimmed());
  217. myApp->broadcast(internalMessage, myActiveClient);
  218. // Message that will be broadcast to other networks (needs namefun parsing)
  219. // this message passes tru central, thus the server ip will be replaced by a hostname
  220. // automatically
  221. nick = parseNameFun(nick);
  222. QString parsedMsg = parseNameFun(args.trimmed());
  223. server.append(" " + QString::number(playerCount()) + "/" + QString::number(myMaxClients)); // append player count to the server (central needs another parm for this!)
  224. if(!Settings::globalInstance()->developerMode())
  225. myApp->requestBroadcast(command, nick, server, parsedMsg);
  226. else
  227. myApp->requestBroadcast("dev", nick, server, parsedMsg);
  228. say("Broadcasting...", bestPlayer.name);
  229. return;
  230. }
  231. if(command == "qw_mute")
  232. {
  233. say(".qw Frequency Muted.", bestPlayer.name);
  234. myQWMutedFlag = true;
  235. return;
  236. }
  237. if(command == "qw_unmute")
  238. {
  239. say(".qw Frequency Unmuted.", bestPlayer.name);
  240. myQWMutedFlag = false;
  241. return;
  242. }
  243. if(command == "spam_mute")
  244. {
  245. say(".spam Frequency Muted.", bestPlayer.name);
  246. mySpamMutedFlag = true;
  247. return;
  248. }
  249. if(command == "spam_unmute")
  250. {
  251. say(".spam Frequency Unmuted.", bestPlayer.name);
  252. mySpamMutedFlag = false;
  253. return;
  254. }
  255. if(command == "lm")
  256. {
  257. QStringList messages = myApp->lastMessages();
  258. if(!messages.size())
  259. {
  260. say("None", bestPlayer.name);
  261. return;
  262. }
  263. QString msg;
  264. int i = 0;
  265. foreach(msg, messages)
  266. {
  267. if(++i > 5)
  268. break;
  269. say(msg, bestPlayer.name);
  270. }
  271. return;
  272. }
  273. }
  274. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  275. int Client::playerCount() const
  276. {
  277. Player c;
  278. int pc = 0;
  279. foreach(c, myPlayerList)
  280. {
  281. if(c.spectator)
  282. continue;
  283. pc++;
  284. }
  285. return pc;
  286. }
  287. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  288. void Client::setMaxClients(int maxClients)
  289. {
  290. myMaxClients = maxClients;
  291. }
  292. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  293. int Client::maxClients() const
  294. {
  295. return myMaxClients;
  296. }
  297. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  298. void Client::onPrint(int, const char *msg)
  299. {
  300. if(!strlen(msg))
  301. return;
  302. QString text(msg);
  303. if(text.endsWith('\n'))
  304. {
  305. myPrintLine.append(text);
  306. parsePrintedLine();
  307. print(myPrintLine);
  308. myPrintLine.clear();
  309. }
  310. else
  311. {
  312. myPrintLine.append(text);
  313. }
  314. }
  315. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  316. bool Client::isOnServer() const
  317. {
  318. return myOnServerFlag;
  319. }
  320. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  321. void Client::onError(const char *description)
  322. {
  323. QString desc(description);
  324. if(desc == "Client Timed Out.")
  325. {
  326. print("Error (" + QString(description) + ")\n");
  327. }
  328. else
  329. {
  330. print("Error (" + QString(description) + ")\n");
  331. }
  332. myOnServerFlag = false;
  333. }
  334. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  335. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  336. {
  337. print(QString(levelName) + "\n");
  338. myDownloadProgressPrintedFlag = false;
  339. mySpamMutedFlag = false;
  340. myQWMutedFlag = false;
  341. setPing(13);
  342. }
  343. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  344. void Client::onChallenge()
  345. {
  346. print("challenge\n");
  347. }
  348. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  349. void Client::onConnection()
  350. {
  351. print("connection\n");
  352. }
  353. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  354. void Client::onConnected()
  355. {
  356. print("connected\n");
  357. }
  358. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  359. void Client::onDownloadStarted(const char *fileName)
  360. {
  361. print("Download started " + QString(fileName) + "\n");
  362. }
  363. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  364. void Client::run()
  365. {
  366. if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
  367. {
  368. say("Hi, I am QWNET's bot, type .help to see my commands.");
  369. myJoinMessagePrinted = true;
  370. }
  371. /* Keep nick... Simply set name again after 10 secs */
  372. if(!myKeepNickTimer->isActive())
  373. {
  374. setName(Settings::globalInstance()->botName().toAscii().data());
  375. myKeepNickTimer->start(30000);
  376. }
  377. QWClient::run();
  378. }
  379. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  380. void Client::onOOBPrint(const char *msg)
  381. {
  382. print(QString(msg));
  383. }
  384. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  385. void Client::onStuffedCmd(const char *cmd)
  386. {
  387. QString strCmd(cmd);
  388. if(strCmd == "skins") //connection sequence complete
  389. {
  390. myOnServerFlag = true;
  391. setPing(Settings::globalInstance()->botPing());
  392. /* Only say hi if hi is scheduled */
  393. if(myJoinMessageScheduled)
  394. {
  395. myJoinMessageTimer->start(Settings::globalInstance()->timeToSayHiAfterConnected()*1000);
  396. myJoinMessagePrinted = false;
  397. myJoinMessageScheduled = false;
  398. }
  399. }
  400. }
  401. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  402. void Client::onDownloadProgress(int percent)
  403. {
  404. if(!(percent % 10))
  405. {
  406. if(!myDownloadProgressPrintedFlag)
  407. {
  408. print("Download " + QString::number(percent) + "%\n");
  409. myDownloadProgressPrintedFlag = true;
  410. }
  411. }
  412. else
  413. {
  414. myDownloadProgressPrintedFlag = false;
  415. }
  416. }
  417. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  418. void Client::onDownloadFinished()
  419. {
  420. print("Download 100% finished.\n");
  421. }
  422. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  423. bool Client::supportsSendPrivate() const
  424. {
  425. return mySupportsSendPrivate;
  426. }
  427. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  428. QString Client::parseNameFun(const QString &string)
  429. {
  430. QByteArray b(string.toAscii());
  431. QWClient::stripColor(b.data());
  432. return QString(b);
  433. }