App.cpp 11 KB

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