App.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. protected:
  99. /**
  100. App Destructor.
  101. */
  102. void timerEvent(QTimerEvent *e);
  103. private:
  104. // Socket for the TelNet administration
  105. // Server for handling connections to the TelNet administration
  106. QTcpSocket* mySocket;
  107. QTcpServer* myServer;
  108. bool mySocketConnectedFlag; // Indicates whether there is someone connected to the TelNet administration
  109. // SSH Client connected to central
  110. SshClient* myQWNETSshClient;
  111. // List of Servers we are monitoring
  112. QList<ActiveClient*> myClients;
  113. // Main loop timer identifier
  114. int myClientsFrameTimerID;
  115. // Last 5 messages list
  116. QStringList myLastMessages;
  117. /**
  118. Loads the server list from the config file.
  119. */
  120. void loadServerList();
  121. /**
  122. Saves the current server list to the config file.
  123. */
  124. void saveServerList();
  125. /**
  126. Disconnect/Stop Monitoring from all servers.
  127. */
  128. void cleanup();
  129. /**
  130. Adds message to the history of 5 messages.
  131. @param msg The message
  132. */
  133. void addMessageToHistory(const QString& msg);
  134. /**
  135. Parses the command line parameters.
  136. @return False if the parameters failed to be parsed, true otherwise
  137. */
  138. bool parseCommandLine();
  139. /**
  140. Checks if the password specified by the user is correct.
  141. @param password The password
  142. @return True if its correct, false otherwise
  143. */
  144. void addClient(const QString& host, quint16 port);
  145. /**
  146. Checks if the password specified by the user is correct.
  147. @param password The password
  148. @return True if its correct, false otherwise
  149. */
  150. void removeClient(const QString& host, quint16 port);
  151. /**
  152. The following functions sets the Team, Color, Nick and Ping
  153. for all the bots on all servers that we are monitoring.
  154. */
  155. void setTeam(const QString& team);
  156. void setColor(const QString& args);
  157. void setNick(const QString& args);
  158. void setPing(const QString& args);
  159. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  160. // The following functions are used to create the TelNet administration
  161. // interface
  162. /**
  163. Checks if the password specified by the user is correct.
  164. @param password The password
  165. @return True if its correct, false otherwise
  166. */
  167. bool checkPassword(const QString& password);
  168. /**
  169. Parses add client command coming from the user.
  170. @param args The command arguments unparsed
  171. */
  172. void parseAddClientCommand(const QString& args);
  173. /**
  174. Parses remove client command coming from the user.
  175. @param args The command arguments unparsed
  176. */
  177. void parseRemoveClientCommand(const QString& args);
  178. /**
  179. Lists all the servers we are monitoring.
  180. */
  181. void listClients();
  182. /**
  183. Displays a list of commands available to the user.
  184. */
  185. void help();
  186. private slots:
  187. /**
  188. Called everytime data arrives coming from the TelNet user.
  189. */
  190. void onDataArrival();
  191. /**
  192. Called everytime an user is trying to connect to the TelNet interface.
  193. */
  194. void onNewConnection();
  195. /**
  196. Called everytime an user is disconnected from the TelNet interface.
  197. */
  198. void onDisconnection();
  199. };
  200. #endif // APP_H