Преглед изворни кода

Added s-p support.
Fixed problem on nick search.

Mihawk пре 12 година
родитељ
комит
483fa1c931
8 измењених фајлова са 113 додато и 63 уклоњено
  1. 2 2
      ActiveClient.cpp
  2. 2 1
      ActiveClient.h
  3. 12 8
      App.cpp
  4. 53 38
      Client.cpp
  5. 15 2
      Client.h
  6. 2 2
      ServerQuery.cpp
  7. 12 6
      Settings.cpp
  8. 15 4
      Settings.h

+ 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, QObject *parent):
+ActiveClient::ActiveClient(App *app, bool supportsSendPrivate, QObject *parent):
   QObject(parent),
   myApp(app),
-  myClient(new Client(app, this)),
+  myClient(new Client(app, this, supportsSendPrivate)),
   myQuery(new ServerQuery(this)),
   myDisconnectTime(new QTime()),
   myBroadcastReplyTimer(new QTimer(this)),

+ 2 - 1
ActiveClient.h

@@ -47,9 +47,10 @@ public:
     Creates a server monitoring object.
 
     @param  app    The application creator of this object
+    @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, QObject *parent = 0);
+  ActiveClient(App* app, bool supportsSendPrivate = 0, QObject *parent = 0);
 
   /**
     Destructor

+ 12 - 8
App.cpp

@@ -133,14 +133,13 @@ void App::onDisconnection()
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::loadServerList()
 {
-  QStringList list = Settings::globalInstance()->serverList();
-  QString sv;
+  Settings::ServerList list = Settings::globalInstance()->serverList();
+  Settings::Server sv;
 
   foreach(sv, list)
   {
-    ActiveClient* ac = new ActiveClient(this);
-    QStringList parms = sv.split(':');
-    ac->setAddress(QHostAddress(parms.at(0)), parms.at(1).toUShort());
+    ActiveClient* ac = new ActiveClient(this, sv.supportsSendPrivate);
+    ac->setAddress(QHostAddress(sv.address), sv.port);
     ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toAscii().data());
     ac->client()->setName(Settings::globalInstance()->botName().toAscii().data());
     ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
@@ -154,12 +153,17 @@ void App::loadServerList()
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::saveServerList()
 {
-  QStringList list;
+  Settings::ServerList list;
   ActiveClient* ac;
 
   foreach(ac, myClients)
-  list.push_back(ac->serverAddressString());
-
+  {
+    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);
 }
 

+ 53 - 38
Client.cpp

@@ -9,7 +9,7 @@
 #include "Settings.h"
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-Client::Client(App *app, ActiveClient* ac):
+Client::Client(App *app, ActiveClient* ac, bool supportsSendPrivate):
   QWClient(),
   myActiveClient(ac),
   myApp(app),
@@ -23,6 +23,7 @@ Client::Client(App *app, ActiveClient* ac):
   mySpamBroadcastFloodTimer(new QTimer()),
   myJoinMessagePrinted(false),
   myJoinMessageScheduled(false),
+  mySupportsSendPrivate(supportsSendPrivate),
   myMaxClients(0)
 {
   myJoinMessageTimer->setSingleShot(true);
@@ -51,9 +52,15 @@ void Client::connect(const char *host, quint16 port)
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-void Client::say(const QString &msg)
+void Client::say(const QString &msg, const QString &nickName)
 {
-  QString cmd = "say " + msg;
+  QString cmd("say ");
+
+  if(!nickName.isEmpty() && mySupportsSendPrivate)
+    cmd.append("s-p " + nickName + " ");
+
+  cmd.append(msg);
+
   sendCmd(cmd.toAscii().data());
 }
 
@@ -111,40 +118,42 @@ bool Client::isSpamMuted() const
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::parsePrintedLine()
 {
-  Player  player;
-  bool    parsed = false;
+  // Find the player that printed this line of text
+  Player  player, bestPlayer;
   quint16 lastMatchSize = 0;
 
-  QByteArray playerName;
-  QByteArray printLine(myPrintLine.toAscii());
-
-  QWClient::stripColor(printLine.data());
-
   foreach(player, myPlayerList)
   {
-    playerName = player.name.toAscii();
-    QWClient::stripColor(playerName.data());
-    if(printLine.startsWith(playerName) || (player.spectator && printLine.startsWith(QString("[SPEC] " + player.name).toAscii())))
+    if(player.spectator && myPrintLine.startsWith("[SPEC] " + player.name))
     {
-      if(player.spectator && printLine.startsWith("[SPEC] "))
+      if(lastMatchSize < (player.name.size() + 7))
       {
-        if(lastMatchSize < (playerName.size() + 7))
-          lastMatchSize = (playerName.size() + 7);
+        lastMatchSize = (player.name.size() + 7);
+        bestPlayer = player;
       }
-      else if(lastMatchSize < playerName.size())
+      continue;
+    }
+
+    if(myPrintLine.startsWith(player.name))
+    {
+      if(lastMatchSize < player.name.size())
       {
-        lastMatchSize = playerName.size();
+        lastMatchSize = player.name.size();
+        bestPlayer = player;
       }
-      parsed = true;
+      continue;
     }
   }
-  if(!parsed)
+  if(!lastMatchSize)
     return;
 
-  QString nick(myPrintLine.left(lastMatchSize));
+  QString nick(bestPlayer.name);
+  if(bestPlayer.spectator)
+    nick.prepend("(spec) ");
+
   QString message(myPrintLine.right(myPrintLine.size() - lastMatchSize));
 
-  QRegExp regex("^:\\s+\\.(spam|qw|help|qw_mute|qw_unmute|spam_mute|spam_unmute|lastmsgs)\\s*(.+)$");
+  QRegExp regex("^:\\s+\\.(spam|qw|help|qw_mute|qw_unmute|spam_mute|spam_unmute|lm)\\s*(.+)$");
   if(regex.indexIn(message) == -1)
     return;
 
@@ -155,7 +164,7 @@ void Client::parsePrintedLine()
   {
     if(!myFloodMsgPrinted)
     {
-      say("> FloodProt: Not so fast, wait " + QString::number(floodProtTime + currentTime.secsTo(myFloodTimerStart)) + " sec(s).");
+      say("FloodProt: Not so fast, wait " + QString::number(floodProtTime + currentTime.secsTo(myFloodTimerStart)) + " sec(s).", bestPlayer.name);
       myFloodMsgPrinted = true;
     }
     return;
@@ -169,11 +178,9 @@ void Client::parsePrintedLine()
 
   if(command == "help")
   {
-    //say("> Commands:");
-    say("> Broadcast a message: .qw <message> and .spam <message>");
-    //, beware of the floodprot limits 600s for .qw and 300s for .spam");
-    say("> (Un)Mute: .qw_mute .qw_unmute or .spam_mute .spam_unmute");
-    say("> Last 5 messages: .lastmsgs");
+    say("Broadcast a message: .qw <message> and .spam <message>", bestPlayer.name);
+    say("(Un)Mute: .qw_mute .qw_unmute or .spam_mute .spam_unmute", bestPlayer.name);
+    say("Last 5 messages: .lm", bestPlayer.name);
 
     return;
   }
@@ -182,7 +189,7 @@ void Client::parsePrintedLine()
   {
     if(!args.trimmed().size())
     {
-      say("> The format is ." + command + " <message>.");
+      say("The format is ." + command + " <message>.", bestPlayer.name);
       return;
     }
 
@@ -192,7 +199,7 @@ void Client::parsePrintedLine()
       int qwFloodProtTime = Settings::globalInstance()->qwFloodProtTime();
       if(myQWBroadcastFloodTimer->isActive())
       {
-        say("> FloodProt: Wait " + QString::number(qwFloodProtTime + currentTime.secsTo(myQWBroadcastFloodTimerStart)) + " secs before new .qw");
+        say("> FloodProt: Wait " + QString::number(qwFloodProtTime + currentTime.secsTo(myQWBroadcastFloodTimerStart)) + " secs before new .qw", bestPlayer.name);
         return;
       }
       myQWBroadcastFloodTimerStart = currentTime;
@@ -203,7 +210,7 @@ void Client::parsePrintedLine()
       int spamFloodProtTime = Settings::globalInstance()->spamFloodProtTime();
       if(mySpamBroadcastFloodTimer->isActive())
       {
-        say("> FloodProt: Wait " + QString::number(spamFloodProtTime + currentTime.secsTo(mySpamBroadcastFloodTimerStart)) + " secs before new .spam");
+        say("FloodProt: Wait " + QString::number(spamFloodProtTime + currentTime.secsTo(mySpamBroadcastFloodTimerStart)) + " secs before new .spam", bestPlayer.name);
         return;
       }
       mySpamBroadcastFloodTimerStart = currentTime;
@@ -225,43 +232,45 @@ void Client::parsePrintedLine()
     else
       myApp->requestBroadcast("dev", nick, server, parsedMsg);
 
+    say("Broadcasting...", bestPlayer.name);
+
     return;
   }
 
   if(command == "qw_mute")
   {
-    say("> .qw Frequency Muted.");
+    say(".qw Frequency Muted.", bestPlayer.name);
     myQWMutedFlag = true;
     return;
   }
 
   if(command == "qw_unmute")
   {
-    say("> .qw Frequency Unmuted.");
+    say(".qw Frequency Unmuted.", bestPlayer.name);
     myQWMutedFlag = false;
     return;
   }
 
   if(command == "spam_mute")
   {
-    say("> .spam Frequency Muted.");
+    say(".spam Frequency Muted.", bestPlayer.name);
     mySpamMutedFlag = true;
     return;
   }
 
   if(command == "spam_unmute")
   {
-    say("> .spam Frequency Unmuted.");
+    say(".spam Frequency Unmuted.", bestPlayer.name);
     mySpamMutedFlag = false;
     return;
   }
 
-  if(command == "lastmsgs")
+  if(command == "lm")
   {
     QStringList messages = myApp->lastMessages();
     if(!messages.size())
     {
-      say("None");
+      say("None", bestPlayer.name);
       return;
     }
 
@@ -272,7 +281,7 @@ void Client::parsePrintedLine()
       if(++i > 5)
         break;
 
-      say(msg);
+      say(msg, bestPlayer.name);
     }
     return;
   }
@@ -444,6 +453,12 @@ void Client::onDownloadFinished()
   print("Download 100% finished.\n");
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+bool Client::supportsSendPrivate() const
+{
+  return mySupportsSendPrivate;
+}
+
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 QString Client::parseNameFun(const QString &string)
 {

+ 15 - 2
Client.h

@@ -27,8 +27,9 @@ public:
 
     @param app Pointer to the application
     @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);
+  Client(App* app, ActiveClient* ac, bool supportsSendPrivate = false);
 
   /**
     Destructor.
@@ -56,8 +57,9 @@ public:
 
   /**
     Says something on the server we are in.
+    If the nickname is specified s-p will be used if possible.
   */
