App.cpp 12 KB

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