Client.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. print(myPrintLine);
  337. myPrintLine.clear();
  338. }
  339. else
  340. {
  341. myPrintLine.append(text);
  342. }
  343. }
  344. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  345. bool Client::isOnServer() const
  346. {
  347. return myOnServerFlag;
  348. }
  349. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  350. void Client::onError(const char *description)
  351. {
  352. QString desc(description);
  353. if(desc == "Client Timed Out.")
  354. {
  355. print("Error (" + QString(description) + ")\n");
  356. }
  357. else
  358. {
  359. print("Error (" + QString(description) + ")\n");
  360. }
  361. myOnServerFlag = false;
  362. }
  363. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  364. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  365. {
  366. print(QString(levelName) + "\n");
  367. myDownloadProgressPrintedFlag = false;
  368. mySpamMutedFlag = false;
  369. myQWMutedFlag = false;
  370. setPing(13);
  371. }
  372. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  373. void Client::onChallenge()
  374. {
  375. print("challenge\n");
  376. }
  377. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  378. void Client::onConnection()
  379. {
  380. print("connection\n");
  381. }
  382. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  383. void Client::onConnected()
  384. {
  385. print("connected\n");
  386. }
  387. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  388. void Client::onDownloadStarted(const char *fileName)
  389. {
  390. print("Download started " + QString(fileName) + "\n");
  391. }
  392. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  393. void Client::run()
  394. {
  395. // if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
  396. // {
  397. // say("Hi, I am QWNET's bot, type .help to see my commands.");
  398. // myJoinMessagePrinted = true;
  399. // }
  400. // Keep nick... Simply set name again after 30 secs
  401. if(!myKeepNickTimer->isActive())
  402. {
  403. setName(Settings::globalInstance()->botName().toAscii().data());
  404. myKeepNickTimer->start(30000);
  405. }
  406. // Scheduled commands
  407. if(!myCmdScheduled.isEmpty() && !myCmdScheduledTimer->isActive())
  408. {
  409. sendCmd(myCmdScheduled.toAscii().data());
  410. myCmdScheduled.clear();
  411. }
  412. QWClient::run();
  413. }
  414. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  415. void Client::onOOBPrint(const char *msg)
  416. {
  417. print(QString(msg));
  418. }
  419. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  420. void Client::onStuffedCmd(const char *cmd)
  421. {
  422. QString strCmd(cmd);
  423. if(strCmd == "skins") //connection sequence complete
  424. {
  425. myOnServerFlag = true;
  426. setPing(Settings::globalInstance()->botPing());
  427. if(mySPAutoDetect)
  428. spDetection();
  429. }
  430. }
  431. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  432. void Client::onDownloadProgress(int percent)
  433. {
  434. if(!(percent % 10))
  435. {
  436. if(!myDownloadProgressPrintedFlag)
  437. {
  438. print("Download " + QString::number(percent) + "%\n");
  439. myDownloadProgressPrintedFlag = true;
  440. }
  441. }
  442. else
  443. {
  444. myDownloadProgressPrintedFlag = false;
  445. }
  446. }
  447. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  448. void Client::onDownloadFinished()
  449. {
  450. print("Download 100% finished.\n");
  451. }
  452. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  453. void Client::setAutoDetectSP(bool autoDetect)
  454. {
  455. mySPAutoDetect = autoDetect;
  456. }
  457. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  458. void Client::spDetection()
  459. {
  460. // scheduleCmd("say s-p", 2000);
  461. say("s-p");
  462. mySPDetectionTimer->start(2000);
  463. }
  464. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  465. QString Client::parseNameFun(const QString &string)
  466. {
  467. QByteArray b(string.toAscii());
  468. QWClient::stripColor(b.data());
  469. return QString(b);
  470. }
  471. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  472. void Client::scheduleCmd(const QString &cmd, int time)
  473. {
  474. myCmdScheduled = cmd;
  475. myCmdScheduledTimer->start(time);
  476. }