-  void say(const QString& msg);
+  void say(const QString& msg, const QString& nickName = "");
 
   /**
     Sets the bot team.
@@ -100,6 +102,13 @@ public:
   */
   void setMaxClients(int maxClients);
 
+  /**
+    Returns whether this server supports s-p feature or not.
+
+    @return  True if supports, otherwise false
+  */
+  bool supportsSendPrivate() const;
+
 protected:
   /**
     Called everytime the level changes.
@@ -222,6 +231,10 @@ private:
   // Inicates whether bot has to say Hi
   bool              myJoinMessageScheduled;
 
+  // Indicates whether this server supports s-p command
+  // This is set manually the client doesn't auto detect it
+  bool              mySupportsSendPrivate;
+
   // List of players on this server we are connected to
   PlayerList        myPlayerList;
 

+ 2 - 2
ServerQuery.cpp

@@ -201,11 +201,11 @@ void ServerQuery::parseServerInfo()
     player.frags = playerInfo.at(2).toInt();
     player.time = playerInfo.at(3).toInt();
     player.ping = playerInfo.at(4).toInt();
-    player.name = convertNameFun(playerInfo.at(5));
+    player.name = playerInfo.at(5);
     player.skin = playerInfo.at(6);
     player.topColor = playerInfo.at(7).toInt();
     player.bottomColor = playerInfo.at(8).toInt();
-    player.team = convertNameFun(playerInfo.at(9));
+    player.team = playerInfo.at(9);
 
     if(player.name.startsWith("\\s\\"))
     {

+ 12 - 6
Settings.cpp

@@ -65,15 +65,20 @@ Settings* Settings::globalInstance()
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-QStringList Settings::serverList()
+Settings::ServerList Settings::serverList()
 {
-  QStringList list;
+  ServerList list;
 
   int size = ourSettings->beginReadArray("servers");
   for(int i = 0; i < size; ++i)
   {
     ourSettings->setArrayIndex(i);
-    list.append(ourSettings->value("address").toString());
+    QStringList svaddr = ourSettings->value("address").toString().split(":");
+    Server sv;
+    sv.address = svaddr.at(0);
+    sv.port = svaddr.at(1).toUShort();
+    sv.supportsSendPrivate = ourSettings->value("supportsSendPrivate", false).toBool();
+    list.append(sv);
   }
   ourSettings->endArray();
 
@@ -81,13 +86,14 @@ QStringList Settings::serverList()
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-void Settings::setServerList(QStringList &list)
+void Settings::setServerList(ServerList &list)
 {
   ourSettings->beginWriteArray("servers");
   for(int i = 0; i < list.size(); ++i)
   {
     ourSettings->setArrayIndex(i);
-    ourSettings->setValue("address", list.at(i));
+    ourSettings->setValue("address", list.at(i).address + ":" + QString::number(list.at(i).port));
+    ourSettings->setValue("supportsSendPrivate", list.at(i).supportsSendPrivate);
   }
   ourSettings->endArray();
 }
@@ -187,6 +193,6 @@ void Settings::save()
   ourSettings->setValue("timeToWaitForCountReply", timeToWaitForCountReply());
   ourSettings->setValue("developerMode", developerMode());
 
-  QStringList list = serverList();
+  ServerList list = serverList();
   setServerList(list);
 }

+ 15 - 4
Settings.h

@@ -20,9 +20,10 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #ifndef SETTINGS_H
 #define SETTINGS_H
 
+#include <QList>
+#include <QString>
+
 class QSettings;
-class QString;
-class QStringList;
 
 /**
   This class provides direct settings file access for the whole application.
@@ -33,6 +34,16 @@ class QStringList;
 class Settings
 {
 public:
+  // Server information struct
+  // Struct to save server information from the config file
+  struct Server
+  {
+    QString address;
+    quint16 port;
+    bool    supportsSendPrivate;
+  };
+  typedef QList<Server> ServerList;
+
   /**
     The class single instance.
 
@@ -51,8 +62,8 @@ public:
   /**
     Gets and sets the serverlist
   */
-  QStringList       serverList();
-  void              setServerList(QStringList& list);
+  ServerList        serverList();
+  void              setServerList(ServerList& list);
 
   /**
     Config file direct parameter accessors