Client.cpp 13 KB

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