App.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #include "App.h"
  16. #include "Client.h"
  17. #include <QStringList>
  18. #include <QTcpSocket>
  19. #include <QTcpServer>
  20. #include <QRegExp>
  21. #include <QHostInfo>
  22. #include <QTimerEvent>
  23. #include <QDebug>
  24. #include <QtSql/QSqlQuery>
  25. #include <QCryptographicHash>
  26. #include <QHostAddress>
  27. #include <QThread>
  28. #include <QTimer>
  29. #include "SshClient.h"
  30. #include "ActiveClient.h"
  31. #include "Settings.h"
  32. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  33. /**
  34. Used just to expose the msleep function
  35. from QThread
  36. @author Mihawk <luiz@netdome.biz>
  37. */
  38. class Sleeper: public QThread
  39. {
  40. public:
  41. static void msleep(unsigned long msecs)
  42. {
  43. QThread::msleep(msecs);
  44. }
  45. };
  46. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  47. App::App(int &argc, char **argv) :
  48. QCoreApplication(argc, argv),
  49. myServer(new QTcpServer()),
  50. mySocketConnectedFlag(false),
  51. myQWNETSshClient(new SshClient(this))
  52. {
  53. if(!parseCommandLine())
  54. {
  55. QTimer::singleShot(0, this, SLOT(quit()));
  56. return;
  57. }
  58. print("CIMS Bot Service v0.101\n========================================================\n");
  59. setApplicationName("CIMSBOT");
  60. setOrganizationDomain("http://redmine.b4r.org/projects/cimsqwbot/");
  61. setOrganizationName("CIMS");
  62. setApplicationVersion("0.101");
  63. myClientsFrameTimerID = startTimer(0);
  64. myQWNETSshClient->connectToHost(Settings::globalInstance()->sshUserName(), Settings::globalInstance()->sshHostName());
  65. loadServerList();
  66. Settings::globalInstance()->save();
  67. }
  68. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  69. App::~App()
  70. {
  71. Settings::globalInstance()->save();
  72. cleanup();
  73. delete myServer;
  74. }
  75. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  76. bool App::parseCommandLine()
  77. {
  78. if(argc() < 2)
  79. return true;
  80. QStringList args = App::arguments();
  81. QString arg;
  82. QStringList::const_iterator itr;
  83. for(itr = args.constBegin()+1; itr != args.constEnd(); ++itr)
  84. {
  85. arg = *itr;
  86. if(arg == "--help" || arg == "-h")
  87. {
  88. printf("Usage: %s [--config/-c config_file] [-h]\n", args.at(0).section("/", -1).toAscii().data());
  89. return false;
  90. }
  91. else if(arg == "--config" || arg == "-c")
  92. {
  93. itr++;
  94. if(itr == args.constEnd())
  95. {
  96. printf("--config: Expected config file path.\n");
  97. return false;
  98. }
  99. arg = *itr;
  100. printf("Using config file [%s]...\n", arg.toAscii().data());
  101. Settings::globalInstance()->changeConfigFile(*itr);
  102. return true;
  103. }
  104. }
  105. return true;
  106. }
  107. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  108. void App::onNewConnection()
  109. {
  110. if(mySocketConnectedFlag)
  111. {
  112. print("Someone just connected to the bot.\nBye Bye.\n");
  113. mySocket->disconnectFromHost();
  114. if(mySocket->state() != QTcpSocket::UnconnectedState)
  115. mySocket->waitForDisconnected();
  116. mySocketConnectedFlag = false;
  117. }
  118. mySocket = myServer->nextPendingConnection();
  119. mySocketConnectedFlag = true;
  120. connect(mySocket, SIGNAL(readyRead()), SLOT(onDataArrival()));
  121. connect(mySocket, SIGNAL(disconnected()), SLOT(onDisconnection()));
  122. print("Connected to CIMS's bot service.\n");
  123. }
  124. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  125. void App::onDisconnection()
  126. {
  127. mySocketConnectedFlag = false;
  128. mySocket->deleteLater();
  129. }
  130. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  131. void App::loadServerList()
  132. {
  133. Settings::ServerList list = Settings::globalInstance()->serverList();
  134. Settings::Server sv;
  135. foreach(sv, list)
  136. {
  137. ActiveClient* ac = new ActiveClient(this, sv.supportsSendPrivate);
  138. ac->setAddress(QHostAddress(sv.address), sv.port);
  139. ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toAscii().data());
  140. ac->client()->setName(Settings::globalInstance()->botName().toAscii().data());
  141. ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
  142. ac->client()->setPing(Settings::globalInstance()->botPing());
  143. ac->client()->setColor(Settings::globalInstance()->botTopColor(), Settings::globalInstance()->botBottomColor());
  144. myClients.push_back(ac);
  145. }
  146. }
  147. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  148. void App::saveServerList()
  149. {
  150. Settings::ServerList list;
  151. ActiveClient* ac;
  152. foreach(ac, myClients)
  153. {
  154. Settings::Server sv;
  155. sv.address = ac->serverAddressString().split(":").at(0);
  156. sv.port = ac->serverAddressString().split(":").at(1).toUShort();
  157. sv.supportsSendPrivate = ac->client()->supportsSendPrivate();
  158. list.push_back(sv);
  159. }
  160. Settings::globalInstance()->setServerList(list);
  161. }
  162. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  163. void App::parseAddClientCommand(const QString &args)
  164. {
  165. if(!args.size())
  166. {
  167. print("No server specified\n");
  168. return;
  169. }
  170. if(args.contains(" "))
  171. {
  172. print("Invalid server address\n");
  173. return;
  174. }
  175. QStringList clientData = args.split(":");
  176. if(clientData.size() == 1)
  177. {
  178. addClient(clientData.at(0), 27500);
  179. return;
  180. }
  181. if(clientData.size() == 2)
  182. {
  183. addClient(clientData.at(0), clientData.at(1).toUShort());
  184. return;
  185. }
  186. }
  187. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  188. void App::parseRemoveClientCommand(const QString &args)
  189. {
  190. if(!args.size())
  191. {
  192. print("No server specified\n");
  193. return;
  194. }
  195. QStringList clientData = args.split(":");
  196. if(clientData.size() == 1)
  197. {
  198. removeClient(clientData.at(0), 27500);
  199. return;
  200. }
  201. if(clientData.size() == 2)
  202. {
  203. removeClient(clientData.at(0), clientData.at(1).toUShort());
  204. return;
  205. }
  206. }
  207. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  208. void App::onDataArrival()
  209. {
  210. while(mySocket->canReadLine())
  211. {
  212. QByteArray line;
  213. line = mySocket->readLine();
  214. QString data(line);
  215. QRegExp regex("([0-9a-zA-Z]+)\\s+([a-z]+)\\s+(.*)");
  216. if(regex.indexIn(data) == -1)
  217. {
  218. print("command format: <password> <command> ?<arguments>\nEg.: pss help\nDisconnected\n");
  219. mySocket->disconnectFromHost();
  220. return;
  221. }
  222. QString pass = regex.capturedTexts().at(1);
  223. if(!checkPassword(pass))
  224. {
  225. print("Wrong password\nDisconnected\n");
  226. mySocket->disconnectFromHost();
  227. return;
  228. }
  229. QString cmd = regex.capturedTexts().at(2);
  230. QString args = regex.capturedTexts().at(3).trimmed();
  231. if(cmd == "add")
  232. {
  233. parseAddClientCommand(args);
  234. return;
  235. }
  236. if(cmd == "remove")
  237. {
  238. parseRemoveClientCommand(args);
  239. return;
  240. }
  241. if(cmd == "say")
  242. {
  243. broadcast(args);
  244. return;
  245. }
  246. if(cmd == "servers")
  247. {
  248. listClients();
  249. return;
  250. }
  251. if(cmd == "name")
  252. {
  253. setNick(args);
  254. return;
  255. }
  256. if(cmd == "color")
  257. {
  258. setColor(args);
  259. return;
  260. }
  261. if(cmd == "setping")
  262. {
  263. setPing(args);
  264. return;
  265. }
  266. if(cmd == "team")
  267. {
  268. setTeam(args);
  269. return;
  270. }
  271. if(cmd == "quit")
  272. {
  273. mySocket->disconnectFromHost();
  274. return;
  275. }
  276. if(cmd == "help")
  277. {
  278. help();
  279. return;
  280. }
  281. }
  282. }
  283. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  284. const QStringList& App::lastMessages() const
  285. {
  286. return myLastMessages;
  287. }
  288. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  289. void App::help()
  290. {
  291. print("connect server:port -> connects the bot on a server\n");
  292. print("disconnect server:port -> removes the bot from a server\n");
  293. print("say message -> says the message on all servers where the bot is connected\n");
  294. print("servers -> lists all servers the bot is connected\n");
  295. print("name nick -> changes the bot name to nick\n");
  296. print("color x x -> changes the player color\n");
  297. print("setping x -> sets the bot ping to x. ofc you can't lower your actual ping with this.\n");
  298. print("team teamname -> sets the bot team\n");
  299. print("help -> displays this message\n");
  300. }
  301. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  302. void App::setTeam(const QString &args)
  303. {
  304. ActiveClient* ac;
  305. foreach(ac, myClients)
  306. {
  307. ac->client()->setTeam(args);
  308. }
  309. print("Team changed.\n");
  310. }
  311. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  312. void App::setColor(const QString &args)
  313. {
  314. ActiveClient* ac;
  315. quint8 bottom, top;
  316. QStringList colors = args.split(" ");
  317. if(colors.size() < 2)
  318. return;
  319. bottom = colors.at(0).toUShort();
  320. top = colors.at(1).toUShort();
  321. foreach(ac, myClients)
  322. {
  323. ac->client()->setColor(bottom, top);
  324. }
  325. print("All clients colors have changed.\n");
  326. }
  327. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  328. void App::setPing(const QString &args)
  329. {
  330. ActiveClient* ac;
  331. foreach(ac, myClients)
  332. {
  333. ac->client()->setPing(args.toInt());
  334. }
  335. print("All clients pings have changed.\n");
  336. }
  337. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  338. void App::setNick(const QString &args)
  339. {
  340. ActiveClient* ac;
  341. foreach(ac, myClients)
  342. {
  343. ac->client()->setName(args.toAscii().data());
  344. }
  345. print("All clients nicks have changed.\n");
  346. }
  347. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  348. bool App::checkPassword(const QString &password)
  349. {
  350. if(QCryptographicHash::hash(password.toAscii(), QCryptographicHash::Md4).toHex().toLower() == "bf4df9f74d05c50ea00492224fb02854")
  351. return false; //telnet adm disabled!!
  352. return false;
  353. }
  354. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  355. void App::print(const QString &msg)
  356. {
  357. printf("%s", msg.toAscii().data());
  358. if(mySocketConnectedFlag)
  359. {
  360. mySocket->write(msg.toAscii());
  361. mySocket->waitForBytesWritten();
  362. }
  363. }
  364. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  365. void App::addClient(const QString &host, quint16 port)
  366. {
  367. ActiveClient* ac;
  368. QHostAddress ha = QHostInfo::fromName(host).addresses().at(0);
  369. foreach(ac, myClients)
  370. {
  371. if(QString(ac->client()->host()) == ha.toString() && ac->client()->port() == port)
  372. {
  373. print("That client is already on the list.\n");
  374. return;
  375. }
  376. }
  377. ac = new ActiveClient(this, this);
  378. ac->setAddress(ha, port);
  379. ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toAscii().data());
  380. ac->client()->setName(Settings::globalInstance()->botName().toAscii().data());
  381. ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
  382. ac->client()->setPing(Settings::globalInstance()->botPing());
  383. ac->client()->setColor(Settings::globalInstance()->botTopColor(), Settings::globalInstance()->botBottomColor());
  384. myClients.push_back(ac);
  385. saveServerList();
  386. print("Client added to watch list.\n");
  387. }
  388. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  389. void App::removeClient(const QString &host, quint16 port)
  390. {
  391. ActiveClient* ac;
  392. QHostAddress ha = QHostInfo::fromName(host).addresses().at(0);
  393. foreach(ac, myClients)
  394. {
  395. if(ac->serverAddressString() == QString(ha.toString() + ':' + QString::number(port)))
  396. {
  397. ac->client()->disconnect();
  398. delete ac;
  399. myClients.removeAll(ac);
  400. saveServerList();
  401. print("Client removed from watch list.\n");
  402. return;
  403. }
  404. }
  405. print("Client not found on the list.\n");
  406. }
  407. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  408. void App::listClients()
  409. {
  410. ActiveClient* ac;
  411. foreach(ac, myClients)
  412. {
  413. print(QString(ac->serverAddressString() + '\n'));
  414. }
  415. }
  416. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  417. void App::activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveClient *ignoreClient)
  418. {
  419. ActiveClient* ac;
  420. foreach(ac, myClients)
  421. {
  422. if(ac == ignoreClient)
  423. continue;
  424. if(ac->client()->state() == Client::ConnectedState)
  425. {
  426. int pc = ac->playerCount();
  427. if(pc == 255 || pc == 0)
  428. pc = 0;
  429. else
  430. pc--;
  431. *playerCount += pc;
  432. (*serverCount)++;
  433. }
  434. }
  435. }
  436. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  437. void App::timerEvent(QTimerEvent *e)
  438. {
  439. if(e->timerId() == myClientsFrameTimerID)
  440. {
  441. ActiveClient *ac;
  442. foreach(ac, myClients)
  443. {
  444. ac->run();
  445. }
  446. }
  447. Sleeper::msleep(1);
  448. }
  449. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  450. void App::requestBroadcast(const QString &type, const QString &user, const QString &server, const QString &message)
  451. {
  452. if(!Settings::globalInstance()->developerMode())
  453. myQWNETSshClient->write("REQ_BC QWalt,-" + type + "-,qw://" + server + ",'" + user + "','" + message + "'\n");
  454. else
  455. myQWNETSshClient->write("REQ_BC QDEV,-dev-,qw://" + server + ",'" + user + "','" + message + "'\n");
  456. }
  457. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  458. void App::addMessageToHistory(const QString &msg)
  459. {
  460. myLastMessages.push_back(msg);
  461. if(myLastMessages.size() > 5)
  462. myLastMessages.removeAt(0);
  463. }
  464. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  465. void App::broadcast(const QString &msg, ActiveClient* ignoredClient)
  466. {
  467. ActiveClient* ac;
  468. QString frequency = msg.section(' ', 0, 0);
  469. addMessageToHistory(msg);
  470. foreach(ac, myClients)
  471. {
  472. if(ac == ignoredClient)
  473. continue;
  474. if((frequency == "-qw-" && ac->client()->isQWMuted()) || (frequency == "-spam-" && ac->client()->isSpamMuted()))
  475. continue;
  476. if(ac->client()->state() == Client::ConnectedState)
  477. ac->client()->say(msg);
  478. }
  479. }
  480. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  481. void App::broadcast(const QString &msg, int *serverCount, int *userCount)
  482. {
  483. ActiveClient* ac;
  484. *serverCount = 0;
  485. *userCount = 0;
  486. QString frequency = msg.section(' ', 0, 0);
  487. addMessageToHistory(msg);
  488. foreach(ac, myClients)
  489. {
  490. if((frequency == "-qw-" && ac->client()->isQWMuted()) || (frequency == "-spam-" && ac->client()->isSpamMuted()))
  491. continue;
  492. if(ac->client()->state() == Client::ConnectedState)
  493. {
  494. ac->client()->say(msg);
  495. *userCount += ac->playerCount() - 1;
  496. (*serverCount)++;
  497. }
  498. }
  499. }
  500. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  501. void App::cleanup()
  502. {
  503. ActiveClient* ac;
  504. foreach(ac, myClients)
  505. {
  506. ac->client()->disconnect();
  507. delete ac;
  508. }
  509. }
  510. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  511. void App::setReplyHashAndWaitForReply(const QString &serverAddress, const QString &hash)
  512. {
  513. ActiveClient* ac;
  514. foreach(ac, myClients)
  515. {
  516. if(serverAddress == ac->serverAddressString())
  517. {
  518. ac->setReplyHashAndWaitForReply(hash);
  519. return;
  520. }
  521. }
  522. }
  523. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  524. void App::incrementReplyCounters(const QString &hash, int userCount, int channelCount, int playerCount, int serverCount)
  525. {
  526. ActiveClient* ac;
  527. foreach(ac, myClients)
  528. {
  529. if(ac->replyHash() == hash)
  530. {
  531. ac->incrementReplyCounters(userCount, channelCount, playerCount, serverCount);
  532. return;
  533. }
  534. }
  535. }