App.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. const QStringList& App::lastMessages() const
  205. {
  206. return myLastMessages;
  207. }
  208. void App::help()
  209. {
  210. print("connect server:port -> connects the bot on a server\n");
  211. print("disconnect server:port -> removes the bot from a server\n");
  212. print("say message -> says the message on all servers where the bot is connected\n");
  213. print("servers -> lists all servers the bot is connected\n");
  214. print("name nick -> changes the bot name to nick\n");
  215. print("color x x -> changes the player color\n");
  216. print("setping x -> sets the bot ping to x. ofc you can't lower your actual ping with this.\n");
  217. print("team teamname -> sets the bot team\n");
  218. print("help -> displays this message\n");
  219. }
  220. void App::setTeam(const QString &args)
  221. {
  222. ActiveClient* ac;
  223. foreach(ac, myClients)
  224. {
  225. ac->client()->setTeam(args);
  226. }
  227. print("Team changed.\n");
  228. }
  229. void App::setColor(const QString &args)
  230. {
  231. ActiveClient* ac;
  232. quint8 bottom, top;
  233. QStringList colors = args.split(" ");
  234. if(colors.size() < 2)
  235. return;
  236. bottom = colors.at(0).toUShort();
  237. top = colors.at(1).toUShort();
  238. foreach(ac, myClients)
  239. {
  240. ac->client()->setColor(bottom, top);
  241. }
  242. print("All clients colors have changed.\n");
  243. }
  244. void App::setPing(const QString &args)
  245. {
  246. ActiveClient* ac;
  247. foreach(ac, myClients)
  248. {
  249. ac->client()->setPing(args.toInt());
  250. }
  251. print("All clients pings have changed.\n");
  252. }
  253. void App::setNick(const QString &args)
  254. {
  255. ActiveClient* ac;
  256. foreach(ac, myClients)
  257. {
  258. ac->client()->setName(args.toAscii().data());
  259. }
  260. print("All clients nicks have changed.\n");
  261. }
  262. bool App::checkPassword(const QString &password)
  263. {
  264. if(QCryptographicHash::hash(password.toAscii(), QCryptographicHash::Md4).toHex().toLower() == "bf4df9f74d05c50ea00492224fb02854")
  265. return false; //telnet adm disabled!!
  266. return false;
  267. }
  268. void App::print(const QString &msg)
  269. {
  270. printf("%s", msg.toAscii().data());
  271. if(mySocketConnectedFlag)
  272. {
  273. mySocket->write(msg.toAscii());
  274. mySocket->waitForBytesWritten();
  275. }
  276. }
  277. void App::addClient(const QString &host, quint16 port)
  278. {
  279. ActiveClient* ac;
  280. QHostAddress ha = QHostInfo::fromName(host).addresses().at(0);
  281. foreach(ac, myClients)
  282. {
  283. if(QString(ac->client()->host()) == ha.toString() && ac->client()->port() == port)
  284. {
  285. print("That client is already on the list.\n");
  286. return;
  287. }
  288. }
  289. ac = new ActiveClient(this, this);
  290. ac->setAddress(ha, port);
  291. ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toAscii().data());
  292. ac->client()->setName(Settings::globalInstance()->botName().toAscii().data());
  293. ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
  294. ac->client()->setPing(Settings::globalInstance()->botPing());
  295. ac->client()->setColor(Settings::globalInstance()->botTopColor(), Settings::globalInstance()->botBottomColor());
  296. myClients.push_back(ac);
  297. saveServerList();
  298. print("Client added to watch list.\n");
  299. }
  300. void App::removeClient(const QString &host, quint16 port)
  301. {
  302. ActiveClient* ac;
  303. QHostAddress ha = QHostInfo::fromName(host).addresses().at(0);
  304. foreach(ac, myClients)
  305. {
  306. if(ac->serverAddressString() == QString(ha.toString() + ':' + QString::number(port)))
  307. {
  308. ac->client()->disconnect();
  309. delete ac;
  310. myClients.removeAll(ac);
  311. saveServerList();
  312. print("Client removed from watch list.\n");
  313. return;
  314. }
  315. }
  316. print("Client not found on the list.\n");
  317. }
  318. void App::listClients()
  319. {
  320. ActiveClient* ac;
  321. foreach(ac, myClients)
  322. {
  323. print(QString(ac->serverAddressString() + '\n'));
  324. }
  325. }
  326. void App::activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveClient *ignoreClient)
  327. {
  328. ActiveClient* ac;
  329. foreach(ac, myClients)
  330. {
  331. if(ac == ignoreClient)
  332. continue;
  333. if(ac->client()->state() == Client::ConnectedState)
  334. {
  335. int pc = ac->playerCount();
  336. if(pc == 255 || pc == 0)
  337. pc = 0;
  338. else
  339. pc--;
  340. *playerCount += pc;
  341. (*serverCount)++;
  342. }
  343. }
  344. }
  345. void App::timerEvent(QTimerEvent *e)
  346. {
  347. if(e->timerId() == myClientsFrameTimerID)
  348. {
  349. ActiveClient *ac;
  350. foreach(ac, myClients)
  351. {
  352. ac->run();
  353. }
  354. return;
  355. }
  356. }
  357. void App::requestBroadcast(const QString &type, const QString &user, const QString &server, const QString &message)
  358. {
  359. if(!Settings::globalInstance()->developerMode())
  360. myQWNETSshClient->write("REQ_BC QWalt,-" + type + "-,qw://" + server + ",'" + user + "','" + message + "'\n");
  361. else
  362. myQWNETSshClient->write("REQ_BC QDEV,-dev-,qw://" + server + ",'" + user + "','" + message + "'\n");
  363. }
  364. void App::addMessageToHistory(const QString &msg)
  365. {
  366. myLastMessages.push_back(msg);
  367. if(myLastMessages.size() > 5)
  368. myLastMessages.removeAt(0);
  369. }
  370. void App::broadcast(const QString &msg, ActiveClient* ignoredClient)
  371. {
  372. ActiveClient* ac;
  373. QString frequency = msg.section(' ', 0, 0);
  374. addMessageToHistory(msg);
  375. foreach(ac, myClients)
  376. {
  377. if(ac == ignoredClient)
  378. continue;
  379. if((frequency == "-qw-" && ac->client()->isQWMuted()) || (frequency == "-spam-" && ac->client()->isSpamMuted()))
  380. continue;
  381. if(ac->client()->state() == Client::ConnectedState)
  382. ac->client()->say(msg);
  383. }
  384. }
  385. void App::broadcast(const QString &msg, int *serverCount, int *userCount)
  386. {
  387. ActiveClient* ac;
  388. *serverCount = 0;
  389. *userCount = 0;
  390. QString frequency = msg.section(' ', 0, 0);
  391. addMessageToHistory(msg);
  392. foreach(ac, myClients)
  393. {
  394. if((frequency == "-qw-" && ac->client()->isQWMuted()) || (frequency == "-spam-" && ac->client()->isSpamMuted()))
  395. continue;
  396. if(ac->client()->state() == Client::ConnectedState)
  397. {
  398. ac->client()->say(msg);
  399. *userCount += ac->playerCount() - 1;
  400. (*serverCount)++;
  401. }
  402. }
  403. }
  404. void App::cleanup()
  405. {
  406. ActiveClient* ac;
  407. foreach(ac, myClients)
  408. {
  409. ac->client()->disconnect();
  410. delete ac;
  411. }
  412. }
  413. void App::setReplyHash(const QString &serverAddress, const QString &hash)
  414. {
  415. ActiveClient* ac;
  416. foreach(ac, myClients)
  417. {
  418. if(serverAddress == ac->serverAddressString())
  419. {
  420. ac->setReplyHash(hash);
  421. return;
  422. }
  423. }
  424. }
  425. void App::incrementReplyCounters(const QString &hash, int userCount, int channelCount, int playerCount, int serverCount)
  426. {
  427. ActiveClient* ac;
  428. foreach(ac, myClients)
  429. {
  430. if(ac->replyHash() == hash)
  431. {
  432. ac->incrementReplyCounters(userCount, channelCount, playerCount, serverCount);
  433. return;
  434. }
  435. }
  436. }