Client.cpp 12 KB

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