Explorar o código

Removed variable supportSP from config file. Auto s-p detection feature
created.

Mihawk %!s(int64=12) %!d(string=hai) anos
pai
achega
903985ed10
Modificáronse 9 ficheiros con 42 adicións e 80 borrados
  1. 2 2
      ActiveClient.cpp
  2. 1 1
      ActiveClient.h
  3. 3 17
      App.cpp
  4. 1 9
      App.h
  5. 25 23
      Client.cpp
  6. 5 8
      Client.h
  7. 2 9
      Settings.cpp
  8. 0 1
      Settings.h
  9. 3 10
      SshClient.cpp

+ 2 - 2
ActiveClient.cpp

@@ -26,10 +26,10 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #include "Settings.h"
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-ActiveClient::ActiveClient(App *app, bool supportsSendPrivate, QObject *parent):
+ActiveClient::ActiveClient(App *app, QObject *parent):
   QObject(parent),
   myApp(app),
-  myClient(new Client(app, this, supportsSendPrivate)),
+  myClient(new Client(app, this)),
   myQuery(new ServerQuery(this)),
   myDisconnectTime(new QTime()),
   myBroadcastReplyTimer(new QTimer(this)),

+ 1 - 1
ActiveClient.h

@@ -50,7 +50,7 @@ public:
     @param  supportsSendPrivate Indicates if this server we are monitoring supports sendPrivate
     @param  parent The parent object (should be the same as the app param)
   */
-  ActiveClient(App* app, bool supportsSendPrivate = 0, QObject *parent = 0);
+  ActiveClient(App* app, QObject *parent = 0);
 
   /**
     Destructor

+ 3 - 17
App.cpp

@@ -137,7 +137,7 @@ void App::loadServerList()
       print("Maximum number of servers allowed reached, some servers weren't added to the monitoring list.\n");
       return;
     }
-    addClient(sv.address, sv.port, sv.supportsSendPrivate);
+    addClient(sv.address, sv.port);
   }
 }
 
@@ -152,7 +152,6 @@ void App::saveServerList()
     Settings::Server sv;
     sv.address = ac->serverAddressString().split(":").at(0);
     sv.port = ac->serverAddressString().split(":").at(1).toUShort();
-    sv.supportsSendPrivate = ac->client()->supportsSendPrivate();
     list.push_back(sv);
   }
   Settings::globalInstance()->setServerList(list);
@@ -176,20 +175,7 @@ void App::print(const QString &msg)
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-bool App::hasSendPrivateSupport(const QString &serverAddress) const
-{
-  ActiveClient* ac;
-
-  foreach(ac, myClients)
-  {
-    if(ac->serverAddressString() == serverAddress)
-      return ac->client()->supportsSendPrivate();
-  }
-  return false;
-}
-
-// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-bool App::addClient(const QString &host, quint16 port, bool sendPrivateSupport)
+bool App::addClient(const QString &host, quint16 port)
 {
   ActiveClient* ac;
   QHostAddress ha(host);
@@ -203,7 +189,7 @@ bool App::addClient(const QString &host, quint16 port, bool sendPrivateSupport)
     }
   }
 
-  ac = new ActiveClient(this, sendPrivateSupport, this);
+  ac = new ActiveClient(this, this);
   ac->setAddress(ha, port);
   ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toAscii().data());
   ac->client()->setName(Settings::globalInstance()->botName().toAscii().data());

+ 1 - 9
App.h

@@ -138,14 +138,6 @@ public:
   */
   QString               serverHostName(const QString& serverAddress) const;
 
-  /**
-    Returns whether a given server supports the s-p command.
-
-    @param serverAddress The server we need this info
-    @return True if it supports, false otherwise
-  */
-  bool                  hasSendPrivateSupport(const QString &serverAddress) const;
-
   /**
     Checks if the password specified by the user is correct.
 
@@ -153,7 +145,7 @@ public:
     @return  True if its correct, false otherwise
   */
 
