Przeglądaj źródła

Cleaned up unused stuff.
Reorganized and commented the classes for the future.

Mihawk 11 lat temu
rodzic
commit
a5b666178f
13 zmienionych plików z 854 dodań i 88 usunięć
  1. 14 3
      ActiveClient.cpp
  2. 97 8
      ActiveClient.h
  3. 42 5
      App.cpp
  4. 177 13
      App.h
  5. 29 23
      Client.cpp
  6. 186 13
      Client.h
  7. 18 3
      ServerQuery.cpp
  8. 117 15
      ServerQuery.h
  9. 21 0
      Settings.cpp
  10. 32 3
      Settings.h
  11. 14 1
      SshClient.cpp
  12. 79 1
      SshClient.h
  13. 28 0
      main.cpp

+ 14 - 3
ActiveClient.cpp

@@ -25,6 +25,7 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #include "App.h"
 #include "Settings.h"
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ActiveClient::ActiveClient(App *app, QObject *parent):
   QObject(parent),
   myApp(app),
@@ -46,33 +47,39 @@ ActiveClient::ActiveClient(App *app, QObject *parent):
   myQueryTimer->setSingleShot(true);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ActiveClient::~ActiveClient()
 {
   delete myClient;
   delete myDisconnectTime;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Client* ActiveClient::client()
 {
   return myClient;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 quint8 ActiveClient::playerCount() const
 {
   return myQuery->playerList().size();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ActiveClient::setAddress(const QHostAddress &address, quint16 port)
 {
   myQuery->setAddress(address, port);
   return;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ActiveClient::queryError(ServerQuery::Error)
 {
   myQueryTimer->start(myQueryInterval);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ActiveClient::queryFinished()
 {
   PlayerList playerList = myQuery->playerList();
@@ -106,6 +113,7 @@ void ActiveClient::queryFinished()
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ActiveClient::run()
 {
   /* Say the broadcast count */
@@ -128,32 +136,35 @@ void ActiveClient::run()
     myClient->run();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 const QString ActiveClient::serverAddressString()
 {
   return QString(myQuery->address().toString() + ':' + QString::number(myQuery->port()));
 }
 
-void ActiveClient::setReplyHash(const QString &hash)
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+void ActiveClient::setReplyHashAndWaitForReply(const QString &hash)
 {
   myReplyHash = hash;
 
   /* Wait for reply */
   myBroadcastReplyTimer->setSingleShot(true);
   myBroadcastReplyTimer->start(Settings::globalInstance()->timeToWaitForCountReply()*1000);
-
-  qDebug() << "reply hash set" << hash;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 const QString& ActiveClient::replyHash() const
 {
   return myReplyHash;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool ActiveClient::isWaitingReply() const
 {
   return myBroadcastReplyTimer->isActive();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ActiveClient::incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount)
 {
   if(!myBroadcastReplyTimer->isActive())

+ 97 - 8
ActiveClient.h

@@ -30,51 +30,140 @@ class QTime;
 class Client;
 class App;
 
+/**
+  Takes care of joining the client to the server in the case there are players
+  and leaving when there are nobody.
+  It constantly queryies the server using myQuery to gather information from the servers.
+
+  @author Mihawk <luiz@netdome.biz>
+  @file   ActiveClient.h
+*/
 class ActiveClient: public QObject
 {
   Q_OBJECT
 
 public:
+  /**
+    Creates a server monitoring object.
+
+    @param  app    The application creator of this object
+    @param  parent The parent object (should be the same as the app param)
+  */
   ActiveClient(App* app, QObject *parent = 0);
+
+  /**
+    Destructor
+  */
   virtual ~ActiveClient();
 
+  /**
+    Returns the QW Client used.
+
+    @return The client used by us
+  */
   Client*           client();
 
+  /**
+    Makes this class work, should be called every frame after creation of the object.
+  */
   void              run();
+
+  /**
+    Set the server that we are going to monitor.
+
+    @param address Server address
+    @param port    Server port
+  */
   void              setAddress(const QHostAddress &address, quint16 port);
+
+  /**
+    Returns the address of the server we are monitoring.
+
+    @return The server address
+  */
   const QString     serverAddressString();
+
+  /**
+    Returns the number of players on the server last time the query was ran.
+
+    @return The number of players
+  */
   quint8            playerCount() const;
 
-  void              setReplyHash(const QString& hash); //this activates the 5 seconds reply timer too
+  /**
+    Sets the hash and start waiting for reply from central on that hash.
+
+    @param hash The reply hash
+  */
+  void              setReplyHashAndWaitForReply(const QString& hash);
+
+  /**
+    Returns the reply hash of the last reply.
+
+    @return The hash
+  */
   const QString&    replyHash() const;
+
+  /**
+    Called to increment the internal reply counters
+    Those reply counters are counters of how many people received the message
+    that we just broadcasted.
+
+    @param  userCount     The irc user count
+    @param  channelCount  The irc channel count
+    @param  playerCount   The QW player count
+    @param  serverCount   The QW servers count
+  */
   void              incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount);
+
+  /**
+    Tells us if we are wating for the count reply from the server.
+
+    @return True if waiting for reply, false otherwise
+  */
   bool              isWaitingReply() const;
 
 private slots:
+  /**
+    Called when a query gathering server information has just finished successfully.
+  */
   void              queryFinished();
+
+  /**
+    Called when a query fails on the process of gathering information the server.
+
+    @param  err Error code
+  */
   void              queryError(ServerQuery::Error err);
 
 private:
+  // The application that has created us
   App*              myApp;
-  Client*           myClient; //the QW bot
-  ServerQuery*      myQuery; //queries all players information from the server
-  QTime*            myDisconnectTime; //timer to avoid too fast reconnections
 
-  /* Count server reply while timer is active */
+  // The QW client connects to the server
+  Client*           myClient;
+
+  // The information gathering query object
+  ServerQuery*      myQuery;
+
+  // Timer to avoid too fast reconnections
+  QTime*            myDisconnectTime;
+
+  // Count server reply while timer is active
   QString           myReplyHash;
   QTimer*           myBroadcastReplyTimer;
 
-  /* How much time to wait b4 next query for player information */
+  // How much time to wait b4 next query for player information
   QTimer*           myQueryTimer;
   quint32           myQueryInterval;
 
-  /* Reply counters */
+  // Reply counters
   int               myUniqueUserCount;
   int               myUniqueChannelCount;
   int               myUniqueServerCount;
   int               myUniquePlayerCount;
 
-  bool              myReplyTimerWasActive; //for printing the reply counters
+  bool              myReplyTimerWasActive; // For printing the reply counters
 };
 
 #endif // ACTIVECLIENT_H

+ 42 - 5
App.cpp

@@ -16,6 +16,13 @@
 #include "ActiveClient.h"
 #include "Settings.h"
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+/**
+  Used just to expose the msleep function
+  from QThread
+
+  @author  Mihawk <luiz@netdome.biz>
+*/
 class Sleeper: public QThread
 {
 public:
@@ -25,6 +32,7 @@ public:
   }
 };
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 App::App(int &argc, char **argv) :
   QCoreApplication(argc, argv),
   myServer(new QTcpServer()),
@@ -32,7 +40,10 @@ App::App(int &argc, char **argv) :
   myQWNETSshClient(new SshClient(this))
 {
   if(!parseCommandLine())
+  {
+    QTimer::singleShot(0, this, SLOT(quit()));
     return;
+  }
 
   print("CIMS Bot Service v0.101\n========================================================\n");
 
@@ -50,6 +61,7 @@ App::App(int &argc, char **argv) :
   Settings::globalInstance()->save();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 App::~App()
 {
   Settings::globalInstance()->save();
@@ -57,6 +69,7 @@ App::~App()
   delete myServer;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool App::parseCommandLine()
 {
   if(argc() < 2)
@@ -71,7 +84,6 @@ bool App::parseCommandLine()
     if(arg == "--help" || arg == "-h")
     {
       printf("Usage: %s [--config/-c config_file] [-h]\n", args.at(0).section("/", -1).toAscii().data());
-      QTimer::singleShot(0, this, SLOT(quit()));
       return false;
     }
     else if(arg == "--config" || arg == "-c")
@@ -80,7 +92,6 @@ bool App::parseCommandLine()
       if(itr == args.constEnd())
       {
         printf("--config: Expected config file path.\n");
-        QTimer::singleShot(0, this, SLOT(quit()));
         return false;
       }
       arg = *itr;
@@ -92,6 +103,7 @@ bool App::parseCommandLine()
   return true;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::onNewConnection()
 {
   if(mySocketConnectedFlag)
@@ -111,12 +123,14 @@ void App::onNewConnection()
   print("Connected to CIMS's bot service.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::onDisconnection()
 {
   mySocketConnectedFlag = false;
   mySocket->deleteLater();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::loadServerList()
 {
   QStringList list = Settings::globalInstance()->serverList();
@@ -137,6 +151,7 @@ void App::loadServerList()
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::saveServerList()
 {
   QStringList list;
@@ -148,6 +163,7 @@ void App::saveServerList()
   Settings::globalInstance()->setServerList(list);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::parseAddClientCommand(const QString &args)
 {
   if(!args.size())
@@ -175,6 +191,7 @@ void App::parseAddClientCommand(const QString &args)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::parseRemoveClientCommand(const QString &args)
 {
   if(!args.size())
@@ -197,6 +214,7 @@ void App::parseRemoveClientCommand(const QString &args)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::onDataArrival()
 {
   while(mySocket->canReadLine())
@@ -287,11 +305,13 @@ void App::onDataArrival()
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 const QStringList& App::lastMessages() const
 {
   return myLastMessages;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::help()
 {
   print("connect server:port -> connects the bot on a server\n");
@@ -305,6 +325,7 @@ void App::help()
   print("help -> displays this message\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::setTeam(const QString &args)
 {
   ActiveClient* ac;
@@ -315,6 +336,7 @@ void App::setTeam(const QString &args)
   print("Team changed.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::setColor(const QString &args)
 {
   ActiveClient* ac;
@@ -334,6 +356,7 @@ void App::setColor(const QString &args)
   print("All clients colors have changed.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::setPing(const QString &args)
 {
   ActiveClient* ac;
@@ -344,6 +367,7 @@ void App::setPing(const QString &args)
   print("All clients pings have changed.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::setNick(const QString &args)
 {
   ActiveClient* ac;
@@ -354,6 +378,7 @@ void App::setNick(const QString &args)
   print("All clients nicks have changed.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool App::checkPassword(const QString &password)
 {
   if(QCryptographicHash::hash(password.toAscii(), QCryptographicHash::Md4).toHex().toLower() == "bf4df9f74d05c50ea00492224fb02854")
@@ -361,6 +386,7 @@ bool App::checkPassword(const QString &password)
   return false;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::print(const QString &msg)
 {
   printf("%s", msg.toAscii().data());
@@ -371,6 +397,7 @@ void App::print(const QString &msg)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::addClient(const QString &host, quint16 port)
 {
   ActiveClient* ac;
@@ -399,6 +426,7 @@ void App::addClient(const QString &host, quint16 port)
   print("Client added to watch list.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::removeClient(const QString &host, quint16 port)
 {
   ActiveClient* ac;
@@ -421,6 +449,7 @@ void App::removeClient(const QString &host, quint16 port)
   print("Client not found on the list.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::listClients()
 {
   ActiveClient* ac;
@@ -430,6 +459,7 @@ void App::listClients()
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveClient *ignoreClient)
 {
   ActiveClient* ac;
@@ -453,6 +483,7 @@ void App::activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveC
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::timerEvent(QTimerEvent *e)
 {
   if(e->timerId() == myClientsFrameTimerID)
@@ -466,6 +497,7 @@ void App::timerEvent(QTimerEvent *e)
   Sleeper::msleep(1);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::requestBroadcast(const QString &type, const QString &user, const QString &server, const QString &message)
 {
   if(!Settings::globalInstance()->developerMode())
@@ -474,6 +506,7 @@ void App::requestBroadcast(const QString &type, const QString &user, const QStri
     myQWNETSshClient->write("REQ_BC QDEV,-dev-,qw://" + server + ",'" + user + "','" + message + "'\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::addMessageToHistory(const QString &msg)
 {
   myLastMessages.push_back(msg);
@@ -481,6 +514,7 @@ void App::addMessageToHistory(const QString &msg)
     myLastMessages.removeAt(0);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::broadcast(const QString &msg, ActiveClient* ignoredClient)
 {
   ActiveClient* ac;
@@ -501,6 +535,7 @@ void App::broadcast(const QString &msg, ActiveClient* ignoredClient)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::broadcast(const QString &msg, int *serverCount, int *userCount)
 {
   ActiveClient* ac;
@@ -526,6 +561,7 @@ void App::broadcast(const QString &msg, int *serverCount, int *userCount)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::cleanup()
 {
   ActiveClient* ac;
@@ -536,19 +572,21 @@ void App::cleanup()
   }
 }
 
-void App::setReplyHash(const QString &serverAddress, const QString &hash)
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+void App::setReplyHashAndWaitForReply(const QString &serverAddress, const QString &hash)
 {
   ActiveClient* ac;
   foreach(ac, myClients)
   {
     if(serverAddress == ac->serverAddressString())
     {
-      ac->setReplyHash(hash);
+      ac->setReplyHashAndWaitForReply(hash);
       return;
     }
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::incrementReplyCounters(const QString &hash, int userCount, int channelCount, int playerCount, int serverCount)
 {
   ActiveClient* ac;
@@ -561,4 +599,3 @@ void App::incrementReplyCounters(const QString &hash, int userCount, int channel
     }
   }
 }
-

+ 177 - 13
App.h

@@ -13,60 +13,224 @@ class QTcpServer;
 class Client;
 class ActiveClient;
 
+/**
+  The application core.
+
+  @author  Mihawk <luiz@netdome.biz>
+  @file    App.h
+*/
 class App : public QCoreApplication
 {
   Q_OBJECT
 public:
+  /**
+    App contructor.
+
+    @param  argc  Command line param count
+    @param  argv  Command line param string array
+  */
   explicit              App(int &argc, char **argv);
-                        ~App();
 
+  /**
+    App Destructor.
+  */
+  ~App();
+
+  /**
+    Prints directly to console and to anyone connected via telnet.
+
+    @param  msg The message
+  */
   void                  print(const QString& msg);
+
+  /**
+    Those two functions broadcast messages to all QW servers that
+    the application is connected to.
+    The first one return the server and player count that the message
+    has reached.
+    The second just broadcasts the message to all QW servers ignoring the
+    ignoredClient param.
+  */
   void                  broadcast(const QString& msg, int *serverCount, int *userCount);
   void                  broadcast(const QString& msg, ActiveClient* ignoredClient = 0);
+
+  /**
+    Request central to broadcast a message.
+
+    @param  type    The type of message QDEV QWalt
+    @param  user    The user that is broadcasting
+    @param  server  The server where the message is coming from
+    @param  message The message
+  */
   void                  requestBroadcast(const QString& type, const QString& user, const QString& server, const QString& message);
-  void                  setReplyHash(const QString& serverAddress, const QString& hash);
+
+  /**
+    Sets the reply hash comming from central and starts timer
+    Until the timer is running the application will increment the user count for the
+    hash specified.
+
+    @param  serverAddress The address of the server that requested the broadcast
+    @param  hash          The hash returned from central server for this message
+  */
+  void                  setReplyHashAndWaitForReply(const QString& serverAddress, const QString& hash);
+
+  /**
+    Increment the reply counters for the server that has the hash specified.
+
+    @param  hash          The broadcasted message hash
+    @param  userCount     The irc user count
+    @param  channelCount  The irc channel count
+    @param  playerCount   The QW player count
+    @param  serverCount   The QW server count
+  */
   void                  incrementReplyCounters(const QString& hash, int userCount, int channelCount, int playerCount, int serverCount);
+
+  /**
+    Gets the servers we are monitoring player counts.
+
+    @param  serverCount   Pointer to variable that will be incremented with the server count
+    @param  playerCount   Pointer to variable that will be incremented with the player count
+    @param  ignoreClient  Client that counters will be ignored
+  */
   void                  activeClientsReplyCounters(int *serverCount, int *playerCount, ActiveClient* ignoreClient = 0);
+
+  /**
+    Last 5 messages broadcasted on central.
+
+    @return The list with the messages
+  */
   const QStringList&    lastMessages() const;
 
 protected:
+  /**
+    App Destructor.
+  */
   void									timerEvent(QTimerEvent *e);
 
 private:
+  // Socket for the TelNet administration
+  // Server for handling connections to the TelNet administration
   QTcpSocket*           mySocket;
   QTcpServer*           myServer;
-  bool                  mySocketConnectedFlag;
+  bool                  mySocketConnectedFlag; // Indicates whether there is someone connected to the TelNet administration
+
+  // SSH Client connected to central
   SshClient*            myQWNETSshClient;
 
+  // List of Servers we are monitoring
   QList<ActiveClient*>  myClients;
-  quint32               myCurrentClient;
-  QSettings             mySettings;
-  int                   myClientsFrameTimerID; //timer for mainloop
+
+  // Main loop timer identifier
+  int                   myClientsFrameTimerID;
+
+  // Last 5 messages list
   QStringList           myLastMessages;
 
+  /**
+    Loads the server list from the config file.
+  */
   void                  loadServerList();
+
+  /**
+    Saves the current server list to the config file.
+  */
   void                  saveServerList();
+
+  /**
+    Disconnect/Stop Monitoring from all servers.
+  */
   void                  cleanup();
+
+  /**
+    Adds message to the history of 5 messages.
+
+    @param  msg The message
+  */
   void                  addMessageToHistory(const QString& msg);
 
-  /* TCP Server */
-  bool                  checkPassword(const QString& password);
+  /**
+    Parses the command line parameters.
+
+    @return False if the parameters failed to be parsed, true otherwise
+  */
+  bool                  parseCommandLine();
+
+  /**
+    Checks if the password specified by the user is correct.
+
+    @param  password  The password
+    @return  True if its correct, false otherwise
+  */
+
   void                  addClient(const QString& host, quint16 port);
+  /**
+    Checks if the password specified by the user is correct.
+
+    @param  password  The password
+    @return  True if its correct, false otherwise
+  */
   void                  removeClient(const QString& host, quint16 port);
-  void                  parseAddClientCommand(const QString& args);
-  void                  parseRemoveClientCommand(const QString& args);
+
+  /**
+    The following functions sets the Team, Color, Nick and Ping
+    for all the bots on all servers that we are monitoring.
+  */
   void                  setTeam(const QString& team);
-  void                  listClients();
   void                  setColor(const QString& args);
   void                  setNick(const QString& args);
   void                  setPing(const QString& args);
+
+  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+  // The following functions are used to create the TelNet administration
+  // interface
+
+  /**
+    Checks if the password specified by the user is correct.
+
+    @param  password  The password
+    @return  True if its correct, false otherwise
+  */
+  bool                  checkPassword(const QString& password);
+
+  /**
+    Parses add client command coming from the user.
+
+    @param  args  The command arguments unparsed
+  */
+  void                  parseAddClientCommand(const QString& args);
+
+  /**
+    Parses remove client command coming from the user.
+
+    @param  args  The command arguments unparsed
+  */
+  void                  parseRemoveClientCommand(const QString& args);
+
+  /**
+    Lists all the servers we are monitoring.
+  */
+  void                  listClients();
+
+  /**
+    Displays a list of commands available to the user.
+  */
   void                  help();
-  bool                  parseCommandLine();
+
 
 private slots:
-  /* TCP server */
+  /**
+    Called everytime data arrives coming from the TelNet user.
+  */
   void                  onDataArrival();
+
+  /**
+    Called everytime an user is trying to connect to the TelNet interface.
+  */
   void                  onNewConnection();
+
+  /**
+    Called everytime an user is disconnected from the TelNet interface.
+  */
   void                  onDisconnection();
 };
 

+ 29 - 23
Client.cpp

@@ -8,11 +8,11 @@
 #include "App.h"
 #include "Settings.h"
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Client::Client(App *app, ActiveClient* ac):
   QWClient(),
   myActiveClient(ac),
   myApp(app),
-  myConnectionRetries(0),
   myOnServerFlag(false),
   mySpamMutedFlag(false),
   myQWMutedFlag(false),
@@ -32,6 +32,7 @@ Client::Client(App *app, ActiveClient* ac):
   mySpamBroadcastFloodTimer->setSingleShot(true);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Client::~Client()
 {
   delete myJoinMessageTimer;
@@ -41,6 +42,7 @@ Client::~Client()
   delete mySpamBroadcastFloodTimer;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::connect(const char *host, quint16 port)
 {
   myJoinMessageScheduled = true; //Hi message only scheduled at bot connection
@@ -48,29 +50,27 @@ void Client::connect(const char *host, quint16 port)
   QWClient::connect(host, port);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::say(const QString &msg)
 {
   QString cmd = "say " + msg;
   sendCmd(cmd.toAscii().data());
 }
 
-void Client::sayTeam(const QString &msg)
-{
-  QString cmd = "say_team " + msg;
-  sendCmd(cmd.toAscii().data());
-}
-
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::setTeam(const QString &msg)
 {
   sendCmd(QString("setinfo \"team\" \"" + msg + "\"").toAscii().data());
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::disconnect()
 {
   QWClient::disconnect();
   myOnServerFlag = false;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::print(const QString &msg)
 {
   QString str;
@@ -83,42 +83,32 @@ void Client::print(const QString &msg)
   myApp->print(str);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::setPlayerList(PlayerList &playerList)
 {
   myPlayerList = playerList;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onDisconnect()
 {
   print("Disconnected..\n");
   myOnServerFlag = false;
 }
 
-void Client::retryConnection()
-{
-  if(myConnectionRetries == ConnectionRetries)
-  {
-    print("Giving up!\n");
-    disconnect();
-    myOnServerFlag = false;
-    return;
-  }
-
-  print("Reconnecting...\n");
-  reconnect();
-  myConnectionRetries++;
-}
-
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool Client::isQWMuted() const
 {
   return myQWMutedFlag;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool Client::isSpamMuted() const
 {
   return mySpamMutedFlag;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::parsePrintedLine()
 {
   Player  player;
@@ -288,6 +278,7 @@ void Client::parsePrintedLine()
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Client::playerCount() const
 {
   Player c;
@@ -301,16 +292,19 @@ int Client::playerCount() const
   return pc;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::setMaxClients(int maxClients)
 {
   myMaxClients = maxClients;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Client::maxClients() const
 {
   return myMaxClients;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onPrint(int, const char *msg)
 {
   if(!strlen(msg))
@@ -330,11 +324,13 @@ void Client::onPrint(int, const char *msg)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool Client::isOnServer() const
 {
   return myOnServerFlag;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onError(const char *description)
 {
   QString desc(description);
@@ -349,6 +345,7 @@ void Client::onError(const char *description)
   myOnServerFlag = false;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
 {
   print(QString(levelName) + "\n");
@@ -357,26 +354,31 @@ void Client::onLevelChanged(int, const char *levelName, float, float, float, flo
   myQWMutedFlag = false;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onChallenge()
 {
   print("challenge\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onConnection()
 {
   print("connection\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onConnected()
 {
   print("connected\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onDownloadStarted(const char *fileName)
 {
   print("Download started " + QString(fileName) + "\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::run()
 {
   if(!myJoinMessageTimer->isActive() && !myJoinMessagePrinted)
@@ -395,17 +397,18 @@ void Client::run()
   QWClient::run();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onOOBPrint(const char *msg)
 {
   print(QString(msg));
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onStuffedCmd(const char *cmd)
 {
   QString strCmd(cmd);
   if(strCmd == "skins") //connection sequence complete
   {
-    myConnectionRetries = 0;
     myOnServerFlag = true;
 
     /* Only say hi if hi is scheduled */
@@ -418,6 +421,7 @@ void Client::onStuffedCmd(const char *cmd)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onDownloadProgress(int percent)
 {
   if(!(percent % 10))
@@ -434,11 +438,13 @@ void Client::onDownloadProgress(int percent)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::onDownloadFinished()
 {
   print("Download 100% finished.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 QString Client::parseNameFun(const QString &string)
 {
   QByteArray b(string.toAscii());

+ 186 - 13
Client.h

@@ -13,80 +13,253 @@ class QTime;
 class QTimer;
 class ActiveClient;
 
+/**
+  The QW BOT itself.
+
+  @author   Mihawk <luiz@netdome.biz>
+  @file     Client.h
+*/
 class Client : public QWClient
 {
 public:
+  /**
+    Constructor.
+
+    @param app Pointer to the application
+    @param ac  Pointer to the monitoring class
+  */
   Client(App* app, ActiveClient* ac);
+
+  /**
+    Destructor.
+  */
   ~Client();
+
+  /**
+    Connects the bot to the QW server specified.
+
+    @param host Server host
+    @param port Server port
+  */
   void connect(const char *host, quint16 port);
+
+  /**
+    Runs the QW client, must be called everyframe or so
+    otherwise the bot won't run.
+  */
   void run();
+
+  /**
+    Disconnects the QW client from the server.
+  */
   void disconnect();
+
+  /**
+    Says something on the server we are in.
+  */
   void say(const QString& msg);
-  void sayTeam(const QString& msg);
+
+  /**
+    Sets the bot team.
+  */
   void setTeam(const QString& msg);
-  void setSocket(class QTcpSocket* socket);
-  void retryConnection(); //unused
+
+  /**
+    Returns whether we are fully connected to a server.
+
+    @return True if fully connected, otherwise false
+  */
   bool isOnServer() const;
+
+  /**
+    Returns whether the frequencies are muted.
+
+    @return True if the frequency is muted, otherwise returns false
+  */
   bool isQWMuted() const;
   bool isSpamMuted() const;
+
+  /**
+    Sets the player list that are on this server
+    including the bot in it. This information comes
+    from the Query object outside this class.
+    FIXME: Make the bot parse the player list inside itself.
+
+    @param playerList The player list
+  */
   void setPlayerList(PlayerList &playerList);
+
+  /**
+    Sets the maxclients that can connect to this server
+    currenctly this information comes from the Query object.
+    FIXME: Make the bot gather this info himself, since he is
+    connected to the server.
+
+    @param  maxClients  The maximum number of clients that can connect to this
+                        particular server
+  */
   void setMaxClients(int maxClients);
 
 protected:
+  /**
+    Called everytime the level changes.
+
+    @param playerNum
+    @param levelName
+    @param gravity
+    @param stopSpeed
+    @param maxSpeed
+    @param spectatorMaxSpeed
+    @param accelerate
+    @param airAccelerate
+    @param waterAccelerate
+    @param friction
+    @param waterFriction
+    @param entGravity
+  */
   void onLevelChanged(int playerNum, const char *levelName, float gravity, float stopSpeed, float maxSpeed, float spectatorMaxSpeed, float accelerate, float airAccelerate, float waterAccelerate, float friction, float waterFriction, float entGravity);
+
+  /**
+    Called when the client is disconnected from the server.
+  */
   void onDisconnect();
+
+  /**
+    Called when the client receives information to be printed on the screen.
+
+    @param level Message level
+    @param msg   The message
+  */
   void onPrint(int level, const char *msg);
+
+  /**
+    Called on (respectively):
+    Challenge response.
+    Connection acknowledged.
+    Connected. (Not fully connected yet, signon start)
+  */
   void onChallenge();
   void onConnection();
   void onConnected();
+
+  /**
+    Called when download of a file started.
+
+    @param fileName The file to be downloaded
+  */
   void onDownloadStarted(const char *fileName);
+
+  /**
+    Called when the last download finished.
+  */
   void onDownloadFinished();
+
+  /**
+    Called when the progress for a download changed.
+
+    @param percent Actual download percentage
+  */
   void onDownloadProgress(int percent);
+
+  /**
+    Called when the server stuffs cmd into the client.
+
+    @param cmd The command being stuffed into the client
+  */
   void onStuffedCmd(const char *cmd);
+
+  /**
+    Called when an error occurs.
+
+    @param description The error description
+  */
   void onError(const char *description);
+
+  /**
+    Called when the client receives an out-of-band print.
+
+    @param msg The message to be printed
+  */
   void onOOBPrint(const char *msg);
 
 private:
+  // The monitor that monitors the server that this client connects to
   ActiveClient*     myActiveClient;
-  App*							myApp;
 
-  int								myConnectionRetries;
-  static const int	ConnectionRetries = 10;
+  // Pointer to the application
+  App*							myApp;
 
+  // The last line printed into our client
   QString						myPrintLine;
 
+  // Flag to avoid information to repeat on the client
   bool							myDownloadProgressPrintedFlag;
 
+  // Flag to indicate if the client is fully connected
   bool              myOnServerFlag;
 
+  // Flags to indicate whether frequencies are muted or not
   bool              mySpamMutedFlag;
   bool              myQWMutedFlag;
 
-  QTimer*           myJoinMessageTimer;             //after that time the bot will say hi.
-  QTimer*           myKeepNickTimer;                //interval in which the bot tries to retake his nickname
+  QTimer*           myJoinMessageTimer; // After that time the bot will say hi.
+  QTimer*           myKeepNickTimer;    // Interval in which the bot tries to retake his nickname
 
-  /* flood timers */
-  QTimer*           myFloodTimer;                   //floodtimer for all commands
-  QTimer*           myQWBroadcastFloodTimer;        //floodtimer for .qw broadcasts
-  QTimer*           mySpamBroadcastFloodTimer;      //floodtimer for .spam broadcasts
+  // Flood timers
+  QTimer*           myFloodTimer;              // Floodtimer for all commands
+  QTimer*           myQWBroadcastFloodTimer;   // Floodtimer for .qw broadcasts
+  QTimer*           mySpamBroadcastFloodTimer; // Floodtimer for .spam broadcasts
 
-  /* time when floodtimer was started for calc time left */
+  // Used to calculate how much time is there left on the flood timers
   QTime             myFloodTimerStart;
   QTime             myQWBroadcastFloodTimerStart;
   QTime             mySpamBroadcastFloodTimerStart;
 
+  // Used to avoid information printed countless times
   bool              myJoinMessagePrinted;
   bool							myFloodMsgPrinted;
+
+  // Inicates whether bot has to say Hi
   bool              myJoinMessageScheduled;
 
+  // List of players on this server we are connected to
   PlayerList        myPlayerList;
+
+  // Max clients allowed on this server
   int               myMaxClients;
 
+  /**
+    Prints a message directly to the console or telnet user.
+
+    @param msg The message
+
+  */
   void print(const QString& msg);
+
+  /**
+    Parses the last line that was printed.
+  */
   void parsePrintedLine();
+
+  /**
+    Returns the player count on this server.
+
+    @return The player count
+  */
   int  playerCount() const;
+
+  /**
+    Returns the maximum number of clients allowed on this server.
+
+    @return The maximum number of clients allowed
+  */
   int  maxClients() const;
 
+  /**
+    Parses any namefun string and converts it to readable text.
+
+    @param string The text to be parsed
+  */
   static QString parseNameFun(const QString& string);
 };
 

+ 18 - 3
ServerQuery.cpp

@@ -18,13 +18,13 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 */
 
 #include "ServerQuery.h"
-
 #include <QUdpSocket>
 #include <QHostInfo>
 #include <QStringList>
 #include <QRegExp>
 #include <QTime>
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ServerQuery::ServerQuery(QObject *parent) :
   QObject(parent),
   mySocket(new QUdpSocket(this)),
@@ -34,17 +34,20 @@ ServerQuery::ServerQuery(QObject *parent) :
   connect(mySocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError(QAbstractSocket::SocketError)));
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ServerQuery::~ServerQuery()
 {
   delete mySocket;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ServerQuery::run()
 {
   if(mySocket->hasPendingDatagrams())
     parseServerInfo();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool ServerQuery::query(bool pingServer)
 {
   myActiveFlag = true;
@@ -88,16 +91,19 @@ bool ServerQuery::query(bool pingServer)
   return true;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 const QHostAddress &ServerQuery::address() const
 {
   return myAddress;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 quint16 ServerQuery::port() const
 {
   return myPort;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool ServerQuery::setAddress(const QHostAddress &address, quint16 port)
 {
   myAddress = address;
@@ -105,6 +111,7 @@ bool ServerQuery::setAddress(const QHostAddress &address, quint16 port)
   return true;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool ServerQuery::setAddress(const QString &address, quint16 port)
 {
   QHostInfo hi = QHostInfo::fromName(address);
@@ -119,6 +126,7 @@ bool ServerQuery::setAddress(const QString &address, quint16 port)
   return true;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ServerQuery::parseServerInfo()
 {
   QByteArray serverData = mySocket->readAll();
@@ -221,17 +229,20 @@ void ServerQuery::parseServerInfo()
   return;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool ServerQuery::isActive() const
 {
   return myActiveFlag;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ServerQuery::socketError(QAbstractSocket::SocketError)
 {
   emit error(UnknownError);
   myActiveFlag = false;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int ServerQuery::ping(int count, int timeout)
 {
   int pong = 0;
@@ -258,16 +269,19 @@ int ServerQuery::ping(int count, int timeout)
   return pong/count;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 PlayerList ServerQuery::playerList() const
 {
   return myPlayers;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ServerRules ServerQuery::serverRules() const
 {
   return myRules;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 QString ServerQuery::serverRuleValue(const QString &ruleName) const
 {
   ServerRule r;
@@ -279,13 +293,13 @@ QString ServerQuery::serverRuleValue(const QString &ruleName) const
   return QString();
 }
 
-//========================================================================
-/* Name fun */
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 char ServerQuery::ourReadableCharsTable[256] = {	'.', '_' , '_' , '_' , '_' , '.' , '_' , '_' , '_' , '_' , '\n' , '_' , '\n' , '>' , '.' , '.',
     '[', ']', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_', '_', '_'
                                                };
 bool ServerQuery::ourReadableCharsTableInitialized = false;
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ServerQuery::fillReadableCharsTable()
 {
   int i;
@@ -303,6 +317,7 @@ void ServerQuery::fillReadableCharsTable()
   ourReadableCharsTableInitialized = true;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 QString ServerQuery::convertNameFun(const QString &name)
 {
   if(!ourReadableCharsTableInitialized)

+ 117 - 15
ServerQuery.h

@@ -17,9 +17,6 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 */
 
-/**
-	Requests a server information and returns everything parsed in a ServerInfo struct
-*/
 #ifndef SERVERQUERY_H
 #define SERVERQUERY_H
 
@@ -28,6 +25,7 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 
 class QUdpSocket;
 
+// Player info struct
 struct Player
 {
   int     id;
@@ -43,6 +41,7 @@ struct Player
 };
 typedef QList<Player> PlayerList;
 
+// Server info struct
 struct ServerRule
 {
   QString   rule;
@@ -50,54 +49,157 @@ struct ServerRule
 };
 typedef QList<ServerRule> ServerRules;
 
+/**
+  This class gathers information for a given server
+  and returns the information in lists of information.
+
+  @author   Mihawk <luiz@netdome.biz>
+  @file     ServerQuery.h
+*/
 class ServerQuery : public QObject
 {
   Q_OBJECT
 public:
-
+  // Error codes
   enum Error { NoError, TimedOutError, EmptyResponseError, InvalidResponseError, InvalidInfoStringError, InvalidPlayerInfoError, SendError, HostLookupError, ConnectionError, UnknownError };
 
+  /**
+    Constructor.
+
+    @param parent The parent object if possible
+  */
   explicit ServerQuery(QObject *parent = 0);
+
+  /**
+    Destructor.
+  */
   ~ServerQuery();
 
+  /**
+    Sets the address we are going to gather information from.
+
+    @param address The server address
+    @param port    The server port
+    @return True on success else false
+  */
   bool          setAddress(const QString &address, quint16 port = 27500);
   bool          setAddress(const QHostAddress &address, quint16 port = 27500);
-  const QHostAddress&  address() const;
-  quint16       port() const;
 
+  /**
+    Returns the address and port of the server we are querying.
+  */
+  const QHostAddress& address() const;
+  quint16 port() const;
+
+  /**
+    Start a new query on the server, this function is non-blocking
+    run() must be called for updates on the current query.
+
+    @param pingServer Whether we should gather ping information too (this blocks the query)
+    @return True on success, false otherwise
+  */
   bool          query(bool pingServer = false);
+
+  /**
+    Must be called when a query is active.
+  */
   void          run();
+
+  /**
+    Returns whether a query is active or not.
+
+    @return True if we are now in the middle of a query process, false otherwise
+  */
   bool          isActive() const;
 
+  /**
+    Returns the player list connected to this server we just gathered info
+    with all their information.
+
+    @return The player list
+  */
   PlayerList    playerList() const;
+
+  /**
+    Returns all the information gathered from the server.
+
+    @return The server rules (as we call it)
+  */
   ServerRules   serverRules() const;
+
+  /**
+    Searches and returns a given rule value from the Server rules list
+    we gathered.
+
+    @param ruleName The rule to look for
+    @return The rule's value
+  */
   QString       serverRuleValue(const QString& ruleName) const;
 
 signals:
+  /**
+    Emitted when an error occured during the information
+    gathering process.
+
+    @param err Error code
+  */
   void          error(ServerQuery::Error err);
+
+  /**
+    Emitted when the information gathering process finished
+    successfully.
+  */
   void          finished();
 
 private slots:
+  /**
+    Parses the server information response from server internally.
+  */
   void          parseServerInfo();
+
+  /**
+    Called when an error occured on the socket level.
+
+    @param err Error code
+  */
   void          socketError(QAbstractSocket::SocketError err);
 
 private:
-  QUdpSocket*   mySocket;
-  QHostAddress	myAddress;
-  quint16				myPort;
-  PlayerList    myPlayers;
-  ServerRules   myRules;
-  quint16       myPing;
-  bool          myIsProxyFlag;
-  bool          myActiveFlag;
+  QUdpSocket*   mySocket;      // The socket used to do the query
+  QHostAddress	myAddress;     // The address of the server
+  quint16				myPort;        // The port of the server
+  PlayerList    myPlayers;     // The parsed player list
+  ServerRules   myRules;       // The parsed server information list
+  quint16       myPing;        // The ping from us to the server
+  bool          myIsProxyFlag; // Indicates whether this server is a proxy
+  bool          myActiveFlag;  // Indicates whether we are in the middle of a query process
+
+  /**
+    Pings the server we are gathering information from.
+    Note: This function blocks code until its finished.
+    FIXME: Make this function non-blocking too.
+
+    @param  count   Number of times we should ping the server
+    @param  timeout How long to wait for a server reply
+    @return The average ping
+  */
+  int           ping(int count = 3, int timeout = 1000);
 
+  // Tables used for namefun conversion
   static char   ourReadableCharsTable[256];
   static bool   ourReadableCharsTableInitialized;
 
-  int           ping(int count = 3, int timeout = 1000);
+  /**
+    Initializes namefun conversion tables.
+  */
   static void   fillReadableCharsTable();
 
 public:
+  /**
+    Converts namefun text to normal text.
+
+    @param name The text/name to convert
+  */
   static QString convertNameFun(const QString &name);
 };
 

+ 21 - 0
Settings.cpp

@@ -23,19 +23,23 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #include <QStringList>
 #include <stdio.h>
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Settings* Settings::ourInstance = NULL;
 QSettings* Settings::ourSettings;
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Settings::Settings()
 {
   ourSettings = new QSettings(QCoreApplication::applicationDirPath() + "/cimsqwbot.cfg", QSettings::IniFormat);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Settings::~Settings()
 {
   delete ourSettings;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool Settings::changeConfigFile(const QString &fileName)
 {
   delete ourSettings;
@@ -52,6 +56,7 @@ bool Settings::changeConfigFile(const QString &fileName)
   return true;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Settings* Settings::globalInstance()
 {
   if(!ourInstance)
@@ -59,6 +64,7 @@ Settings* Settings::globalInstance()
   return ourInstance;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 QStringList Settings::serverList()
 {
   QStringList list;
@@ -74,6 +80,7 @@ QStringList Settings::serverList()
   return list;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Settings::setServerList(QStringList &list)
 {
   ourSettings->beginWriteArray("servers");
@@ -85,71 +92,85 @@ void Settings::setServerList(QStringList &list)
   ourSettings->endArray();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 QString Settings::quakeFolder() const
 {
   return ourSettings->value("quakeFolder", QCoreApplication::applicationDirPath()).toString();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 QString Settings::botName() const
 {
   return ourSettings->value("botName", "[ServeMe]").toString();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Settings::botPing() const
 {
   return ourSettings->value("botPing", 666).toInt();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Settings::botTopColor() const
 {
   return ourSettings->value("botTopColor", 11).toInt();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Settings::botBottomColor() const
 {
   return ourSettings->value("botBottomColor", 12).toInt();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool Settings::botSpectator() const
 {
   return ourSettings->value("botSpectator", true).toBool();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Settings::floodProtTime() const
 {
   return qBound<int>(6, ourSettings->value("floodProtTime", 6).toInt(), 9999);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Settings::qwFloodProtTime() const
 {
   return ourSettings->value("qwFloodProtTime", 600).toInt();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Settings::spamFloodProtTime() const
 {
   return ourSettings->value("spamFloodProtTime", 300).toInt();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 unsigned int Settings::queryInterval() const
 {
   return ourSettings->value("queryInterval", 1000).toUInt();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Settings::timeToSayHiAfterConnected() const
 {
   return ourSettings->value("timeToSayHiAfterConnected", 7).toInt();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 int Settings::timeToWaitForCountReply() const
 {
   return ourSettings->value("timeToWaitForCountReply", 7).toInt();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool Settings::developerMode() const
 {
   return ourSettings->value("developerMode", false).toBool();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Settings::save()
 {
   ourSettings->setValue("quakeFolder", quakeFolder());

+ 32 - 3
Settings.h

@@ -24,15 +24,40 @@ class QSettings;
 class QString;
 class QStringList;
 
+/**
+  This class provides direct settings file access for the whole application.
+
+  @author   Mihawk <luiz@netdome.biz>
+  @file     Settings.h
+*/
 class Settings
 {
 public:
+  /**
+    The class single instance.
+
+    @return The settings class
+  */
   static Settings*  globalInstance();
+
+  /**
+    Changes the config file used by us.
+
+    @param fileName The filename to be used
+    @return True if the file was loaded successfully, false otherwise
+  */
   bool              changeConfigFile(const QString& fileName);
+
+  /**
+    Gets and sets the serverlist
+  */
   QStringList       serverList();
   void              setServerList(QStringList& list);
-  QString           quakeFolder() const;
 
+  /**
+    Config file direct parameter accessors
+  */
+  QString           quakeFolder() const;
   QString           botName() const;
   int               botPing() const;
   int               botTopColor() const;
@@ -46,12 +71,16 @@ public:
   int               timeToWaitForCountReply() const;
   bool              developerMode() const;
 
+  /**
+    Save current settings to the file.
+  */
   void              save();
 
 private:
-  static Settings*  ourInstance;
-  static QSettings* ourSettings;
+  static Settings*  ourInstance; // Myself
+  static QSettings* ourSettings; // The QSettings object
 
+  // Disable direct object creation, copy and destruction
   Settings();
   ~Settings();
   Settings(Settings&) {}

+ 14 - 1
SshClient.cpp

@@ -27,6 +27,7 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #include <QStringList>
 #include <QDebug>
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 SshClient::SshClient(App* app, QObject *parent) :
   QObject(parent),
   myApp(app),
@@ -42,17 +43,20 @@ SshClient::SshClient(App* app, QObject *parent) :
   connect(myProcess, SIGNAL(finished(int)), SLOT(exited(int)));
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 SshClient::~SshClient()
 {
   delete myCommandRegex;
   delete myClients;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool SshClient::isConnected() const
 {
   return myConnectedFlag;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::read()
 {
   QString data(myProcess->readAll().trimmed());
@@ -77,29 +81,34 @@ void SshClient::read()
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::exited(int)
 {
   reconnect();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::ping()
 {
   write("PING\n");
   myPingTimerID = startTimer(30000);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::pong()
 {
   killTimer(myPingTimerID);
   myPongTimerID = startTimer(30000);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::write(const QString &data)
 {
   myProcess->write(data.toAscii());
   myProcess->waitForBytesWritten();
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::parse(const QDateTime &time, const QString &command, const QString &commandData)
 {
   /* JOINED - Another user has just joined central */
@@ -179,7 +188,7 @@ void SshClient::parse(const QDateTime &time, const QString &command, const QStri
     //QString frequency = a.cap(2);
     QString server = a.cap(3);
 
-    myApp->setReplyHash(server, hash);
+    myApp->setReplyHashAndWaitForReply(server, hash);
 
     return;
   }
@@ -231,6 +240,7 @@ void SshClient::parse(const QDateTime &time, const QString &command, const QStri
   myApp->print("Unparsed cmd from central: " + time.toString() + " " + command + " " + commandData + "\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 bool SshClient::connectToHost(const QString &user, const QString &host)
 {
   myProcess->start("ssh", QStringList() << user + "@" + host);
@@ -247,12 +257,14 @@ bool SshClient::connectToHost(const QString &user, const QString &host)
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::reconnect()
 {
   if(!myUsername.isEmpty() && !myHostname.isEmpty())
     connectToHost(myUsername, myHostname);
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::disconnectFromHost()
 {
   killTimer(myConnectionTimerID);
@@ -265,6 +277,7 @@ void SshClient::disconnectFromHost()
   myConnectedFlag = false;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void SshClient::timerEvent(QTimerEvent *e)
 {
   if(e->timerId() == myConnectionTimerID)

+ 79 - 1
SshClient.h

@@ -17,7 +17,6 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 */
 
-/* SSH client for connecting to QWNET central */
 #ifndef SSHCLIENT_H
 #define SSHCLIENT_H
 
@@ -29,29 +28,94 @@ class QDateTime;
 class QStringList;
 class App;
 
+/**
+  SSH Client exclusively for connecting to QWNET's central.
+  It depends on openssh installed on the system and opens a process
+  with the ssh binary to make all the communication.
+
+  @author   Mihawk <luiz@netdome.biz>
+  @file     SshClient.h
+*/
 class SshClient : public QObject
 {
   Q_OBJECT
 
 public:
+  // Error codes
   enum Error { NoError, ConnectionTimedOutError };
 
+  /**
+    Constructor
+
+    @param app    The main application
+    @param parent The parent object if possible
+  */
   explicit SshClient(App* app, QObject *parent = 0);
+
+  /**
+    Destructor
+  */
   ~SshClient();
+
+  /**
+    Connects to a given ssh server.
+
+    @param user Username
+    @param host Hostname
+  */
   bool connectToHost(const QString& user, const QString &host);
+
+  /**
+    Returns whether we are connected right now.
+
+    @return True if connected, false otherwise
+  */
   bool isConnected() const;
+
+  /**
+    Disconnects from current server.
+  */
   void disconnectFromHost();
+
+  /**
+    Writes data to the server.
+
+    @param data Data to write
+  */
   void write(const QString& data);
 
 signals:
+  /**
+    Emitted when an error occurs.
+
+    @param errorCode The error code
+  */
   void error(Error errorCode);
+
+  /**
+    Emitted when we just connected to a server.
+  */
   void connected();
 
 protected:
+  /**
+    Handles events that occur periodically like PING PONG and connection timeout.
+
+    @param e Pointer to the event that happened
+  */
   void timerEvent(QTimerEvent *e);
 
 private slots:
+  /**
+    Called when there is data to be read coming from the server.
+  */
   void read();
+
+  /**
+    Called when the ssh binary process has exited.
+
+    @param exitCode The exit code
+  */
   void exited(int exitCode);
 
 private:
@@ -66,11 +130,25 @@ private:
   QString   myUsername;
   QString   myHostname;
 
+  /**
+    Parses all commands coming from the server and execute the appropriate
+    command.
+
+    @param time        Time the message arrived
+    @param command     The command that just arrived
+    @param commandData The command data
+  */
   void parse(const QDateTime& time, const QString& command, const QString& commandData);
 
+  /**
+    Ping and pong functions used to keep the connection alive.
+  */
   void ping();
   void pong();
 
+  /**
+    Reconnects to the server.
+  */
   void reconnect();
 };
 

+ 28 - 0
main.cpp

@@ -1,5 +1,33 @@
+/*
+GNU General Public License version 3 notice
+
+Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see < http://www.gnu.org/licenses/ >.
+*/
+
 #include "App.h"
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+/**
+  Main app starting point.
+
+  @param argc Command line arg count
+  @param argv Command line arg array
+  @return Exit code
+*/
 int main(int argc, char **argv)
 {
   App a(argc, argv);