Client.cpp 14 KB

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