diff options
| author | Sadie Powell <sadie@witchery.services> | 2024-03-11 13:53:05 +0000 |
|---|---|---|
| committer | Sadie Powell <sadie@witchery.services> | 2024-03-11 19:17:29 +0000 |
| commit | 29e7674e56bf2b829bba22def2760d034a76e788 (patch) | |
| tree | f40049ba995b03dd7c510d88f9f19db2d2e65a2e /modules/chanserv | |
| parent | e2df7d4d01f8fdb41c49ce8efc462cab005e7d5c (diff) | |
Replace convertTo/stringify with non-throwing alternatives.
Having these throw is terrible for ergonomics and there are loads
of places where the exception was either silently ignored or not
handled at all. Having a function which returns an optional and
another that returns a default works a lot better imo.
Diffstat (limited to 'modules/chanserv')
| -rw-r--r-- | modules/chanserv/cs_access.cpp | 33 | ||||
| -rw-r--r-- | modules/chanserv/cs_akick.cpp | 4 | ||||
| -rw-r--r-- | modules/chanserv/cs_enforce.cpp | 10 | ||||
| -rw-r--r-- | modules/chanserv/cs_entrymsg.cpp | 23 | ||||
| -rw-r--r-- | modules/chanserv/cs_flags.cpp | 2 | ||||
| -rw-r--r-- | modules/chanserv/cs_info.cpp | 2 | ||||
| -rw-r--r-- | modules/chanserv/cs_list.cpp | 12 | ||||
| -rw-r--r-- | modules/chanserv/cs_log.cpp | 2 | ||||
| -rw-r--r-- | modules/chanserv/cs_mode.cpp | 2 | ||||
| -rw-r--r-- | modules/chanserv/cs_set.cpp | 17 | ||||
| -rw-r--r-- | modules/chanserv/cs_xop.cpp | 4 |
11 files changed, 44 insertions, 67 deletions
diff --git a/modules/chanserv/cs_access.cpp b/modules/chanserv/cs_access.cpp index 3d2c3c8d6..f86d9f5bc 100644 --- a/modules/chanserv/cs_access.cpp +++ b/modules/chanserv/cs_access.cpp @@ -37,18 +37,13 @@ public: Anope::string AccessSerialize() const override { - return stringify(this->level); + return Anope::ToString(this->level); } void AccessUnserialize(const Anope::string &data) override { - try - { - this->level = convertTo<int>(data); - } - catch (const ConvertException &) - { - } + if (auto l = Anope::TryConvert<int>(data)) + this->level = l.value(); } bool operator>(const ChanAccess &other) const override @@ -95,11 +90,9 @@ class CommandCSAccess final Privilege *p = NULL; int level = ACCESS_INVALID; - try - { - level = convertTo<int>(params[3]); - } - catch (const ConvertException &) + if (auto lvl = Anope::TryConvert<int>(params[3])) + level = lvl.value(); + else { p = PrivilegeManager::FindPrivilege(params[3]); if (p != NULL && defaultLevels[p->name]) @@ -402,7 +395,7 @@ class CommandCSAccess final } ListFormatter::ListEntry entry; - entry["Number"] = stringify(number); + entry["Number"] = Anope::ToString(number); entry["Level"] = access->AccessSerialize(); entry["Mask"] = access->Mask(); entry["By"] = access->creator; @@ -442,7 +435,7 @@ class CommandCSAccess final } ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Level"] = access->AccessSerialize(); entry["Mask"] = access->Mask(); entry["By"] = access->creator; @@ -652,11 +645,9 @@ class CommandCSLevels final level = ACCESS_FOUNDER; else { - try - { - level = convertTo<int>(lev); - } - catch (const ConvertException &) + if (auto lvl = Anope::TryConvert<int>(lev)) + level = lvl.value(); + else { this->OnSyntaxError(source, "SET"); return; @@ -734,7 +725,7 @@ class CommandCSLevels final else if (j == ACCESS_FOUNDER) entry["Level"] = Language::Translate(source.GetAccount(), _("(founder only)")); else - entry["Level"] = stringify(j); + entry["Level"] = Anope::ToString(j); list.AddEntry(entry); } diff --git a/modules/chanserv/cs_akick.cpp b/modules/chanserv/cs_akick.cpp index 6457f2856..35052e63d 100644 --- a/modules/chanserv/cs_akick.cpp +++ b/modules/chanserv/cs_akick.cpp @@ -311,7 +311,7 @@ class CommandCSAKick final lastused = UNKNOWN; ListFormatter::ListEntry entry; - entry["Number"] = stringify(number); + entry["Number"] = Anope::ToString(number); if (akick->nc) entry["Mask"] = akick->nc->display; else @@ -351,7 +351,7 @@ class CommandCSAKick final lastused = UNKNOWN; ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); if (akick->nc) entry["Mask"] = akick->nc->display; else diff --git a/modules/chanserv/cs_enforce.cpp b/modules/chanserv/cs_enforce.cpp index 825301cdd..3c0a464ef 100644 --- a/modules/chanserv/cs_enforce.cpp +++ b/modules/chanserv/cs_enforce.cpp @@ -166,14 +166,8 @@ private: return; } - int l; - try - { - l = convertTo<int>(l_str); - if (l < 0) - throw ConvertException(); - } - catch (const ConvertException &) + auto l = Anope::Convert<int>(l_str, -1); + if (l < 0) { source.Reply(_("The limit on %s is not valid."), ci->name.c_str()); return; diff --git a/modules/chanserv/cs_entrymsg.cpp b/modules/chanserv/cs_entrymsg.cpp index 03e4b22de..efe13d1aa 100644 --- a/modules/chanserv/cs_entrymsg.cpp +++ b/modules/chanserv/cs_entrymsg.cpp @@ -123,7 +123,7 @@ private: EntryMsg *msg = (*messages)->at(i); ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Creator"] = msg->creator; entry["Created"] = Anope::strftime(msg->when, NULL, true); entry["Message"] = msg->message; @@ -162,21 +162,16 @@ private: source.Reply(_("Entry message list for \002%s\002 is empty."), ci->name.c_str()); else { - try + auto i = Anope::Convert<unsigned>(message, 0); + if (i > 0 && i <= (*messages)->size()) { - unsigned i = convertTo<unsigned>(message); - if (i > 0 && i <= (*messages)->size()) - { - delete (*messages)->at(i - 1); - if ((*messages)->empty()) - ci->Shrink<EntryMessageList>("entrymsg"); - Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to remove a message"; - source.Reply(_("Entry message \002%i\002 for \002%s\002 deleted."), i, ci->name.c_str()); - } - else - throw ConvertException(); + delete (*messages)->at(i - 1); + if ((*messages)->empty()) + ci->Shrink<EntryMessageList>("entrymsg"); + Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to remove a message"; + source.Reply(_("Entry message \002%i\002 for \002%s\002 deleted."), i, ci->name.c_str()); } - catch (const ConvertException &) + else { source.Reply(_("Entry message \002%s\002 not found on channel \002%s\002."), message.c_str(), ci->name.c_str()); } diff --git a/modules/chanserv/cs_flags.cpp b/modules/chanserv/cs_flags.cpp index feb64aacd..7404aef57 100644 --- a/modules/chanserv/cs_flags.cpp +++ b/modules/chanserv/cs_flags.cpp @@ -331,7 +331,7 @@ class CommandCSFlags final ListFormatter::ListEntry entry; ++count; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Mask"] = access->Mask(); entry["Flags"] = flags; entry["Creator"] = access->creator; diff --git a/modules/chanserv/cs_info.cpp b/modules/chanserv/cs_info.cpp index 685538284..e02660301 100644 --- a/modules/chanserv/cs_info.cpp +++ b/modules/chanserv/cs_info.cpp @@ -58,7 +58,7 @@ public: if (show_all) { - info[_("Ban type")] = stringify(ci->bantype); + info[_("Ban type")] = Anope::ToString(ci->bantype); } FOREACH_MOD(OnChanInfo, (source, ci, info, show_all)); diff --git a/modules/chanserv/cs_list.cpp b/modules/chanserv/cs_list.cpp index 29add4596..c9589a268 100644 --- a/modules/chanserv/cs_list.cpp +++ b/modules/chanserv/cs_list.cpp @@ -36,12 +36,10 @@ public: sepstream(pattern.substr(1), '-').GetToken(n1, 0); sepstream(pattern, '-').GetToken(n2, 1); - try - { - from = convertTo<int>(n1); - to = convertTo<int>(n2); - } - catch (const ConvertException &) + auto num1 = Anope::TryConvert<int>(n1); + auto num2 = Anope::TryConvert<int>(n2); + + if (!num1.has_value() || !num2.has_value()) { source.Reply(LIST_INCORRECT_RANGE); source.Reply(_("To search for channels starting with #, search for the channel\n" @@ -49,6 +47,8 @@ public: return; } + from = num1.value(); + to = num2.value(); pattern = "*"; } diff --git a/modules/chanserv/cs_log.cpp b/modules/chanserv/cs_log.cpp index 213940a5c..8c1004eeb 100644 --- a/modules/chanserv/cs_log.cpp +++ b/modules/chanserv/cs_log.cpp @@ -135,7 +135,7 @@ public: const LogSetting *log = (*ls)->at(i); ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Service"] = log->command_service; entry["Command"] = !log->command_name.empty() ? log->command_name : log->service_name; entry["Method"] = log->method; diff --git a/modules/chanserv/cs_mode.cpp b/modules/chanserv/cs_mode.cpp index 8c7c7c1c3..8f462a3cb 100644 --- a/modules/chanserv/cs_mode.cpp +++ b/modules/chanserv/cs_mode.cpp @@ -716,7 +716,7 @@ class CommandCSMode final std::vector<Anope::string> new_params; new_params.push_back(params[0]); new_params.emplace_back("SET"); - new_params.push_back("-" + stringify(cm->mchar)); + new_params.push_back("-" + Anope::ToString(cm->mchar)); new_params.emplace_back("*"); this->DoSet(source, ci, new_params); } diff --git a/modules/chanserv/cs_set.cpp b/modules/chanserv/cs_set.cpp index d5ed4713f..c7952baba 100644 --- a/modules/chanserv/cs_set.cpp +++ b/modules/chanserv/cs_set.cpp @@ -165,19 +165,16 @@ public: return; } - try - { - int16_t new_type = convertTo<int16_t>(params[1]); - if (new_type < 0 || new_type > 3) - throw ConvertException("Invalid range"); - Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to change the ban type to " << new_type; - ci->bantype = new_type; - source.Reply(_("Ban type for channel %s is now #%d."), ci->name.c_str(), ci->bantype); - } - catch (const ConvertException &) + auto new_type = Anope::Convert<int16_t>(params[1], -1); + if (new_type < 0 || new_type > 3) { source.Reply(_("\002%s\002 is not a valid ban type."), params[1].c_str()); + return; } + + Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to change the ban type to " << new_type; + ci->bantype = new_type; + source.Reply(_("Ban type for channel %s is now #%d."), ci->name.c_str(), ci->bantype); } bool OnHelp(CommandSource &source, const Anope::string &) override diff --git a/modules/chanserv/cs_xop.cpp b/modules/chanserv/cs_xop.cpp index b47e1e988..0ec8e8d25 100644 --- a/modules/chanserv/cs_xop.cpp +++ b/modules/chanserv/cs_xop.cpp @@ -420,7 +420,7 @@ private: return; ListFormatter::ListEntry entry; - entry["Number"] = stringify(Number); + entry["Number"] = Anope::ToString(Number); entry["Mask"] = a->Mask(); entry["Description"] = a->description; this->list.AddEntry(entry); @@ -440,7 +440,7 @@ private: continue; ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Mask"] = a->Mask(); entry["Description"] = a->description; list.AddEntry(entry); |
