App.cpp 11 KB

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