App.cpp 12 KB

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