From b498f4f4d91ff09120bfce6a10f7362fc40a011a Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Thu, 20 Feb 2025 12:33:59 +0000 Subject: Add support for more RPC data types. --- modules/extra/xmlrpc.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++----- modules/rpc/jsonrpc.cpp | 54 ++++++++++++++++++++++++++++++++++------ modules/rpc/rpc_main.cpp | 31 ++++++++++++----------- 3 files changed, 122 insertions(+), 27 deletions(-) (limited to 'modules') diff --git a/modules/extra/xmlrpc.cpp b/modules/extra/xmlrpc.cpp index 63ed55433..2051f35e1 100644 --- a/modules/extra/xmlrpc.cpp +++ b/modules/extra/xmlrpc.cpp @@ -15,6 +15,9 @@ #include "modules/rpc.h" #include "modules/httpd.h" +template struct overloaded : Ts... { using Ts::operator()...; }; +template overloaded(Ts...) -> overloaded; + class MyXMLRPCServiceInterface final : public RPCServiceInterface , public HTTPPage @@ -22,7 +25,7 @@ class MyXMLRPCServiceInterface final private: Anope::map events; - void SendError(HTTPReply &reply, xmlrpc_env &env) + static void SendError(HTTPReply &reply, xmlrpc_env &env) { Log(LOG_DEBUG) << "XML-RPC error " << env.fault_code << ": " << env.fault_string; @@ -39,6 +42,59 @@ private: xmlrpc_env_clean(&env); } + static void SerializeObject(xmlrpc_env &env, xmlrpc_value *value, const RPCBlock &block) + { + for (const auto &[k, v] : block.GetReplies()) + { + xmlrpc_value *elem; + std::visit(overloaded + { + [&env, &elem](const RPCBlock &b) + { + elem = xmlrpc_struct_new(&env); + SerializeObject(env, elem, b); + }, + [&env, &elem](const Anope::string &s) + { + elem = xmlrpc_string_new_lp(&env, s.length(), s.c_str()); + }, + [&env, &elem](std::nullptr_t) + { + elem = xmlrpc_nil_new(&env); + }, + [&env, &elem](bool b) + { + elem = xmlrpc_bool_new(&env, b); + }, + [&env, &elem](double d) + { + elem = xmlrpc_double_new(&env, d); + }, + [&env, &elem](int64_t i) + { + elem = xmlrpc_i8_new(&env, i); + }, + [&env, &elem](uint64_t u) + { + // XML-RPC does not support unsigned data types. + if (u > INT64_MAX) + { + // We need to convert this to a string. + auto s = Anope::ToString(u); + elem = xmlrpc_string_new_lp(&env, s.length(), s.c_str()); + } + else + { + // We can fit this into a i8. + elem = xmlrpc_i8_new(&env, u); + } + }, + }, v); + + xmlrpc_struct_set_value_n(&env, value, k.c_str(), k.length(), elem); + } + } + public: MyXMLRPCServiceInterface(Module *creator, const Anope::string &sname) : RPCServiceInterface(creator, sname) @@ -138,11 +194,7 @@ public: } auto *value = xmlrpc_struct_new(&env); - for (const auto &[k, v] : request.GetReplies()) - { - auto *str = xmlrpc_string_new_lp(&env, v.length(), v.c_str()); - xmlrpc_struct_set_value_n(&env, value, k.c_str(), k.length(), str); - } + SerializeObject(env, value, request); auto *response = xmlrpc_mem_block_new(&env, 0); xmlrpc_serialize_response(&env, response, value); diff --git a/modules/rpc/jsonrpc.cpp b/modules/rpc/jsonrpc.cpp index cd24dc55c..1442115b7 100644 --- a/modules/rpc/jsonrpc.cpp +++ b/modules/rpc/jsonrpc.cpp @@ -12,6 +12,9 @@ #include "yyjson/yyjson.c" +template struct overloaded : Ts... { using Ts::operator()...; }; +template overloaded(Ts...) -> overloaded; + inline Anope::string yyjson_get_astr(yyjson_val *val, const char *key) { const auto *str = yyjson_get_str(yyjson_obj_get(val, key)); @@ -25,7 +28,7 @@ class MyJSONRPCServiceInterface final private: Anope::map events; - void SendError(HTTPReply &reply, int64_t code, const Anope::string &message, const Anope::string &id) + static void SendError(HTTPReply &reply, int64_t code, const Anope::string &message, const Anope::string &id) { Log(LOG_DEBUG) << "JSON-RPC error " << code << ": " << message; @@ -56,6 +59,48 @@ private: yyjson_mut_doc_free(doc); } + static void SerializeObject(yyjson_mut_doc *doc, yyjson_mut_val *root, const char *key, const RPCBlock &block) + { + auto *result = yyjson_mut_obj(doc); + for (const auto &reply : block.GetReplies()) + { + // Captured structured bindings are a C++20 extension. + const auto &k = reply.first; + std::visit(overloaded + { + [&doc, &result, &k](const RPCBlock &b) + { + SerializeObject(doc, result, k.c_str(), b); + }, + [&doc, &result, &k](const Anope::string &s) + { + yyjson_mut_obj_add_strn(doc, result, k.c_str(), s.c_str(), s.length()); + }, + [&doc, &result, &k](std::nullptr_t) + { + yyjson_mut_obj_add_null(doc, result, k.c_str()); + }, + [&doc, &result, &k](bool b) + { + yyjson_mut_obj_add_bool(doc, result, k.c_str(), b); + }, + [&doc, &result, &k](double d) + { + yyjson_mut_obj_add_real(doc, result, k.c_str(), d); + }, + [&doc, &result, &k](int64_t i) + { + yyjson_mut_obj_add_int(doc, result, k.c_str(), i); + }, + [&doc, &result, &k](uint64_t u) + { + yyjson_mut_obj_add_uint(doc, result, k.c_str(), u); + }, + }, reply.second); + } + yyjson_mut_obj_add_val(doc, root, key, result); + } + public: MyJSONRPCServiceInterface(Module *creator, const Anope::string &sname) : RPCServiceInterface(creator, sname) @@ -149,12 +194,7 @@ public: yyjson_mut_obj_add_strn(doc, root, "id", request.id.c_str(), request.id.length()); if (!request.GetReplies().empty()) - { - auto *result = yyjson_mut_obj(doc); - for (const auto &[k, v] : request.GetReplies()) - yyjson_mut_obj_add_strn(doc, result, k.c_str(), v.c_str(), v.length()); - yyjson_mut_obj_add_val(doc, root, "result", result); - } + SerializeObject(doc, root, "result", request); yyjson_mut_obj_add_str(doc, root, "jsonrpc", "2.0"); diff --git a/modules/rpc/rpc_main.cpp b/modules/rpc/rpc_main.cpp index a634e783f..0cf8b8b0b 100644 --- a/modules/rpc/rpc_main.cpp +++ b/modules/rpc/rpc_main.cpp @@ -152,7 +152,7 @@ public: bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override { - request.Reply("uptime", Anope::ToString(Anope::CurTime - Anope::StartTime)); + request.ReplyInt("uptime", Anope::CurTime - Anope::StartTime); request.Reply("uplinkname", Me->GetLinks().front()->GetName()); { Anope::string buf; @@ -162,9 +162,9 @@ public: buf.erase(buf.begin()); request.Reply("uplinkcapab", buf); } - request.Reply("usercount", Anope::ToString(UserListByNick.size())); - request.Reply("maxusercount", Anope::ToString(MaxUserCount)); - request.Reply("channelcount", Anope::ToString(ChannelList.size())); + request.ReplyUInt("usercount", UserListByNick.size()); + request.ReplyUInt("maxusercount", MaxUserCount); + request.ReplyUInt("channelcount", ChannelList.size()); return true; } }; @@ -192,20 +192,23 @@ public: if (c) { - request.Reply("bancount", Anope::ToString(c->HasMode("BAN"))); + auto &bans = request.ReplyBlock("bans"); + bans.ReplyUInt("count", c->HasMode("BAN")); int count = 0; for (auto &ban : c->GetModeList("BAN")) - request.Reply("ban" + Anope::ToString(++count), ban); + bans.Reply(Anope::ToString(++count), ban); - request.Reply("exceptcount", Anope::ToString(c->HasMode("EXCEPT"))); + auto &excepts = request.ReplyBlock("excepts"); + excepts.ReplyUInt("count", c->HasMode("EXCEPT")); count = 0; for (auto &except : c->GetModeList("EXCEPT")) - request.Reply("except" + Anope::ToString(++count), except); + excepts.Reply(Anope::ToString(++count), except); - request.Reply("invitecount", Anope::ToString(c->HasMode("INVITEOVERRIDE"))); + auto &invites = request.ReplyBlock("invites"); + invites.ReplyUInt("count", c->HasMode("INVITEOVERRIDE")); count = 0; for (auto &invite : c->GetModeList("INVITEOVERRIDE")) - request.Reply("invite" + Anope::ToString(++count), invite); + invites.Reply(Anope::ToString(++count), invite); Anope::string users; for (Channel::ChanUserList::const_iterator it = c->users.begin(); it != c->users.end(); ++it) @@ -225,8 +228,8 @@ public: if (!c->topic_setter.empty()) request.Reply("topicsetter", c->topic_setter); - request.Reply("topictime", Anope::ToString(c->topic_time)); - request.Reply("topicts", Anope::ToString(c->topic_ts)); + request.ReplyInt("topictime", c->topic_time); + request.ReplyInt("topicts", c->topic_ts); } return true; } @@ -263,8 +266,8 @@ public: if (!u->chost.empty()) request.Reply("chost", u->chost); request.Reply("ip", u->ip.addr()); - request.Reply("timestamp", Anope::ToString(u->timestamp)); - request.Reply("signon", Anope::ToString(u->signon)); + request.ReplyUInt("timestamp", u->timestamp); + request.ReplyUInt("signon", u->signon); if (u->IsIdentified()) { request.Reply("account", u->Account()->display); -- cgit