From e5e0bb356e1700b738d093d109e7252a2f65662b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 19 Aug 2014 19:48:48 +0200 Subject: [PATCH] Refactor flooddetach to use command callbacks This gives a proper help command "for free", and makes it more pleasant to add new commands (silent) later. --- modules/flooddetach.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/modules/flooddetach.cpp b/modules/flooddetach.cpp index f4ed8921..831f7b11 100644 --- a/modules/flooddetach.cpp +++ b/modules/flooddetach.cpp @@ -24,6 +24,11 @@ public: MODCONSTRUCTOR(CFloodDetachMod) { m_iThresholdSecs = 0; m_iThresholdMsgs = 0; + + AddHelpCommand(); + AddCommand("Show", static_cast(&CFloodDetachMod::ShowCommand), ""); + AddCommand("Secs", static_cast(&CFloodDetachMod::SecsCommand), "[]"); + AddCommand("Lines", static_cast(&CFloodDetachMod::LinesCommand), "[]"); } ~CFloodDetachMod() { @@ -163,31 +168,37 @@ public: return CONTINUE; } - void OnModCommand(const CString& sCommand) { - const CString& sCmd = sCommand.Token(0); - const CString& sArg = sCommand.Token(1, true); + void ShowCommand(const CString& sLine) { + PutModule("Current limit is " + CString(m_iThresholdMsgs) + " lines " + "in " + CString(m_iThresholdSecs) + " secs."); + } - if (sCmd.Equals("secs") && !sArg.empty()) { + void SecsCommand(const CString& sLine) { + const CString sArg = sLine.Token(1, true); + + if (!sArg.empty()) { m_iThresholdSecs = sArg.ToUInt(); if (m_iThresholdSecs == 0) m_iThresholdSecs = 1; PutModule("Set seconds limit to [" + CString(m_iThresholdSecs) + "]"); Save(); - } else if (sCmd.Equals("lines") && !sArg.empty()) { + } + } + + void LinesCommand(const CString& sLine) { + const CString sArg = sLine.Token(1, true); + + if (!sArg.empty()) { m_iThresholdMsgs = sArg.ToUInt(); if (m_iThresholdMsgs == 0) m_iThresholdMsgs = 2; PutModule("Set lines limit to [" + CString(m_iThresholdMsgs) + "]"); Save(); - } else if (sCmd.Equals("show")) { - PutModule("Current limit is " + CString(m_iThresholdMsgs) + " lines " - "in " + CString(m_iThresholdSecs) + " secs."); - } else { - PutModule("Commands: show, secs , lines "); } } + private: typedef map > Limits; Limits m_chans;