App.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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("CIMS Bot Service v0.12\n========================================================\n");
  59. setApplicationName("CIMSBOT");
  60. setOrganizationDomain("http://redmine.b4r.org/projects/cimsqwbot/");
  61. setOrganizationName("CIMS");
  62. setApplicationVersion("0.12");
  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(argc() < 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).toAscii().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.toAscii().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->serverAddressString().split(":").at(0);
  133. sv.port = ac->serverAddressString().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.toAscii().data());
  147. if(mySocketConnectedFlag)
  148. {
  149. mySocket->write(msg.toAscii());
  150. mySocket->waitForBytesWritten();
  151. }
  152. }
  153. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  154. bool App::addClient(const QString &host, quint16 port, const QString& password)
  155. {
  156. ActiveClient* ac;
  157. QHostAddress ha(host);
  158. QString fullAddress = host + ":" + QString::number(port);
  159. foreach(ac, myClients)
  160. {
  161. if(ac->serverAddressString() == fullAddress)
  162. {
  163. print("That client is already on the list [" + fullAddress + "]\n");
  164. return false;
  165. }
  166. }
  167. ac = new ActiveClient(this, password, this);
  168. ac->setAddress(ha, port);
  169. ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toAscii().data());
  170. ac->client()->setName(Settings::globalInstance()->botName().toAscii().data());
  171. ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
  172. ac->client()->setPing(Settings::globalInstance()->botPing());
  173. ac->client()->setColor(Settings::globalInstance()->botTopColor(), Settings::globalInstance()->botBottomColor());
  174. myClients.push_back(ac);
  175. saveServerList();
  176. print("Client added to watch list [" + fullAddress + "]\n");
  177. return true;
  178. }
  179. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  180. bool App::removeClient(const QString &host, quint16 port)
  181. {
  182. ActiveClient* ac;
  183. QHostAddress ha(host);
  184. QString fullAddress = host + ":" + QString::number(port);
  185. foreach(ac, myClients)
  186. {
  187. if(ac->serverAddressString() == fullAddress)
  188. {
  189. ac->client()->disconnect();
  190. delete ac;
  191. myClients.removeAll(ac);
  192. saveServerList();
  193. print("Client removed from watch list [" + fullAddress + "]\n");
  194. return true;
  195. }
  196. }
  197. print("Client not found on the list [" + fullAddress + "]\n");
  198. return false;
  199. }
  200. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  201. void App::activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveClient *ignoreClient)
  202. {
  203. ActiveClient* ac;
  204. foreach(ac, myClients)
  205. {
  206. if(ac == ignoreClient)
  207. continue;
  208. if(ac->client()->state() == Client::ConnectedState)
  209. {
  210. int pc = ac->playerCount();
  211. if(pc == 255 || pc == 0)
  212. pc = 0;
  213. else
  214. pc--;
  215. *playerCount += pc;
  216. (*serverCount)++;
  217. }
  218. }
  219. }
  220. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  221. void App::timerEvent(QTimerEvent *e)
  222. {
  223. if(e->timerId() == myClientsFrameTimerID)
  224. {
  225. ActiveClient *ac;
  226. foreach(ac, myClients)
  227. {
  228. ac->run();
  229. }
  230. // HostNames scheduled daily refresh
  231. int currentHour = QTime::currentTime().hour();
  232. int refreshHour = Settings::globalInstance()->refreshHostNamesHour();
  233. if(currentHour == refreshHour && !myHostNamesRequested)
  234. {
  235. print("Refreshing hostname cache...\n");
  236. requestCachedHostNames();
  237. myHostNamesRequested = true;
  238. }
  239. else if(currentHour != refreshHour && myHostNamesRequested)
  240. {
  241. myHostNamesRequested = false;
  242. }
  243. }
  244. Sleeper::msleep(1);
  245. }
  246. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  247. void App::requestBroadcast(const QString &type, const QString &user, const QString &server, const QString &message)
  248. {
  249. if(!Settings::globalInstance()->developerMode())
  250. mySshClient->write("REQ_BC QWalt,-" + type + "-,qw://" + server + ",'" + user + "','" + message + "'\n");
  251. else
  252. mySshClient->write("REQ_BC QDEV,-dev-,qw://" + server + ",'" + user + "','" + message + "'\n");
  253. }
  254. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  255. void App::addMessageToHistory(const QString &msg)
  256. {
  257. myLastMessages.push_back(msg);
  258. if(myLastMessages.size() > 5)
  259. myLastMessages.removeAt(0);
  260. }
  261. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  262. void App::broadcast(const QString &msg, ActiveClient* ignoredClient)
  263. {
  264. ActiveClient* ac;
  265. QString frequency = msg.section(' ', 0, 0);
  266. addMessageToHistory(msg);
  267. foreach(ac, myClients)
  268. {
  269. if(ac == ignoredClient)
  270. continue;
  271. if((frequency == "-qw-" && ac->client()->isQWMuted()) || (frequency == "-spam-" && ac->client()->isSpamMuted()))
  272. continue;
  273. if(ac->client()->state() == Client::ConnectedState)
  274. ac->client()->say(msg);
  275. }
  276. }
  277. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  278. void App::broadcast(const QString &msg, int *serverCount, int *userCount)
  279. {
  280. ActiveClient* ac;
  281. *serverCount = 0;
  282. *userCount = 0;
  283. QString frequency = msg.section(' ', 0, 0);
  284. addMessageToHistory(msg);
  285. foreach(ac, myClients)
  286. {
  287. if((frequency == "-qw-" && ac->client()->isQWMuted()) || (frequency == "-spam-" && ac->client()->isSpamMuted()))
  288. continue;
  289. if(ac->client()->state() == Client::ConnectedState)
  290. {
  291. ac->client()->say(msg);
  292. *userCount += ac->playerCount() - 1;
  293. (*serverCount)++;
  294. }
  295. }
  296. }
  297. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  298. void App::cleanup()
  299. {
  300. ActiveClient* ac;
  301. foreach(ac, myClients)
  302. {
  303. ac->client()->disconnect();
  304. delete ac;
  305. }
  306. }
  307. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  308. void App::setReplyHashAndWaitForReply(const QString &serverAddress, const QString &hash)
  309. {
  310. ActiveClient* ac;
  311. foreach(ac, myClients)
  312. {
  313. if(serverAddress == ac->serverAddressString())
  314. {
  315. ac->setReplyHashAndWaitForReply(hash);
  316. return;
  317. }
  318. }
  319. }
  320. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  321. void App::incrementReplyCounters(const QString &hash, int userCount, int channelCount, int playerCount, int serverCount)
  322. {
  323. ActiveClient* ac;
  324. foreach(ac, myClients)
  325. {
  326. if(ac->replyHash() == hash)
  327. {
  328. ac->incrementReplyCounters(userCount, channelCount, playerCount, serverCount);
  329. return;
  330. }
  331. }
  332. }
  333. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  334. void App::requestCachedHostNames()
  335. {
  336. ActiveClient* ac;
  337. foreach(ac, myClients)
  338. {
  339. print("Requested HostName for server " + ac->serverAddressString() + "\n");
  340. mySshClient->write("REQ_DNS " + ac->serverAddressString() + "\n");
  341. }
  342. }
  343. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  344. void App::setServerHostName(const QString &serverAddress, const QString &hostName)
  345. {
  346. ActiveClient* ac;
  347. foreach(ac, myClients)
  348. {
  349. if(ac->serverAddressString() == serverAddress)
  350. {
  351. print("HostName for " + serverAddress + " set to " + hostName + "\n");
  352. ac->setHostName(hostName);
  353. return;
  354. }
  355. }
  356. }
  357. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  358. QString App::serverHostName(const QString &serverAddress) const
  359. {
  360. ActiveClient* ac;
  361. foreach(ac, myClients)
  362. {
  363. if(ac->serverAddressString() == serverAddress)
  364. {
  365. return ac->hostName();
  366. }
  367. }
  368. return QString();
  369. }
  370. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  371. void App::onCentralConnection()
  372. {
  373. print("Connected!\nRefreshing cached hostnames...\n");
  374. requestCachedHostNames();
  375. }