]> jfr.im git - irc/znc/znc.git/commitdiff
refactor: added AddCommand instead of manual handling with OnModCommand
authorparadix <redacted>
Mon, 6 Jan 2020 16:06:50 +0000 (17:06 +0100)
committerparadix <redacted>
Mon, 6 Jan 2020 16:06:50 +0000 (17:06 +0100)
modules/watch.cpp

index 46819f88e02d5938b1dc019d5c30de62404a6977..7366f685586bb7054471436aeec4fb4883daf5e3 100644 (file)
@@ -16,7 +16,7 @@
 
 #include <znc/Chan.h>
 #include <znc/IRCNetwork.h>
-#include <znc/Query.h>                 
+#include <znc/Query.h>
 
 using std::list;
 using std::vector;
@@ -174,11 +174,61 @@ class CWatchEntry {
 class CWatcherMod : public CModule {
   public:
     MODCONSTRUCTOR(CWatcherMod) {
-        Load();
+        AddHelpCommand();
+        AddCommand("Add", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::Watch), "<HostMask> [Target] [Pattern]", "Used to add an entry to watch for.");        
+        AddCommand("List", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::List), "", "List all entries being watched.");        
+        AddCommand("Dump", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::Dump), "", "Dump a list of all current entries to be used later.");        
+        AddCommand("Del", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::Remove), "<Id>", "Deletes Id from the list of watched entries.");        
+        AddCommand("Clear", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::Clear), "", "Delete all entries.");        
+        AddCommand("Enable", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::Enable), "<Id | *>", "Enable a disabled entry.");        
+        AddCommand("Disable", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::Disable), "<Id | *>", "Disable (but don't delete) an entry.");        
+        AddCommand("SetDetachedClientOnly", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::SetDetachedClientOnly), "<Id | *> <True | False>", "Enable or disable detached client only for an entry.");        
+        AddCommand("SetDetachedChannelOnly", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::SetDetachedChannelOnly), "<Id | *> <True | False>", "Enable or disable detached channel only for an entry.");        
+        AddCommand("SetSources", static_cast<CModCommand::ModCmdFunc>(&CWatcherMod::SetSources), "<Id> [#chan priv #foo* !#bar]", "Set the source channels that you care about.");        
     }
 
     ~CWatcherMod() override {}
 
