App.h 6.5 KB

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