1
0
mirror of https://github.com/anope/anope.git synced 2026-07-01 08:16:39 +02:00

Add support for more RPC data types.

This commit is contained in:
Sadie Powell
2025-02-20 12:33:59 +00:00
parent 8330cd119a
commit b498f4f4d9
4 changed files with 182 additions and 36 deletions
+60 -9
View File
@@ -10,11 +10,69 @@
#include "httpd.h"
#include <variant>
class RPCBlock
{
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)
{
this->replies.emplace(key, Anope::ToString(value));
return *this;
}
inline RPCBlock &ReplyBlock(const Anope::string &key)
{
auto it = this->replies.emplace(key, RPCBlock());
return std::get<RPCBlock>(it.first->second);
}
inline RPCBlock &ReplyBool(const Anope::string &key, bool value)
{
this->replies.emplace(key, value);
return *this;
}
inline RPCBlock &ReplyFloat(const Anope::string &key, double value)
{
this->replies.emplace(key, value);
return *this;
}
inline RPCBlock &ReplyInt(const Anope::string &key, int64_t value)
{
this->replies.emplace(key, value);
return *this;
}
inline RPCBlock &ReplyNull(const Anope::string &key)
{
this->replies.emplace(key, nullptr);
return *this;
}
inline RPCBlock &ReplyUInt(const Anope::string &key, uint64_t value)
{
this->replies.emplace(key, value);
return *this;
}
private:
Anope::map<RPCValue> replies;
};
class RPCRequest final
: public RPCBlock
{
private:
std::optional<std::pair<int64_t, Anope::string>> error;
std::map<Anope::string, Anope::string> replies;
public:
Anope::string name;
@@ -32,14 +90,7 @@ public:
this->error.emplace(errcode, errstr);
}
inline void Reply(const Anope::string &dname, const Anope::string &ddata)
{
this->replies.emplace(dname, ddata);
}
inline const auto &GetError() { return this->error; }
inline const auto &GetReplies() { return this->replies; }
inline const auto &GetError() const { return this->error; }
};
class RPCServiceInterface;
+58 -6
View File
@@ -15,6 +15,9 @@
#include "modules/rpc.h"
#include "modules/httpd.h"
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
class MyXMLRPCServiceInterface final
: public RPCServiceInterface
, public HTTPPage
@@ -22,7 +25,7 @@ class MyXMLRPCServiceInterface final
private:
Anope::map<RPCEvent *> 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);
+47 -7
View File
@@ -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");
+17 -14
View File
@@ -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);