1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/*
*
* (C) 2010-2025 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*/
#pragma once
#include "httpd.h"
#include <variant>
namespace RPC
{
class Array;
class Event;
class Map;
class Request;
class ServiceInterface;
class Value;
/** Represents possible types of RPC value. */
using ValueUnion = std::variant<Array, Map, 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
{
ERR_CUSTOM_END = -32000,
ERR_CUSTOM_START = -32099,
ERR_PARSE_ERROR = -32700,
ERR_INVALID_REQUEST = -32600,
ERR_METHOD_NOT_FOUND = -32601,
ERR_INVALID_PARAMS = -32602,
};
}
class RPC::Array final
{
private:
std::vector<Value> replies;
public:
/** Retrieves the list of RPC replies. */
inline const auto &GetReplies() const { return this->replies; }
template <typename T>
inline Array &Reply(const T &t)
{
this->replies.emplace_back(RPC::Value(t));
return *this;
}
inline Array &ReplyArray();
inline Map &ReplyMap();
};
class RPC::Map final
{
private:
Anope::map<Value> replies;
public:
/** Retrieves the list of RPC replies. */
inline const auto &GetReplies() const { return this->replies; }
template <typename T>
inline Map &Reply(const Anope::string &key, const T &t)
{
this->replies.emplace(key, RPC::Value(t));
return *this;
}
inline Array &ReplyArray(const Anope::string &key);
inline Map &ReplyMap(const Anope::string &key);
};
class RPC::Value final
{
private:
RPC::ValueUnion value;
public:
explicit Value(const ValueUnion &v)
: value(v)
{
}
explicit Value(const Array &a)
: value(a)
{
}
explicit Value(const Map &m)
: value(m)
{
}
explicit Value(std::nullptr_t)
: value(nullptr)
{
}
explicit Value(bool b)
: value(b)
{
}
Value(double d)
: value(d)
{
}
Value(int64_t i)
: value(i)
{
}
Value(uint64_t u)
: value(u)
{
}
template <typename T>
Value(const T &t)
: value(Anope::ToString(t))
{
}
inline auto &Get() { return this->value; }
inline const auto &Get() const { return this->value; }
};
class RPC::Request final
{
private:
std::optional<std::pair<int64_t, Anope::string>> error;
std::optional<Value> root;
public:
Anope::string name;
Anope::string id;
std::deque<Anope::string> data;
HTTPReply &reply;
Request(HTTPReply &r)
: reply(r)
{
}
inline void Error(uint64_t errcode, const Anope::string &errstr)
{
this->error.emplace(errcode, errstr);
}
template<typename T = Map>
inline T &Root();
inline const auto &GetError() const { return this->error; }
inline const auto &GetRoot() const { return this->root; }
};
#define RPC_EVENT "RPC::Event"
class RPC::Event
: public Service
{
private:
size_t minparams;
protected:
Event(Module *o, const Anope::string& e, size_t mp = 0)
: Service(o, RPC_EVENT, e)
, minparams(mp)
{
}
public:
virtual ~Event() = default;
const auto &GetMinParams() const { return minparams; }
virtual bool Run(ServiceInterface *iface, HTTPClient *client, Request &request) = 0;
};
class RPC::ServiceInterface
: public Service
{
public:
ServiceInterface(Module *creator)
: Service(creator, "RPC::ServiceInterface", "rpc")
{
}
virtual void Reply(Request &request) = 0;
};
inline RPC::Array &RPC::Array::ReplyArray()
{
auto &reply = this->replies.emplace_back(RPC::Array());
return std::get<RPC::Array>(reply.Get());
}
inline RPC::Map &RPC::Array::ReplyMap()
{
auto &reply = this->replies.emplace_back(RPC::Map());
return std::get<RPC::Map>(reply.Get());
}
inline RPC::Array &RPC::Map::ReplyArray(const Anope::string &key)
{
auto it = this->replies.emplace(key, RPC::Array());
return std::get<RPC::Array>(it.first->second.Get());
}
inline RPC::Map &RPC::Map::ReplyMap(const Anope::string &key)
{
auto it = this->replies.emplace(key, RPC::Map());
return std::get<RPC::Map>(it.first->second.Get());
}
template<typename T>
inline T &RPC::Request::Root()
{
if (!this->root.has_value())
this->root = RPC::Value(T());
return std::get<T>(this->root.value().Get());
}
|