Client.cpp 8.2 KB

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