-  bool                  addClient(const QString& host, quint16 port, bool sendPrivateSupport = false);
+  bool                  addClient(const QString& host, quint16 port);
   /**
     Checks if the password specified by the user is correct.
 

+ 25 - 23
Client.cpp

@@ -28,7 +28,7 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #include "Settings.h"
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-Client::Client(App *app, ActiveClient* ac, bool supportsSendPrivate):
+Client::Client(App *app, ActiveClient* ac):
   QWClient(),
   myActiveClient(ac),
   myApp(app),
@@ -40,7 +40,8 @@ Client::Client(App *app, ActiveClient* ac, bool supportsSendPrivate):
   myFloodTimer(new QTimer()),
   myQWBroadcastFloodTimer(new QTimer()),
   mySpamBroadcastFloodTimer(new QTimer()),
-  mySupportsSendPrivate(supportsSendPrivate),
+  mySPSupport(false),
+  myAutoDetectSP(true),
   myMaxClients(0)
 {
   myKeepNickTimer->setSingleShot(true);
@@ -71,7 +72,7 @@ void Client::say(const QString &msg, const QString &nickName)
 {
   QString cmd("say ");
 
-  if(!nickName.isEmpty() && mySupportsSendPrivate)
+  if(!nickName.isEmpty() && mySPSupport)
     cmd.append("s-p \"" + nickName + "\" ");
 
   cmd.append(msg);
@@ -133,6 +134,23 @@ bool Client::isSpamMuted() const
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::parsePrintedLine()
 {
+  // Detects whether s-p command is supported
+  if(mySPDetectionTimer->isActive())
+  {
+    if(myPrintLine.startsWith(Settings::globalInstance()->botName() + ": s-p"))
+    {
+      mySPSupport = false;
+      mySPDetectionTimer->stop();
+      setAutoDetectSP(false); // Don't try detecting again on next map
+    }
+    else if(myPrintLine.startsWith("usage: s-p id/name txt"))
+    {
+      mySPSupport = true;
+      mySPDetectionTimer->stop();
+      setAutoDetectSP(false); // Don't try detecting again on next map
+    }
+  }
+
   // Find the player that printed this line of text
   Player  player, bestPlayer;
   quint16 lastMatchSize = 0;
@@ -168,22 +186,6 @@ void Client::parsePrintedLine()
 
   QString message(myPrintLine.right(myPrintLine.size() - lastMatchSize));
 
-  // Detects whether s-p command is supported
-  if(mySPDetectionActive)
-  {
-    if(mySPDetectionTimer->isActive() && nick == Settings::globalInstance()->botName() && message == "s-p")
-    {
-      mySupportsSendPrivate = false;
-      mySPDetectionTimer->stop();
-      mySPDetectionActive = false;
-    }
-    else if(!mySPDetectionTimer->isActive())
-    {
-      mySupportsSendPrivate = true;
-      mySPDetectionTimer->stop();
-      mySPDetectionActive = false;
-    }
-  }
 
   QRegExp regex("^:\\s+\\.(spam|qw|help|qw_mute|qw_unmute|spam_mute|spam_unmute|lm)\\s*(.+)$");
   if(regex.indexIn(message) == -1)
@@ -469,7 +471,8 @@ void Client::onStuffedCmd(const char *cmd)
     myOnServerFlag = true;
 
     setPing(Settings::globalInstance()->botPing());
-    spDetection();
+    if(myAutoDetectSP)
+      spDetection();
   }
 }
 
@@ -497,9 +500,9 @@ void Client::onDownloadFinished()
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-bool Client::supportsSendPrivate() const
+void Client::setAutoDetectSP(bool autoDetect)
 {
-  return mySupportsSendPrivate;
+  myAutoDetectSP = autoDetect;
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -508,7 +511,6 @@ void Client::spDetection()
   say("s-p");
   mySPDetectionTimer->setSingleShot(true);
   mySPDetectionTimer->start(2000);
-  mySPDetectionActive = true;
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

+ 5 - 8
Client.h

@@ -48,7 +48,7 @@ public:
     @param ac  Pointer to the monitoring class
     @param supportsSendPrivate Indicates if this server supports s-p command (All messages are sent privately to the user on the server)
   */
-  Client(App* app, ActiveClient* ac, bool supportsSendPrivate = false);
+  Client(App* app, ActiveClient* ac);
 
   /**
     Destructor.
@@ -122,11 +122,9 @@ public:
   void setMaxClients(int maxClients);
 
   /**
-    Returns whether this server supports s-p feature or not.
-
-    @return  True if supports, otherwise false
+    Sets the client to try to autodetect s-p support.
   */
