Client.cpp 15 KB

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