From 96839ae1b82fa51340ad9769fe57087c8dac5ede Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Thu, 20 Feb 2025 14:46:31 +0000 Subject: [PATCH] Move RPC types to the RPC namespace. --- include/modules/rpc.h | 62 ++++++++++++++++++++++------------------ modules/extra/xmlrpc.cpp | 18 ++++++------ modules/rpc/jsonrpc.cpp | 18 ++++++------ modules/rpc/rpc_main.cpp | 50 ++++++++++++++++---------------- 4 files changed, 77 insertions(+), 71 deletions(-) diff --git a/include/modules/rpc.h b/include/modules/rpc.h index 68523e5fa..b33c26800 100644 --- a/include/modules/rpc.h +++ b/include/modules/rpc.h @@ -14,6 +14,14 @@ namespace RPC { + class Block; + class Event; + class Request; + class ServiceInterface; + + /** Represents possible types of RPC value. */ + using Value = std::variant; + /** Represents standard RPC errors from the JSON-RPC and XML-RPC specifications. */ enum Error : int64_t @@ -25,64 +33,61 @@ namespace RPC }; } -class RPCBlock +class RPC::Block { public: - /** Represents possible types of RPC value. */ - using RPCValue = std::variant; - /** Retrieves the list of RPC replies. */ inline const auto &GetReplies() const { return this->replies; } template - inline RPCBlock &Reply(const Anope::string &key, const Stringable &value) + inline Block &Reply(const Anope::string &key, const Stringable &value) { this->replies.emplace(key, Anope::ToString(value)); return *this; } - inline RPCBlock &ReplyBlock(const Anope::string &key) + inline Block &ReplyBlock(const Anope::string &key) { - auto it = this->replies.emplace(key, RPCBlock()); - return std::get(it.first->second); + auto it = this->replies.emplace(key, Block()); + return std::get(it.first->second); } - inline RPCBlock &ReplyBool(const Anope::string &key, bool value) + inline Block &ReplyBool(const Anope::string &key, bool value) { this->replies.emplace(key, value); return *this; } - inline RPCBlock &ReplyFloat(const Anope::string &key, double value) + inline Block &ReplyFloat(const Anope::string &key, double value) { this->replies.emplace(key, value); return *this; } - inline RPCBlock &ReplyInt(const Anope::string &key, int64_t value) + inline Block &ReplyInt(const Anope::string &key, int64_t value) { this->replies.emplace(key, value); return *this; } - inline RPCBlock &ReplyNull(const Anope::string &key) + inline Block &ReplyNull(const Anope::string &key) { this->replies.emplace(key, nullptr); return *this; } - inline RPCBlock &ReplyUInt(const Anope::string &key, uint64_t value) + inline Block &ReplyUInt(const Anope::string &key, uint64_t value) { this->replies.emplace(key, value); return *this; } private: - Anope::map replies; + Anope::map replies; }; -class RPCRequest final - : public RPCBlock +class RPC::Request final + : public RPC::Block { private: std::optional> error; @@ -93,7 +98,7 @@ public: std::deque data; HTTPReply &reply; - RPCRequest(HTTPReply &r) + Request(HTTPReply &r) : reply(r) { } @@ -106,36 +111,37 @@ public: inline const auto &GetError() const { return this->error; } }; -class RPCServiceInterface; - -class RPCEvent +class RPC::Event { private: Anope::string event; protected: - RPCEvent(const Anope::string& e) + Event(const Anope::string& e) : event(e) { } public: - virtual ~RPCEvent() = default; + virtual ~Event() = default; const auto &GetEvent() const { return event; } - virtual bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) = 0; + virtual bool Run(ServiceInterface *iface, HTTPClient *client, Request &request) = 0; }; -class RPCServiceInterface +class RPC::ServiceInterface : public Service { public: - RPCServiceInterface(Module *creator, const Anope::string &sname) : Service(creator, "RPCServiceInterface", sname) { } + ServiceInterface(Module *creator, const Anope::string &sname) + : Service(creator, "RPCServiceInterface", sname) + { + } - virtual bool Register(RPCEvent *event) = 0; + virtual bool Register(Event *event) = 0; - virtual bool Unregister(RPCEvent *event) = 0; + virtual bool Unregister(Event *event) = 0; - virtual void Reply(RPCRequest &request) = 0; + virtual void Reply(Request &request) = 0; }; diff --git a/modules/extra/xmlrpc.cpp b/modules/extra/xmlrpc.cpp index cd51f5889..df566c0a4 100644 --- a/modules/extra/xmlrpc.cpp +++ b/modules/extra/xmlrpc.cpp @@ -19,11 +19,11 @@ template struct overloaded : Ts... { using Ts::operator()...; }; template overloaded(Ts...) -> overloaded; class MyXMLRPCServiceInterface final - : public RPCServiceInterface + : public RPC::ServiceInterface , public HTTPPage { private: - Anope::map events; + Anope::map events; static void SendError(HTTPReply &reply, xmlrpc_env &env) { @@ -42,14 +42,14 @@ private: xmlrpc_env_clean(&env); } - static void SerializeObject(xmlrpc_env &env, xmlrpc_value *value, const RPCBlock &block) + static void SerializeObject(xmlrpc_env &env, xmlrpc_value *value, const RPC::Block &block) { for (const auto &[k, v] : block.GetReplies()) { xmlrpc_value *elem; std::visit(overloaded { - [&env, &elem](const RPCBlock &b) + [&env, &elem](const RPC::Block &b) { elem = xmlrpc_struct_new(&env); SerializeObject(env, elem, b); @@ -97,17 +97,17 @@ private: public: MyXMLRPCServiceInterface(Module *creator, const Anope::string &sname) - : RPCServiceInterface(creator, sname) + : RPC::ServiceInterface(creator, sname) , HTTPPage("/xmlrpc", "text/xml") { } - bool Register(RPCEvent *event) override + bool Register(RPC::Event *event) override { return this->events.emplace(event->GetEvent(), event).second; } - bool Unregister(RPCEvent *event) override + bool Unregister(RPC::Event *event) override { return this->events.erase(event->GetEvent()) != 0; } @@ -127,7 +127,7 @@ public: return true; } - RPCRequest request(reply); + RPC::Request request(reply); request.name = method; delete method; @@ -181,7 +181,7 @@ public: return true; } - void Reply(RPCRequest &request) override + void Reply(RPC::Request &request) override { xmlrpc_env env; xmlrpc_env_init(&env); diff --git a/modules/rpc/jsonrpc.cpp b/modules/rpc/jsonrpc.cpp index ab7617c4d..1b1c35a4d 100644 --- a/modules/rpc/jsonrpc.cpp +++ b/modules/rpc/jsonrpc.cpp @@ -22,11 +22,11 @@ inline Anope::string yyjson_get_astr(yyjson_val *val, const char *key) } class MyJSONRPCServiceInterface final - : public RPCServiceInterface + : public RPC::ServiceInterface , public HTTPPage { private: - Anope::map events; + Anope::map events; static void SendError(HTTPReply &reply, int64_t code, const Anope::string &message, const Anope::string &id) { @@ -59,7 +59,7 @@ private: yyjson_mut_doc_free(doc); } - static void SerializeObject(yyjson_mut_doc *doc, yyjson_mut_val *root, const char *key, const RPCBlock &block) + static void SerializeObject(yyjson_mut_doc *doc, yyjson_mut_val *root, const char *key, const RPC::Block &block) { auto *result = yyjson_mut_obj(doc); for (const auto &reply : block.GetReplies()) @@ -68,7 +68,7 @@ private: const auto &k = reply.first; std::visit(overloaded { - [&doc, &result, &k](const RPCBlock &b) + [&doc, &result, &k](const RPC::Block &b) { SerializeObject(doc, result, k.c_str(), b); }, @@ -103,17 +103,17 @@ private: public: MyJSONRPCServiceInterface(Module *creator, const Anope::string &sname) - : RPCServiceInterface(creator, sname) + : RPC::ServiceInterface(creator, sname) , HTTPPage("/jsonrpc", "application/json") { } - bool Register(RPCEvent *event) override + bool Register(RPC::Event *event) override { return this->events.emplace(event->GetEvent(), event).second; } - bool Unregister(RPCEvent *event) override + bool Unregister(RPC::Event *event) override { return this->events.erase(event->GetEvent()) != 0; } @@ -145,7 +145,7 @@ public: return true; } - RPCRequest request(reply); + RPC::Request request(reply); request.id = id; request.name = yyjson_get_astr(root, "method"); @@ -174,7 +174,7 @@ public: return true; } - void Reply(RPCRequest &request) override + void Reply(RPC::Request &request) override { if (request.GetError()) { diff --git a/modules/rpc/rpc_main.cpp b/modules/rpc/rpc_main.cpp index 012010fcc..8b6a1797e 100644 --- a/modules/rpc/rpc_main.cpp +++ b/modules/rpc/rpc_main.cpp @@ -14,14 +14,14 @@ static Module *me; class RPCIdentifyRequest final : public IdentifyRequest { - RPCRequest request; + RPC::Request request; HTTPReply repl; /* Request holds a reference to the HTTPReply, because we might exist long enough to invalidate it we'll copy it here then reset the reference before we use it */ Reference client; - Reference xinterface; + Reference xinterface; public: - RPCIdentifyRequest(Module *m, RPCRequest &req, HTTPClient *c, RPCServiceInterface *iface, const Anope::string &acc, const Anope::string &pass) + RPCIdentifyRequest(Module *m, RPC::Request &req, HTTPClient *c, RPC::ServiceInterface *iface, const Anope::string &acc, const Anope::string &pass) : IdentifyRequest(m, acc, pass) , request(req) , repl(request.reply) @@ -58,15 +58,15 @@ public: }; class CommandRPCEvent final - : public RPCEvent + : public RPC::Event { public: CommandRPCEvent() - : RPCEvent("command") + : RPC::Event("command") { } - bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override + bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { Anope::string service = request.data.size() > 0 ? request.data[0] : ""; Anope::string user = request.data.size() > 1 ? request.data[1] : ""; @@ -115,15 +115,15 @@ public: }; class CheckAuthenticationRPCEvent final - : public RPCEvent + : public RPC::Event { public: CheckAuthenticationRPCEvent() - : RPCEvent("checkAuthentication") + : RPC::Event("checkAuthentication") { } - bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override + bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { Anope::string username = request.data.size() > 0 ? request.data[0] : ""; Anope::string password = request.data.size() > 1 ? request.data[1] : ""; @@ -142,15 +142,15 @@ public: }; class StatsRPCEvent final - : public RPCEvent + : public RPC::Event { public: StatsRPCEvent() - : RPCEvent("stats") + : RPC::Event("stats") { } - bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override + bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { request.ReplyInt("uptime", Anope::CurTime - Anope::StartTime); request.Reply("uplinkname", Me->GetLinks().front()->GetName()); @@ -170,15 +170,15 @@ public: }; class ChannelRPCEvent final - : public RPCEvent + : public RPC::Event { public: ChannelRPCEvent() - : RPCEvent("channel") + : RPC::Event("channel") { } - bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override + bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { if (request.data.empty()) { @@ -236,15 +236,15 @@ public: }; class UserRPCEvent final - : public RPCEvent + : public RPC::Event { public: UserRPCEvent() - : RPCEvent("user") + : RPC::Event("user") { } - bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override + bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { if (request.data.empty()) { @@ -292,15 +292,15 @@ public: }; class OpersRPCEvent final - : public RPCEvent + : public RPC::Event { public: OpersRPCEvent() - : RPCEvent("opers") + : RPC::Event("opers") { } - bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override + bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { for (auto *ot : Config->MyOperTypes) { @@ -316,15 +316,15 @@ public: }; class NoticeRPCEvent final - : public RPCEvent + : public RPC::Event { public: NoticeRPCEvent() - : RPCEvent("notice") + : RPC::Event("notice") { } - bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override + bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override { Anope::string from = request.data.size() > 0 ? request.data[0] : ""; Anope::string to = request.data.size() > 1 ? request.data[1] : ""; @@ -348,7 +348,7 @@ class ModuleRPCMain final : public Module { private: - ServiceReference rpc; + ServiceReference rpc; CommandRPCEvent commandrpcevent; CheckAuthenticationRPCEvent checkauthenticationrpcevent; StatsRPCEvent statsrpcevent;