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/operserv | |
| 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/operserv')
| -rw-r--r-- | modules/operserv/os_akill.cpp | 4 | ||||
| -rw-r--r-- | modules/operserv/os_defcon.cpp | 8 | ||||
| -rw-r--r-- | modules/operserv/os_dns.cpp | 25 | ||||
| -rw-r--r-- | modules/operserv/os_list.cpp | 4 | ||||
| -rw-r--r-- | modules/operserv/os_logsearch.cpp | 26 | ||||
| -rw-r--r-- | modules/operserv/os_news.cpp | 18 | ||||
| -rw-r--r-- | modules/operserv/os_session.cpp | 26 | ||||
| -rw-r--r-- | modules/operserv/os_set.cpp | 6 | ||||
| -rw-r--r-- | modules/operserv/os_sxline.cpp | 4 |
9 files changed, 44 insertions, 77 deletions
diff --git a/modules/operserv/os_akill.cpp b/modules/operserv/os_akill.cpp index 3f5e9c7d2..7fcacd9ad 100644 --- a/modules/operserv/os_akill.cpp +++ b/modules/operserv/os_akill.cpp @@ -285,7 +285,7 @@ private: return; ListFormatter::ListEntry entry; - entry["Number"] = stringify(number); + entry["Number"] = Anope::ToString(number); entry["Mask"] = x->mask; entry["Creator"] = x->by; entry["Created"] = Anope::strftime(x->created, NULL, true); @@ -307,7 +307,7 @@ private: if (mask.empty() || mask.equals_ci(x->mask) || mask == x->id || Anope::Match(x->mask, mask, false, true)) { ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Mask"] = x->mask; entry["Creator"] = x->by; entry["Created"] = Anope::strftime(x->created, NULL, true); diff --git a/modules/operserv/os_defcon.cpp b/modules/operserv/os_defcon.cpp index dbb0edb69..564cfec50 100644 --- a/modules/operserv/os_defcon.cpp +++ b/modules/operserv/os_defcon.cpp @@ -192,13 +192,7 @@ public: return; } - int newLevel = 0; - try - { - newLevel = convertTo<int>(lvl); - } - catch (const ConvertException &) { } - + auto newLevel = Anope::Convert<int>(lvl, 0); if (newLevel < 1 || newLevel > 5) { this->OnSyntaxError(source, ""); diff --git a/modules/operserv/os_dns.cpp b/modules/operserv/os_dns.cpp index 99d01e7b7..18e53b25a 100644 --- a/modules/operserv/os_dns.cpp +++ b/modules/operserv/os_dns.cpp @@ -42,7 +42,7 @@ struct DNSZone final data["name"] << name; unsigned count = 0; for (const auto &server : servers) - data["server" + stringify(count++)] << server; + data["server" + Anope::ToString(count++)] << server; } static Serializable *Unserialize(Serializable *obj, Serialize::Data &data) @@ -64,7 +64,7 @@ struct DNSZone final for (unsigned count = 0; true; ++count) { Anope::string server_str; - data["server" + stringify(count)] >> server_str; + data["server" + Anope::ToString(count)] >> server_str; if (server_str.empty()) break; zone->servers.insert(server_str); @@ -146,12 +146,12 @@ public: { data["server_name"] << server_name; for (unsigned i = 0; i < ips.size(); ++i) - data["ip" + stringify(i)] << ips[i]; + data["ip" + Anope::ToString(i)] << ips[i]; data["limit"] << limit; data["pooled"] << pooled; unsigned count = 0; for (const auto &zone : zones) - data["zone" + stringify(count++)] << zone; + data["zone" + Anope::ToString(count++)] << zone; } static Serializable *Unserialize(Serializable *obj, Serialize::Data &data) @@ -172,7 +172,7 @@ public: for (unsigned i = 0; true; ++i) { Anope::string ip_str; - data["ip" + stringify(i)] >> ip_str; + data["ip" + Anope::ToString(i)] >> ip_str; if (ip_str.empty()) break; req->ips.push_back(ip_str); @@ -185,7 +185,7 @@ public: for (unsigned i = 0; true; ++i) { Anope::string zone_str; - data["zone" + stringify(i)] >> zone_str; + data["zone" + Anope::ToString(i)] >> zone_str; if (zone_str.empty()) break; req->zones.insert(zone_str); @@ -225,7 +225,7 @@ class CommandOSDNS final ListFormatter::ListEntry entry; entry["Server"] = s->GetName(); - entry["Limit"] = s->GetLimit() ? stringify(s->GetLimit()) : Language::Translate(source.GetAccount(), _("None")); + entry["Limit"] = s->GetLimit() ? Anope::ToString(s->GetLimit()) : Language::Translate(source.GetAccount(), _("None")); Anope::string ip_str; for (const auto &ip : s->GetIPs()) @@ -579,16 +579,15 @@ class CommandOSDNS final if (params[2].equals_ci("LIMIT")) { - try + if (auto l = Anope::TryConvert<unsigned>(params[3])) { - unsigned l = convertTo<unsigned>(params[3]); - s->SetLimit(l); - if (l) - source.Reply(_("User limit for %s set to %d."), s->GetName().c_str(), l); + s->SetLimit(l.value()); + if (s->GetLimit()) + source.Reply(_("User limit for %s set to %d."), s->GetName().c_str(), s->GetLimit()); else source.Reply(_("User limit for %s removed."), s->GetName().c_str()); } - catch (const ConvertException &ex) + else { source.Reply(_("Invalid value for LIMIT. Must be numerical.")); } diff --git a/modules/operserv/os_list.cpp b/modules/operserv/os_list.cpp index a6969dc24..ba1156967 100644 --- a/modules/operserv/os_list.cpp +++ b/modules/operserv/os_list.cpp @@ -60,7 +60,7 @@ public: ListFormatter::ListEntry entry; entry["Name"] = cc->chan->name; - entry["Users"] = stringify(cc->chan->users.size()); + entry["Users"] = Anope::ToString(cc->chan->users.size()); entry["Modes"] = cc->chan->GetModes(true, true); entry["Topic"] = cc->chan->topic; list.AddEntry(entry); @@ -87,7 +87,7 @@ public: ListFormatter::ListEntry entry; entry["Name"] = c->name; - entry["Users"] = stringify(c->users.size()); + entry["Users"] = Anope::ToString(c->users.size()); entry["Modes"] = c->GetModes(true, true); entry["Topic"] = c->topic; list.AddEntry(entry); diff --git a/modules/operserv/os_logsearch.cpp b/modules/operserv/os_logsearch.cpp index 606a74d78..0282644b2 100644 --- a/modules/operserv/os_logsearch.cpp +++ b/modules/operserv/os_logsearch.cpp @@ -47,32 +47,22 @@ public: if (params[i].length() > 2) { Anope::string dur = params[i].substr(1, params[i].length() - 2); - try - { - days = convertTo<int>(dur); - if (days <= 0) - throw ConvertException(); - } - catch (const ConvertException &) - { + auto d = Anope::Convert<int>(dur, 0); + if (d > 0) + days = d; + else source.Reply(_("Invalid duration %s, using %d days."), dur.c_str(), days); - } } break; case 'l': if (params[i].length() > 2) { Anope::string dur = params[i].substr(1, params[i].length() - 2); - try - { - replies = convertTo<int>(dur); - if (replies <= 0) - throw ConvertException(); - } - catch (const ConvertException &) - { + auto r = Anope::Convert<int>(dur, 0); + if (r > 0) + replies = r; + else source.Reply(_("Invalid limit %s, using %d."), dur.c_str(), replies); - } } break; default: diff --git a/modules/operserv/os_news.cpp b/modules/operserv/os_news.cpp index 067bfbc74..2cf011c0e 100644 --- a/modules/operserv/os_news.cpp +++ b/modules/operserv/os_news.cpp @@ -168,7 +168,7 @@ protected: for (unsigned i = 0, end = list.size(); i < end; ++i) { ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Creator"] = list[i]->who; entry["Created"] = Anope::strftime(list[i]->time, NULL, true); entry["Text"] = list[i]->text; @@ -232,18 +232,14 @@ protected: source.Reply(READ_ONLY_MODE); if (!text.equals_ci("ALL")) { - try + unsigned num = Anope::Convert<unsigned>(text, 0); + if (num > 0 && num <= list.size()) { - unsigned num = convertTo<unsigned>(text); - if (num > 0 && num <= list.size()) - { - this->ns->DelNewsItem(list[num - 1]); - source.Reply(msgs[MSG_DELETED], num); - Log(LOG_ADMIN, source, this) << "to delete a news item"; - return; - } + this->ns->DelNewsItem(list[num - 1]); + source.Reply(msgs[MSG_DELETED], num); + Log(LOG_ADMIN, source, this) << "to delete a news item"; + return; } - catch (const ConvertException &) { } source.Reply(msgs[MSG_DEL_NOT_FOUND], text.c_str()); } diff --git a/modules/operserv/os_session.cpp b/modules/operserv/os_session.cpp index 02695c55a..1e59f7d04 100644 --- a/modules/operserv/os_session.cpp +++ b/modules/operserv/os_session.cpp @@ -178,13 +178,7 @@ private: { Anope::string param = params[1]; - unsigned mincount = 0; - try - { - mincount = convertTo<unsigned>(param); - } - catch (const ConvertException &) { } - + auto mincount = Anope::Convert<unsigned>(param, 0); if (mincount <= 1) source.Reply(_("Invalid threshold value. It must be a valid integer greater than 1.")); else @@ -197,7 +191,7 @@ private: if (session->count >= mincount) { ListFormatter::ListEntry entry; - entry["Session"] = stringify(session->count); + entry["Session"] = Anope::ToString(session->count); entry["Host"] = session->addr.mask(); list.AddEntry(entry); } @@ -327,13 +321,7 @@ private: else if (expires > 0) expires += Anope::CurTime; - unsigned limit = -1; - try - { - limit = convertTo<unsigned>(limitstr); - } - catch (const ConvertException &) { } - + auto limit = Anope::Convert<unsigned>(limitstr, -1); if (limit > max_exception_limit) { source.Reply(_("Invalid session limit. It must be a valid integer greater than or equal to zero and less than \002%d\002."), max_exception_limit); @@ -453,12 +441,12 @@ private: Exception *e = session_service->GetExceptions()[Number - 1]; ListFormatter::ListEntry entry; - entry["Number"] = stringify(Number); + entry["Number"] = Anope::ToString(Number); entry["Mask"] = e->mask; entry["By"] = e->who; entry["Created"] = Anope::strftime(e->time, NULL, true); entry["Expires"] = Anope::Expires(e->expires, source.GetAccount()); - entry["Limit"] = stringify(e->limit); + entry["Limit"] = Anope::ToString(e->limit); entry["Reason"] = e->reason; this->list.AddEntry(entry); } @@ -474,12 +462,12 @@ private: if (mask.empty() || Anope::Match(e->mask, mask)) { ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Mask"] = e->mask; entry["By"] = e->who; entry["Created"] = Anope::strftime(e->time, NULL, true); entry["Expires"] = Anope::Expires(e->expires, source.GetAccount()); - entry["Limit"] = stringify(e->limit); + entry["Limit"] = Anope::ToString(e->limit); entry["Reason"] = e->reason; list.AddEntry(entry); } diff --git a/modules/operserv/os_set.cpp b/modules/operserv/os_set.cpp index a6cad4f07..b9c948e30 100644 --- a/modules/operserv/os_set.cpp +++ b/modules/operserv/os_set.cpp @@ -122,14 +122,14 @@ private: } else { - try + auto debug = Anope::TryConvert<int>(setting); + if (debug.has_value()) { - Anope::Debug = convertTo<int>(setting); + Anope::Debug = debug.value(); Log(LOG_ADMIN, source, this) << "DEBUG " << Anope::Debug; source.Reply(_("Services are now in \002debug\002 mode (level %d)."), Anope::Debug); return; } - catch (const ConvertException &) { } source.Reply(_("Setting for DEBUG must be \002ON\002, \002OFF\002, or a positive number.")); } diff --git a/modules/operserv/os_sxline.cpp b/modules/operserv/os_sxline.cpp index 769a25982..2460a5fe8 100644 --- a/modules/operserv/os_sxline.cpp +++ b/modules/operserv/os_sxline.cpp @@ -142,7 +142,7 @@ private: return; ListFormatter::ListEntry entry; - entry["Number"] = stringify(number); + entry["Number"] = Anope::ToString(number); entry["Mask"] = x->mask; entry["By"] = x->by; entry["Created"] = Anope::strftime(x->created, NULL, true); @@ -164,7 +164,7 @@ private: if (mask.empty() || mask.equals_ci(x->mask) || mask == x->id || Anope::Match(x->mask, mask, false, true)) { ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Mask"] = x->mask; entry["By"] = x->by; entry["Created"] = Anope::strftime(x->created, NULL, true); |
