From 958e9ad61c8c7683cd7a618177d326e7e64c8208 Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Sat, 7 Sep 2013 12:27:39 +0400 Subject: [PATCH] ControlPanel: support network module manipulation Fix #380 --- modules/controlpanel.cpp | 174 +++++++++++++++++++++++++++++---------- 1 file changed, 130 insertions(+), 44 deletions(-) diff --git a/modules/controlpanel.cpp b/modules/controlpanel.cpp index 98ac44e6..f1fdcf81 100644 --- a/modules/controlpanel.cpp +++ b/modules/controlpanel.cpp @@ -991,35 +991,22 @@ class CAdminMod : public CModule { PutModule("Error: [" + sCTCPRequest + "] not found for user [" + pUser->GetUserName() + "]!"); } - void LoadModuleForUser(const CString& sLine) { - CString sUsername = sLine.Token(1); - CString sModName = sLine.Token(2); - CString sArgs = sLine.Token(3, true); - CString sModRet; - - if (sModName.empty()) { - PutModule("Usage: loadmodule "); - return; - } - - CUser* pUser = GetUser(sUsername); - if (!pUser) - return; - + void LoadModuleFor(CModules& Modules, const CString& sModName, const CString& sArgs, CModInfo::EModuleType eType, CUser* pUser, CIRCNetwork* pNetwork) { if (pUser->DenyLoadMod() && !m_pUser->IsAdmin()) { PutModule("Loading modules has been disabled."); return; } - CModule *pMod = (pUser)->GetModules().FindModule(sModName); + CString sModRet; + CModule *pMod = Modules.FindModule(sModName); if (!pMod) { - if (!(pUser)->GetModules().LoadModule(sModName, sArgs, CModInfo::UserModule, pUser, NULL, sModRet)) { + if (!Modules.LoadModule(sModName, sArgs, eType, pUser, pNetwork, sModRet)) { PutModule("Unable to load module [" + sModName + "] [" + sModRet + "]"); } else { PutModule("Loaded module [" + sModName + "]"); } } else if (pMod->GetArgs() != sArgs) { - if (!(pUser)->GetModules().ReloadModule(sModName, sArgs, pUser, NULL, sModRet)) { + if (!Modules.ReloadModule(sModName, sArgs, pUser, pNetwork, sModRet)) { PutModule("Unable to reload module [" + sModName + "] [" + sModRet + "]"); } else { PutModule("Reloaded module [" + sModName + "]"); @@ -1029,11 +1016,69 @@ class CAdminMod : public CModule { } } - void UnLoadModuleForUser(const CString& sLine) { + void LoadModuleForUser(const CString& sLine) { CString sUsername = sLine.Token(1); CString sModName = sLine.Token(2); CString sArgs = sLine.Token(3, true); + + if (sModName.empty()) { + PutModule("Usage: loadmodule []"); + return; + } + + CUser* pUser = GetUser(sUsername); + if (!pUser) + return; + + LoadModuleFor(pUser->GetModules(), sModName, sArgs, CModInfo::UserModule, pUser, NULL); + } + + void LoadModuleForNetwork(const CString& sLine) { + CString sUsername = sLine.Token(1); + CString sNetwork = sLine.Token(2); + CString sModName = sLine.Token(3); + CString sArgs = sLine.Token(4, true); + + if (sModName.empty()) { + PutModule("Usage: loadnetmodule []"); + return; + } + + CUser* pUser = GetUser(sUsername); + if (!pUser) + return; + + CIRCNetwork* pNetwork = pUser->FindNetwork(sNetwork); + if (!pNetwork) { + PutModule("Network not found"); + return; + } + + LoadModuleFor(pNetwork->GetModules(), sModName, sArgs, CModInfo::NetworkModule, pUser, pNetwork); + } + + void UnLoadModuleFor(CModules& Modules, const CString& sModName, CUser* pUser) { + if (pUser->DenyLoadMod() && !m_pUser->IsAdmin()) { + PutModule("Loading modules has been disabled."); + return; + } + + if (Modules.FindModule(sModName) == this) { + PutModule("Please use /znc unloadmod " + sModName); + return; + } + CString sModRet; + if (!Modules.UnloadModule(sModName, sModRet)) { + PutModule("Unable to unload module [" + sModName + "] [" + sModRet + "]"); + } else { + PutModule("Unloaded module [" + sModName + "] [" + sModRet + "]"); + } + } + + void UnLoadModuleForUser(const CString& sLine) { + CString sUsername = sLine.Token(1); + CString sModName = sLine.Token(2); if (sModName.empty()) { PutModule("Usage: unloadmodule "); @@ -1044,38 +1089,37 @@ class CAdminMod : public CModule { if (!pUser) return; - if (pUser->DenyLoadMod() && !m_pUser->IsAdmin()) { - PutModule("Loading modules has been disabled."); - return; - } - - if (pUser->GetModules().FindModule(sModName) == this) { - PutModule("Please use /znc unloadmod " + sModName); - return; - } - - if (!(pUser)->GetModules().UnloadModule(sModName, sModRet)) { - PutModule("Unable to unload module [" + sModName + "] [" + sModRet + "]"); - } else { - PutModule("Unloaded module [" + sModName + "] [" + sModRet + "]"); - } + UnLoadModuleFor(pUser->GetModules(), sModName, pUser); } - void ListModuleForUser(const CString& sLine) { - CString sUsername = sLine.Token(1, true); + void UnLoadModuleForNetwork(const CString& sLine) { + CString sUsername = sLine.Token(1); + CString sNetwork = sLine.Token(2); + CString sModName = sLine.Token(3); - CUser* pUser = GetUser(sUsername); - if (!pUser || (pUser != m_pUser && !m_pUser->IsAdmin())) { - PutModule("Usage: listmods "); + if (sModName.empty()) { + PutModule("Usage: unloadnetmodule "); return; } - CModules& Modules = pUser->GetModules(); + CUser* pUser = GetUser(sUsername); + if (!pUser) + return; + CIRCNetwork* pNetwork = pUser->FindNetwork(sNetwork); + if (!pNetwork) { + PutModule("Network not found"); + return; + } + + UnLoadModuleFor(pNetwork->GetModules(), sModName, pUser); + } + + void ListModulesFor(CModules& Modules, const CString& sWhere) { if (!Modules.size()) { - PutModule("User [" + pUser->GetUserName() + "] has no modules loaded."); + PutModule(sWhere + " has no modules loaded."); } else { - PutModule("Modules loaded for user [" + pUser->GetUserName() + "]:"); + PutModule("Modules loaded for " + sWhere + ":"); CTable Table; Table.AddColumn("Name"); Table.AddColumn("Arguments"); @@ -1088,7 +1132,43 @@ class CAdminMod : public CModule { PutModule(Table); } + } + void ListModulesForUser(const CString& sLine) { + CString sUsername = sLine.Token(1); + + if (sUsername.empty()) { + PutModule("Usage: listmods "); + return; + } + + CUser* pUser = GetUser(sUsername); + if (!pUser) + return; + + ListModulesFor(pUser->GetModules(), "User [" + pUser->GetUserName() + "]"); + } + + void ListModulesForNetwork(const CString& sLine) { + CString sUsername = sLine.Token(1); + CString sNetwork = sLine.Token(2); + + if (sNetwork.empty()) { + PutModule("Usage: listnetmods "); + return; + } + + CUser* pUser = GetUser(sUsername); + if (!pUser) + return; + + CIRCNetwork* pNetwork = pUser->FindNetwork(sNetwork); + if (!pNetwork) { + PutModule("Network not found"); + return; + } + + ListModulesFor(pNetwork->GetModules(), "Network [" + pNetwork->GetName() + "] of user [" + pUser->GetUserName() + "]"); } public: @@ -1122,11 +1202,17 @@ public: AddCommand("Disconnect", static_cast(&CAdminMod::DisconnectUser), "username network", "Disconnects the user from their IRC server"); AddCommand("LoadModule", static_cast(&CAdminMod::LoadModuleForUser), - "username modulename", "Loads a Module for a user"); + "username modulename [args]", "Loads a Module for a user"); AddCommand("UnLoadModule", static_cast(&CAdminMod::UnLoadModuleForUser), "username modulename", "Removes a Module of a user"); - AddCommand("ListMods", static_cast(&CAdminMod::ListModuleForUser), + AddCommand("ListMods", static_cast(&CAdminMod::ListModulesForUser), "username", "Get the list of modules for a user"); + AddCommand("LoadNetModule",static_cast(&CAdminMod::LoadModuleForNetwork), + "username network modulename [args]", "Loads a Module for a network"); + AddCommand("UnLoadNetModule",static_cast(&CAdminMod::UnLoadModuleForNetwork), + "username network modulename", "Removes a Module of a network"); + AddCommand("ListNetMods", static_cast(&CAdminMod::ListModulesForNetwork), + "username network", "Get the list of modules for a network"); AddCommand("ListCTCPs", static_cast(&CAdminMod::ListCTCP), "username", "List the configured CTCP replies"); AddCommand("AddCTCP", static_cast(&CAdminMod::AddCTCP),