App.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /**
  113. Checks if the password specified by the user is correct.
  114. @param password The password
  115. @return True if its correct, false otherwise
  116. */
  117. bool addClient(const QString& host, quint16 port, const QString &password = "");
  118. /**
  119. Checks if the password specified by the user is correct.
  120. @param password The password
  121. @return True if its correct, false otherwise
  122. */
  123. bool removeClient(const QString& host, quint16 port);
  124. protected:
  125. /**
  126. App's MainLoop is here.
  127. */
  128. void timerEvent(QTimerEvent *e);
  129. private:
  130. // Socket for the TelNet administration
  131. // Server for handling connections to the TelNet administration
  132. QTcpSocket* mySocket;
  133. QTcpServer* myServer;
  134. bool mySocketConnectedFlag; // Indicates whether there is someone connected to the TelNet administration
  135. // SSH Client connected to central
  136. SshClient* mySshClient;
  137. // List of Servers we are monitoring
  138. QList<ActiveClient*> myClients;
  139. // Main loop timer identifier
  140. int myClientsFrameTimerID;
  141. // Last 5 messages list
  142. QStringList myLastMessages;
  143. // Indicates whether I've requested the hostnames, used to avoid requesting the entire scheduled hour
  144. bool myHostNamesRequested;
  145. /**
  146. Loads the server list from the config file.
  147. */
  148. void loadServerList();
  149. /**
  150. Saves the current server list to the config file.
  151. */
  152. void saveServerList();
  153. /**
  154. Disconnect/Stop Monitoring from all servers.
  155. */
  156. void cleanup();
  157. /**
  158. Adds message to the history of 5 messages.
  159. @param msg The message
  160. */
  161. void addMessageToHistory(const QString& msg);
  162. /**
  163. Parses the command line parameters.
  164. @return False if the parameters failed to be parsed, true otherwise
  165. */
  166. bool parseCommandLine();
  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. private slots:
  174. /**
  175. Called everytime we are fully connected to central.
  176. */
  177. void onCentralConnection();
  178. };
  179. #endif // APP_H