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/protocol | |
| 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/protocol')
| -rw-r--r-- | modules/protocol/bahamut.cpp | 38 | ||||
| -rw-r--r-- | modules/protocol/hybrid.cpp | 24 | ||||
| -rw-r--r-- | modules/protocol/inspircd.cpp | 103 | ||||
| -rw-r--r-- | modules/protocol/ngircd.cpp | 7 | ||||
| -rw-r--r-- | modules/protocol/plexus.cpp | 22 | ||||
| -rw-r--r-- | modules/protocol/ratbox.cpp | 4 | ||||
| -rw-r--r-- | modules/protocol/solanum.cpp | 2 | ||||
| -rw-r--r-- | modules/protocol/unrealircd.cpp | 91 |
8 files changed, 93 insertions, 198 deletions
diff --git a/modules/protocol/bahamut.cpp b/modules/protocol/bahamut.cpp index 9c88b94a3..e80de36ad 100644 --- a/modules/protocol/bahamut.cpp +++ b/modules/protocol/bahamut.cpp @@ -19,15 +19,15 @@ public: bool IsValid(Anope::string &value) const override { - try - { - Anope::string rest; - if (!value.empty() && value[0] != ':' && convertTo<int>(value[0] == '*' ? value.substr(1) : value, rest, false) > 0 && rest[0] == ':' && rest.length() > 1 && convertTo<int>(rest.substr(1), rest, false) > 0 && rest.empty()) - return true; - } - catch (const ConvertException &) { } + if (value.empty() || value[0] == ':') + return false; + + Anope::string rest; + auto num1 = Anope::Convert<int>(value[0] == '*' ? value.substr(1) : value, 0, &rest); + if (num1 <= 0 || rest[0] != ':' || rest.length() <= 1) + return false; - return false; + return Anope::Convert<int>(rest.substr(1), 0, &rest) > 0 && rest.empty(); } }; @@ -52,7 +52,7 @@ public: if (Servers::Capab.count("TSMODE") > 0) { auto params = values; - params.insert(params.begin(), { chan->name, stringify(chan->creation_time), modes }); + params.insert(params.begin(), { chan->name, Anope::ToString(chan->creation_time), modes }); Uplink::SendInternal({}, source, "MODE", params); } else @@ -62,7 +62,7 @@ public: void SendModeInternal(const MessageSource &source, User *u, const Anope::string &modes, const std::vector<Anope::string> &values) override { auto params = values; - params.insert(params.begin(), { u->nick, stringify(u->timestamp), modes }); + params.insert(params.begin(), { u->nick, Anope::ToString(u->timestamp), modes }); Uplink::SendInternal({}, source, "SVSMODE", params); } @@ -323,13 +323,7 @@ struct IRCDMessageMode final if (params.size() > 2 && IRCD->IsChannelValid(params[0])) { Channel *c = Channel::Find(params[0]); - time_t ts = 0; - - try - { - ts = convertTo<time_t>(params[1]); - } - catch (const ConvertException &) { } + auto ts = Anope::Convert<time_t>(params[1], 0); Anope::string modes = params[2]; for (unsigned int i = 3; i < params.size(); ++i) @@ -382,8 +376,8 @@ struct IRCDMessageNick final } NickAlias *na = NULL; - time_t signon = params[2].is_pos_number_only() ? convertTo<time_t>(params[2]) : 0, - stamp = params[7].is_pos_number_only() ? convertTo<time_t>(params[7]) : 0; + auto signon = Anope::Convert<time_t>(params[2], 0); + auto stamp = Anope::Convert<time_t>(params[7], 0); if (signon && signon == stamp) na = NickAlias::Find(params[0]); @@ -406,7 +400,7 @@ struct IRCDMessageServer final void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { - unsigned int hops = Anope::string(params[1]).is_pos_number_only() ? convertTo<unsigned>(params[1]) : 0; + auto hops = Anope::Convert<unsigned>(params[1], 0); new Server(source.GetServer() == NULL ? Me : source.GetServer(), params[0], hops, params[2]); } }; @@ -463,7 +457,7 @@ struct IRCDMessageSJoin final } } - time_t ts = Anope::string(params[0]).is_pos_number_only() ? convertTo<time_t>(params[0]) : Anope::CurTime; + auto ts = Anope::Convert<time_t>(params[0], Anope::CurTime); Message::Join::SJoin(source, params[1], ts, modes, users); } }; @@ -477,7 +471,7 @@ struct IRCDMessageTopic final { Channel *c = Channel::Find(params[0]); if (c) - c->ChangeTopicInternal(source.GetUser(), params[1], params[3], Anope::string(params[2]).is_pos_number_only() ? convertTo<time_t>(params[2]) : Anope::CurTime); + c->ChangeTopicInternal(source.GetUser(), params[1], params[3], Anope::Convert<time_t>(params[2], Anope::CurTime)); } }; diff --git a/modules/protocol/hybrid.cpp b/modules/protocol/hybrid.cpp index 0f3f767d3..fc937d819 100644 --- a/modules/protocol/hybrid.cpp +++ b/modules/protocol/hybrid.cpp @@ -203,7 +203,7 @@ public: void SendModeInternal(const MessageSource &source, User *u, const Anope::string &modes, const std::vector<Anope::string> &values) override { auto params = values; - params.insert(params.begin(), { u->GetUID(), stringify(u->timestamp), modes }); + params.insert(params.begin(), { u->GetUID(), Anope::ToString(u->timestamp), modes }); Uplink::SendInternal({}, source, "SVSMODE", params); } @@ -468,7 +468,7 @@ struct IRCDMessageNick final /* :0MCAAAAAB NICK newnick 1350157102 */ void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { - source.GetUser()->ChangeNick(params[0], convertTo<time_t>(params[1])); + source.GetUser()->ChangeNick(params[0], Anope::Convert<time_t>(params[1], Anope::CurTime)); } }; @@ -531,7 +531,7 @@ struct IRCDMessageSID final /* :0MC SID hades.arpa 2 4XY + :ircd-hybrid test server */ void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { - unsigned int hops = params[1].is_pos_number_only() ? convertTo<unsigned>(params[1]) : 0; + auto hops = Anope::Convert(params[1], 0); new Server(source.GetServer() == NULL ? Me : source.GetServer(), params[0], hops, params.back(), params[2]); IRCD->SendPing(Me->GetName(), params[0]); @@ -581,7 +581,7 @@ struct IRCDMessageSJoin final users.push_back(sju); } - time_t ts = Anope::string(params[0]).is_pos_number_only() ? convertTo<time_t>(params[0]) : Anope::CurTime; + auto ts = Anope::Convert<time_t>(params[0], Anope::CurTime); Message::Join::SJoin(source, params[1], ts, modes, users); } }; @@ -600,7 +600,7 @@ struct IRCDMessageSVSMode final if (!u) return; - if (!params[1].is_pos_number_only() || convertTo<time_t>(params[1]) != u->timestamp) + if (Anope::Convert<time_t>(params[1], 0) != u->timestamp) return; u->SetModesInternal(source, params[2]); @@ -618,7 +618,7 @@ struct IRCDMessageTBurst final { Anope::string setter; sepstream(params[3], '!').GetToken(setter, 0); - time_t topic_time = Anope::string(params[2]).is_pos_number_only() ? convertTo<time_t>(params[2]) : Anope::CurTime; + auto topic_time = Anope::Convert<time_t>(params[2], Anope::CurTime); Channel *c = Channel::Find(params[1]); if (c) @@ -635,14 +635,7 @@ struct IRCDMessageTMode final /* :0MC TMODE 1654867975 #nether +ntR */ void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { - time_t ts = 0; - - try - { - ts = convertTo<time_t>(params[0]); - } - catch (const ConvertException &) { } - + auto ts = Anope::Convert<time_t>(params[0], 0); Channel *c = Channel::Find(params[1]); Anope::string modes = params[2]; @@ -670,8 +663,7 @@ struct IRCDMessageUID final /* Source is always the server */ User::OnIntroduce(params[0], params[4], params[6], params[5], params[7], source.GetServer(), params[10], - params[2].is_pos_number_only() ? convertTo<time_t>(params[2]) : 0, - params[3], params[8], na ? *na->nc : NULL); + Anope::Convert<time_t>(params[2], 0), params[3], params[8], na ? *na->nc : NULL); } }; diff --git a/modules/protocol/inspircd.cpp b/modules/protocol/inspircd.cpp index fa7a509d6..72f1b1cf1 100644 --- a/modules/protocol/inspircd.cpp +++ b/modules/protocol/inspircd.cpp @@ -130,7 +130,7 @@ private: static void SendAccount(const Anope::string &uid, NickAlias *na) { - Uplink::Send("METADATA", uid, "accountid", na ? na->nc->GetId() : Anope::string()); + Uplink::Send("METADATA", uid, "accountid", na ? Anope::ToString(na->nc->GetId()) : Anope::string()); Uplink::Send("METADATA", uid, "accountname", na ? na->nc->display : Anope::string()); if (spanningtree_proto_ver >= 1206) Uplink::Send("METADATA", uid, "accountnicks", GetAccountNicks(na)); @@ -401,14 +401,14 @@ public: void SendNumericInternal(int numeric, const Anope::string &dest, const std::vector<Anope::string> ¶ms) override { auto newparams = params; - newparams.insert(newparams.begin(), { Me->GetSID(), dest, stringify(numeric) }); + newparams.insert(newparams.begin(), { Me->GetSID(), dest, Anope::ToString(numeric) }); Uplink::SendInternal({}, Me, "NUM", newparams); } void SendModeInternal(const MessageSource &source, Channel *chan, const Anope::string &modes, const std::vector<Anope::string> &values) override { auto params = values; - params.insert(params.begin(), { chan->name, stringify(chan->creation_time), modes }); + params.insert(params.begin(), { chan->name, Anope::ToString(chan->creation_time), modes }); Uplink::SendInternal({}, source, "FMODE", params); } @@ -923,32 +923,20 @@ public: return false; // no ':' or it's the first char, both are invalid Anope::string rest; - try - { - if (convertTo<int>(value, rest, false) <= 0) - return false; // negative numbers and zero are invalid - - rest = rest.substr(1); - int n; - if (historymode) - { - // For the history mode, the part after the ':' is a duration and it - // can be in the user friendly "1d3h20m" format, make sure we accept that - n = Anope::DoTime(rest); - } - else - n = convertTo<int>(rest); + if (Anope::Convert<int>(value, 0, &rest) <= 0) + return false; // negative numbers and zero are invalid - if (n <= 0) - return false; + rest = rest.substr(1); + if (historymode) + { + // For the history mode, the part after the ':' is a duration and it + // can be in the user friendly "1d3h20m" format, make sure we accept that + return Anope::DoTime(rest) <= 0; } - catch (const ConvertException &e) + else { - // conversion error, invalid - return false; + return Anope::Convert(rest, 0) <= 0; } - - return true; } }; @@ -963,18 +951,7 @@ public: if (value.empty()) return false; // empty param is never valid - try - { - if (convertTo<int>(value) <= 0) - return false; - } - catch (const ConvertException &e) - { - // conversion error, invalid - return false; - } - - return true; + return Anope::Convert<int>(value, 0) <= 0; } }; @@ -1068,15 +1045,15 @@ struct IRCDMessageCapab final static std::pair<Anope::string, size_t> ParseCapability(const Anope::string &token) { - auto sep = token.find(':'); + auto sep = token.find('='); if (sep == Anope::string::npos) return { token, 0 }; - auto value = token.substr(sep); + auto value = token.substr(sep + 1); if (!value.is_pos_number_only()) return { token, 0 }; - return { token.substr(0, sep), convertTo<size_t>(value) }; + return { token.substr(0, sep), Anope::Convert<size_t>(value, 0) }; } static bool ParseExtBan(const Anope::string &token, ExtBanInfo &extban) @@ -1117,7 +1094,7 @@ struct IRCDMessageCapab final return false; const Anope::string modelevel = token.substr(a + 1, b - a - 1); - mode.level = modelevel.is_pos_number_only() ? convertTo<unsigned>(modelevel) : 0; + mode.level = Anope::Convert<unsigned>(modelevel, 0); a = b; } @@ -1159,7 +1136,7 @@ struct IRCDMessageCapab final { spanningtree_proto_ver = 0; if (params.size() >= 2) - spanningtree_proto_ver = params[1].is_pos_number_only() ? convertTo<size_t>(params[1]) : 0; + spanningtree_proto_ver = Anope::Convert<size_t>(params[1], 0); if (spanningtree_proto_ver < 1205) { @@ -1769,18 +1746,8 @@ struct IRCDMessageSave final void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { User *targ = User::Find(params[0]); - time_t ts; - - try - { - ts = convertTo<time_t>(params[1]); - } - catch (const ConvertException &) - { - return; - } - - if (!targ || targ->timestamp != ts) + auto ts = Anope::Convert<time_t>(params[1], 0); + if (!targ || !ts || targ->timestamp != ts) return; BotInfo *bi; @@ -1847,7 +1814,7 @@ public: Anope::string modechr, modelimit; while (limitstream.GetToken(modechr) && limitstream.GetToken(modelimit)) { - limits.emplace(modechr[0], convertTo<unsigned>(modelimit)); + limits.emplace(modechr[0], Anope::Convert<unsigned>(modelimit, 0)); } maxlist.Set(c, limits); } @@ -2048,7 +2015,7 @@ struct IRCDMessageFJoin final users.push_back(sju); } - time_t ts = Anope::string(params[1]).is_pos_number_only() ? convertTo<time_t>(params[1]) : Anope::CurTime; + auto ts = Anope::Convert<time_t>(params[0], Anope::CurTime); Message::Join::SJoin(source, params[0], ts, modes, users); } }; @@ -2067,17 +2034,7 @@ struct IRCDMessageFMode final modes += " " + params[n]; Channel *c = Channel::Find(params[0]); - time_t ts; - - try - { - ts = convertTo<time_t>(params[1]); - } - catch (const ConvertException &) - { - ts = 0; - } - + auto ts = Anope::Convert<time_t>(params[1], 0); if (c) c->SetModesInternal(source, modes, ts); } @@ -2098,7 +2055,7 @@ struct IRCDMessageFTopic final Channel *c = Channel::Find(params[0]); if (c) - c->ChangeTopicInternal(NULL, setby, topic, params[2].is_pos_number_only() ? convertTo<time_t>(params[2]) : Anope::CurTime); + c->ChangeTopicInternal(NULL, setby, topic, Anope::Convert<time_t>(params[2], Anope::CurTime)); } }; @@ -2147,7 +2104,7 @@ struct IRCDMessageIJoin final time_t chants = Anope::CurTime; if (params.size() >= 4) { - chants = params[2].is_pos_number_only() ? convertTo<unsigned>(params[2]) : 0; + chants = Anope::Convert<time_t>(params[2], 0); for (auto mode : params[3]) user.first.AddMode(mode); } @@ -2175,7 +2132,7 @@ struct IRCDMessageLMode final return; // Channel doesn't exist. // If the TS is greater than ours, we drop the mode and don't pass it anywhere. - auto chants = convertTo<time_t>(params[1]); + auto chants = Anope::Convert<time_t>(params[1], Anope::CurTime); if (chants > chan->creation_time) return; @@ -2307,7 +2264,7 @@ struct IRCDMessageServer final * 3: numeric * 4: desc */ - unsigned int hops = Anope::string(params[2]).is_pos_number_only() ? convertTo<unsigned>(params[2]) : 0; + auto hops = Anope::Convert<unsigned>(params[2], 0); new Server(Me, params[0], hops, params[4], params[3]); } else if (source.GetServer()) @@ -2380,7 +2337,7 @@ struct IRCDMessageUID final void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { size_t offset = params[9][0] == '+' ? 1 : 0; - time_t ts = convertTo<time_t>(params[1]); + time_t ts = Anope::Convert<time_t>(params[1], 0); Anope::string modes = params[8]; for (unsigned i = 9; i < params.size() - 1; ++i) @@ -2405,7 +2362,7 @@ struct IRCDMessageUID final User *u = User::OnIntroduce(params[2], params[5+offset], params[3], params[4], params[6+offset], source.GetServer(), params[params.size() - 1], ts, modes, params[0], na ? *na->nc : NULL); if (u) - u->signon = convertTo<time_t>(params[7+offset]); + u->signon = Anope::Convert<time_t>(params[7+offset], 0); } }; diff --git a/modules/protocol/ngircd.cpp b/modules/protocol/ngircd.cpp index 773fab805..ea9d92294 100644 --- a/modules/protocol/ngircd.cpp +++ b/modules/protocol/ngircd.cpp @@ -178,12 +178,11 @@ struct IRCDMessage005 final data = param.substr(pos+1, param.length()); if (parameter == "MODES") { - unsigned maxmodes = convertTo<unsigned>(data); - IRCD->MaxModes = maxmodes; + IRCD->MaxModes = Anope::Convert<unsigned>(data, IRCD->MaxModes); } else if (parameter == "NICKLEN") { - nicklen = data.is_pos_number_only() ? convertTo<size_t>(data) : 0; + nicklen = Anope::Convert<size_t>(data, 0); } } } @@ -557,7 +556,7 @@ struct IRCDMessageServer final else { // our uplink is introducing a new server - unsigned int hops = params[1].is_pos_number_only() ? convertTo<unsigned>(params[1]) : 0; + auto hops = Anope::Convert<unsigned>(params[1], 0); new Server(source.GetServer(), params[0], hops, params[3], params[2]); } /* diff --git a/modules/protocol/plexus.cpp b/modules/protocol/plexus.cpp index 1e5ae714b..a2282276a 100644 --- a/modules/protocol/plexus.cpp +++ b/modules/protocol/plexus.cpp @@ -144,7 +144,7 @@ public: void SendModeInternal(const MessageSource &source, User *u, const Anope::string &modes, const std::vector<Anope::string> &values) override { auto params = values; - params.insert(params.begin(), { "*", "SVSMODE", u->GetUID(), stringify(u->timestamp), modes }); + params.insert(params.begin(), { "*", "SVSMODE", u->GetUID(), Anope::ToString(u->timestamp), modes }); Uplink::SendInternal({}, source, "ENCAP", params); } @@ -306,23 +306,11 @@ struct IRCDMessageUID final if (ip == "0") ip.clear(); - time_t ts; - try - { - ts = convertTo<time_t>(params[2]); - } - catch (const ConvertException &) - { - ts = Anope::CurTime; - } - + auto ts = Anope::Convert<time_t>(params[2], Anope::CurTime); NickAlias *na = NULL; - try - { - if (params[8].is_pos_number_only() && convertTo<time_t>(params[8]) == ts) - na = NickAlias::Find(params[0]); - } - catch (const ConvertException &) { } + if (Anope::Convert<time_t>(params[8], 0) == ts) + na = NickAlias::Find(params[0]); + if (params[8] != "0" && !na) na = NickAlias::Find(params[8]); diff --git a/modules/protocol/ratbox.cpp b/modules/protocol/ratbox.cpp index 18dd448af..d6f7a1b3a 100644 --- a/modules/protocol/ratbox.cpp +++ b/modules/protocol/ratbox.cpp @@ -228,7 +228,7 @@ struct IRCDMessageTBurst final */ void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { - time_t topic_time = Anope::string(params[1]).is_pos_number_only() ? convertTo<time_t>(params[1]) : Anope::CurTime; + auto topic_time = Anope::Convert<time_t>(params[1], Anope::CurTime); Channel *c = Channel::Find(params[0]); if (!c) @@ -250,7 +250,7 @@ struct IRCDMessageUID final void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { /* Source is always the server */ - User::OnIntroduce(params[0], params[4], params[5], "", params[6], source.GetServer(), params[8], params[2].is_pos_number_only() ? convertTo<time_t>(params[2]) : 0, params[3], params[7], NULL); + User::OnIntroduce(params[0], params[4], params[5], "", params[6], source.GetServer(), params[8], Anope::Convert<time_t>(params[2], 0), params[3], params[7], NULL); } }; diff --git a/modules/protocol/solanum.cpp b/modules/protocol/solanum.cpp index 59d401741..6ac5cbc69 100644 --- a/modules/protocol/solanum.cpp +++ b/modules/protocol/solanum.cpp @@ -254,7 +254,7 @@ struct IRCDMessageEUID final if (params[9] != "*") na = NickAlias::Find(params[9]); - User::OnIntroduce(params[0], params[4], (params[8] != "*" ? params[8] : params[5]), params[5], params[6], source.GetServer(), params[10], params[2].is_pos_number_only() ? convertTo<time_t>(params[2]) : Anope::CurTime, params[3], params[7], na ? *na->nc : NULL); + User::OnIntroduce(params[0], params[4], (params[8] != "*" ? params[8] : params[5]), params[5], params[6], source.GetServer(), params[10], Anope::Convert<time_t>(params[2], Anope::CurTime), params[3], params[7], na ? *na->nc : NULL); } }; diff --git a/modules/protocol/unrealircd.cpp b/modules/protocol/unrealircd.cpp index c36cd10ed..d1d852d37 100644 --- a/modules/protocol/unrealircd.cpp +++ b/modules/protocol/unrealircd.cpp @@ -675,15 +675,16 @@ public: /* Borrowed part of this check from UnrealIRCd */ bool IsValid(Anope::string &value) const override { - if (value.empty()) + if (value.empty() || value[0] == ':') return false; - try - { - Anope::string rest; - if (value[0] != ':' && convertTo<unsigned>(value[0] == '*' ? value.substr(1) : value, rest, false) > 0 && rest[0] == ':' && rest.length() > 1 && convertTo<unsigned>(rest.substr(1), rest, false) > 0 && rest.empty()) - return true; - } - catch (const ConvertException &) { } + + Anope::string rest; + auto num1 = Anope::Convert<unsigned>(value[0] == '*' ? value.substr(1) : value, 0, &rest); + if (!num1 || rest[0] != ':' || rest.length() <= 1) + return false; + + if (Anope::Convert<int>(rest.substr(1), 0, &rest) > 0 && rest.empty()) + return true; /* '['<number><1 letter>[optional: '#'+1 letter],[next..]']'':'<number> */ size_t end_bracket = value.find(']', 1); @@ -702,16 +703,10 @@ public: ++p; if (p == arg.length() || (arg[p] != 'c' && arg[p] != 'j' && arg[p] != 'k' && arg[p] != 'm' && arg[p] != 'n' && arg[p] != 't')) continue; /* continue instead of break for forward compatibility. */ - try - { - int v = arg.substr(0, p).is_number_only() ? convertTo<int>(arg.substr(0, p)) : 0; - if (v < 1 || v > 999) - return false; - } - catch (const ConvertException &) - { + + auto v = Anope::Convert<int>(arg.substr(0, p), 0); + if (v < 1 || v > 999) return false; - } } return true; @@ -734,27 +729,14 @@ public: return false; // no ':' or it's the first char, both are invalid Anope::string rest; - try - { - if (convertTo<int>(value, rest, false) <= 0) - return false; // negative numbers and zero are invalid - - rest = rest.substr(1); - int n; - // The part after the ':' is a duration and it - // can be in the user friendly "1d3h20m" format, make sure we accept that - n = Anope::DoTime(rest); + if (Anope::Convert<int>(value, 0, &rest) <= 0) + return false; // negative numbers and zero are invalid - if (n <= 0) - return false; - } - catch (const ConvertException &e) - { - // conversion error, invalid + // The part after the ':' is a duration and it + // can be in the user friendly "1d3h20m" format, make sure we accept that + auto n = Anope::DoTime(rest.substr(1)); + return n <= 0; return false; - } - - return true; } }; @@ -1177,14 +1159,7 @@ struct IRCDMessageMode final if (IRCD->IsChannelValid(params[0])) { Channel *c = Channel::Find(params[0]); - time_t ts = 0; - - try - { - if (server_source) - ts = convertTo<time_t>(params[params.size() - 1]); - } - catch (const ConvertException &) { } + auto ts = Anope::Convert<time_t>(params[params.size() - 1], 0); if (c) c->SetModesInternal(source, modes, ts); @@ -1215,7 +1190,7 @@ struct IRCDMessageNetInfo final void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { - Uplink::Send("NETINFO", MaxUserCount, Anope::CurTime, convertTo<int>(params[2]), params[3], 0, 0, 0, params[7]); + Uplink::Send("NETINFO", MaxUserCount, Anope::CurTime, params[2], params[3], 0, 0, 0, params[7]); } }; @@ -1263,8 +1238,7 @@ struct IRCDMessageNick final if (vhost.equals_cs("*")) vhost.clear(); - time_t user_ts = params[2].is_pos_number_only() ? convertTo<time_t>(params[2]) : Anope::CurTime; - + auto user_ts = Anope::Convert<time_t>(params[2], Anope::CurTime); Server *s = Server::Find(params[5]); if (s == NULL) { @@ -1278,7 +1252,7 @@ struct IRCDMessageNick final ; else if (params[6].is_pos_number_only()) { - if (convertTo<time_t>(params[6]) == user_ts) + if (Anope::Convert<time_t>(params[6], 0) == user_ts) na = NickAlias::Find(params[0]); } else @@ -1398,7 +1372,7 @@ struct IRCDMessageServer final void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { - unsigned int hops = Anope::string(params[1]).is_pos_number_only() ? convertTo<unsigned>(params[1]) : 0; + auto hops = Anope::Convert<unsigned>(params[1], 0); if (params[1].equals_cs("1")) { @@ -1421,7 +1395,7 @@ struct IRCDMessageSID final void Run(MessageSource &source, const std::vector<Anope::string> ¶ms, const Anope::map<Anope::string> &tags) override { - unsigned int hops = Anope::string(params[1]).is_pos_number_only() ? convertTo<unsigned>(params[1]) : 0; + auto hops = Anope::Convert<unsigned>(params[1], 0); new Server(source.GetServer(), params[0], hops, params[3], params[2]); @@ -1503,7 +1477,7 @@ struct IRCDMessageSJoin final } } - time_t ts = Anope::string(params[0]).is_pos_number_only() ? convertTo<time_t>(params[0]) : Anope::CurTime; + auto ts = Anope::Convert<time_t>(params[0], Anope::CurTime); Message::Join::SJoin(source, params[1], ts, modes, users); if (!bans.empty() || !excepts.empty() || !invites.empty()) @@ -1582,7 +1556,7 @@ struct IRCDMessageTopic final { Channel *c = Channel::Find(params[0]); if (c) - c->ChangeTopicInternal(source.GetUser(), params[1], params[3], Anope::string(params[2]).is_pos_number_only() ? convertTo<time_t>(params[2]) : Anope::CurTime); + c->ChangeTopicInternal(source.GetUser(), params[1], params[3], Anope::Convert<time_t>(params[2], Anope::CurTime)); } }; @@ -1637,16 +1611,7 @@ struct IRCDMessageUID final if (chost == "*") chost.clear(); - time_t user_ts; - try - { - user_ts = convertTo<time_t>(timestamp); - } - catch (const ConvertException &) - { - user_ts = Anope::CurTime; - } - + auto user_ts = Anope::Convert<time_t>(timestamp, Anope::CurTime); NickAlias *na = NULL; if (account == "0") @@ -1655,7 +1620,7 @@ struct IRCDMessageUID final } else if (account.is_pos_number_only()) { - if (convertTo<time_t>(account) == user_ts) + if (Anope::Convert<time_t>(account, 0) == user_ts) na = NickAlias::Find(nickname); } else |
