App.cpp 15 KB

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