App.cpp 11 KB

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