mirror of
https://github.com/anope/anope.git
synced 2026-06-12 19:14:47 +02:00
Move RPC types to the RPC namespace.
This commit is contained in:
+34
-28
@@ -14,6 +14,14 @@
|
||||
|
||||
namespace RPC
|
||||
{
|
||||
class Block;
|
||||
class Event;
|
||||
class Request;
|
||||
class ServiceInterface;
|
||||
|
||||
/** Represents possible types of RPC value. */
|
||||
using Value = std::variant<Block, Anope::string, std::nullptr_t, bool, double, int64_t, uint64_t>;
|
||||
|
||||
/** 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<RPCBlock, Anope::string, std::nullptr_t, bool, double, int64_t, uint64_t>;
|
||||
|
||||
/** Retrieves the list of RPC replies. */
|
||||
inline const auto &GetReplies() const { return this->replies; }
|
||||
|
||||
template <typename Stringable>
|
||||
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<RPCBlock>(it.first->second);
|
||||
auto it = this->replies.emplace(key, Block());
|
||||
return std::get<Block>(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<RPCValue> replies;
|
||||
Anope::map<Value> replies;
|
||||
};
|
||||
|
||||
class RPCRequest final
|
||||
: public RPCBlock
|
||||
class RPC::Request final
|
||||
: public RPC::Block
|
||||
{
|
||||
private:
|
||||
std::optional<std::pair<int64_t, Anope::string>> error;
|
||||
@@ -93,7 +98,7 @@ public:
|
||||
std::deque<Anope::string> 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;
|
||||
};
|
||||
|
||||
@@ -19,11 +19,11 @@ template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
||||
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
||||
|
||||
class MyXMLRPCServiceInterface final
|
||||
: public RPCServiceInterface
|
||||
: public RPC::ServiceInterface
|
||||
, public HTTPPage
|
||||
{
|
||||
private:
|
||||
Anope::map<RPCEvent *> events;
|
||||
Anope::map<RPC::Event *> 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);
|
||||
|
||||
@@ -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<RPCEvent *> events;
|
||||
Anope::map<RPC::Event *> 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())
|
||||
{
|
||||
|
||||
+25
-25
@@ -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<HTTPClient> client;
|
||||
Reference<RPCServiceInterface> xinterface;
|
||||
Reference<RPC::ServiceInterface> 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<RPCServiceInterface> rpc;
|
||||
ServiceReference<RPC::ServiceInterface> rpc;
|
||||
CommandRPCEvent commandrpcevent;
|
||||
CheckAuthenticationRPCEvent checkauthenticationrpcevent;
|
||||
StatsRPCEvent statsrpcevent;
|
||||
|
||||
Reference in New Issue
Block a user