diff options
| author | Sadie Powell <sadie@witchery.services> | 2025-02-20 12:33:59 +0000 |
|---|---|---|
| committer | Sadie Powell <sadie@witchery.services> | 2025-02-20 12:53:15 +0000 |
| commit | b498f4f4d91ff09120bfce6a10f7362fc40a011a (patch) | |
| tree | ab773a5316a1d33c291441e8f882930b37222d1b /modules/rpc | |
| parent | 8330cd119ac390036cb5cadd51019e6b496c1bfc (diff) | |
Add support for more RPC data types.
Diffstat (limited to 'modules/rpc')
| -rw-r--r-- | modules/rpc/jsonrpc.cpp | 54 | ||||
| -rw-r--r-- | modules/rpc/rpc_main.cpp | 31 |
2 files changed, 64 insertions, 21 deletions
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<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; +template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; + 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<RPCEvent *> 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); |
