From 8adda0dca4cb94be6a071e67b3e3c89a22375aca Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Tue, 18 Mar 2025 13:12:28 +0000 Subject: Add a formatting overload of the ModuleException constructor. --- include/exception.h | 12 ++++++++++++ src/coremods/core_channel/extban.cpp | 8 ++++---- src/listmode.cpp | 2 +- src/mode.cpp | 16 ++++++++-------- src/modules/extra/m_geo_maxmind.cpp | 4 ++-- src/modules/m_chghost.cpp | 2 +- src/modules/m_cloak_md5.cpp | 4 ++-- src/modules/m_cloak_sha256.cpp | 2 +- src/modules/m_cloak_static.cpp | 4 ++-- src/modules/m_cloak_user.cpp | 2 +- src/modules/m_codepage.cpp | 8 ++++---- src/modules/m_disable.cpp | 12 ++++++------ src/modules/m_dnsbl.cpp | 4 ++-- src/modules/m_help.cpp | 10 +++++----- src/modules/m_sethost.cpp | 2 +- src/modules/m_spanningtree/utils.cpp | 2 +- 16 files changed, 53 insertions(+), 41 deletions(-) diff --git a/include/exception.h b/include/exception.h index b0c3c9c06..693e926b7 100644 --- a/include/exception.h +++ b/include/exception.h @@ -54,6 +54,18 @@ private: const Module* module; public: + + /** Creates a new instance of the ModuleException class with the specified module instance and reason. + * @param mod The module which threw this exception. + * @param format A format string for a message that contains the reason this exception was thrown. + * @param args The arguments to format the message. + */ + template + ModuleException(const Module* mod, const char* format, Args&&... args) + : ModuleException(mod, fmt::vformat(format, fmt::make_format_args(args...))) + { + } + /** Creates a new instance of the ModuleException class with the specified module instance and reason. * @param mod The module which threw this exception. * @param message A message that contains the reason this exception was thrown. diff --git a/src/coremods/core_channel/extban.cpp b/src/coremods/core_channel/extban.cpp index 1728c2aaa..de19d4b54 100644 --- a/src/coremods/core_channel/extban.cpp +++ b/src/coremods/core_channel/extban.cpp @@ -26,8 +26,8 @@ void ExtBanManager::AddExtBan(ExtBan::Base* extban) { auto lit = byletter.emplace(extban->GetLetter(), extban); if (!lit.second) - throw ModuleException(creator, INSP_FORMAT("ExtBan letter \"{}\" is already in use by the {} extban from {}", - extban->GetLetter(), lit.first->second->GetName(), lit.first->second->creator->ModuleFile)); + throw ModuleException(creator, "ExtBan letter \"{}\" is already in use by the {} extban from {}", + extban->GetLetter(), lit.first->second->GetName(), lit.first->second->creator->ModuleFile); } auto nit = byname.emplace(extban->GetName(), extban); @@ -36,8 +36,8 @@ void ExtBanManager::AddExtBan(ExtBan::Base* extban) if (extban->GetLetter()) byletter.erase(extban->GetLetter()); - throw ModuleException(creator, INSP_FORMAT("ExtBan name \"{}\" is already in use by the {} extban from {}", - extban->GetName(), nit.first->second->GetLetter(), nit.first->second->creator->ModuleFile)); + throw ModuleException(creator, "ExtBan name \"{}\" is already in use by the {} extban from {}", + extban->GetName(), nit.first->second->GetLetter(), nit.first->second->creator->ModuleFile); } } diff --git a/src/listmode.cpp b/src/listmode.cpp index 99ae46374..7a833a2ee 100644 --- a/src/listmode.cpp +++ b/src/listmode.cpp @@ -76,7 +76,7 @@ void ListModeBase::DoRehash() ListLimit limit(c->getString("chan", "*", 1), c->getNum("limit", DEFAULT_LIST_SIZE)); if (limit.mask.empty()) - throw ModuleException(creator, INSP_FORMAT(" is empty, at {}", c->source.str())); + throw ModuleException(creator, " is empty, at {}", c->source.str()); if (limit.mask == "*" || limit.mask == "#*") seen_default = true; diff --git a/src/mode.cpp b/src/mode.cpp index 48d2663f6..125506ffc 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -563,7 +563,7 @@ ModeHandler::Id ModeParser::AllocateModeId(ModeHandler* mh) void ModeParser::AddMode(ModeHandler* mh) { if (!ModeParser::IsModeChar(mh->GetModeChar())) - throw ModuleException(mh->creator, INSP_FORMAT("Mode letter for {} is invalid: {}", mh->name, mh->GetModeChar())); + throw ModuleException(mh->creator, "Mode letter for {} is invalid: {}", mh->name, mh->GetModeChar()); /* A mode prefix of ',' is not acceptable, it would fuck up server to server. * A mode prefix of ':' will fuck up both server to server, and client to server. @@ -573,21 +573,21 @@ void ModeParser::AddMode(ModeHandler* mh) if (pm) { if ((pm->GetPrefix() > 126) || (pm->GetPrefix() == ',') || (pm->GetPrefix() == ':') || ServerInstance->Channels.IsPrefix(pm->GetPrefix())) - throw ModuleException(mh->creator, INSP_FORMAT("Mode prefix for {} is invalid: {}", mh->name, pm->GetPrefix())); + throw ModuleException(mh->creator, "Mode prefix for {} is invalid: {}", mh->name, pm->GetPrefix()); PrefixMode* otherpm = FindPrefix(pm->GetPrefix()); if (otherpm) { - throw ModuleException(mh->creator, INSP_FORMAT("Mode prefix for {} already used by {} from {}: {}", - mh->name, otherpm->name, otherpm->creator->ModuleFile, pm->GetPrefix())); + throw ModuleException(mh->creator, "Mode prefix for {} already used by {} from {}: {}", + mh->name, otherpm->name, otherpm->creator->ModuleFile, pm->GetPrefix()); } } ModeHandler*& slot = modehandlers[mh->GetModeType()][ModeParser::GetModeIndex(mh->GetModeChar())]; if (slot) { - throw ModuleException(mh->creator, INSP_FORMAT("Mode letter for {} already used by {} from {}: {}", - mh->name, slot->name, slot->creator->ModuleFile, mh->GetModeChar())); + throw ModuleException(mh->creator, "Mode letter for {} already used by {} from {}: {}", + mh->name, slot->name, slot->creator->ModuleFile, mh->GetModeChar()); } // The mode needs an id if it is either a user mode, a simple mode (flag) or a parameter mode. @@ -600,8 +600,8 @@ void ModeParser::AddMode(ModeHandler* mh) if (!res.second) { ModeHandler* othermh = res.first->second; - throw ModuleException(mh->creator, INSP_FORMAT("Mode name {} already used by {} from {}", - mh->name, othermh->GetModeChar(), othermh->creator->ModuleFile)); + throw ModuleException(mh->creator, "Mode name {} already used by {} from {}", + mh->name, othermh->GetModeChar(), othermh->creator->ModuleFile); } // Everything is fine, add the mode diff --git a/src/modules/extra/m_geo_maxmind.cpp b/src/modules/extra/m_geo_maxmind.cpp index 6c54c7087..6499f2f81 100644 --- a/src/modules/extra/m_geo_maxmind.cpp +++ b/src/modules/extra/m_geo_maxmind.cpp @@ -181,8 +181,8 @@ public: MMDB_s mmdb; int result = MMDB_open(file.c_str(), MMDB_MODE_MMAP, &mmdb); if (result != MMDB_SUCCESS) - throw ModuleException(this, INSP_FORMAT("Unable to load the MaxMind database ({}): {}", - file, MMDB_strerror(result))); + throw ModuleException(this, "Unable to load the MaxMind database ({}): {}", + file, MMDB_strerror(result)); // Swap the new database with the old database. std::swap(mmdb, geoapi.mmdb); diff --git a/src/modules/m_chghost.cpp b/src/modules/m_chghost.cpp index 28ce29ac4..589e8702f 100644 --- a/src/modules/m_chghost.cpp +++ b/src/modules/m_chghost.cpp @@ -107,7 +107,7 @@ public: { // A hostname can not contain NUL, LF, CR, or SPACE. if (chr == 0x00 || chr == 0x0A || chr == 0x0D || chr == 0x20) - throw ModuleException(this, INSP_FORMAT(" can not contain character 0x{:02X} ({})", chr, chr)); + throw ModuleException(this, " can not contain character 0x{:02X} ({})", chr, chr); newhostmap.set(static_cast(chr)); } std::swap(newhostmap, cmd.hostmap); diff --git a/src/modules/m_cloak_md5.cpp b/src/modules/m_cloak_md5.cpp index a8c9a5101..e589d2d72 100644 --- a/src/modules/m_cloak_md5.cpp +++ b/src/modules/m_cloak_md5.cpp @@ -299,13 +299,13 @@ public: // Ensure that we have the parameter. const std::string key = tag->getString("key"); if (key.empty()) - throw ModuleException(creator, INSP_FORMAT("You have not defined a cloaking key. Define as a {}+ character network-wide secret, at {}", minkeylen, tag->source.str())); + throw ModuleException(creator, "You have not defined a cloaking key. Define as a {}+ character network-wide secret, at {}", minkeylen, tag->source.str()); // If we are the first cloak method then mandate a strong key. if (primary) { if (key.length() < minkeylen) - throw ModuleException(creator, INSP_FORMAT("Your cloaking key is not secure. It should be at least {} characters long, at {}", minkeylen, tag->source.str())); + throw ModuleException(creator, "Your cloaking key is not secure. It should be at least {} characters long, at {}", minkeylen, tag->source.str()); ServerInstance->Logs.Normal(MODNAME, "The {} cloak method is deprecated and will be removed in the next major version of InspIRCd. Consider migrating to cloak_sha256 instead. See " INSPIRCD_DOCS "modules/cloak_md5 for more information.", name.c_str() + 6); diff --git a/src/modules/m_cloak_sha256.cpp b/src/modules/m_cloak_sha256.cpp index 4b842f615..f62a3536b 100644 --- a/src/modules/m_cloak_sha256.cpp +++ b/src/modules/m_cloak_sha256.cpp @@ -311,7 +311,7 @@ public: // Ensure that we have the parameter. const std::string key = tag->getString("key"); if (key.length() < minkeylen) - throw ModuleException(creator, INSP_FORMAT("Your cloak key should be at least {} characters long, at {}", minkeylen, tag->source.str())); + throw ModuleException(creator, "Your cloak key should be at least {} characters long, at {}", minkeylen, tag->source.str()); psl_ctx_t* psl = nullptr; std::string psldb = tag->getString("psl"); diff --git a/src/modules/m_cloak_static.cpp b/src/modules/m_cloak_static.cpp index 4109b4291..64bd9c889 100644 --- a/src/modules/m_cloak_static.cpp +++ b/src/modules/m_cloak_static.cpp @@ -66,8 +66,8 @@ public: const std::string cloak = tag->getString("cloak"); if (cloak.empty() || cloak.length() > ServerInstance->Config->Limits.MaxHost) { - throw ModuleException(creator, INSP_FORMAT("Your static cloak must be between 1 and {} characters long, at {}", - ServerInstance->Config->Limits.MaxHost, tag->source.str())); + throw ModuleException(creator, "Your static cloak must be between 1 and {} characters long, at {}", + ServerInstance->Config->Limits.MaxHost, tag->source.str()); } return std::make_shared(this, tag, cloak); diff --git a/src/modules/m_cloak_user.cpp b/src/modules/m_cloak_user.cpp index d656a7179..aadb1c42b 100644 --- a/src/modules/m_cloak_user.cpp +++ b/src/modules/m_cloak_user.cpp @@ -352,7 +352,7 @@ public: { // A hostname can not contain NUL, LF, CR, or SPACE. if (chr == 0x00 || chr == 0x0A || chr == 0x0D || chr == 0x20) - throw ModuleException(this, INSP_FORMAT(" can not contain character 0x{:02X} ({})", chr, chr)); + throw ModuleException(this, " can not contain character 0x{:02X} ({})", chr, chr); newhostmap.set(static_cast(chr)); } std::swap(newhostmap, hostmap); diff --git a/src/modules/m_codepage.cpp b/src/modules/m_codepage.cpp index 5c187126a..9f45b91d0 100644 --- a/src/modules/m_codepage.cpp +++ b/src/modules/m_codepage.cpp @@ -389,12 +389,12 @@ public: break; case Codepage::AllowCharacterResult::NOT_VALID: - throw ModuleException(this, INSP_FORMAT(" tag contains a forbidden character: {} at {}", - GetPrintable(pos), tag->source.str())); + throw ModuleException(this, " tag contains a forbidden character: {} at {}", + GetPrintable(pos), tag->source.str()); case Codepage::AllowCharacterResult::NOT_VALID_AT_FRONT: - throw ModuleException(this, INSP_FORMAT(" tag contains a forbidden front character: {} at {}", - GetPrintable(pos), tag->source.str())); + throw ModuleException(this, " tag contains a forbidden front character: {} at {}", + GetPrintable(pos), tag->source.str()); } } } diff --git a/src/modules/m_disable.cpp b/src/modules/m_disable.cpp index 20092ae75..02326a417 100644 --- a/src/modules/m_disable.cpp +++ b/src/modules/m_disable.cpp @@ -47,14 +47,14 @@ private: { // Check that the character is a valid mode letter. if (!ModeParser::IsModeChar(chr)) - throw ModuleException(this, INSP_FORMAT("Invalid mode '{}' was specified in at {}", - chr, field, tag->source.str())); + throw ModuleException(this, "Invalid mode '{}' was specified in at {}", + chr, field, tag->source.str()); // Check that the mode actually exists. ModeHandler* mh = ServerInstance->Modes.FindMode(chr, type); if (!mh) - throw ModuleException(this, INSP_FORMAT("Nonexistent mode '{}' was specified in at {}", - chr, field, tag->source.str())); + throw ModuleException(this, "Nonexistent mode '{}' was specified in at {}", + chr, field, tag->source.str()); // Disable the mode. ServerInstance->Logs.Debug(MODNAME, "The {} ({}) {} mode has been disabled", @@ -90,8 +90,8 @@ public: // Check that the command actually exists. Command* handler = ServerInstance->Parser.GetHandler(command); if (!handler) - throw ModuleException(this, INSP_FORMAT("Nonexistent command '{}' was specified in at {}", - command, tag->source.str())); + throw ModuleException(this, "Nonexistent command '{}' was specified in at {}", + command, tag->source.str()); // Prevent admins from disabling MODULES for transparency reasons. if (handler->name == "MODULES") diff --git a/src/modules/m_dnsbl.cpp b/src/modules/m_dnsbl.cpp index d66c2a008..28dd5f77d 100644 --- a/src/modules/m_dnsbl.cpp +++ b/src/modules/m_dnsbl.cpp @@ -624,8 +624,8 @@ public: const auto dnsblreply = tag->getNum("reply", std::numeric_limits::max()); if (dnsblreply > 16'777'215) { - throw ModuleException(this, INSP_FORMAT(" ({}) is not a valid DNSBL reply at {}", - dnsblreply, tag->source.str())); + throw ModuleException(this, " ({}) is not a valid DNSBL reply at {}", + dnsblreply, tag->source.str()); } (*dnsbl)->replies[dnsblreply] = dnsbldesc; diff --git a/src/modules/m_help.cpp b/src/modules/m_help.cpp index dda487c5c..eebc093d6 100644 --- a/src/modules/m_help.cpp +++ b/src/modules/m_help.cpp @@ -115,16 +115,16 @@ public: // Attempt to read the help key. const std::string key = tag->getString("key"); if (key.empty()) - throw ModuleException(this, INSP_FORMAT("<{}:key> is empty at {}", tag->name, tag->source.str())); + throw ModuleException(this, "<{}:key> is empty at {}", tag->name, tag->source.str()); else if (irc::equals(key, "index")) - throw ModuleException(this, INSP_FORMAT("<{}:key> is set to \"index\" which is reserved at {}", tag->name, tag->source.str())); + throw ModuleException(this, "<{}:key> is set to \"index\" which is reserved at {}", tag->name, tag->source.str()); else if (key.length() > longestkey) longestkey = key.length(); // Attempt to read the help value. std::string value; if (!tag->readString("value", value, true) || value.empty()) - throw ModuleException(this, INSP_FORMAT("<{}:value> is empty at {}", tag->name, tag->source.str())); + throw ModuleException(this, "<{}:value> is empty at {}", tag->name, tag->source.str()); // Parse the help body. Empty lines are replaced with a single // space because some clients are unable to show blank lines. @@ -137,8 +137,8 @@ public: const std::string title = tag->getString("title", INSP_FORMAT("*** Help for {}", key), 1); if (!newhelp.emplace(key, HelpTopic(helpmsg, title)).second) { - throw ModuleException(this, INSP_FORMAT("<{}> tag with duplicate key '{}' at {}", - tag->name, key, tag->source.str())); + throw ModuleException(this, "<{}> tag with duplicate key '{}' at {}", + tag->name, key, tag->source.str()); } } diff --git a/src/modules/m_sethost.cpp b/src/modules/m_sethost.cpp index ad8a8dfee..be15e2401 100644 --- a/src/modules/m_sethost.cpp +++ b/src/modules/m_sethost.cpp @@ -83,7 +83,7 @@ public: { // A hostname can not contain NUL, LF, CR, or SPACE. if (chr == 0x00 || chr == 0x0A || chr == 0x0D || chr == 0x20) - throw ModuleException(this, INSP_FORMAT(" can not contain character 0x{:02X} ({})", chr, chr)); + throw ModuleException(this, " can not contain character 0x{:02X} ({})", chr, chr); newhostmap.set(static_cast(chr)); } std::swap(newhostmap, cmd.hostmap); diff --git a/src/modules/m_spanningtree/utils.cpp b/src/modules/m_spanningtree/utils.cpp index 9a10450f9..9b43d9bb1 100644 --- a/src/modules/m_spanningtree/utils.cpp +++ b/src/modules/m_spanningtree/utils.cpp @@ -301,7 +301,7 @@ void SpanningTreeUtilities::ReadConfiguration() throw ModuleException((Module*)Creator, "The link name '"+L->Name+"' is invalid as it must contain at least one '.' character"); if (L->Name.length() > ServerInstance->Config->Limits.MaxHost) - throw ModuleException((Module*)Creator, INSP_FORMAT("The link name '{}' is invalid as it is longer than {} characters", L->Name, ServerInstance->Config->Limits.MaxHost)); + throw ModuleException((Module*)Creator, "The link name '{}' is invalid as it is longer than {} characters", L->Name, ServerInstance->Config->Limits.MaxHost); if (L->RecvPass.empty()) throw ModuleException((Module*)Creator, "Invalid configuration for server '"+L->Name+"', recvpass not defined"); -- cgit v1.3.1-10-gc9f91