+
+    bool OnLoad(const CString& sArgs, CString& sMessage) override {
+        // Just to make sure we don't mess up badly
+        m_lsWatchers.clear();
+
+        bool bWarn = false;
+
+        for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) {
+            VCString vList;
+            it->first.Split("\n", vList);
+
+            // Backwards compatibility with the old save format
+            if (vList.size() != 5 && vList.size() != 7) {
+                bWarn = true;
+                continue;
+            }
+
+            CWatchEntry WatchEntry(vList[0], vList[1], vList[2]);
+            if (vList[3].Equals("disabled"))
+                WatchEntry.SetDisabled(true);
+            else
+                WatchEntry.SetDisabled(false);
+
+            // Backwards compatibility with the old save format
+            if (vList.size() == 5) {
+                WatchEntry.SetSources(vList[4]);
+            } else {
+                WatchEntry.SetDetachedClientOnly(vList[4].ToBool());
+                WatchEntry.SetDetachedChannelOnly(vList[5].ToBool());
+                WatchEntry.SetSources(vList[6]);
+            }
+            m_lsWatchers.push_back(WatchEntry);
+        }
+
+        if (bWarn)
+            sMessage = t_s("WARNING: malformed entry found while loading");
+        
+        return true;
+    }
+    
     void OnRawMode(const CNick& OpNick, CChan& Channel, const CString& sModes,
                    const CString& sArgs) override {
         Process(OpNick, "* " + OpNick.GetNick() + " sets mode: " + sModes +
@@ -271,64 +321,6 @@ class CWatcherMod : public CModule {
         return CONTINUE;
     }
 
-    void OnModCommand(const CString& sCommand) override {
-        CString sCmdName = sCommand.Token(0);
-        if (sCmdName.Equals("ADD") || sCmdName.Equals("WATCH")) {
-            Watch(sCommand.Token(1), sCommand.Token(2),
-                  sCommand.Token(3, true));
-        } else if (sCmdName.Equals("HELP")) {
-            Help();
-        } else if (sCmdName.Equals("LIST")) {
-            List();
-        } else if (sCmdName.Equals("DUMP")) {
-            Dump();
-        } else if (sCmdName.Equals("ENABLE")) {
-            CString sTok = sCommand.Token(1);
-
-            if (sTok == "*") {
-                SetDisabled(~0, false);
-            } else {
-                SetDisabled(sTok.ToUInt(), false);
-            }
-        } else if (sCmdName.Equals("DISABLE")) {
-            CString sTok = sCommand.Token(1);
-
-            if (sTok == "*") {
-                SetDisabled(~0, true);
-            } else {
-                SetDisabled(sTok.ToUInt(), true);
-            }
-        } else if (sCmdName.Equals("SETDETACHEDCLIENTONLY")) {
-            CString sTok = sCommand.Token(1);
-            bool bDetachedClientOnly = sCommand.Token(2).ToBool();
-
-            if (sTok == "*") {
-                SetDetachedClientOnly(~0, bDetachedClientOnly);
-            } else {
-                SetDetachedClientOnly(sTok.ToUInt(), bDetachedClientOnly);
-            }
-        } else if (sCmdName.Equals("SETDETACHEDCHANNELONLY")) {
-            CString sTok = sCommand.Token(1);
-            bool bDetachedchannelOnly = sCommand.Token(2).ToBool();
-
-            if (sTok == "*") {
-                SetDetachedChannelOnly(~0, bDetachedchannelOnly);
-            } else {
-                SetDetachedChannelOnly(sTok.ToUInt(), bDetachedchannelOnly);
-            }
-        } else if (sCmdName.Equals("SETSOURCES")) {
-            SetSources(sCommand.Token(1).ToUInt(), sCommand.Token(2, true));
-        } else if (sCmdName.Equals("CLEAR")) {
-            m_lsWatchers.clear();
-            PutModule(t_s("All entries cleared."));
-            Save();
-        } else if (sCmdName.Equals("DEL")) {
-            Remove(sCommand.Token(1).ToUInt());
-        } else {
-            PutModule(t_f("Unknown command: {1}")(sCmdName));
-        }
-    }
-
   private:
     void Process(const CNick& Nick, const CString& sMessage,
                  const CString& sSource) {
@@ -401,7 +393,17 @@ class CWatcherMod : public CModule {
         Save();
     }
 
-    void SetDetachedClientOnly(unsigned int uIdx, bool bDetachedClientOnly) {
+    void SetDetachedClientOnly(const CString& line) {
+        bool bDetachedClientOnly = line.Token(2).ToBool();
+        CString sTok = line.Token(1);
+        unsigned int uIdx;
+        
+        if (sTok == "*") {
+            uIdx = ~0;
+        } else {
+            uIdx = sTok.ToUInt();
+        }
+        
         if (uIdx == (unsigned int)~0) {
             for (list<CWatchEntry>::iterator it = m_lsWatchers.begin();
                  it != m_lsWatchers.end(); ++it) {
@@ -433,7 +435,17 @@ class CWatcherMod : public CModule {
         Save();
     }
 
-    void SetDetachedChannelOnly(unsigned int uIdx, bool bDetachedChannelOnly) {
+    void SetDetachedChannelOnly(const CString& line) {
+        bool bDetachedChannelOnly = line.Token(2).ToBool();
+        CString sTok = line.Token(1);
+        unsigned int uIdx;
+
+        if (sTok == "*") {
+            uIdx = ~0;
+        } else {
+            uIdx = sTok.ToUInt();
+        }
+        
         if (uIdx == (unsigned int)~0) {
             for (list<CWatchEntry>::iterator it = m_lsWatchers.begin();
                  it != m_lsWatchers.end(); ++it) {
@@ -441,8 +453,7 @@ class CWatcherMod : public CModule {
             }
 
             if (bDetachedChannelOnly)
-                PutModule(
-                    t_s("Set DetachedChannelOnly for all entries to Yes"));
+                PutModule(t_s("Set DetachedChannelOnly for all entries to Yes"));
             else
                 PutModule(t_s("Set DetachedChannelOnly for all entries to No"));
             Save();
@@ -466,7 +477,7 @@ class CWatcherMod : public CModule {
         Save();
     }
 
-    void List() {
+    void List(const CString& line) {
         CTable Table;
         Table.AddColumn(t_s("Id"));
         Table.AddColumn(t_s("HostMask"));
@@ -506,7 +517,7 @@ class CWatcherMod : public CModule {
         }
     }
 
-    void Dump() {
+    void Dump(const CString& line) {
         if (m_lsWatchers.empty()) {
             PutModule(t_s("You have no entries."));
             return;
@@ -548,7 +559,10 @@ class CWatcherMod : public CModule {
         PutModule("---------------");
     }
 
-    void SetSources(unsigned int uIdx, const CString& sSources) {
+    void SetSources(const CString& line) {
+        unsigned int uIdx = line.Token(1).ToUInt();
+        CString sSources = line.Token(2, true);
+        
         uIdx--;  // "convert" index to zero based
         if (uIdx >= m_lsWatchers.size()) {
             PutModule(t_s("Invalid Id"));
@@ -563,7 +577,34 @@ class CWatcherMod : public CModule {
         Save();
     }
 
-    void Remove(unsigned int uIdx) {
+    void Enable(const CString& line) {
+        CString sTok = line.Token(1);
+        if (sTok == "*") {
+            SetDisabled(~0, false);
+        } else {
+            SetDisabled(sTok.ToUInt(), false);
+        }
+    }
+    
+    void Disable(const CString& line) {
+        CString sTok = line.Token(1);
+        if (sTok == "*") {
+            SetDisabled(~0, true);
+        } else {
+            SetDisabled(sTok.ToUInt(), true);
+        }
+    }
+
+    void Clear(const CString& line) {
+        m_lsWatchers.clear();
+        PutModule(t_s("All entries cleared."));
+        Save();
+    }
+
+    void Remove(const CString& line) {
+        
+        unsigned int uIdx = line.Token(1).ToUInt();
+        
         uIdx--;  // "convert" index to zero based
         if (uIdx >= m_lsWatchers.size()) {
             PutModule(t_s("Invalid Id"));
@@ -578,76 +619,12 @@ class CWatcherMod : public CModule {
         Save();
     }
 
-    void Help() {
-        CTable Table;
+    void Watch(const CString& line) {
+    
+        CString sHostMask = line.Token(1);
+        CString sTarget = line.Token(2);
+        CString sPattern = line.Token(3);
 
-        Table.AddColumn(t_s("Command"));
-        Table.AddColumn(t_s("Description"));
-        Table.SetStyle(CTable::ListStyle);
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"), t_s("Add <HostMask> [Target] [Pattern]"));
-        Table.SetCell(t_s("Description"),
-                      t_s("Used to add an entry to watch for."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"), t_s("List"));
-        Table.SetCell(t_s("Description"),
-                      t_s("List all entries being watched."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"), t_s("Dump"));
-        Table.SetCell(
-            t_s("Description"),
-            t_s("Dump a list of all current entries to be used later."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"), t_s("Del <Id>"));
-        Table.SetCell(t_s("Description"),
-                      t_s("Deletes Id from the list of watched entries."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"), t_s("Clear"));
-        Table.SetCell(t_s("Description"), t_s("Delete all entries."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"), t_s("Enable <Id | *>"));
-        Table.SetCell(t_s("Description"), t_s("Enable a disabled entry."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"), t_s("Disable <Id | *>"));
-        Table.SetCell(t_s("Description"),
-                      t_s("Disable (but don't delete) an entry."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"),
-                      t_s("SetDetachedClientOnly <Id | *> <True | False>"));
-        Table.SetCell(
-            t_s("Description"),
-            t_s("Enable or disable detached client only for an entry."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"),
-                      t_s("SetDetachedChannelOnly <Id | *> <True | False>"));
-        Table.SetCell(
-            t_s("Description"),
-            t_s("Enable or disable detached channel only for an entry."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"),
-                      t_s("SetSources <Id> [#chan priv #foo* !#bar]"));
-        Table.SetCell(t_s("Description"),
-                      t_s("Set the source channels that you care about."));
-
-        Table.AddRow();
-        Table.SetCell(t_s("Command"), t_s("Help"));
-        Table.SetCell(t_s("Description"), t_s("This help."));
-
-        PutModule(Table);
-    }
-
-    void Watch(const CString& sHostMask, const CString& sTarget,
-               const CString& sPattern, bool bNotice = false) {
         CString sMessage;
 
         if (sHostMask.size()) {
@@ -674,11 +651,8 @@ class CWatcherMod : public CModule {
             sMessage = t_s("Watch: Not enough arguments.  Try Help");
         }
 
-        if (bNotice) {
-            PutModNotice(sMessage);
-        } else {
-            PutModule(sMessage);
-        }
+        PutModNotice(sMessage);
+
         Save();
     }
 
@@ -706,43 +680,6 @@ class CWatcherMod : public CModule {
         SaveRegistry();
     }
 
-    void Load() {
-        // Just to make sure we don't mess up badly
-        m_lsWatchers.clear();
-
-        bool bWarn = false;
-
-        for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) {
-            VCString vList;
-            it->first.Split("\n", vList);
-
-            // Backwards compatibility with the old save format
-            if (vList.size() != 5 && vList.size() != 7) {
-                bWarn = true;
-                continue;
-            }
-
-            CWatchEntry WatchEntry(vList[0], vList[1], vList[2]);
-            if (vList[3].Equals("disabled"))
-                WatchEntry.SetDisabled(true);
-            else
-                WatchEntry.SetDisabled(false);
-
-            // Backwards compatibility with the old save format
-            if (vList.size() == 5) {
-                WatchEntry.SetSources(vList[4]);
-            } else {
-                WatchEntry.SetDetachedClientOnly(vList[4].ToBool());
-                WatchEntry.SetDetachedChannelOnly(vList[5].ToBool());
-                WatchEntry.SetSources(vList[6]);
-            }
-            m_lsWatchers.push_back(WatchEntry);
-        }
-
-        if (bWarn)
-            PutModule(t_s("WARNING: malformed entry found while loading"));
-    }
-
     list<CWatchEntry> m_lsWatchers;
 };