Client.cpp 14 KB

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