App.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. mySshClient(new SshClient(this))
  52. {
  53. if(!parseCommandLine())
  54. {
  55. QTimer::singleShot(0, this, SLOT(quit()));
  56. return;
  57. }
  58. print("CMP Bot Service v0.13\n========================================================\n");
  59. setApplicationName("CMP QWBOT");
  60. setOrganizationDomain("https://gogs.netdome.biz/community-messaging-project/qwbot");
  61. setOrganizationName("CMP");
  62. setApplicationVersion("0.13");
  63. myClientsFrameTimerID = startTimer(0);
  64. loadServerList();
  65. print("Connecting to central...\n");
  66. connect(mySshClient, SIGNAL(connected()), SLOT(onCentralConnection()));
  67. mySshClient->connectToHost(Settings::globalInstance()->sshUserName(), Settings::globalInstance()->sshHostName(), Settings::globalInstance()->sshPort());
  68. Settings::globalInstance()->save();
  69. }
  70. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  71. App::~App()
  72. {
  73. Settings::globalInstance()->save();
  74. cleanup();
  75. delete myServer;
  76. }
  77. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  78. bool App::parseCommandLine()
  79. {
  80. if(arguments().size() < 2)
  81. return true;
  82. QStringList args = App::arguments();
  83. QString arg;
  84. QStringList::const_iterator itr;
  85. for(itr = args.constBegin()+1; itr != args.constEnd(); ++itr)
  86. {
  87. arg = *itr;
  88. if(arg == "--help" || arg == "-h")
  89. {
  90. printf("Usage: %s [--config/-c config_file] [-h]\n", args.at(0).section("/", -1).toLatin1().data());
  91. return false;
  92. }
  93. else if(arg == "--config" || arg == "-c")
  94. {
  95. itr++;
  96. if(itr == args.constEnd())
  97. {
  98. printf("--config: Expected config file path.\n");
  99. return false;
  100. }
  101. arg = *itr;
  102. printf("Using config file [%s]...\n", arg.toLatin1().data());
  103. Settings::globalInstance()->changeConfigFile(*itr);
  104. return true;
  105. }
  106. }
  107. return true;
  108. }
  109. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  110. void App::loadServerList()
  111. {
  112. Settings::ServerList list = Settings::globalInstance()->serverList();
  113. Settings::Server sv;
  114. foreach(sv, list)
  115. {
  116. if(myClients.size() == Settings::globalInstance()->maxServers())
  117. {
  118. print("Maximum number of servers allowed reached, some servers weren't added to the monitoring list.\n");
  119. return;
  120. }
  121. addClient(sv.address, sv.port, sv.password);
  122. }
  123. }
  124. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  125. void App::saveServerList()
  126. {
  127. Settings::ServerList list;
  128. ActiveClient* ac;
  129. foreach(ac, myClients)
  130. {
  131. Settings::Server sv;
  132. sv.address = ac->serverHostNameString().split(":").at(0);
  133. sv.port = ac->serverHostNameString().split(":").at(1).toUShort();
  134. list.push_back(sv);
  135. }
  136. Settings::globalInstance()->setServerList(list);
  137. }
  138. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  139. const QStringList& App::lastMessages() const
  140. {
  141. return myLastMessages;
  142. }
  143. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  144. void App::print(const QString &msg)
  145. {
  146. printf("%s", msg.toLatin1().data());
  147. if(mySocketConnectedFlag)
  148. {
  149. mySocket->write(msg.toLatin1());
  150. mySocket->waitForBytesWritten();
  151. }
  152. }
  153. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  154. bool App::addClient(const QString &hostName, quint16 port, const QString& password)
  155. {
  156. QHostInfo hi = QHostInfo::fromName(hostName);
  157. if (hi.error() != QHostInfo::NoError) {
  158. print("Couldn't add server " + hostName + ": " + hi.errorString());
  159. return false;
  160. }
  161. QHostAddress hostAddress = hi.addresses().first();
  162. QString fullAddress = hostAddress.toString() + ":" + QString::number(port);
  163. ActiveClient* ac;
  164. foreach(ac, myClients)
  165. {
  166. if(ac->serverAddressString() == fullAddress)
  167. {
  168. print("That client is already on the list [" + fullAddress + "]\n");
  169. return false;
  170. }
  171. }
  172. ac = new ActiveClient(this, password, this);
  173. ac->setAddress(hostName, hostAddress, port);
  174. ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toLatin1().data());
  175. ac->client()->setName(Settings::globalInstance()->botName().toLatin1().data());
  176. ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
  177. ac->client()->setPing(Settings::globalInstance()->botPing());
  178. ac->client()->setColor(Settings::globalInstance()->botTopColor(), Settings::globalInstance()->botBottomColor());
  179. myClients.push_back(ac);
  180. saveServerList();
  181. print("Client added to watch list [" + fullAddress + "]\n");
  182. return true;
  183. }
  184. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  185. bool App::removeClient(const QString &hostName, quint16 port)
  186. {
  187. QHostInfo hi = QHostInfo::fromName(hostName);
  188. if (hi.error() != QHostInfo::NoError) {
  189. print("Couldn't add server " + hostName + ": " + hi.errorString());
  190. return false;
  191. }
  192. QHostAddress hostAddress = hi.addresses().first();
  193. QString fullAddress = hostAddress.toString() + ":" + QString::number(port);
  194. ActiveClient* ac;
  195. foreach(ac, myClients)
  196. {
  197. if(ac->serverAddressString() == fullAddress)
  198. {
  199. ac->client()->disconnect();
  200. delete ac;
  201. myClients.removeAll(ac);
  202. saveServerList();
  203. print("Client removed from watch list [" + fullAddress + "]\n");
  204. return true;
  205. }
  206. }
  207. print("Client not found on the list [" + fullAddress + "]\n");
  208. return false;
  209. }
  210. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  211. void App::activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveClient *ignoreClient)
  212. {
  213. ActiveClient* ac;
  214. foreach(ac, myClients)
  215. {
  216. if(ac == ignoreClient)
  217. continue;
  218. if(ac->client()->state() == Client::ConnectedState)
  219. {
  220. int pc = ac->playerCount();
  221. if(pc == 255 || pc == 0)
  222. pc = 0;
  223. else
  224. pc--;
  225. *playerCount += pc;
  226. (*serverCount)++;
  227. }
  228. }
  229. }
  230. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  231. void App::timerEvent(QTimerEvent *e)
  232. {
  233. if(e->timerId() == myClientsFrameTimerID)
  234. {
  235. ActiveClient *ac;
  236. foreach(ac, myClients)
  237. {
  238. ac->run();
  239. }
  240. // HostNames scheduled daily refresh
  241. int currentHour = QTime::currentTime().hour();
  242. int refreshHour = Settings::globalInstance()->refreshHostNamesHour();
  243. if(currentHour == refreshHour && !myHostNamesRequested)
  244. {
  245. print("Refreshing hostname cache...\n");
  246. requestCachedHostNames();
  247. myHostNamesRequested = true;
  248. }
  249. else if(currentHour != refreshHour && myHostNamesRequested)
  250. {
  251. myHostNamesRequested = false;
  252. }
  253. }
  254. Sleeper::msleep(1);
  255. }
  256. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  257. void App::requestBroadcast(const QString &type, const QString &user, const QString &server, const QString &message)
  258. {
  259. if(!Settings::globalInstance()->developerMode())
  260. mySshClient->write("REQ_BC QWalt,-" + type + "-,qw://" + server + ",'" + user + "','" + message + "'\n");
  261. else
  262. mySshClient->write("REQ_BC QDEV,-dev-,qw://" + server + ",'" + user + "','" + message + "'\n");
  263. }
  264. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  265. void App::addMessageToHistory(const QString &msg)
  266. {
  267. myLastMessages.push_back(msg);
  268. if(myLastMessages.size() > 5)
  269. myLastMessages.removeAt(0);
  270. }
  271. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  272. void App::broadcast(const QString &msg, ActiveClient* ignoredClient)
  273. {
  274. ActiveClient* ac;
  275. QString frequency = msg.section(' ', 0, 0);
  276. addMessageToHistory(msg);
  277. foreach(ac, myClients)
  278. {
  279. if(ac == ignoredClient)
  280. continue;
  281. if((frequency == "-qw-" && ac->client()->isQWMuted()) || (frequency == "-spam-" && ac->client()->isSpamMuted()))
  282. continue;
  283. if(ac->client()->state() == Client::ConnectedState)
  284. ac->client()->say(msg);
  285. }
  286. }
  287. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  288. void App::broadcast(const QString &msg, int *serverCount, int *userCount)
  289. {
  290. ActiveClient* ac;
  291. *serverCount = 0;
  292. *userCount = 0;
  293. QString frequency = msg.section(' ', 0, 0);
  294. addMessageToHistory(msg);
  295. foreach(ac, myClients)
  296. {
  297. if((frequency == "-qw-" && ac->client()->isQWMuted()) || (frequency == "-spam-" && ac->client()->isSpamMuted()))
  298. continue;
  299. if(ac->client()->state() == Client::ConnectedState)
  300. {
  301. ac->client()->say(msg);
  302. *userCount += ac->playerCount() - 1;
  303. (*serverCount)++;
  304. }
  305. }
  306. }
  307. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  308. void App::cleanup()
  309. {
  310. ActiveClient* ac;
  311. foreach(ac, myClients)
  312. {
  313. ac->client()->disconnect();
  314. delete ac;
  315. }
  316. }
  317. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  318. void App::setReplyHashAndWaitForReply(const QString &serverAddress, const QString &hash)
  319. {
  320. ActiveClient* ac;
  321. foreach(ac, myClients)
  322. {
  323. if(serverAddress == ac->serverAddressString())
  324. {
  325. ac->setReplyHashAndWaitForReply(hash);
  326. return;
  327. }
  328. }
  329. }
  330. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  331. const QList<ActiveClient*> App::clients() const {
  332. return myClients;
  333. }
  334. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  335. void App::incrementReplyCounters(const QString &hash, int userCount, int channelCount, int playerCount, int serverCount)
  336. {
  337. ActiveClient* ac;
  338. foreach(ac, myClients)
  339. {
  340. if(ac->replyHash() == hash)
  341. {
  342. ac->incrementReplyCounters(userCount, channelCount, playerCount, serverCount);
  343. return;
  344. }
  345. }
  346. }
  347. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  348. void App::requestCachedHostNames()
  349. {
  350. ActiveClient* ac;
  351. foreach(ac, myClients)
  352. {
  353. if (!ac->hasHostName()) {
  354. print("Requested HostName for server " + ac->serverAddressString() + "\n");
  355. mySshClient->write("REQ_DNS " + ac->serverAddressString() + "\n");
  356. }
  357. }
  358. }
  359. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  360. void App::setServerHostName(const QString &serverAddress, const QString &hostName)
  361. {
  362. ActiveClient* ac;
  363. foreach(ac, myClients)
  364. {
  365. if(ac->serverAddressString() == serverAddress)
  366. {
  367. if (!ac->hasHostName() && ac->hostName() != hostName) {
  368. print("HostName for " + serverAddress + " set to " + hostName + "\n");
  369. ac->setHostName(hostName);
  370. }
  371. return;
  372. }
  373. }
  374. }
  375. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  376. QString App::serverHostName(const QString &serverAddress) const
  377. {
  378. ActiveClient* ac;
  379. foreach(ac, myClients)
  380. {
  381. if(ac->serverAddressString() == serverAddress)
  382. {
  383. return ac->hostName();
  384. }
  385. }
  386. return QString();
  387. }
  388. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  389. void App::onCentralConnection()
  390. {
  391. print("Connected!\nRefreshing cached hostnames...\n");
  392. requestCachedHostNames();
  393. }