Client.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "Client.h"
  2. #include <QString>
  3. #include <stdio.h>
  4. #include <QTcpSocket>
  5. #include <QStringList>
  6. #include <QTime>
  7. #include "App.h"
  8. #include "Settings.h"
  9. Client::Client(App *app):
  10. QWClient(),
  11. myApp(app),
  12. mySocket(NULL),
  13. myEndFloodTimer(new QTime()),
  14. myConnectionRetries(0),
  15. myOnServerFlag(false)
  16. {
  17. *myEndFloodTimer = QTime::currentTime();
  18. }
  19. Client::~Client()
  20. {
  21. delete myEndFloodTimer;
  22. }
  23. void Client::setSocket(QTcpSocket *socket)
  24. {
  25. mySocket = socket;
  26. }
  27. void Client::say(const QString &msg)
  28. {
  29. QString cmd = "say " + msg;
  30. sendCmd(cmd.toAscii().data());
  31. }
  32. void Client::sayTeam(const QString &msg)
  33. {
  34. QString cmd = "say_team " + msg;
  35. sendCmd(cmd.toAscii().data());
  36. }
  37. void Client::setTeam(const QString &msg)
  38. {
  39. sendCmd(QString("setinfo \"team\" \"" + msg + "\"").toAscii().data());
  40. }
  41. void Client::disconnect()
  42. {
  43. QWClient::disconnect();
  44. myOnServerFlag = false;
  45. }
  46. void Client::print(const QString &msg)
  47. {
  48. QString str;
  49. str = QString(host()) + ":" + QString::number(port()) + "> " + msg;
  50. QByteArray b = str.toAscii();
  51. Client::stripColor(b.data());
  52. str = QString::fromAscii(b.data());
  53. printf("%s", str.toAscii().data());
  54. if(mySocket)
  55. {
  56. mySocket->write(str.toAscii());
  57. mySocket->waitForBytesWritten();
  58. }
  59. }
  60. void Client::onDisconnect()
  61. {
  62. print("Disconnected..\n");
  63. myOnServerFlag = false;
  64. }
  65. void Client::retryConnection()
  66. {
  67. if(myConnectionRetries == ConnectionRetries)
  68. {
  69. print("Giving up!\n");
  70. disconnect();
  71. myOnServerFlag = false;
  72. return;
  73. }
  74. print("Reconnecting...\n");
  75. reconnect();
  76. myConnectionRetries++;
  77. }
  78. void Client::parsePrintedLine()
  79. {
  80. QRegExp regex("^(.+):\\s+\\.([A-Za-z]+)\\s*(.*)$");
  81. if(regex.indexIn(myPrintLine) == -1)
  82. return;
  83. /* Flood prot */
  84. int timeLeft = QTime::currentTime().secsTo(*myEndFloodTimer);
  85. if(timeLeft > 0)
  86. {
  87. if(!myFloodMsgPrinted)
  88. {
  89. say("> Wait " + QString::number(timeLeft) + " second(s) before issuing a new command.");
  90. myFloodMsgPrinted = true;
  91. }
  92. return;
  93. }
  94. *myEndFloodTimer = QTime::currentTime().addSecs(Settings::globalInstance()->floodProtTime());
  95. QString nick = regex.capturedTexts().at(1);
  96. QString command = regex.capturedTexts().at(2);
  97. QString args = regex.capturedTexts().at(3);
  98. myFloodMsgPrinted = false;
  99. if(command == "help")
  100. {
  101. say("> commands:");
  102. say("> .qw <message>");
  103. say("> .spam <message>");
  104. say("> .help to show this help message.");
  105. return;
  106. }
  107. if(command == "qw" || command == "spam")
  108. {
  109. if(!args.trimmed().size())
  110. {
  111. say("> The format is ." + command + " <message>.");
  112. return;
  113. }
  114. say("> Broadcasting...");
  115. QString server(QString(host()) + ":" + QString::number(port()));
  116. QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
  117. //-qw- Skillah - #crazy88 : 4on4 MIX qw.foppa.dk:27503 7/8
  118. myApp->broadcast(message);
  119. //myApp->requestBroadcast(command, nick, server, args.trimmed());
  120. nick = parseNameFun(nick); //for the irc message namefun must be removed.
  121. QString parsedMsg = parseNameFun(args.trimmed());
  122. myApp->requestBroadcast("dev", nick, server, parsedMsg);
  123. return;
  124. }
  125. }
  126. void Client::onPrint(int, const char *msg)
  127. {
  128. if(!strlen(msg))
  129. return;
  130. QString text(msg);
  131. if(text.endsWith('\n'))
  132. {
  133. myPrintLine.append(text);
  134. parsePrintedLine();
  135. print(myPrintLine);
  136. myPrintLine.clear();
  137. }
  138. else
  139. {
  140. myPrintLine.append(text);
  141. }
  142. }
  143. bool Client::isOnServer() const
  144. {
  145. return myOnServerFlag;
  146. }
  147. void Client::onError(const char *description)
  148. {
  149. QString desc(description);
  150. if(desc == "Client Timed Out.")
  151. {
  152. print("Error (" + QString(description) + ")\n");
  153. }
  154. else
  155. {
  156. print("Error (" + QString(description) + ")\n");
  157. }
  158. myOnServerFlag = false;
  159. }
  160. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  161. {
  162. print(QString(levelName) + "\n");
  163. myDownloadProgressPrintedFlag = false;
  164. }
  165. void Client::onChallenge()
  166. {
  167. print("challenge\n");
  168. }
  169. void Client::onConnection()
  170. {
  171. print("connection\n");
  172. }
  173. void Client::onConnected()
  174. {
  175. print("connected\n");
  176. }
  177. void Client::onDownloadStarted(const char *fileName)
  178. {
  179. print("Download started " + QString(fileName) + "\n");
  180. }
  181. void Client::onOOBPrint(const char *msg)
  182. {
  183. print(QString(msg));
  184. }
  185. void Client::onStuffedCmd(const char *cmd)
  186. {
  187. QString strCmd(cmd);
  188. if(strCmd == "skins") //connection sequence complete
  189. {
  190. myConnectionRetries = 0;
  191. myOnServerFlag = true;
  192. }
  193. }
  194. void Client::onDownloadProgress(int percent)
  195. {
  196. if(!(percent % 10))
  197. {
  198. if(!myDownloadProgressPrintedFlag)
  199. {
  200. print("Download " + QString::number(percent) + "%\n");
  201. myDownloadProgressPrintedFlag = true;
  202. }
  203. }
  204. else
  205. {
  206. myDownloadProgressPrintedFlag = false;
  207. }
  208. }
  209. void Client::onDownloadFinished()
  210. {
  211. print("Download 100% finished.\n");
  212. }
  213. QString Client::parseNameFun(const QString &string)
  214. {
  215. QByteArray b(string.toAscii());
  216. QWClient::stripColor(b.data());
  217. return QString(b);
  218. }