-  bool supportsSendPrivate() const;
+  void setAutoDetectSP(bool autoDetect);
 
 protected:
   /**
@@ -232,7 +230,6 @@ private:
 
   QTimer*           myKeepNickTimer;    // Interval in which the bot tries to retake his nickname
   QTimer*           mySPDetectionTimer; // Timer to wait for a say s-p command reply (if it replies there is support for it)
-  bool              mySPDetectionActive;
 
   // Flood timers
   QTimer*           myFloodTimer;              // Floodtimer for all commands
@@ -248,8 +245,8 @@ private:
   bool							myFloodMsgPrinted;
 
   // Indicates whether this server supports s-p command
-  // This is set manually the client doesn't auto detect it
-  bool              mySupportsSendPrivate;
+  bool              mySPSupport;
+  bool              myAutoDetectSP; // If this is true the client will try to auto-detect the SP support
 
   // List of players on this server we are connected to
   PlayerList        myPlayerList;

+ 2 - 9
Settings.cpp

@@ -75,17 +75,10 @@ Settings::ServerList Settings::serverList()
     ourSettings->setArrayIndex(i);
     QStringList svaddr = ourSettings->value("address").toString().split(":");
     Server sv;
-    if(svaddr.size() == 3)
+    if(svaddr.size() == 2)
     {
       sv.address = svaddr.at(0);
       sv.port = svaddr.at(1).toUShort();
-      sv.supportsSendPrivate = svaddr.at(2).toInt();
-    }
-    else if(svaddr.size() == 2)
-    {
-      sv.address = svaddr.at(0);
-      sv.port = svaddr.at(1).toUShort();
-      sv.supportsSendPrivate = false;
     }
     else
       continue;
@@ -103,7 +96,7 @@ void Settings::setServerList(ServerList &list)
   for(int i = 0; i < list.size(); ++i)
   {
     ourSettings->setArrayIndex(i);
-    ourSettings->setValue("address", list.at(i).address + ":" + QString::number(list.at(i).port) + ":" + QString::number(QVariant(list.at(i).supportsSendPrivate).toInt()));
+    ourSettings->setValue("address", list.at(i).address + ":" + QString::number(list.at(i).port));
   }
   ourSettings->endArray();
 }

+ 0 - 1
Settings.h

@@ -40,7 +40,6 @@ public:
   {
     QString address;
     quint16 port;
-    bool    supportsSendPrivate;
   };
   typedef QList<Server> ServerList;
 

+ 3 - 10
SshClient.cpp

@@ -300,20 +300,13 @@ void SshClient::parse(const QDateTime &time, const QString &command, const QStri
   /* REQ_ASSIGN */
   if(command == "REQ_ASSIGN")
   {
-    QRegExp rx("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d{1,5}),?(true|false)?");
+    QRegExp rx("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d{1,5})");
     if(rx.indexIn(commandData) == -1)
     {
       write("Malformed REQ_ASSIGN request.\n");
       return;
     }
-
-    bool spSupport = false;
-    if(rx.cap(3).size())
-    {
-      if(rx.cap(3) == "true")
-        spSupport = true;
-    }
-    if(myApp->addClient(rx.cap(1), rx.cap(2).toUShort(), spSupport))
+    if(myApp->addClient(rx.cap(1), rx.cap(2).toUShort()))
       write("ASSIGN_RE " + rx.cap(1) + ":" + rx.cap(2) + " OK\n");
     else
       write("ASSIGN_RE " + rx.cap(1) + ":" + rx.cap(2) + " FAILED Client already on my list.\n");
@@ -425,7 +418,7 @@ void SshClient::assignmentsReply(const QHostAddress &host, quint16 port, int ms)
 {
   QString fullAddress = host.toString() + ":" + QString::number(port);
 
-  write("ASSIGNMENTS_RE " + host.toString() + ":" + QString::number(port) + "," + QVariant(myApp->hasSendPrivateSupport(fullAddress)).toString() + "," + QString::number(ms) + "\n");
+  write("ASSIGNMENTS_RE " + host.toString() + ":" + QString::number(port) + "," + QString::number(ms) + "\n");
 
   sender()->deleteLater();
 }