App.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. const QList<ActiveClient*> clients() const;
  47. /**
  48. Prints directly to console and to anyone connected via telnet.
  49. @param msg The message
  50. */
  51. void print(const QString& msg);
  52. /**
  53. Those two functions broadcast messages to all QW servers that
  54. the application is connected to.
  55. The first one return the server and player count that the message
  56. has reached.
  57. The second just broadcasts the message to all QW servers ignoring the
  58. ignoredClient param.
  59. */
  60. void broadcast(const QString& msg, int *serverCount, int *userCount);
  61. void broadcast(const QString& msg, ActiveClient* ignoredClient = 0);
  62. /**
  63. Request central to broadcast a message.
  64. @param type The type of message QDEV QWalt
  65. @param user The user that is broadcasting
  66. @param server The server where the message is coming from
  67. @param message The message
  68. */
  69. void requestBroadcast(const QString& type, const QString& user, const QString& server, const QString& message);
  70. /**
  71. Sets the reply hash comming from central and starts timer
  72. Until the timer is running the application will increment the user count for the
  73. hash specified.
  74. @param serverAddress The address of the server that requested the broadcast
  75. @param hash The hash returned from central server for this message
  76. */
  77. void setReplyHashAndWaitForReply(const QString& serverAddress, const QString& hash);
  78. /**
  79. Increment the reply counters for the server that has the hash specified.
  80. @param hash The broadcasted message hash
  81. @param userCount The irc user count
  82. @param channelCount The irc channel count
  83. @param playerCount The QW player count
  84. @param serverCount The QW server count
  85. */
  86. void incrementReplyCounters(const QString& hash, int userCount, int channelCount, int playerCount, int serverCount);
  87. /**
  88. Gets the servers we are monitoring player counts.
  89. @param serverCount Pointer to variable that will be incremented with the server count
  90. @param playerCount Pointer to variable that will be incremented with the player count
  91. @param ignoreClient Client that counters will be ignored
  92. */
  93. void activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveClient* ignoreClient = 0);
  94. /**
  95. Last 5 messages broadcasted on central.
  96. @return The list with the messages
  97. */
  98. const QStringList& lastMessages() const;
  99. /**
  100. Sets monitored server HostName, this function should be called from SshClient (central)
  101. in reply to REQ_DNS request (DNS_RE).
  102. @param serverAddress The server address full string ip:port
  103. @param hostName The server hostname (obtained from REQ_DNS request to central)
  104. */
  105. void setServerHostName(const QString& serverAddress, const QString& hostName);
  106. /**
  107. Gets monitored server HostName, this function should be called from QWClient (BOT)
  108. when broadcasting inside it's own network of bots.
  109. @param serverAddress The server address full string ip:port
  110. @return hostName The server hostname
  111. */
  112. QString serverHostName(const QString& serverAddress) const;
  113. /**
  114. Checks if the password specified by the user is correct.
  115. @param password The password
  116. @return True if its correct, false otherwise
  117. */
  118. bool addClient(const QString& hostName, quint16 port, const QString &password = "");
  119. /**
  120. Removes a client.
  121. @param hostName hostname
  122. @param port port
  123. @return True if its removed, false otherwise
  124. */
  125. bool removeClient(const QString& hostName, quint16 port);
  126. protected:
  127. /**
  128. App's MainLoop is here.
  129. */
  130. void timerEvent(QTimerEvent *e);
  131. private:
  132. // Socket for the TelNet administration
  133. // Server for handling connections to the TelNet administration
  134. QTcpSocket* mySocket;
  135. QTcpServer* myServer;
  136. bool mySocketConnectedFlag; // Indicates whether there is someone connected to the TelNet administration
  137. // SSH Client connected to central
  138. SshClient* mySshClient;
  139. // List of Servers we are monitoring
  140. QList<ActiveClient*> myClients;
  141. // Main loop timer identifier
  142. int myClientsFrameTimerID;
  143. // Last 5 messages list
  144. QStringList myLastMessages;
  145. // Indicates whether I've requested the hostnames, used to avoid requesting the entire scheduled hour
  146. bool myHostNamesRequested;
  147. /**
  148. Loads the server list from the config file.
  149. */
  150. void loadServerList();
  151. /**
  152. Saves the current server list to the config file.
  153. */
  154. void saveServerList();
  155. /**
  156. Disconnect/Stop Monitoring from all servers.
  157. */
  158. void cleanup();
  159. /**
  160. Adds message to the history of 5 messages.
  161. @param msg The message
  162. */
  163. void addMessageToHistory(const QString& msg);
  164. /**
  165. Parses the command line parameters.
  166. @return False if the parameters failed to be parsed, true otherwise
  167. */
  168. bool parseCommandLine();
  169. /**
  170. Requests the hostnames of all servers being monitored.
  171. Those hostnames are obtained from central, that has a smart
  172. hostname lookup system.
  173. */
  174. void requestCachedHostNames();
  175. private slots:
  176. /**
  177. Called everytime we are fully connected to central.
  178. */
  179. void onCentralConnection();
  180. };
  181. #endif // APP_H