Client.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. say("Only .qw is allowed within QW servers. Please use .qw to broadcast your message.");
  226. return;
  227. // int spamFloodProtTime = Settings::globalInstance()->spamFloodProtTime();
  228. // if(mySpamBroadcastFloodTimer->isActive())
  229. // {
  230. // say("FloodProt: Wait " + QString::number(spamFloodProtTime + currentTime.secsTo(mySpamBroadcastFloodTimerStart)) + " secs before new .spam", bestPlayer.name);
  231. // return;
  232. // }
  233. // mySpamBroadcastFloodTimerStart = currentTime;
  234. // mySpamBroadcastFloodTimer->start(spamFloodProtTime*1000);
  235. }
  236. // Prepare all strings to be broadcasted
  237. QString server(QString(host()) + ":" + QString::number(port()));
  238. // Tries to find the hostname for this ip in the cache
  239. QString resolvedServer(myApp->serverHostName(server));
  240. if(!resolvedServer.isEmpty())
  241. resolvedServer.append(":" + QString::number(port()));
  242. else
  243. resolvedServer = server;
  244. // Internal message to be broadcast within our network of bots (don't need to have namefun parsed)
  245. // The internal message uses the resolvedServer, because this message doesn't pass tru central, thus
  246. // the server ip isn't replaced by a hostname automatically
  247. QString internalMessage("-" + command + "- " + nick + " - " + resolvedServer + " " + QString::number(playerCount()) + "/" + QString::number(myMaxClients) + " : " + args.trimmed());
  248. myApp->broadcast(internalMessage, myActiveClient);
  249. // Message that will be broadcast to other networks (needs namefun parsing)
  250. // this message passes tru central, thus the server ip will be replaced by a hostname
  251. // automatically
  252. nick = parseNameFun(nick);
  253. QString parsedMsg = parseNameFun(args.trimmed());
  254. server.append(" " + QString::number(playerCount()) + "/" + QString::number(myMaxClients)); // append player count to the server (central needs another parm for this!)
  255. if(!Settings::globalInstance()->developerMode())
  256. myApp->requestBroadcast(command, nick, server, parsedMsg);
  257. else
  258. myApp->requestBroadcast("dev", nick, server, parsedMsg);
  259. say("Broadcasting...", bestPlayer.name);
  260. return;
  261. }
  262. if(command == "qw_mute")
  263. {
  264. say("Qw frequency muted.", bestPlayer.name);
  265. myQWMutedFlag = true;
  266. return;
  267. }
  268. if(command == "qw_unmute")
  269. {
  270. say("Qw frequency unmuted.", bestPlayer.name);
  271. myQWMutedFlag = false;
  272. return;
  273. }
  274. if(command == "spam_mute")
  275. {
  276. say("Spam frequency muted.", bestPlayer.name);
  277. mySpamMutedFlag = true;
  278. return;
  279. }
  280. if(command == "spam_unmute")
  281. {
  282. say("Spam frequency unmuted.", bestPlayer.name);
  283. mySpamMutedFlag = false;
  284. return;
  285. }
  286. if(command == "lm")
  287. {
  288. QStringList messages = myApp->lastMessages();
  289. if(!messages.size())
  290. {
  291. say("None", bestPlayer.name);
  292. return;
  293. }
  294. QString msg;
  295. int i = 0;
  296. foreach(msg, messages)
  297. {
  298. if(++i > 5)
  299. break;
  300. say(msg, bestPlayer.name);
  301. }
  302. return;
  303. }
  304. }
  305. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  306. int Client::playerCount() const
  307. {
  308. Player c;
  309. int pc = 0;
  310. foreach(c, myPlayerList)
  311. {
  312. if(c.spectator)
  313. continue;
  314. pc++;
  315. }
  316. return pc;
  317. }
  318. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  319. void Client::setMaxClients(int maxClients)
  320. {
  321. myMaxClients = maxClients;
  322. }
  323. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  324. int Client::maxClients() const
  325. {
  326. return myMaxClients;
  327. }
  328. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  329. void Client::onPrint(int, const char *msg)
  330. {
  331. if(!strlen(msg))
  332. return;
  333. QString text(msg);
  334. if(text.endsWith('\n'))
  335. {
  336. myPrintLine.append(text);
  337. parsePrintedLine();
  338. myPrintLine.clear();
  339. }
  340. else
  341. {
  342. myPrintLine.append(text);
  343. }
  344. }
  345. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  346. bool Client::isOnServer() const
  347. {
  348. return myOnServerFlag;
  349. }
  350. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  351. void Client::onError(const char *description)
  352. {
  353. QString desc(description);
  354. if(desc == "Client Timed Out.")
  355. {
  356. print("Error (" + QString(description) + ")\n");
  357. }
  358. else
  359. {
  360. print("Error (" + QString(description) + ")\n");
  361. }
  362. myOnServerFlag = false;
  363. }
  364. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  365. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  366. {
  367. print(QString(levelName) + "\n");
  368. myDownloadProgressPrintedFlag = false;
  369. mySpamMutedFlag = false;
  370. myQWMutedFlag = false;
  371. setPing(13);
  372. }
  373. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  374. void Client::onChallenge()
  375. {
  376. print("challenge\n");
  377. }
  378. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  379. void Client::onConnection()
  380. {
  381. print("connection\n");
  382. }
  383. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  384. void Client::onConnected()
  385. {
  386. print("connected\n");
  387. }
  388. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  389. void Client::onDownloadStarted(const char *fileName)
  390. {
  391. print("Download started " + QString(fileName) + "\n");
  392. }
  393. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  394. void Client::run()
  395. {
  396. // if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
  397. // {
  398. // say("Hi, I am QWNET's bot, type .help to see my commands.");
  399. // myJoinMessagePrinted = true;
  400. // }
  401. // Keep nick... Simply set name again after 30 secs
  402. if(!myKeepNickTimer->isActive())
  403. {
  404. setName(Settings::globalInstance()->botName().toAscii().data());
  405. myKeepNickTimer->start(30000);
  406. }
  407. // Scheduled commands
  408. if(!myCmdScheduled.isEmpty() && !myCmdScheduledTimer->isActive())
  409. {
  410. sendCmd(myCmdScheduled.toAscii().data());
  411. myCmdScheduled.clear();
  412. }
  413. QWClient::run();
  414. }
  415. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  416. void Client::onOOBPrint(const char *msg)
  417. {
  418. print(QString(msg));
  419. }
  420. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  421. void Client::onStuffedCmd(const char *cmd)
  422. {
  423. QString strCmd(cmd);
  424. if(strCmd == "skins") //connection sequence complete
  425. {
  426. myOnServerFlag = true;
  427. setPing(Settings::globalInstance()->botPing());
  428. if(mySPAutoDetect)
  429. spDetection();
  430. }
  431. }
  432. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  433. void Client::onDownloadProgress(int percent)
  434. {
  435. if(!(percent % 10))
  436. {
  437. if(!myDownloadProgressPrintedFlag)
  438. {
  439. print("Download " + QString::number(percent) + "%\n");
  440. myDownloadProgressPrintedFlag = true;
  441. }
  442. }
  443. else
  444. {
  445. myDownloadProgressPrintedFlag = false;
  446. }
  447. }
  448. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  449. void Client::onDownloadFinished()
  450. {
  451. print("Download 100% finished.\n");
  452. }
  453. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  454. void Client::setAutoDetectSP(bool autoDetect)
  455. {
  456. mySPAutoDetect = autoDetect;
  457. }
  458. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  459. void Client::spDetection()
  460. {
  461. // scheduleCmd("say s-p", 2000);
  462. say("s-p");
  463. mySPDetectionTimer->start(2000);
  464. }
  465. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  466. QString Client::parseNameFun(const QString &string)
  467. {
  468. QByteArray b(string.toAscii());
  469. QWClient::stripColor(b.data());
  470. return QString(b);
  471. }
  472. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  473. void Client::scheduleCmd(const QString &cmd, int time)
  474. {
  475. myCmdScheduled = cmd;
  476. myCmdScheduledTimer->start(time);
  477. }