Client.cpp 14 KB

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