Client.cpp 12 KB

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