Client.cpp 13 KB

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