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/botserv | |
| 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/botserv')
| -rw-r--r-- | modules/botserv/botserv.cpp | 8 | ||||
| -rw-r--r-- | modules/botserv/bs_badwords.cpp | 4 | ||||
| -rw-r--r-- | modules/botserv/bs_info.cpp | 2 | ||||
| -rw-r--r-- | modules/botserv/bs_kick.cpp | 96 |
4 files changed, 26 insertions, 84 deletions
diff --git a/modules/botserv/botserv.cpp b/modules/botserv/botserv.cpp index d2b8df4bb..706865522 100644 --- a/modules/botserv/botserv.cpp +++ b/modules/botserv/botserv.cpp @@ -67,12 +67,8 @@ public: Anope::string Limit; unsigned limit = 0; - try - { - if (c->GetParam("LIMIT", Limit)) - limit = convertTo<unsigned>(Limit); - } - catch (const ConvertException &) { } + if (c->GetParam("LIMIT", Limit)) + limit = Anope::Convert<unsigned>(Limit, limit); /* Should we be invited? */ if (c->HasMode("INVITE") || (limit && c->users.size() >= limit)) diff --git a/modules/botserv/bs_badwords.cpp b/modules/botserv/bs_badwords.cpp index 5dee8ed2c..09e96564b 100644 --- a/modules/botserv/bs_badwords.cpp +++ b/modules/botserv/bs_badwords.cpp @@ -222,7 +222,7 @@ private: const BadWord *b = bw->GetBadWord(Number - 1); ListFormatter::ListEntry entry; - entry["Number"] = stringify(Number); + entry["Number"] = Anope::ToString(Number); entry["Word"] = b->word; entry["Type"] = b->type == BW_SINGLE ? "(SINGLE)" : (b->type == BW_START ? "(START)" : (b->type == BW_END ? "(END)" : "")); this->list.AddEntry(entry); @@ -241,7 +241,7 @@ private: continue; ListFormatter::ListEntry entry; - entry["Number"] = stringify(i + 1); + entry["Number"] = Anope::ToString(i + 1); entry["Word"] = b->word; entry["Type"] = b->type == BW_SINGLE ? "(SINGLE)" : (b->type == BW_START ? "(START)" : (b->type == BW_END ? "(END)" : "")); list.AddEntry(entry); diff --git a/modules/botserv/bs_info.cpp b/modules/botserv/bs_info.cpp index c8e05577b..b4a5592b1 100644 --- a/modules/botserv/bs_info.cpp +++ b/modules/botserv/bs_info.cpp @@ -55,7 +55,7 @@ public: info[_("Real name")] = bi->realname; info[_("Created")] = Anope::strftime(bi->created, source.GetAccount()); info[_("Options")] = bi->oper_only ? _("Private") : _("None"); - info[_("Used on")] = stringify(bi->GetChannelCount()) + " channel(s)"; + info[_("Used on")] = Anope::ToString(bi->GetChannelCount()) + " channel(s)"; FOREACH_MOD(OnBotInfo, (source, bi, ci, info)); diff --git a/modules/botserv/bs_kick.cpp b/modules/botserv/bs_kick.cpp index 29c8cb598..9183eae22 100644 --- a/modules/botserv/bs_kick.cpp +++ b/modules/botserv/bs_kick.cpp @@ -106,11 +106,10 @@ struct KickerDataImpl final data["ttb"] >> ttb; spacesepstream sep(ttb); for (int i = 0; sep.GetToken(tok) && i < TTB_SIZE; ++i) - try - { - kd->ttb[i] = convertTo<int16_t>(tok); - } - catch (const ConvertException &) { } + { + if (auto n = Anope::TryConvert<int16_t>(tok)) + kd->ttb[i] = n.value(); + } kd->Check(ci); } @@ -206,21 +205,13 @@ protected: { if (!ttb.empty()) { - int16_t i; - - try - { - i = convertTo<int16_t>(ttb); - if (i < 0) - throw ConvertException(); - } - catch (const ConvertException &) + kd->ttb[ttb_idx] = Anope::Convert<int16_t>(ttb, -1); + if (kd->ttb[ttb_idx] < 0) { + kd->ttb[ttb_idx] = 0; source.Reply(_("\002%s\002 cannot be taken as times to ban."), ttb.c_str()); return; } - - kd->ttb[ttb_idx] = i; } else kd->ttb[ttb_idx] = 0; @@ -386,13 +377,8 @@ public: if (!ttb.empty()) { - try - { - kd->ttb[TTB_CAPS] = convertTo<int16_t>(ttb); - if (kd->ttb[TTB_CAPS] < 0) - throw ConvertException(); - } - catch (const ConvertException &) + kd->ttb[TTB_CAPS] = Anope::Convert<int16_t>(ttb, -1); + if (kd->ttb[TTB_CAPS] < 0) { kd->ttb[TTB_CAPS] = 0; source.Reply(_("\002%s\002 cannot be taken as times to ban."), ttb.c_str()); @@ -402,21 +388,11 @@ public: else kd->ttb[TTB_CAPS] = 0; - kd->capsmin = 10; - try - { - kd->capsmin = convertTo<int16_t>(min); - } - catch (const ConvertException &) { } + kd->capsmin = Anope::Convert(min, 0); if (kd->capsmin < 1) kd->capsmin = 10; - kd->capspercent = 25; - try - { - kd->capspercent = convertTo<int16_t>(percent); - } - catch (const ConvertException &) { } + kd->capspercent = Anope::Convert(percent, 0); if (kd->capspercent < 1 || kd->capspercent > 100) kd->capspercent = 25; @@ -518,42 +494,25 @@ public: if (!ttb.empty()) { - int16_t i; - - try - { - i = convertTo<int16_t>(ttb); - if (i < 0) - throw ConvertException(); - } - catch (const ConvertException &) + kd->ttb[TTB_FLOOD] = Anope::Convert<int16_t>(ttb, -1); + if (kd->ttb[TTB_FLOOD] < 0) { + kd->ttb[TTB_FLOOD] = 0; source.Reply(_("\002%s\002 cannot be taken as times to ban."), ttb.c_str()); return; } - - kd->ttb[TTB_FLOOD] = i; } else kd->ttb[TTB_FLOOD] = 0; - kd->floodlines = 6; - try - { - kd->floodlines = convertTo<int16_t>(lines); - } - catch (const ConvertException &) { } + kd->floodlines = Anope::Convert(lines, -1); if (kd->floodlines < 2) kd->floodlines = 6; - kd->floodsecs = 10; - try - { - kd->floodsecs = convertTo<int16_t>(secs); - } - catch (const ConvertException &) { } + kd->floodsecs = Anope::Convert(secs, -1); if (kd->floodsecs < 1) kd->floodsecs = 10; + if (kd->floodsecs > Config->GetModule(me)->Get<time_t>("keepdata")) kd->floodsecs = Config->GetModule(me)->Get<time_t>("keepdata"); @@ -651,31 +610,18 @@ public: if (!ttb.empty()) { - int16_t i; - - try - { - i = convertTo<int16_t>(ttb); - if (i < 0) - throw ConvertException(); - } - catch (const ConvertException &) + kd->ttb[TTB_REPEAT] = Anope::Convert(ttb, -1); + if (kd->ttb[TTB_REPEAT] < 0) { + kd->ttb[TTB_REPEAT] = 0; source.Reply(_("\002%s\002 cannot be taken as times to ban."), ttb.c_str()); return; } - - kd->ttb[TTB_REPEAT] = i; } else kd->ttb[TTB_REPEAT] = 0; - kd->repeattimes = 3; - try - { - kd->repeattimes = convertTo<int16_t>(times); - } - catch (const ConvertException &) { } + kd->repeattimes = Anope::Convert<int16_t>(times, -1); if (kd->repeattimes < 1) kd->repeattimes = 3; |
