Client.cpp 12 KB

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