Client.cpp 9.5 KB

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