mirror of
https://github.com/anope/anope.git
synced 2026-06-12 19:14:47 +02:00
Deduplicate RPC parameter count checks.
This commit is contained in:
@@ -173,10 +173,12 @@ class RPC::Event
|
||||
{
|
||||
private:
|
||||
Anope::string event;
|
||||
size_t minparams;
|
||||
|
||||
protected:
|
||||
Event(const Anope::string& e)
|
||||
Event(const Anope::string& e, size_t mp = 0)
|
||||
: event(e)
|
||||
, minparams(mp)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -185,6 +187,8 @@ public:
|
||||
|
||||
const auto &GetEvent() const { return event; }
|
||||
|
||||
const auto &GetMinParams() const { return minparams; }
|
||||
|
||||
virtual bool Run(ServiceInterface *iface, HTTPClient *client, Request &request) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -145,7 +145,17 @@ public:
|
||||
}
|
||||
xmlrpc_DECREF(params);
|
||||
|
||||
if (!event->second->Run(this, client, request))
|
||||
auto *eh = event->second;
|
||||
if (request.data.size() < eh->GetMinParams())
|
||||
{
|
||||
auto error = Anope::printf("Not enough parameters (given %zu, expected %zu)",
|
||||
request.data.size(), eh->GetMinParams());
|
||||
xmlrpc_env_set_fault(&env, RPC::ERR_INVALID_PARAMS, error.c_str());
|
||||
SendError(reply, env);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!eh->Run(this, client, request))
|
||||
return false;
|
||||
|
||||
this->Reply(request);
|
||||
|
||||
+10
-1
@@ -150,7 +150,16 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!event->second->Run(this, client, request))
|
||||
auto *eh = event->second;
|
||||
if (request.data.size() < eh->GetMinParams())
|
||||
{
|
||||
auto error = Anope::printf("Not enough parameters (given %zu, expected %zu)",
|
||||
request.data.size(), eh->GetMinParams());
|
||||
SendError(reply, RPC::ERR_INVALID_PARAMS, error, id);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!eh->Run(this, client, request))
|
||||
return false;
|
||||
|
||||
this->Reply(request);
|
||||
|
||||
@@ -38,18 +38,12 @@ class AnopeChannelRPCEvent final
|
||||
{
|
||||
public:
|
||||
AnopeChannelRPCEvent()
|
||||
: RPC::Event("anope.channel")
|
||||
: RPC::Event("anope.channel", 1)
|
||||
{
|
||||
}
|
||||
|
||||
bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override
|
||||
{
|
||||
if (request.data.empty())
|
||||
{
|
||||
request.Error(RPC::ERR_INVALID_PARAMS, "Not enough parameters");
|
||||
return true;
|
||||
}
|
||||
|
||||
auto *c = Channel::Find(request.data[0]);
|
||||
if (!c)
|
||||
{
|
||||
@@ -137,18 +131,12 @@ class AnopeOperRPCEvent final
|
||||
{
|
||||
public:
|
||||
AnopeOperRPCEvent()
|
||||
: RPC::Event("anope.oper")
|
||||
: RPC::Event("anope.oper", 1)
|
||||
{
|
||||
}
|
||||
|
||||
bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override
|
||||
{
|
||||
if (request.data.empty())
|
||||
{
|
||||
request.Error(RPC::ERR_INVALID_PARAMS, "Not enough parameters");
|
||||
return true;
|
||||
}
|
||||
|
||||
auto *o = Oper::Find(request.data[0]);
|
||||
if (!o)
|
||||
{
|
||||
@@ -230,12 +218,6 @@ public:
|
||||
|
||||
bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override
|
||||
{
|
||||
if (request.data.empty())
|
||||
{
|
||||
request.Error(RPC::ERR_INVALID_PARAMS, "Not enough parameters");
|
||||
return true;
|
||||
}
|
||||
|
||||
auto *s = Server::Find(request.data[0]);
|
||||
if (!s)
|
||||
{
|
||||
@@ -291,18 +273,12 @@ class AnopeUserRPCEvent final
|
||||
{
|
||||
public:
|
||||
AnopeUserRPCEvent()
|
||||
: RPC::Event("anope.user")
|
||||
: RPC::Event("anope.user", 1)
|
||||
{
|
||||
}
|
||||
|
||||
bool Run(RPC::ServiceInterface *iface, HTTPClient *client, RPC::Request &request) override
|
||||
{
|
||||
if (request.data.empty())
|
||||
{
|
||||
request.Error(RPC::ERR_INVALID_PARAMS, "Not enough parameters");
|
||||
return true;
|
||||
}
|
||||
|
||||
auto *u = User::Find(request.data[0]);
|
||||
if (!u)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user