App.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #ifndef APP_H
  16. #define APP_H
  17. #include <QCoreApplication>
  18. #include <QList>
  19. #include <QSettings>
  20. #include <QHostAddress>
  21. #include <QStringList>
  22. class SshClient;
  23. class QTcpSocket;
  24. class QTcpServer;
  25. class Client;
  26. class ActiveClient;
  27. /**
  28. The application core.
  29. @author Mihawk <luiz@netdome.biz>
  30. @file App.h
  31. */
  32. class App : public QCoreApplication
  33. {
  34. Q_OBJECT
  35. public:
  36. /**
  37. App contructor.
  38. @param argc Command line param count
  39. @param argv Command line param string array
  40. */
  41. explicit App(int &argc, char **argv);
  42. /**
  43. App Destructor.
  44. */
  45. ~App();
  46. /**
  47. Prints directly to console and to anyone connected via telnet.
  48. @param msg The message
  49. */
  50. void print(const QString& msg);
  51. /**
  52. Those two functions broadcast messages to all QW servers that
  53. the application is connected to.
  54. The first one return the server and player count that the message
  55. has reached.
  56. The second just broadcasts the message to all QW servers ignoring the
  57. ignoredClient param.
  58. */
  59. void broadcast(const QString& msg, int *serverCount, int *userCount);
  60. void broadcast(const QString& msg, ActiveClient* ignoredClient = 0);
  61. /**
  62. Request central to broadcast a message.
  63. @param type The type of message QDEV QWalt
  64. @param user The user that is broadcasting
  65. @param server The server where the message is coming from
  66. @param message The message
  67. */
  68. void requestBroadcast(const QString& type, const QString& user, const QString& server, const QString& message);
  69. /**
  70. Sets the reply hash comming from central and starts timer
  71. Until the timer is running the application will increment the user count for the
  72. hash specified.
  73. @param serverAddress The address of the server that requested the broadcast
  74. @param hash The hash returned from central server for this message
  75. */
  76. void setReplyHashAndWaitForReply(const QString& serverAddress, const QString& hash);
  77. /**
  78. Increment the reply counters for the server that has the hash specified.
  79. @param hash The broadcasted message hash
  80. @param userCount The irc user count
  81. @param channelCount The irc channel count
  82. @param playerCount The QW player count
  83. @param serverCount The QW server count
  84. */
  85. void incrementReplyCounters(const QString& hash, int userCount, int channelCount, int playerCount, int serverCount);
  86. /**
  87. Gets the servers we are monitoring player counts.
  88. @param serverCount Pointer to variable that will be incremented with the server count
  89. @param playerCount Pointer to variable that will be incremented with the player count
  90. @param ignoreClient Client that counters will be ignored
  91. */
  92. void activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveClient* ignoreClient = 0);
  93. /**
  94. Last 5 messages broadcasted on central.
  95. @return The list with the messages
  96. */
  97. const QStringList& lastMessages() const;
  98. /**
  99. Sets monitored server HostName, this function should be called from SshClient (central)
  100. in reply to REQ_DNS request (DNS_RE).
  101. @param serverAddress The server address full string ip:port
  102. @param hostName The server hostname (obtained from REQ_DNS request to central)
  103. */
  104. void setServerHostName(const QString& serverAddress, const QString& hostName);
  105. /**
  106. Gets monitored server HostName, this function should be called from QWClient (BOT)
  107. when broadcasting inside it's own network of bots.
  108. @param serverAddress The server address full string ip:port
  109. @return hostName The server hostname
  110. */
  111. QString serverHostName(const QString& serverAddress) const;
  112. protected:
  113. /**
  114. App's MainLoop is here.
  115. */
  116. void timerEvent(QTimerEvent *e);
  117. private:
  118. // Socket for the TelNet administration
  119. // Server for handling connections to the TelNet administration
  120. QTcpSocket* mySocket;
  121. QTcpServer* myServer;
  122. bool mySocketConnectedFlag; // Indicates whether there is someone connected to the TelNet administration
  123. // SSH Client connected to central
  124. SshClient* mySshClient;
  125. // List of Servers we are monitoring
  126. QList<ActiveClient*> myClients;
  127. // Main loop timer identifier
  128. int myClientsFrameTimerID;
  129. // Last 5 messages list
  130. QStringList myLastMessages;
  131. // Indicates whether I've requested the hostnames, used to avoid requesting the entire scheduled hour
  132. bool myHostNamesRequested;
  133. /**
  134. Loads the server list from the config file.
  135. */
  136. void loadServerList();
  137. /**
  138. Saves the current server list to the config file.
  139. */
  140. void saveServerList();
  141. /**
  142. Disconnect/Stop Monitoring from all servers.
  143. */
  144. void cleanup();
  145. /**
  146. Adds message to the history of 5 messages.
  147. @param msg The message
  148. */
  149. void addMessageToHistory(const QString& msg);
  150. /**
  151. Parses the command line parameters.
  152. @return False if the parameters failed to be parsed, true otherwise
  153. */
  154. bool parseCommandLine();
  155. /**
  156. Checks if the password specified by the user is correct.
  157. @param password The password
  158. @return True if its correct, false otherwise
  159. */
  160. void addClient(const QString& host, quint16 port);
  161. /**
  162. Checks if the password specified by the user is correct.
  163. @param password The password
  164. @return True if its correct, false otherwise
  165. */
  166. void removeClient(const QString& host, quint16 port);
  167. /**
  168. Requests the hostnames of all servers being monitored.
  169. Those hostnames are obtained from central, that has a smart
  170. hostname lookup system.
  171. */
  172. void requestCachedHostNames();
  173. /**
  174. The following functions sets the Team, Color, Nick and Ping
  175. for all the bots on all servers that we are monitoring.
  176. */
  177. void setTeam(const QString& team);
  178. void setColor(const QString& args);
  179. void setNick(const QString& args);
  180. void setPing(const QString& args);
  181. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  182. // The following functions are used to create the TelNet administration
  183. // interface
  184. /**
  185. Checks if the password specified by the user is correct.
  186. @param password The password
  187. @return True if its correct, false otherwise
  188. */
  189. bool checkPassword(const QString& password);
  190. /**
  191. Parses add client command coming from the user.
  192. @param args The command arguments unparsed
  193. */
  194. void parseAddClientCommand(const QString& args);
  195. /**
  196. Parses remove client command coming from the user.
  197. @param args The command arguments unparsed
  198. */
  199. void parseRemoveClientCommand(const QString& args);
  200. /**
  201. Lists all the servers we are monitoring.
  202. */
  203. void listClients();
  204. /**
  205. Displays a list of commands available to the user.
  206. */
  207. void help();
  208. private slots:
  209. /**
  210. Called everytime data arrives coming from the TelNet user.
  211. */
  212. void onDataArrival();
  213. /**
  214. Called everytime an user is trying to connect to the TelNet interface.
  215. */
  216. void onNewConnection();
  217. /**
  218. Called everytime an user is disconnected from the TelNet interface.
  219. */
  220. void onDisconnection();
  221. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  222. /**
  223. Called everytime we are fully connected to central.
  224. */
  225. void onCentralConnection();
  226. };
  227. #endif // APP_H