Client.cpp 15 KB

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