App.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include "App.h"
  2. #include "Client.h"
  3. #include <QStringList>
  4. #include <QTcpSocket>
  5. #include <QTcpServer>
  6. #include <QRegExp>
  7. #include <QHostInfo>
  8. #include <QTimerEvent>
  9. #include <QDebug>
  10. #include <QtSql/QSqlQuery>
  11. #include <QCryptographicHash>
  12. #include <QHostAddress>
  13. #include "SshClient.h"
  14. #include "ServerQueryThread.h"
  15. App::App(int &argc, char **argv) :
  16. QCoreApplication(argc, argv),
  17. myServer(new QTcpServer()),
  18. mySocketConnectedFlag(false),
  19. myQWNETSshClient(new SshClient(this))
  20. {
  21. print("CIMS Bot Service v0.101\n=========================================================\n");
  22. setApplicationName("CIMSBOT");
  23. setOrganizationDomain("qwbr.tk");
  24. setOrganizationName("CIMS");
  25. setApplicationVersion("0.101");
  26. myServer->listen(QHostAddress::Any, 45000);
  27. connect(myServer, SIGNAL(newConnection()), SLOT(onNewConnection()));
  28. myClientsFrameTimerID = startTimer(0);
  29. }
  30. void App::onNewConnection()
  31. {
  32. if(mySocketConnectedFlag)
  33. {
  34. print("Someone just connected to the bot.\nBye Bye.\n");
  35. mySocket->disconnectFromHost();
  36. if(mySocket->state() != QTcpSocket::UnconnectedState)
  37. mySocket->waitForDisconnected();
  38. mySocketConnectedFlag = false;
  39. }
  40. mySocket = myServer->nextPendingConnection();
  41. mySocketConnectedFlag = true;
  42. connect(mySocket, SIGNAL(readyRead()), SLOT(onDataArrival()));
  43. connect(mySocket, SIGNAL(disconnected()), SLOT(onDisconnection()));
  44. print("Connected to CIMS's bot service.\n");
  45. /* Set socket on clients for echoing */
  46. ActiveClient* ac;
  47. foreach(ac, myClients)
  48. {
  49. ac->client()->setSocket(mySocket);
  50. }
  51. }
  52. void App::onDisconnection()
  53. {
  54. /* set all sockets to null so it doenst echo anything on a dangling pointer */
  55. ActiveClient* ac;
  56. foreach(ac, myClients)
  57. {
  58. ac->client()->setSocket(NULL);
  59. }
  60. mySocketConnectedFlag = false;
  61. mySocket->deleteLater();
  62. }
  63. void App::loadClients()
  64. {
  65. }
  66. void App::connectToServer(const QString &args)
  67. {
  68. if(!args.size())
  69. {
  70. print("No server specified\n");
  71. return;
  72. }
  73. if(args.contains(" "))
  74. {
  75. print("Invalid server address\n");
  76. return;
  77. }
  78. QStringList clientData = args.split(":");
  79. if(clientData.size() == 1)
  80. {
  81. addClient(clientData.at(0), 27500);
  82. return;
  83. }
  84. if(clientData.size() == 2)
  85. {
  86. addClient(clientData.at(0), clientData.at(1).toUShort());
  87. return;
  88. }
  89. }
  90. void App::disconnectFromServer(const QString &args)
  91. {
  92. if(!args.size())
  93. {
  94. print("No server specified\n");
  95. return;
  96. }
  97. QStringList clientData = args.split(":");
  98. if(clientData.size() == 1)
  99. {
  100. removeClient(clientData.at(0), 27500);
  101. return;
  102. }
  103. if(clientData.size() == 2)
  104. {
  105. removeClient(clientData.at(0), clientData.at(1).toUShort());
  106. return;
  107. }
  108. }
  109. void App::onDataArrival()
  110. {
  111. while(mySocket->canReadLine())
  112. {
  113. QByteArray line;
  114. line = mySocket->readLine();
  115. QString data(line);
  116. QRegExp regex("([0-9a-zA-Z]+)\\s+([a-z]+)\\s+(.*)");
  117. if(regex.indexIn(data) == -1)
  118. {
  119. print("command format: <password> <command> ?<arguments>\nEg.: pss help\nDisconnected\n");
  120. mySocket->disconnectFromHost();
  121. return;
  122. }
  123. QString pass = regex.capturedTexts().at(1);
  124. if(!checkPassword(pass))
  125. {
  126. print("Wrong password\nDisconnected\n");
  127. mySocket->disconnectFromHost();
  128. return;
  129. }
  130. QString cmd = regex.capturedTexts().at(2);
  131. QString args = regex.capturedTexts().at(3).trimmed();
  132. if(cmd == "connect")
  133. {
  134. connectToServer(args);
  135. return;
  136. }
  137. if(cmd == "disconnect")
  138. {
  139. disconnectFromServer(args);
  140. return;
  141. }
  142. if(cmd == "say")
  143. {
  144. say(args);
  145. return;
  146. }
  147. if(cmd == "say_team")
  148. {
  149. sayTeam(args);
  150. return;
  151. }
  152. if(cmd == "servers")
  153. {
  154. listClients();
  155. return;
  156. }
  157. if(cmd == "name")
  158. {
  159. setNick(args);
  160. return;
  161. }
  162. if(cmd == "color")
  163. {
  164. setColor(args);
  165. return;
  166. }
  167. if(cmd == "setping")
  168. {
  169. setPing(args);
  170. return;
  171. }
  172. if(cmd == "team")
  173. {
  174. setTeam(args);
  175. return;
  176. }
  177. if(cmd == "quit")
  178. {
  179. mySocket->disconnectFromHost();
  180. return;
  181. }
  182. if(cmd == "help")
  183. {
  184. help();
  185. return;
  186. }
  187. }
  188. }
  189. void App::help()
  190. {
  191. print("connect server:port -> connects the bot on a server\n");
  192. print("disconnect server:port -> removes the bot from a server\n");
  193. print("say message -> says the message on all servers where the bot is connected\n");
  194. print("say_team message -> says the message on all servers where the bot is connected\n");
  195. print("servers -> lists all servers the bot is connected\n");
  196. print("name nick -> changes the bot name to nick\n");
  197. print("color x x -> changes the player color\n");
  198. print("setping x -> sets the bot ping to x. ofc you can't lower your actual ping with this.\n");
  199. print("team teamname -> sets the bot team\n");
  200. print("help -> displays this message\n");
  201. }
  202. void App::setTeam(const QString &args)
  203. {
  204. ActiveClient* ac;
  205. foreach(ac, myClients)
  206. {
  207. ac->client()->setTeam(args);
  208. }
  209. print("Team changed.\n");
  210. }
  211. void App::setColor(const QString &args)
  212. {
  213. ActiveClient* ac;
  214. quint8 bottom, top;
  215. QStringList colors = args.split(" ");
  216. if(colors.size() < 2)
  217. return;
  218. bottom = colors.at(0).toUShort();
  219. top = colors.at(1).toUShort();
  220. foreach(ac, myClients)
  221. {
  222. ac->client()->setColor(bottom, top);
  223. }
  224. print("All clients colors have changed.\n");
  225. }
  226. void App::setPing(const QString &args)
  227. {
  228. ActiveClient* ac;
  229. foreach(ac, myClients)
  230. {
  231. ac->client()->setPing(args.toInt());
  232. }
  233. print("All clients pings have changed.\n");
  234. }
  235. void App::setNick(const QString &args)
  236. {
  237. ActiveClient* ac;
  238. foreach(ac, myClients)
  239. {
  240. ac->client()->setName(args.toAscii().data());
  241. }
  242. print("All clients nicks have changed.\n");
  243. }
  244. bool App::checkPassword(const QString &password)
  245. {
  246. if(QCryptographicHash::hash(password.toAscii(), QCryptographicHash::Md4).toHex().toLower() == "bf4df9f74d05c50ea00492224fb02854")
  247. return true;
  248. return false;
  249. }
  250. void App::print(const QString &msg)
  251. {
  252. printf("%s", msg.toAscii().data());
  253. if(mySocketConnectedFlag)
  254. {
  255. mySocket->write(msg.toAscii());
  256. mySocket->waitForBytesWritten();
  257. }
  258. }
  259. void App::addClient(const QString &host, quint16 port)
  260. {
  261. ActiveClient* ac;
  262. QHostAddress ha = QHostInfo::fromName(host).addresses().at(0);
  263. foreach(ac, myClients)
  264. {
  265. if(QString(ac->client()->host()) == ha.toString() && ac->client()->port() == port)
  266. {
  267. print("That client is already on the list.\n");
  268. return;
  269. }
  270. }
  271. ac = new ActiveClient(this);
  272. if(mySocketConnectedFlag)
  273. {
  274. if(mySocket->state() == QTcpSocket::ConnectedState)
  275. {
  276. ac->client()->setSocket(mySocket);
  277. }
  278. }
  279. ac->setAddress(ha, port);
  280. ac->client()->setQuakeFolder(mySettings.value("quakeFolder", App::applicationDirPath()).toString().toAscii().data());
  281. ac->client()->setName(mySettings.value("botName", "cimsbot").toString().toAscii().data());
  282. ac->client()->setSpectator(true);
  283. ac->client()->setPing(120);
  284. ac->client()->setColor(11, 13);
  285. myClients.push_back(ac);
  286. }
  287. void App::removeClient(const QString &host, quint16 port)
  288. {
  289. ActiveClient* ac;
  290. QHostAddress ha = QHostInfo::fromName(host).addresses().at(0);
  291. foreach(ac, myClients)
  292. {
  293. if(QString(ac->client()->host()) == ha.toString() && ac->client()->port() == port)
  294. {
  295. ac->client()->disconnect();
  296. delete ac;
  297. myClients.removeAll(ac);
  298. print("Client removed.\n");
  299. return;
  300. }
  301. }
  302. print("Client not found on the list.\n");
  303. }
  304. void App::say(const QString &msg)
  305. {
  306. ActiveClient* ac;
  307. foreach(ac, myClients)
  308. {
  309. ac->client()->say(msg);
  310. }
  311. print("Say command sent to all clients\n");
  312. }
  313. void App::sayTeam(const QString &msg)
  314. {
  315. ActiveClient* ac;
  316. foreach(ac, myClients)
  317. {
  318. ac->client()->sayTeam(msg);
  319. }
  320. print("Say_team command sent to all clients\n");
  321. }
  322. void App::listClients()
  323. {
  324. ActiveClient* ac;
  325. foreach(ac, myClients)
  326. {
  327. QString reply(ac->client()->host());
  328. reply.append(":" + QString::number(ac->client()->port()) + "\n");
  329. print(reply.toAscii().data());
  330. }
  331. }
  332. void App::timerEvent(QTimerEvent *e)
  333. {
  334. if(e->timerId() == myClientsFrameTimerID)
  335. {
  336. ActiveClient *ac;
  337. foreach(ac, myClients)
  338. {
  339. ac->run();
  340. }
  341. return;
  342. }
  343. }
  344. void App::broadcast(const QString &msg)
  345. {
  346. say(msg);
  347. }
  348. void App::cleanup()
  349. {
  350. ActiveClient* ac;
  351. foreach(ac, myClients)
  352. {
  353. ac->client()->disconnect();
  354. delete ac;
  355. }
  356. }
  357. App::~App()
  358. {
  359. cleanup();
  360. delete myServer;
  361. }
  362. //========================================================================
  363. App::ActiveClient::ActiveClient(App *app):
  364. myApp(app),
  365. myClient(new Client(app)),
  366. myQueryThread(new ServerQueryThread())
  367. {
  368. }
  369. App::ActiveClient::~ActiveClient()
  370. {
  371. delete myClient;
  372. if(queryThread()->isRunning())
  373. queryThread()->stopQueryThread();
  374. while(queryThread()->isRunning());
  375. delete myQueryThread;
  376. }
  377. Client* App::ActiveClient::client()
  378. {
  379. return myClient;
  380. }
  381. ServerQueryThread* App::ActiveClient::queryThread()
  382. {
  383. return myQueryThread;
  384. }
  385. void App::ActiveClient::setAddress(const QHostAddress &address, quint16 port)
  386. {
  387. queryThread()->setAddress(address, port);
  388. }
  389. void App::ActiveClient::run()
  390. {
  391. if(client()->state() == Client::DisconnectedState)
  392. {
  393. if(!queryThread()->isRunning())
  394. {
  395. queryThread()->startQueryThread();
  396. return;
  397. }
  398. int playerCount = queryThread()->playerCount();
  399. if(playerCount != 255 && playerCount > 0 && myDisconnectTime.elapsed() > 10000)
  400. {
  401. myApp->print("Players online on server " + queryThread()->serverAddress().toString() + ":" + QString::number(queryThread()->serverPort()) + ". Joining...\n");
  402. client()->connect(queryThread()->serverAddress().toString().toAscii(), queryThread()->serverPort());
  403. }
  404. return;
  405. }
  406. if(client()->state() == Client::ConnectedState)
  407. {
  408. int playerCount = queryThread()->playerCount();
  409. if(playerCount == 1 && client()->isOnServer())
  410. {
  411. myApp->print("I was left alone on " + queryThread()->serverAddress().toString() + ":" + QString::number(queryThread()->serverPort()) + ". Leaving...\n");
  412. client()->disconnect();
  413. myDisconnectTime.restart();
  414. return;
  415. }
  416. }
  417. client()->run();
  418. }