App.cpp 12 KB

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