From f8c1b8f4f9bcb06242aa7d7360ea610764565c75 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Thu, 13 Feb 2025 01:28:28 +0000 Subject: Rework the RPC modules in preparation for the new JSON-RPC module. --- data/modules.example.conf | 8 +- docs/RPC/XMLRPC | 29 +++++ docs/RPC/xmlrpc.php | 145 ++++++++++++++++++++++ docs/XMLRPC/XMLRPC | 29 ----- docs/XMLRPC/xmlrpc.php | 145 ---------------------- include/modules/rpc.h | 50 ++++++++ include/modules/xmlrpc.h | 50 -------- modules/rpc/rpc_main.cpp | 306 ++++++++++++++++++++++++++++++++++++++++++++++ modules/rpc/xmlrpc.cpp | 229 ++++++++++++++++++++++++++++++++++ modules/xmlrpc.cpp | 229 ---------------------------------- modules/xmlrpc_main.cpp | 306 ---------------------------------------------- 11 files changed, 763 insertions(+), 763 deletions(-) create mode 100644 docs/RPC/XMLRPC create mode 100644 docs/RPC/xmlrpc.php delete mode 100644 docs/XMLRPC/XMLRPC delete mode 100644 docs/XMLRPC/xmlrpc.php create mode 100644 include/modules/rpc.h delete mode 100644 include/modules/xmlrpc.h create mode 100644 modules/rpc/rpc_main.cpp create mode 100644 modules/rpc/xmlrpc.cpp delete mode 100644 modules/xmlrpc.cpp delete mode 100644 modules/xmlrpc_main.cpp diff --git a/data/modules.example.conf b/data/modules.example.conf index 917f72c61..604f05c43 100644 --- a/data/modules.example.conf +++ b/data/modules.example.conf @@ -789,7 +789,7 @@ module { name = "sasl" } * xmlrpc * * Allows remote applications (websites) to execute queries in real time to retrieve data from Anope. - * By itself this module does nothing, but allows other modules (xmlrpc_main) to receive and send XMLRPC queries. + * By itself this module does nothing, but allows other modules (rpc_main) to receive and send XMLRPC queries. */ #module { @@ -800,9 +800,9 @@ module { name = "sasl" } } /* - * xmlrpc_main + * rpc_main * - * Adds the main XMLRPC core functions. + * Adds the main RPC core functions. * Requires xmlrpc. */ -#module { name = "xmlrpc_main" } +#module { name = "rpc_main" } diff --git a/docs/RPC/XMLRPC b/docs/RPC/XMLRPC new file mode 100644 index 000000000..ac3f1059b --- /dev/null +++ b/docs/RPC/XMLRPC @@ -0,0 +1,29 @@ +XMLRPC using PHP's xmlrpc_encode_request and xmlrpc_decode functions is supported. +This allows external applications, such as websites, to execute remote procedure calls to Anope in real time. + +Currently there are 5 supported XMLRPC calls, provided by rpc_main: + +checkAuthentication - Takes two parameters, an account name and a password. Checks if the account name is valid and the password + is correct for the account name, useful for making login pages on websites. + +command - Takes three parameters, a service name (BotServ, ChanServ, NickServ), a user name (whether online or not), and the command + to execute. This will execute the given command to Anope using the given service name. If the user given is online, the + command reply will go to them, if not it is returned by XMLRPC. + +stats - Takes no parameters, returns miscellaneous stats that can be found in the /operserv stats command. + +channel - Takes one parameter, a channel name, and returns real time information regarding that channel, such as users, modes + (ban lists and such), topic etc. + +user - Takes one parameter, a user name, and returns real time information regarding that user. + +opers - Takes no parameters, returns opertypes, their privileges and commands. + +notice - Takes three parameters, source user, target user, and message. Sends a message to the user. + +XMLRPC was designed to be used with db_sql, and will not return any information that can be pulled from the SQL +database, such as accounts and registered channel information. It is instead used for pulling realtime data such +as users and channels currently online. For examples on how to use these calls in PHP, see xmlrpc.php in docs/XMLRPC. + +Also note that the parameter named "id" is reserved for query ID. If you pass a query to Anope containing a value for id. it will +be stored by Anope and the same id will be passed back in the result. diff --git a/docs/RPC/xmlrpc.php b/docs/RPC/xmlrpc.php new file mode 100644 index 000000000..90f07a944 --- /dev/null +++ b/docs/RPC/xmlrpc.php @@ -0,0 +1,145 @@ +host = $host; + } + + /** + * Run an XMLRPC command. Name should be a query name and params an array of parameters, eg: + * $this->raw("checkAuthentication", ["adam", "qwerty"]); + * If successful returns back an array of useful information. + * + * Note that $params["id"] is reserved for query ID, you may set it to something if you wish. + * If you do, the same ID will be passed back with the reply from Anope. + * + * @param $name + * @param $params + * @return array|null + */ + public function run($name, $params) + { + $xmlquery = xmlrpc_encode_request($name, $params); + $context = stream_context_create(["http" => [ + "method" => "POST", + "header" => "Content-Type: text/xml", + "content" => $xmlquery]]); + + $inbuf = file_get_contents($this->host, false, $context); + $response = xmlrpc_decode($inbuf); + + if ($response) { + return $response; + } + + return null; + } + + /** + * Do Command on Service as User, eg: + * $anope->command("ChanServ", "Adam", "REGISTER #adam"); + * Returns an array of information regarding the command execution, if + * If 'online' is set to yes, then the reply to the command was sent to the user on IRC. + * If 'online' is set to no, then the reply to the command is in the array member 'return' + * + * @param $service + * @param $user + * @param $command + * @return array|null + */ + public function command($service, $user, $command) + { + return $this->run("command", [$service, $user, $command]); + } + + /** + * Check an account/nick name and password to see if they are valid + * Returns the account display name if valid + * + * @param $account + * @param $pass + * @return string|null + */ + public function auth($account, $pass) + { + $ret = $this->run("checkAuthentication", [$account, $pass]); + + if ($ret && $ret["result"] == "Success") { + return $ret["account"]; + } + + return null; + } + + /** + * Returns an array of misc stats regarding Anope + * + * @return array|null + */ + public function stats() + { + return $this->run("stats", null); + } + + /** + * Look up data for a channel + * Returns an array containing channel information, or an array of size one + * (just containing the name) if the channel does not exist + * + * @param $channel + * @return array|null + */ + public function channel($channel) + { + return $this->run("channel", [$channel]); + } + + /** + * Sent a notice to a user. + * Returns an array containing channel information, or an array of size one + * (just containing the name) if the channel does not exist + * + * @param $source + * @param $target + * @param $message + * @return array|null + */ + public function notice($source, $target, $message) + { + return $this->run("notice", [$source, $target, $message]); + } + + /** + * Like channel(), but different. + * + * @param $user + * @return array|null + */ + public function user($user) + { + return $this->run("user", [$user]); + } +} + +$anope = new AnopeXMLRPC("http://127.0.0.1:8080/xmlrpc"); diff --git a/docs/XMLRPC/XMLRPC b/docs/XMLRPC/XMLRPC deleted file mode 100644 index 72f81e4b8..000000000 --- a/docs/XMLRPC/XMLRPC +++ /dev/null @@ -1,29 +0,0 @@ -XMLRPC using PHP's xmlrpc_encode_request and xmlrpc_decode functions is supported. -This allows external applications, such as websites, to execute remote procedure calls to Anope in real time. - -Currently there are 5 supported XMLRPC calls, provided by xmlrpc_main: - -checkAuthentication - Takes two parameters, an account name and a password. Checks if the account name is valid and the password - is correct for the account name, useful for making login pages on websites. - -command - Takes three parameters, a service name (BotServ, ChanServ, NickServ), a user name (whether online or not), and the command - to execute. This will execute the given command to Anope using the given service name. If the user given is online, the - command reply will go to them, if not it is returned by XMLRPC. - -stats - Takes no parameters, returns miscellaneous stats that can be found in the /operserv stats command. - -channel - Takes one parameter, a channel name, and returns real time information regarding that channel, such as users, modes - (ban lists and such), topic etc. - -user - Takes one parameter, a user name, and returns real time information regarding that user. - -opers - Takes no parameters, returns opertypes, their privileges and commands. - -notice - Takes three parameters, source user, target user, and message. Sends a message to the user. - -XMLRPC was designed to be used with db_sql, and will not return any information that can be pulled from the SQL -database, such as accounts and registered channel information. It is instead used for pulling realtime data such -as users and channels currently online. For examples on how to use these calls in PHP, see xmlrpc.php in docs/XMLRPC. - -Also note that the parameter named "id" is reserved for query ID. If you pass a query to Anope containing a value for id. it will -be stored by Anope and the same id will be passed back in the result. diff --git a/docs/XMLRPC/xmlrpc.php b/docs/XMLRPC/xmlrpc.php deleted file mode 100644 index 90f07a944..000000000 --- a/docs/XMLRPC/xmlrpc.php +++ /dev/null @@ -1,145 +0,0 @@ -host = $host; - } - - /** - * Run an XMLRPC command. Name should be a query name and params an array of parameters, eg: - * $this->raw("checkAuthentication", ["adam", "qwerty"]); - * If successful returns back an array of useful information. - * - * Note that $params["id"] is reserved for query ID, you may set it to something if you wish. - * If you do, the same ID will be passed back with the reply from Anope. - * - * @param $name - * @param $params - * @return array|null - */ - public function run($name, $params) - { - $xmlquery = xmlrpc_encode_request($name, $params); - $context = stream_context_create(["http" => [ - "method" => "POST", - "header" => "Content-Type: text/xml", - "content" => $xmlquery]]); - - $inbuf = file_get_contents($this->host, false, $context); - $response = xmlrpc_decode($inbuf); - - if ($response) { - return $response; - } - - return null; - } - - /** - * Do Command on Service as User, eg: - * $anope->command("ChanServ", "Adam", "REGISTER #adam"); - * Returns an array of information regarding the command execution, if - * If 'online' is set to yes, then the reply to the command was sent to the user on IRC. - * If 'online' is set to no, then the reply to the command is in the array member 'return' - * - * @param $service - * @param $user - * @param $command - * @return array|null - */ - public function command($service, $user, $command) - { - return $this->run("command", [$service, $user, $command]); - } - - /** - * Check an account/nick name and password to see if they are valid - * Returns the account display name if valid - * - * @param $account - * @param $pass - * @return string|null - */ - public function auth($account, $pass) - { - $ret = $this->run("checkAuthentication", [$account, $pass]); - - if ($ret && $ret["result"] == "Success") { - return $ret["account"]; - } - - return null; - } - - /** - * Returns an array of misc stats regarding Anope - * - * @return array|null - */ - public function stats() - { - return $this->run("stats", null); - } - - /** - * Look up data for a channel - * Returns an array containing channel information, or an array of size one - * (just containing the name) if the channel does not exist - * - * @param $channel - * @return array|null - */ - public function channel($channel) - { - return $this->run("channel", [$channel]); - } - - /** - * Sent a notice to a user. - * Returns an array containing channel information, or an array of size one - * (just containing the name) if the channel does not exist - * - * @param $source - * @param $target - * @param $message - * @return array|null - */ - public function notice($source, $target, $message) - { - return $this->run("notice", [$source, $target, $message]); - } - - /** - * Like channel(), but different. - * - * @param $user - * @return array|null - */ - public function user($user) - { - return $this->run("user", [$user]); - } -} - -$anope = new AnopeXMLRPC("http://127.0.0.1:8080/xmlrpc"); diff --git a/include/modules/rpc.h b/include/modules/rpc.h new file mode 100644 index 000000000..b3c8afdb4 --- /dev/null +++ b/include/modules/rpc.h @@ -0,0 +1,50 @@ +/* + * + * (C) 2010-2025 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + */ + +#pragma once + +#include "httpd.h" + +class RPCRequest final +{ + std::map replies; + +public: + Anope::string name; + Anope::string id; + std::deque data; + HTTPReply &r; + + RPCRequest(HTTPReply &_r) : r(_r) { } + inline void reply(const Anope::string &dname, const Anope::string &ddata) { this->replies.emplace(dname, ddata); } + inline const std::map &get_replies() { return this->replies; } +}; + +class RPCServiceInterface; + +class RPCEvent +{ +public: + virtual ~RPCEvent() = default; + virtual bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) = 0; +}; + +class RPCServiceInterface + : public Service +{ +public: + RPCServiceInterface(Module *creator, const Anope::string &sname) : Service(creator, "RPCServiceInterface", sname) { } + + virtual void Register(RPCEvent *event) = 0; + + virtual void Unregister(RPCEvent *event) = 0; + + virtual Anope::string Sanitize(const Anope::string &string) = 0; + + virtual void Reply(RPCRequest &request) = 0; +}; diff --git a/include/modules/xmlrpc.h b/include/modules/xmlrpc.h deleted file mode 100644 index 833792888..000000000 --- a/include/modules/xmlrpc.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * - * (C) 2010-2025 Anope Team - * Contact us at team@anope.org - * - * Please read COPYING and README for further details. - */ - -#pragma once - -#include "httpd.h" - -class XMLRPCRequest final -{ - std::map replies; - -public: - Anope::string name; - Anope::string id; - std::deque data; - HTTPReply &r; - - XMLRPCRequest(HTTPReply &_r) : r(_r) { } - inline void reply(const Anope::string &dname, const Anope::string &ddata) { this->replies.emplace(dname, ddata); } - inline const std::map &get_replies() { return this->replies; } -}; - -class XMLRPCServiceInterface; - -class XMLRPCEvent -{ -public: - virtual ~XMLRPCEvent() = default; - virtual bool Run(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) = 0; -}; - -class XMLRPCServiceInterface - : public Service -{ -public: - XMLRPCServiceInterface(Module *creator, const Anope::string &sname) : Service(creator, "XMLRPCServiceInterface", sname) { } - - virtual void Register(XMLRPCEvent *event) = 0; - - virtual void Unregister(XMLRPCEvent *event) = 0; - - virtual Anope::string Sanitize(const Anope::string &string) = 0; - - virtual void Reply(XMLRPCRequest &request) = 0; -}; diff --git a/modules/rpc/rpc_main.cpp b/modules/rpc/rpc_main.cpp new file mode 100644 index 000000000..4a87d9e05 --- /dev/null +++ b/modules/rpc/rpc_main.cpp @@ -0,0 +1,306 @@ +/* + * + * (C) 2010-2025 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + */ + +#include "module.h" +#include "modules/rpc.h" + +static Module *me; + +class RPCIdentifyRequest final + : public IdentifyRequest +{ + RPCRequest 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; + +public: + RPCIdentifyRequest(Module *m, RPCRequest &req, HTTPClient *c, RPCServiceInterface *iface, const Anope::string &acc, const Anope::string &pass) : IdentifyRequest(m, acc, pass), request(req), repl(request.r), client(c), xinterface(iface) { } + + void OnSuccess() override + { + if (!xinterface || !client) + return; + + request.r = this->repl; + + request.reply("result", "Success"); + request.reply("account", GetAccount()); + + xinterface->Reply(request); + client->SendReply(&request.r); + } + + void OnFail() override + { + if (!xinterface || !client) + return; + + request.r = this->repl; + + request.reply("error", "Invalid password"); + + xinterface->Reply(request); + client->SendReply(&request.r); + } +}; + +class MyRPCEvent final + : public RPCEvent +{ +public: + bool Run(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) override + { + if (request.name == "command") + this->DoCommand(iface, client, request); + else if (request.name == "checkAuthentication") + return this->DoCheckAuthentication(iface, client, request); + else if (request.name == "stats") + this->DoStats(iface, client, request); + else if (request.name == "channel") + this->DoChannel(iface, client, request); + else if (request.name == "user") + this->DoUser(iface, client, request); + else if (request.name == "opers") + this->DoOperType(iface, client, request); + else if (request.name == "notice") + this->DoNotice(iface, client, request); + + return true; + } + +private: + void DoCommand(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) + { + Anope::string service = request.data.size() > 0 ? request.data[0] : ""; + Anope::string user = request.data.size() > 1 ? request.data[1] : ""; + Anope::string command = request.data.size() > 2 ? request.data[2] : ""; + + if (service.empty() || user.empty() || command.empty()) + request.reply("error", "Invalid parameters"); + else + { + BotInfo *bi = BotInfo::Find(service, true); + if (!bi) + request.reply("error", "Invalid service"); + else + { + request.reply("result", "Success"); + + NickAlias *na = NickAlias::Find(user); + + Anope::string out; + + struct RPCommandReply final + : CommandReply + { + Anope::string &str; + + RPCommandReply(Anope::string &s) : str(s) { } + + void SendMessage(BotInfo *source, const Anope::string &msg) override + { + str += msg + "\n"; + }; + } + reply(out); + + User *u = User::Find(user, true); + CommandSource source(user, u, na ? *na->nc : NULL, &reply, bi); + Command::Run(source, command); + + if (!out.empty()) + request.reply("return", iface->Sanitize(out)); + } + } + } + + static bool DoCheckAuthentication(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) + { + Anope::string username = request.data.size() > 0 ? request.data[0] : ""; + Anope::string password = request.data.size() > 1 ? request.data[1] : ""; + + if (username.empty() || password.empty()) + request.reply("error", "Invalid parameters"); + else + { + auto *req = new RPCIdentifyRequest(me, request, client, iface, username, password); + FOREACH_MOD(OnCheckAuthentication, (NULL, req)); + req->Dispatch(); + return false; + } + + return true; + } + + static void DoStats(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) + { + request.reply("uptime", Anope::ToString(Anope::CurTime - Anope::StartTime)); + request.reply("uplinkname", Me->GetLinks().front()->GetName()); + { + Anope::string buf; + for (const auto &capab : Servers::Capab) + buf += " " + capab; + if (!buf.empty()) + 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())); + } + + static void DoChannel(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) + { + if (request.data.empty()) + return; + + Channel *c = Channel::Find(request.data[0]); + + request.reply("name", iface->Sanitize(c ? c->name : request.data[0])); + + if (c) + { + request.reply("bancount", Anope::ToString(c->HasMode("BAN"))); + int count = 0; + for (auto &ban : c->GetModeList("BAN")) + request.reply("ban" + Anope::ToString(++count), iface->Sanitize(ban)); + + request.reply("exceptcount", Anope::ToString(c->HasMode("EXCEPT"))); + count = 0; + for (auto &except : c->GetModeList("EXCEPT")) + request.reply("except" + Anope::ToString(++count), iface->Sanitize(except)); + + request.reply("invitecount", Anope::ToString(c->HasMode("INVITEOVERRIDE"))); + count = 0; + for (auto &invite : c->GetModeList("INVITEOVERRIDE")) + request.reply("invite" + Anope::ToString(++count), iface->Sanitize(invite)); + + Anope::string users; + for (Channel::ChanUserList::const_iterator it = c->users.begin(); it != c->users.end(); ++it) + { + ChanUserContainer *uc = it->second; + users += uc->status.BuildModePrefixList() + uc->user->nick + " "; + } + if (!users.empty()) + { + users.erase(users.length() - 1); + request.reply("users", iface->Sanitize(users)); + } + + if (!c->topic.empty()) + request.reply("topic", iface->Sanitize(c->topic)); + + if (!c->topic_setter.empty()) + request.reply("topicsetter", iface->Sanitize(c->topic_setter)); + + request.reply("topictime", Anope::ToString(c->topic_time)); + request.reply("topicts", Anope::ToString(c->topic_ts)); + } + } + + static void DoUser(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) + { + if (request.data.empty()) + return; + + User *u = User::Find(request.data[0]); + + request.reply("nick", iface->Sanitize(u ? u->nick : request.data[0])); + + if (u) + { + request.reply("ident", iface->Sanitize(u->GetIdent())); + request.reply("vident", iface->Sanitize(u->GetVIdent())); + request.reply("host", iface->Sanitize(u->host)); + if (!u->vhost.empty()) + request.reply("vhost", iface->Sanitize(u->vhost)); + if (!u->chost.empty()) + request.reply("chost", iface->Sanitize(u->chost)); + request.reply("ip", u->ip.addr()); + request.reply("timestamp", Anope::ToString(u->timestamp)); + request.reply("signon", Anope::ToString(u->signon)); + if (u->IsIdentified()) + { + request.reply("account", iface->Sanitize(u->Account()->display)); + if (u->Account()->o) + request.reply("opertype", iface->Sanitize(u->Account()->o->ot->GetName())); + } + + Anope::string channels; + for (User::ChanUserList::const_iterator it = u->chans.begin(); it != u->chans.end(); ++it) + { + ChanUserContainer *cc = it->second; + channels += cc->status.BuildModePrefixList() + cc->chan->name + " "; + } + if (!channels.empty()) + { + channels.erase(channels.length() - 1); + request.reply("channels", channels); + } + } + } + + static void DoOperType(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) + { + for (auto *ot : Config->MyOperTypes) + { + Anope::string perms; + for (const auto &priv : ot->GetPrivs()) + perms += " " + priv; + for (const auto &command : ot->GetCommands()) + perms += " " + command; + request.reply(ot->GetName(), perms); + } + } + + static void DoNotice(RPCServiceInterface *iface, HTTPClient *client, RPCRequest &request) + { + Anope::string from = request.data.size() > 0 ? request.data[0] : ""; + Anope::string to = request.data.size() > 1 ? request.data[1] : ""; + Anope::string message = request.data.size() > 2 ? request.data[2] : ""; + + BotInfo *bi = BotInfo::Find(from, true); + User *u = User::Find(to, true); + + if (!bi || !u || message.empty()) + return; + + u->SendMessage(bi, message); + + request.reply("result", "Success"); + } +}; + +class ModuleRPCMain final + : public Module +{ + ServiceReference rpc; + + MyRPCEvent stats; + +public: + ModuleRPCMain(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR), rpc("RPCServiceInterface", "rpc") + { + me = this; + + if (!rpc) + throw ModuleException("Unable to find RPC interface, is xmlrpc loaded?"); + + rpc->Register(&stats); + } + + ~ModuleRPCMain() override + { + if (rpc) + rpc->Unregister(&stats); + } +}; + +MODULE_INIT(ModuleRPCMain) diff --git a/modules/rpc/xmlrpc.cpp b/modules/rpc/xmlrpc.cpp new file mode 100644 index 000000000..f1db6efba --- /dev/null +++ b/modules/rpc/xmlrpc.cpp @@ -0,0 +1,229 @@ +/* + * + * (C) 2010-2025 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + */ + +#include "module.h" +#include "modules/rpc.h" +#include "modules/httpd.h" + +static struct special_chars final +{ + Anope::string character; + Anope::string replace; + + special_chars(const Anope::string &c, const Anope::string &r) : character(c), replace(r) { } +} +special[] = { + special_chars("&", "&"), + special_chars("\"", """), + special_chars("<", "<"), + special_chars(">", "&qt;"), + special_chars("'", "'"), + special_chars("\n", " "), + special_chars("\002", ""), // bold + special_chars("\003", ""), // color + special_chars("\035", ""), // italics + special_chars("\037", ""), // underline + special_chars("\026", ""), // reverses + special_chars("", "") +}; + +class MyXMLRPCServiceInterface final + : public RPCServiceInterface + , public HTTPPage +{ + std::deque events; + +public: + MyXMLRPCServiceInterface(Module *creator, const Anope::string &sname) : RPCServiceInterface(creator, sname), HTTPPage("/xmlrpc", "text/xml") { } + + void Register(RPCEvent *event) override + { + this->events.push_back(event); + } + + void Unregister(RPCEvent *event) override + { + std::deque::iterator it = std::find(this->events.begin(), this->events.end(), event); + + if (it != this->events.end()) + this->events.erase(it); + } + + Anope::string Sanitize(const Anope::string &string) override + { + Anope::string ret = string; + for (int i = 0; !special[i].character.empty(); ++i) + ret = ret.replace_all_cs(special[i].character, special[i].replace); + return ret; + } + + static Anope::string Unescape(const Anope::string &string) + { + Anope::string ret = string; + for (int i = 0; !special[i].character.empty(); ++i) + if (!special[i].replace.empty()) + ret = ret.replace_all_cs(special[i].replace, special[i].character); + + for (size_t i, last = 0; (i = string.find("&#", last)) != Anope::string::npos;) + { + last = i + 1; + + size_t end = string.find(';', i); + if (end == Anope::string::npos) + break; + + Anope::string ch = string.substr(i + 2, end - (i + 2)); + + if (ch.empty()) + continue; + + long l; + if (!ch.empty() && ch[0] == 'x') + l = strtol(ch.substr(1).c_str(), NULL, 16); + else + l = strtol(ch.c_str(), NULL, 10); + + if (l > 0 && l < 256) + ret = ret.replace_all_cs("&#" + ch + ";", Anope::string(l)); + } + + return ret; + } + +private: + static bool GetData(Anope::string &content, Anope::string &tag, Anope::string &data) + { + if (content.empty()) + return false; + + Anope::string prev, cur; + bool istag; + + do + { + prev = cur; + cur.clear(); + + size_t len = 0; + istag = false; + + if (content[0] == '<') + { + len = content.find_first_of('>'); + istag = true; + } + else if (content[0] != '>') + { + len = content.find_first_of('<'); + } + + // len must advance + if (len == Anope::string::npos || len == 0) + break; + + if (istag) + { + cur = content.substr(1, len - 1); + content.erase(0, len + 1); + while (!content.empty() && content[0] == ' ') + content.erase(content.begin()); + } + else + { + cur = content.substr(0, len); + content.erase(0, len); + } + } + while (istag && !content.empty()); + + tag = Unescape(prev); + data = Unescape(cur); + return !istag && !data.empty(); + } + +public: + bool OnRequest(HTTPProvider *provider, const Anope::string &page_name, HTTPClient *client, HTTPMessage &message, HTTPReply &reply) override + { + Anope::string content = message.content, tname, data; + RPCRequest request(reply); + + while (GetData(content, tname, data)) + { + Log(LOG_DEBUG) << "xmlrpc: Tag name: " << tname << ", data: " << data; + if (tname == "methodName") + request.name = data; + else if (tname == "name" && data == "id") + { + GetData(content, tname, data); + request.id = data; + } + else if (tname == "string") + request.data.push_back(data); + } + + for (auto *e : this->events) + { + if (!e->Run(this, client, request)) + return false; + else if (!request.get_replies().empty()) + { + this->Reply(request); + return true; + } + } + + reply.error = HTTP_PAGE_NOT_FOUND; + reply.Write("Unrecognized query"); + return true; + } + + void Reply(RPCRequest &request) override + { + if (!request.id.empty()) + request.reply("id", request.id); + + Anope::string r = "\n\n\n\n\n\n"; + for (const auto &[name, value] : request.get_replies()) + r += "\n" + name + "\n\n" + this->Sanitize(value) + "\n\n\n"; + r += "\n\n\n\n"; + + request.r.Write(r); + } +}; + +class ModuleXMLRPC final + : public Module +{ + ServiceReference httpref; +public: + MyXMLRPCServiceInterface xmlrpcinterface; + + ModuleXMLRPC(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR), + xmlrpcinterface(this, "rpc") + { + + } + + ~ModuleXMLRPC() override + { + if (httpref) + httpref->UnregisterPage(&xmlrpcinterface); + } + + void OnReload(Configuration::Conf *conf) override + { + if (httpref) + httpref->UnregisterPage(&xmlrpcinterface); + this->httpref = ServiceReference("HTTPProvider", conf->GetModule(this)->Get("server", "httpd/main")); + if (!httpref) + throw ConfigException("Unable to find http reference, is httpd loaded?"); + httpref->RegisterPage(&xmlrpcinterface); + } +}; + +MODULE_INIT(ModuleXMLRPC) diff --git a/modules/xmlrpc.cpp b/modules/xmlrpc.cpp deleted file mode 100644 index 480c248bf..000000000 --- a/modules/xmlrpc.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/* - * - * (C) 2010-2025 Anope Team - * Contact us at team@anope.org - * - * Please read COPYING and README for further details. - */ - -#include "module.h" -#include "modules/xmlrpc.h" -#include "modules/httpd.h" - -static struct special_chars final -{ - Anope::string character; - Anope::string replace; - - special_chars(const Anope::string &c, const Anope::string &r) : character(c), replace(r) { } -} -special[] = { - special_chars("&", "&"), - special_chars("\"", """), - special_chars("<", "<"), - special_chars(">", "&qt;"), - special_chars("'", "'"), - special_chars("\n", " "), - special_chars("\002", ""), // bold - special_chars("\003", ""), // color - special_chars("\035", ""), // italics - special_chars("\037", ""), // underline - special_chars("\026", ""), // reverses - special_chars("", "") -}; - -class MyXMLRPCServiceInterface final - : public XMLRPCServiceInterface - , public HTTPPage -{ - std::deque events; - -public: - MyXMLRPCServiceInterface(Module *creator, const Anope::string &sname) : XMLRPCServiceInterface(creator, sname), HTTPPage("/xmlrpc", "text/xml") { } - - void Register(XMLRPCEvent *event) override - { - this->events.push_back(event); - } - - void Unregister(XMLRPCEvent *event) override - { - std::deque::iterator it = std::find(this->events.begin(), this->events.end(), event); - - if (it != this->events.end()) - this->events.erase(it); - } - - Anope::string Sanitize(const Anope::string &string) override - { - Anope::string ret = string; - for (int i = 0; !special[i].character.empty(); ++i) - ret = ret.replace_all_cs(special[i].character, special[i].replace); - return ret; - } - - static Anope::string Unescape(const Anope::string &string) - { - Anope::string ret = string; - for (int i = 0; !special[i].character.empty(); ++i) - if (!special[i].replace.empty()) - ret = ret.replace_all_cs(special[i].replace, special[i].character); - - for (size_t i, last = 0; (i = string.find("&#", last)) != Anope::string::npos;) - { - last = i + 1; - - size_t end = string.find(';', i); - if (end == Anope::string::npos) - break; - - Anope::string ch = string.substr(i + 2, end - (i + 2)); - - if (ch.empty()) - continue; - - long l; - if (!ch.empty() && ch[0] == 'x') - l = strtol(ch.substr(1).c_str(), NULL, 16); - else - l = strtol(ch.c_str(), NULL, 10); - - if (l > 0 && l < 256) - ret = ret.replace_all_cs("&#" + ch + ";", Anope::string(l)); - } - - return ret; - } - -private: - static bool GetData(Anope::string &content, Anope::string &tag, Anope::string &data) - { - if (content.empty()) - return false; - - Anope::string prev, cur; - bool istag; - - do - { - prev = cur; - cur.clear(); - - size_t len = 0; - istag = false; - - if (content[0] == '<') - { - len = content.find_first_of('>'); - istag = true; - } - else if (content[0] != '>') - { - len = content.find_first_of('<'); - } - - // len must advance - if (len == Anope::string::npos || len == 0) - break; - - if (istag) - { - cur = content.substr(1, len - 1); - content.erase(0, len + 1); - while (!content.empty() && content[0] == ' ') - content.erase(content.begin()); - } - else - { - cur = content.substr(0, len); - content.erase(0, len); - } - } - while (istag && !content.empty()); - - tag = Unescape(prev); - data = Unescape(cur); - return !istag && !data.empty(); - } - -public: - bool OnRequest(HTTPProvider *provider, const Anope::string &page_name, HTTPClient *client, HTTPMessage &message, HTTPReply &reply) override - { - Anope::string content = message.content, tname, data; - XMLRPCRequest request(reply); - - while (GetData(content, tname, data)) - { - Log(LOG_DEBUG) << "xmlrpc: Tag name: " << tname << ", data: " << data; - if (tname == "methodName") - request.name = data; - else if (tname == "name" && data == "id") - { - GetData(content, tname, data); - request.id = data; - } - else if (tname == "string") - request.data.push_back(data); - } - - for (auto *e : this->events) - { - if (!e->Run(this, client, request)) - return false; - else if (!request.get_replies().empty()) - { - this->Reply(request); - return true; - } - } - - reply.error = HTTP_PAGE_NOT_FOUND; - reply.Write("Unrecognized query"); - return true; - } - - void Reply(XMLRPCRequest &request) override - { - if (!request.id.empty()) - request.reply("id", request.id); - - Anope::string r = "\n\n\n\n\n\n"; - for (const auto &[name, value] : request.get_replies()) - r += "\n" + name + "\n\n" + this->Sanitize(value) + "\n\n\n"; - r += "\n\n\n\n"; - - request.r.Write(r); - } -}; - -class ModuleXMLRPC final - : public Module -{ - ServiceReference httpref; -public: - MyXMLRPCServiceInterface xmlrpcinterface; - - ModuleXMLRPC(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR), - xmlrpcinterface(this, "xmlrpc") - { - - } - - ~ModuleXMLRPC() override - { - if (httpref) - httpref->UnregisterPage(&xmlrpcinterface); - } - - void OnReload(Configuration::Conf *conf) override - { - if (httpref) - httpref->UnregisterPage(&xmlrpcinterface); - this->httpref = ServiceReference("HTTPProvider", conf->GetModule(this)->Get("server", "httpd/main")); - if (!httpref) - throw ConfigException("Unable to find http reference, is httpd loaded?"); - httpref->RegisterPage(&xmlrpcinterface); - } -}; - -MODULE_INIT(ModuleXMLRPC) diff --git a/modules/xmlrpc_main.cpp b/modules/xmlrpc_main.cpp deleted file mode 100644 index 3cfec74e8..000000000 --- a/modules/xmlrpc_main.cpp +++ /dev/null @@ -1,306 +0,0 @@ -/* - * - * (C) 2010-2025 Anope Team - * Contact us at team@anope.org - * - * Please read COPYING and README for further details. - */ - -#include "module.h" -#include "modules/xmlrpc.h" - -static Module *me; - -class XMLRPCIdentifyRequest final - : public IdentifyRequest -{ - XMLRPCRequest 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; - -public: - XMLRPCIdentifyRequest(Module *m, XMLRPCRequest &req, HTTPClient *c, XMLRPCServiceInterface *iface, const Anope::string &acc, const Anope::string &pass) : IdentifyRequest(m, acc, pass), request(req), repl(request.r), client(c), xinterface(iface) { } - - void OnSuccess() override - { - if (!xinterface || !client) - return; - - request.r = this->repl; - - request.reply("result", "Success"); - request.reply("account", GetAccount()); - - xinterface->Reply(request); - client->SendReply(&request.r); - } - - void OnFail() override - { - if (!xinterface || !client) - return; - - request.r = this->repl; - - request.reply("error", "Invalid password"); - - xinterface->Reply(request); - client->SendReply(&request.r); - } -}; - -class MyXMLRPCEvent final - : public XMLRPCEvent -{ -public: - bool Run(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) override - { - if (request.name == "command") - this->DoCommand(iface, client, request); - else if (request.name == "checkAuthentication") - return this->DoCheckAuthentication(iface, client, request); - else if (request.name == "stats") - this->DoStats(iface, client, request); - else if (request.name == "channel") - this->DoChannel(iface, client, request); - else if (request.name == "user") - this->DoUser(iface, client, request); - else if (request.name == "opers") - this->DoOperType(iface, client, request); - else if (request.name == "notice") - this->DoNotice(iface, client, request); - - return true; - } - -private: - void DoCommand(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) - { - Anope::string service = request.data.size() > 0 ? request.data[0] : ""; - Anope::string user = request.data.size() > 1 ? request.data[1] : ""; - Anope::string command = request.data.size() > 2 ? request.data[2] : ""; - - if (service.empty() || user.empty() || command.empty()) - request.reply("error", "Invalid parameters"); - else - { - BotInfo *bi = BotInfo::Find(service, true); - if (!bi) - request.reply("error", "Invalid service"); - else - { - request.reply("result", "Success"); - - NickAlias *na = NickAlias::Find(user); - - Anope::string out; - - struct XMLRPCommandReply final - : CommandReply - { - Anope::string &str; - - XMLRPCommandReply(Anope::string &s) : str(s) { } - - void SendMessage(BotInfo *source, const Anope::string &msg) override - { - str += msg + "\n"; - }; - } - reply(out); - - User *u = User::Find(user, true); - CommandSource source(user, u, na ? *na->nc : NULL, &reply, bi); - Command::Run(source, command); - - if (!out.empty()) - request.reply("return", iface->Sanitize(out)); - } - } - } - - static bool DoCheckAuthentication(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) - { - Anope::string username = request.data.size() > 0 ? request.data[0] : ""; - Anope::string password = request.data.size() > 1 ? request.data[1] : ""; - - if (username.empty() || password.empty()) - request.reply("error", "Invalid parameters"); - else - { - auto *req = new XMLRPCIdentifyRequest(me, request, client, iface, username, password); - FOREACH_MOD(OnCheckAuthentication, (NULL, req)); - req->Dispatch(); - return false; - } - - return true; - } - - static void DoStats(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) - { - request.reply("uptime", Anope::ToString(Anope::CurTime - Anope::StartTime)); - request.reply("uplinkname", Me->GetLinks().front()->GetName()); - { - Anope::string buf; - for (const auto &capab : Servers::Capab) - buf += " " + capab; - if (!buf.empty()) - 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())); - } - - static void DoChannel(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) - { - if (request.data.empty()) - return; - - Channel *c = Channel::Find(request.data[0]); - - request.reply("name", iface->Sanitize(c ? c->name : request.data[0])); - - if (c) - { - request.reply("bancount", Anope::ToString(c->HasMode("BAN"))); - int count = 0; - for (auto &ban : c->GetModeList("BAN")) - request.reply("ban" + Anope::ToString(++count), iface->Sanitize(ban)); - - request.reply("exceptcount", Anope::ToString(c->HasMode("EXCEPT"))); - count = 0; - for (auto &except : c->GetModeList("EXCEPT")) - request.reply("except" + Anope::ToString(++count), iface->Sanitize(except)); - - request.reply("invitecount", Anope::ToString(c->HasMode("INVITEOVERRIDE"))); - count = 0; - for (auto &invite : c->GetModeList("INVITEOVERRIDE")) - request.reply("invite" + Anope::ToString(++count), iface->Sanitize(invite)); - - Anope::string users; - for (Channel::ChanUserList::const_iterator it = c->users.begin(); it != c->users.end(); ++it) - { - ChanUserContainer *uc = it->second; - users += uc->status.BuildModePrefixList() + uc->user->nick + " "; - } - if (!users.empty()) - { - users.erase(users.length() - 1); - request.reply("users", iface->Sanitize(users)); - } - - if (!c->topic.empty()) - request.reply("topic", iface->Sanitize(c->topic)); - - if (!c->topic_setter.empty()) - request.reply("topicsetter", iface->Sanitize(c->topic_setter)); - - request.reply("topictime", Anope::ToString(c->topic_time)); - request.reply("topicts", Anope::ToString(c->topic_ts)); - } - } - - static void DoUser(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) - { - if (request.data.empty()) - return; - - User *u = User::Find(request.data[0]); - - request.reply("nick", iface->Sanitize(u ? u->nick : request.data[0])); - - if (u) - { - request.reply("ident", iface->Sanitize(u->GetIdent())); - request.reply("vident", iface->Sanitize(u->GetVIdent())); - request.reply("host", iface->Sanitize(u->host)); - if (!u->vhost.empty()) - request.reply("vhost", iface->Sanitize(u->vhost)); - if (!u->chost.empty()) - request.reply("chost", iface->Sanitize(u->chost)); - request.reply("ip", u->ip.addr()); - request.reply("timestamp", Anope::ToString(u->timestamp)); - request.reply("signon", Anope::ToString(u->signon)); - if (u->IsIdentified()) - { - request.reply("account", iface->Sanitize(u->Account()->display)); - if (u->Account()->o) - request.reply("opertype", iface->Sanitize(u->Account()->o->ot->GetName())); - } - - Anope::string channels; - for (User::ChanUserList::const_iterator it = u->chans.begin(); it != u->chans.end(); ++it) - { - ChanUserContainer *cc = it->second; - channels += cc->status.BuildModePrefixList() + cc->chan->name + " "; - } - if (!channels.empty()) - { - channels.erase(channels.length() - 1); - request.reply("channels", channels); - } - } - } - - static void DoOperType(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) - { - for (auto *ot : Config->MyOperTypes) - { - Anope::string perms; - for (const auto &priv : ot->GetPrivs()) - perms += " " + priv; - for (const auto &command : ot->GetCommands()) - perms += " " + command; - request.reply(ot->GetName(), perms); - } - } - - static void DoNotice(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) - { - Anope::string from = request.data.size() > 0 ? request.data[0] : ""; - Anope::string to = request.data.size() > 1 ? request.data[1] : ""; - Anope::string message = request.data.size() > 2 ? request.data[2] : ""; - - BotInfo *bi = BotInfo::Find(from, true); - User *u = User::Find(to, true); - - if (!bi || !u || message.empty()) - return; - - u->SendMessage(bi, message); - - request.reply("result", "Success"); - } -}; - -class ModuleXMLRPCMain final - : public Module -{ - ServiceReference xmlrpc; - - MyXMLRPCEvent stats; - -public: - ModuleXMLRPCMain(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR), xmlrpc("XMLRPCServiceInterface", "xmlrpc") - { - me = this; - - if (!xmlrpc) - throw ModuleException("Unable to find xmlrpc reference, is xmlrpc loaded?"); - - xmlrpc->Register(&stats); - } - - ~ModuleXMLRPCMain() override - { - if (xmlrpc) - xmlrpc->Unregister(&stats); - } -}; - -MODULE_INIT(ModuleXMLRPCMain) -- cgit