App.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. }
  57. void App::onDisconnection()
  58. {
  59. mySocketConnectedFlag = false;
  60. mySocket->deleteLater();
  61. }
  62. void App::loadServerList()
  63. {
  64. QStringList list = Settings::globalInstance()->serverList();
  65. QString sv;
  66. foreach(sv, list)
  67. {
  68. ActiveClient* ac = new ActiveClient(this);
  69. QStringList parms = sv.split(':');
  70. ac->setAddress(QHostAddress(parms.at(0)), parms.at(1).toUShort());
  71. ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toAscii().data());
  72. ac->client()->setName(Settings::globalInstance()->botName().toAscii().data());
  73. ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
  74. ac->client()->setPing(Settings::globalInstance()->botPing());
  75. ac->client()->setColor(Settings::globalInstance()->botTopColor(), Settings::globalInstance()->botBottomColor());
  76. myClients.push_back(ac);
  77. }
  78. }
  79. void App::saveServerList()
  80. {
  81. QStringList list;
  82. ActiveClient* ac;
  83. foreach(ac, myClients)
  84. list.push_back(ac->serverAddressString());
  85. Settings::globalInstance()->setServerList(list);
  86. }
  87. void App::parseAddClientCommand(const QString &args)
  88. {
  89. if(!args.size())
  90. {
  91. print("No server specified\n");
  92. return;
  93. }
  94. if(args.contains(" "))
  95. {
  96. print("Invalid server address\n");
  97. return;
  98. }
  99. QStringList clientData = args.split(":");
  100. if(clientData.size() == 1)
  101. {
  102. addClient(clientData.at(0), 27500);
  103. return;
  104. }
  105. if(clientData.size() == 2)
  106. {
  107. addClient(clientData.at(0), clientData.at(1).toUShort());
  108. return;
  109. }
  110. }
  111. void App::parseRemoveClientCommand(const QString &args)
  112. {
  113. if(!args.size())
  114. {
  115. print("No server specified\n");
  116. return;
  117. }
  118. QStringList clientData = args.split(":");
  119. if(clientData.size() == 1)
  120. {
  121. removeClient(clientData.at(0), 27500);
  122. return;
  123. }
  124. if(clientData.size() == 2)
  125. {
  126. removeClient(clientData.at(0), clientData.at(1).toUShort());
  127. return;
  128. }
  129. }
  130. void App::onDataArrival()
  131. {
  132. while(mySocket->canReadLine())
  133. {
  134. QByteArray line;
  135. line = mySocket->readLine();
  136. QString data(line);
  137. QRegExp regex("([0-9a-zA-Z]+)\\s+([a-z]+)\\s+(.*)");
  138. if(regex.indexIn(data) == -1)
  139. {
  140. print("command format: <password> <command> ?<arguments>\nEg.: pss help\nDisconnected\n");
  141. mySocket->disconnectFromHost();
  142. return;
  143. }
  144. QString pass = regex.capturedTexts().at(1);
  145. if(!checkPassword(pass))
  146. {
  147. print("Wrong password\nDisconnected\n");
  148. mySocket->disconnectFromHost();
  149. return;
  150. }
  151. QString cmd = regex.capturedTexts().at(2);
  152. QString args = regex.capturedTexts().at(3).trimmed();
  153. if(cmd == "add")
  154. {
  155. parseAddClientCommand(args);
  156. return;
  157. }
  158. if(cmd == "remove")
  159. {
  160. parseRemoveClientCommand(args);
  161. return;
  162. }
  163. if(cmd == "say")
  164. {
  165. broadcast(args);
  166. return;
  167. }
  168. if(cmd == "servers")
  169. {
  170. listClients();
  171. return;
  172. }
  173. if(cmd == "name")
  174. {
  175. setNick(args);
  176. return;
  177. }
  178. if(cmd == "color")
  179. {
  180. setColor(args);
  181. return;
  182. }
  183. if(cmd == "setping")
  184. {
  185. setPing(args);
  186. return;
  187. }
  188. if(cmd == "team")
  189. {
  190. setTeam(args);
  191. return;
  192. }
  193. if(cmd == "quit")
  194. {
  195. mySocket->disconnectFromHost();
  196. return;
  197. }
  198. if(cmd == "help")
  199. {
  200. help();
  201. return;
  202. }
  203. }
  204. }
  205. void App::help()
  206. {
  207. print("connect server:port -> connects the bot on a server\n");
  208. print("disconnect server:port -> removes the bot from a server\n");
  209. print("say message -> says the message on all servers where the bot is connected\n");
  210. print("servers -> lists all servers the bot is connected\n");
  211. print("name nick -> changes the bot name to nick\n");
  212. print("color x x -> changes the player color\n");
  213. print("setping x -> sets the bot ping to x. ofc you can't lower your actual ping with this.\n");
  214. print("team teamname -> sets the bot team\n");
  215. print("help -> displays this message\n");
  216. }
  217. void App::setTeam(const QString &args)
  218. {
  219. ActiveClient* ac;
  220. foreach(ac, myClients)
  221. {
  222. ac->client()->setTeam(args);
  223. }
  224. print("Team changed.\n");
  225. }
  226. void App::setColor(const QString &args)
  227. {
  228. ActiveClient* ac;
  229. quint8 bottom, top;
  230. QStringList colors = args.split(" ");
  231. if(colors.size() < 2)
  232. return;
  233. bottom = colors.at(0).toUShort();
  234. top = colors.at(1).toUShort();
  235. foreach(ac, myClients)
  236. {
  237. ac->client()->setColor(bottom, top);
  238. }
  239. print("All clients colors have changed.\n");
  240. }
  241. void App::setPing(const QString &args)
  242. {
  243. ActiveClient* ac;
  244. foreach(ac, myClients)
  245. {
  246. ac->client()->setPing(args.toInt());
  247. }
  248. print("All clients pings have changed.\n");
  249. }
  250. void App::setNick(const QString &args)
  251. {
  252. ActiveClient* ac;
  253. foreach(ac, myClients)
  254. {
  255. ac->client()->setName(args.toAscii().data());
  256. }
  257. print("All clients nicks have changed.\n");
  258. }
  259. bool App::checkPassword(const QString &password)
  260. {
  261. if(QCryptographicHash::hash(password.toAscii(), QCryptographicHash::Md4).toHex().toLower() == "bf4df9f74d05c50ea00492224fb02854")
  262. return true;
  263. return false;
  264. }
  265. void App::print(const QString &msg)
  266. {
  267. printf("%s", msg.toAscii().data());
  268. if(mySocketConnectedFlag)
  269. {
  270. mySocket->write(msg.toAscii());
  271. mySocket->waitForBytesWritten();
  272. }
  273. }
  274. void App::addClient(const QString &host, quint16 port)
  275. {
  276. ActiveClient* ac;
  277. QHostAddress ha = QHostInfo::fromName(host).addresses().at(0);
  278. foreach(ac, myClients)
  279. {
  280. if(QString(ac->client()->host()) == ha.toString() && ac->client()->port() == port)
  281. {
  282. print("That client is already on the list.\n");
  283. return;
  284. }
  285. }
  286. ac = new ActiveClient(this, this);
  287. ac->setAddress(ha, port);
  288. ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toAscii().data());
  289. ac->client()->setName(Settings::globalInstance()->botName().toAscii().data());
  290. ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
  291. ac->client()->setPing(Settings::globalInstance()->botPing());
  292. ac->client()->setColor(Settings::globalInstance()->botTopColor(), Settings::globalInstance()->botBottomColor());
  293. myClients.push_back(ac);
  294. saveServerList();
  295. print("Client added to watch list.\n");
  296. }
  297. void App::removeClient(const QString &host, quint16 port)
  298. {
  299. ActiveClient* ac;
  300. QHostAddress ha = QHostInfo::fromName(host).addresses().at(0);
  301. foreach(ac, myClients)
  302. {
  303. if(ac->serverAddressString() == QString(ha.toString() + ':' + QString::number(port)))
  304. {
  305. ac->client()->disconnect();
  306. delete ac;
  307. myClients.removeAll(ac);
  308. saveServerList();
  309. print("Client removed from watch list.\n");
  310. return;
  311. }
  312. }
  313. print("Client not found on the list.\n");
  314. }
  315. void App::listClients()
  316. {
  317. ActiveClient* ac;
  318. foreach(ac, myClients)
  319. {
  320. print(QString(ac->serverAddressString() + '\n'));
  321. }
  322. }
  323. void App::activeClientsReplyCounters(int *serverCount, int *playerCount)
  324. {
  325. ActiveClient* ac;
  326. foreach(ac, myClients)
  327. {
  328. if(ac->client()->state() == Client::ConnectedState)
  329. {
  330. int pc = ac->playerCount();
  331. if(pc == 255 || pc == 0)
  332. pc = 0;
  333. else
  334. pc--;
  335. *playerCount += pc;
  336. (*serverCount)++;
  337. }
  338. }
  339. }
  340. void App::timerEvent(QTimerEvent *e)
  341. {
  342. if(e->timerId() == myClientsFrameTimerID)
  343. {
  344. ActiveClient *ac;
  345. foreach(ac, myClients)
  346. {
  347. ac->run();
  348. }
  349. return;
  350. }
  351. }
  352. void App::requestBroadcast(const QString &type, const QString &user, const QString &server, const QString &message)
  353. {
  354. myQWNETSshClient->write("REQ_BC QDEV,-" + type + "-,qw://" + server + ",'" + user + "','" + message + "'\n");
  355. }
  356. void App::broadcast(const QString &msg, ActiveClient* ignoredClient)
  357. {
  358. ActiveClient* ac;
  359. foreach(ac, myClients)
  360. {
  361. if(ac == ignoredClient)
  362. continue;
  363. if(ac->client()->state() == Client::ConnectedState && !ac->client()->isMuted())
  364. ac->client()->say(msg);
  365. }
  366. }
  367. void App::broadcast(const QString &msg, int *serverCount, int *userCount)
  368. {
  369. ActiveClient* ac;
  370. *serverCount = 0;
  371. *userCount = 0;
  372. foreach(ac, myClients)
  373. {
  374. if(ac->client()->state() == Client::ConnectedState && !ac->client()->isMuted())
  375. {
  376. ac->client()->say(msg);
  377. *userCount += ac->playerCount() - 1;
  378. (*serverCount)++;
  379. }
  380. }
  381. }
  382. void App::cleanup()
  383. {
  384. ActiveClient* ac;
  385. foreach(ac, myClients)
  386. {
  387. ac->client()->disconnect();
  388. delete ac;
  389. }
  390. }
  391. void App::setReplyHash(const QString &serverAddress, const QString &hash)
  392. {
  393. ActiveClient* ac;
  394. foreach(ac, myClients)
  395. {
  396. if(serverAddress == ac->serverAddressString())
  397. {
  398. ac->setReplyHash(hash);
  399. return;
  400. }
  401. }
  402. }
  403. void App::incrementReplyCounters(const QString &hash, int userCount, int channelCount, int playerCount, int serverCount)
  404. {
  405. ActiveClient* ac;
  406. foreach(ac, myClients)
  407. {
  408. if(ac->replyHash() == hash)
  409. {
  410. ac->incrementReplyCounters(userCount, channelCount, playerCount, serverCount);
  411. return;
  412. }
  413. }
  414. }