From bfb47ab5a681edc44f969ba86672edaea74ac8fe Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sat, 10 Jan 2026 21:23:23 +0000 Subject: [PATCH] Rework the interface for service references. --- include/service.h | 24 +++++++++++++++++------- modules/chanserv/cs_fantasy_stats.cpp | 14 +++++++++----- modules/chanserv/cs_fantasy_top.cpp | 17 +++++++++++------ modules/chanstats.cpp | 19 +++++++++++-------- modules/database/db_redis.cpp | 7 +++++-- modules/database/db_sql.cpp | 9 +++++---- modules/database/db_sql_live.cpp | 7 +++++-- modules/extra/xmlrpc.cpp | 3 ++- modules/rpc/jsonrpc.cpp | 3 ++- modules/rpc/rpc_message.cpp | 1 - modules/sql_authentication.cpp | 14 ++++++-------- modules/sql_log.cpp | 4 ++-- modules/sql_oper.cpp | 10 ++++------ modules/webcpanel/webcpanel.cpp | 5 +++-- 14 files changed, 82 insertions(+), 55 deletions(-) diff --git a/include/service.h b/include/service.h index b06f42121..082a35d33 100644 --- a/include/service.h +++ b/include/service.h @@ -138,20 +138,30 @@ class ServiceReference Anope::string name; public: - ServiceReference() = default; - - ServiceReference(const Anope::string &t, const Anope::string &n) : type(t), name(n) + ServiceReference(const Anope::string &t, const Anope::string &n = "") + : type(t) + , name(n) { } - const Anope::string &GetServiceName() const { return name; } + /** Retrieves the name of the service (e.g. mysql/main). */ + const auto &GetServiceName() const { return name; } - const Anope::string &GetServiceType() const { return type; } + /** Retrieves the type of the service (e.g. SQL::Provider). */ + const auto &GetServiceType() const { return type; } - inline void operator=(const Anope::string &n) + /** Invalidates the reference and changes the name of the referenced service. */ + void SetServiceName(const Anope::string& newname) { - this->name = n; this->invalid = true; + this->name = newname; + } + + /** Invalidates the reference and changes the type of the referenced service. */ + void SetServiceType(const Anope::string& newtype) + { + this->invalid = true; + this->type = newtype; } operator bool() override diff --git a/modules/chanserv/cs_fantasy_stats.cpp b/modules/chanserv/cs_fantasy_stats.cpp index 818f83a59..acaace47d 100644 --- a/modules/chanserv/cs_fantasy_stats.cpp +++ b/modules/chanserv/cs_fantasy_stats.cpp @@ -73,17 +73,21 @@ class CSStats final MySQLInterface sqlinterface; Anope::string prefix; public: - CSStats(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), - commandcsstats(this), commandcsgstats(this), sql("", ""), sqlinterface(this) + CSStats(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, VENDOR) + , commandcsstats(this) + , commandcsgstats(this) + , sql("SQL::Provider") + , sqlinterface(this) { me = this; - } void OnReload(Configuration::Conf &conf) override { - prefix = conf.GetModule("chanstats").Get("prefix", "anope_"); - this->sql = ServiceReference("SQL::Provider", conf.GetModule("chanstats").Get("engine")); + const auto &block = conf.GetModule("chanstats"); + prefix = block.Get("prefix", "anope_"); + this->sql.SetServiceName(block.Get("engine")); } SQL::Result RunQuery(const SQL::Query &query) diff --git a/modules/chanserv/cs_fantasy_top.cpp b/modules/chanserv/cs_fantasy_top.cpp index 1edcb022e..045ffb0c9 100644 --- a/modules/chanserv/cs_fantasy_top.cpp +++ b/modules/chanserv/cs_fantasy_top.cpp @@ -99,18 +99,23 @@ class CSTop final Anope::string prefix; public: - CSTop(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), - commandcstop(this), commandcsgtop(this), commandcstop10(this), commandcsgtop10(this), sql("", ""), - sqlinterface(this) + CSTop(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, VENDOR) + , commandcstop(this) + , commandcsgtop(this) + , commandcstop10(this) + , commandcsgtop10(this) + , sql("SQL::Provider") + , sqlinterface(this) { me = this; - } void OnReload(Configuration::Conf &conf) override { - prefix = conf.GetModule("chanstats").Get("prefix", "anope_"); - this->sql = ServiceReference("SQL::Provider", conf.GetModule("chanstats").Get("engine")); + const auto &block = conf.GetModule("chanstats"); + prefix = block.Get("prefix", "anope_"); + this->sql.SetServiceName(block.Get("engine")); } SQL::Result RunQuery(const SQL::Query &query) diff --git a/modules/chanstats.cpp b/modules/chanstats.cpp index f318eac93..9a356085b 100644 --- a/modules/chanstats.cpp +++ b/modules/chanstats.cpp @@ -494,11 +494,15 @@ class MChanstats final public: - MChanstats(const Anope::string &modname, const Anope::string &creator) : - Module(modname, creator, EXTRA | VENDOR), - cs_stats(this, "CS_STATS"), ns_stats(this, "NS_STATS"), - commandcssetchanstats(this), commandnssetchanstats(this), commandnssasetchanstats(this), - sqlinterface(this) + MChanstats(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, EXTRA | VENDOR) + , cs_stats(this, "CS_STATS") + , ns_stats(this, "NS_STATS") + , commandcssetchanstats(this) + , commandnssetchanstats(this) + , commandnssasetchanstats(this) + , sql("SQL::Provider") + , sqlinterface(this) { } @@ -509,12 +513,11 @@ public: SmileysHappy = block.Get("SmileysHappy"); SmileysSad = block.Get("SmileysSad"); SmileysOther = block.Get("SmileysOther"); - Anope::string engine = block.Get("engine"); - this->sql = ServiceReference("SQL::Provider", engine); + this->sql.SetServiceName(block.Get("engine")); if (sql) this->CheckTables(); else - Log(this) << "no database connection to " << engine; + Log(this) << "no database connection to " << this->sql.GetServiceName(); } void OnChanInfo(CommandSource &source, ChannelInfo *ci, InfoFormatter &info, bool show_all) override diff --git a/modules/database/db_redis.cpp b/modules/database/db_redis.cpp index c36b6df48..b15790162 100644 --- a/modules/database/db_redis.cpp +++ b/modules/database/db_redis.cpp @@ -134,7 +134,10 @@ class DatabaseRedis final public: ServiceReference redis; - DatabaseRedis(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE | VENDOR), sl(this) + DatabaseRedis(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, DATABASE | VENDOR) + , sl(this) + , redis("Redis::Provider") { me = this; @@ -180,7 +183,7 @@ public: void OnReload(Configuration::Conf &conf) override { const auto &block = conf.GetModule(this); - this->redis = ServiceReference("Redis::Provider", block.Get("engine", "redis/main")); + this->redis.SetServiceName(block.Get("engine", "redis/main")); } EventReturn OnLoadDatabase() override diff --git a/modules/database/db_sql.cpp b/modules/database/db_sql.cpp index 98493b6a5..ecdcc39cf 100644 --- a/modules/database/db_sql.cpp +++ b/modules/database/db_sql.cpp @@ -102,10 +102,11 @@ class DBSQL final } public: - DBSQL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE | VENDOR), sql("", ""), sqlinterface(this) + DBSQL(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, DATABASE | VENDOR) + , sql("SQL::Provider") + , sqlinterface(this) { - - if (ModuleManager::FindModule("db_sql_live") != NULL) throw ModuleException("db_sql can not be loaded after db_sql_live"); } @@ -166,7 +167,7 @@ public: void OnReload(Configuration::Conf &conf) override { const auto &block = conf.GetModule(this); - this->sql = ServiceReference("SQL::Provider", block.Get("engine")); + this->sql.SetServiceName(block.Get("engine")); this->prefix = block.Get("prefix", "anope_db_"); this->import = block.Get("import"); } diff --git a/modules/database/db_sql_live.cpp b/modules/database/db_sql_live.cpp index 6ed1ef34a..ab1d3f0ef 100644 --- a/modules/database/db_sql_live.cpp +++ b/modules/database/db_sql_live.cpp @@ -79,7 +79,9 @@ private: } public: - DBMySQL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE | VENDOR), SQL("", "") + DBMySQL(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, DATABASE | VENDOR) + , SQL("SQL::Provider") { this->lastwarn = 0; this->ro = false; @@ -148,7 +150,8 @@ public: void OnReload(Configuration::Conf &conf) override { const auto &block = conf.GetModule(this); - this->SQL = ServiceReference("SQL::Provider", block.Get("engine")); + + this->SQL.SetServiceName(block.Get("engine")); this->prefix = block.Get("prefix", "anope_db_"); } diff --git a/modules/extra/xmlrpc.cpp b/modules/extra/xmlrpc.cpp index ee5080e16..078e17bcf 100644 --- a/modules/extra/xmlrpc.cpp +++ b/modules/extra/xmlrpc.cpp @@ -291,6 +291,7 @@ private: public: ModuleXMLRPC(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR) + , httpref(HTTP_PROVIDER) , xmlrpcinterface(this) { xmlrpc_env env; @@ -325,7 +326,7 @@ public: XMLRPCServiceInterface::enable_i8 = modconf.Get("enable_i8", "yes"); XMLRPCServiceInterface::enable_nil = modconf.Get("enable_nil", "yes"); - this->httpref = ServiceReference(HTTP_PROVIDER, modconf.Get("server", "httpd/main")); + this->httpref.SetServiceName(modconf.Get("server", "httpd/main")); if (!httpref) throw ConfigException("Unable to find http reference, is httpd loaded?"); diff --git a/modules/rpc/jsonrpc.cpp b/modules/rpc/jsonrpc.cpp index 4f9add3ce..d59b4c080 100644 --- a/modules/rpc/jsonrpc.cpp +++ b/modules/rpc/jsonrpc.cpp @@ -289,6 +289,7 @@ private: public: ModuleJSONRPC(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR) + , httpref(HTTP_PROVIDER) , jsonrpcinterface(this) { } @@ -307,7 +308,7 @@ public: const auto &modconf = conf.GetModule(this); JSONRPCServiceInterface::integer_bits = modconf.Get("integer_bits", "64"); - this->httpref = ServiceReference(HTTP_PROVIDER, modconf.Get("server", "httpd/main")); + this->httpref.SetServiceName(modconf.Get("server", "httpd/main")); if (!httpref) throw ConfigException("Unable to find http reference, is httpd loaded?"); diff --git a/modules/rpc/rpc_message.cpp b/modules/rpc/rpc_message.cpp index c079c64cf..3c0ad76c9 100644 --- a/modules/rpc/rpc_message.cpp +++ b/modules/rpc/rpc_message.cpp @@ -117,7 +117,6 @@ class ModuleRPCSystem final : public Module { private: - ServiceReference rpc; MessageNetworkRPCEvent messagenetworkrpcevent; MessageServerRPCEvent messageserverrpcevent; MessageUserRPCEvent messageuserrpcevent; diff --git a/modules/sql_authentication.cpp b/modules/sql_authentication.cpp index 7bc89390c..9f401f16d 100644 --- a/modules/sql_authentication.cpp +++ b/modules/sql_authentication.cpp @@ -20,7 +20,7 @@ namespace { Module *me; - ServiceReference encryption; + ServiceReference encryption("Encryption::Provider"); Anope::string password_hash, password_field; } class SQLAuthenticationResult final @@ -110,34 +110,32 @@ public: class ModuleSQLAuthentication final : public Module { - Anope::string engine; Anope::string query; Anope::string disable_reason, disable_email_reason; ServiceReference SQL; public: - ModuleSQLAuthentication(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR) + ModuleSQLAuthentication(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, EXTRA | VENDOR) + , SQL("SQL::Provider") { me = this; - } void OnReload(Configuration::Conf &conf) override { const auto &config = conf.GetModule(this); - this->engine = config.Get("engine"); this->query = config.Get("query"); this->disable_reason = config.Get("disable_reason"); this->disable_email_reason = config.Get("disable_email_reason"); - - this->SQL = ServiceReference("SQL::Provider", this->engine); + this->SQL.SetServiceName(config.Get("engine")); password_hash = config.Get("password_hash"); if (!password_hash.empty()) { password_field = config.Get("password_field", "password"); - encryption = ServiceReference("Encryption::Provider", password_hash); + encryption.SetServiceName(password_hash); } } diff --git a/modules/sql_log.cpp b/modules/sql_log.cpp index 2ccb69f45..304840e6a 100644 --- a/modules/sql_log.cpp +++ b/modules/sql_log.cpp @@ -35,7 +35,7 @@ public: void OnLogMessage(LogInfo *li, const Log *l, const Anope::string &msg) override { Anope::string ref_name; - ServiceReference SQL; + ServiceReference SQL("SQL::Provider"); for (const auto &target : li->targets) { @@ -43,7 +43,7 @@ public: if (!sz) { ref_name = target.substr(8); - SQL = ServiceReference("SQL::Provider", ref_name); + SQL.SetServiceName(ref_name); break; } } diff --git a/modules/sql_oper.cpp b/modules/sql_oper.cpp index 6d342aa9b..5fe02c729 100644 --- a/modules/sql_oper.cpp +++ b/modules/sql_oper.cpp @@ -134,13 +134,14 @@ public: class ModuleSQLOper final : public Module { - Anope::string engine; Anope::string query; ServiceReference SQL; public: - ModuleSQLOper(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR) + ModuleSQLOper(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, EXTRA | VENDOR) + , SQL("SQL::Provider") { } @@ -159,11 +160,8 @@ public: void OnReload(Configuration::Conf &conf) override { const auto &config = conf.GetModule(this); - - this->engine = config.Get("engine"); this->query = config.Get("query"); - - this->SQL = ServiceReference("SQL::Provider", this->engine); + this->SQL.SetServiceName(config.Get("engine")); } void OnNickIdentify(User *u) override diff --git a/modules/webcpanel/webcpanel.cpp b/modules/webcpanel/webcpanel.cpp index db89ee5b3..a345d61fc 100644 --- a/modules/webcpanel/webcpanel.cpp +++ b/modules/webcpanel/webcpanel.cpp @@ -15,7 +15,7 @@ #include "webcpanel.h" Module *me; -Anope::string provider_name, template_base, page_title; +Anope::string template_base, page_title; class ModuleWebCPanel final : public Module @@ -53,6 +53,7 @@ class ModuleWebCPanel final public: ModuleWebCPanel(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR), + provider(HTTP_PROVIDER), panel(this, "webcpanel"), id(this, "webcpanel_id"), ip(this, "webcpanel_ip"), last_login(this, "webcpanel_last_login"), style_css("style.css", "/static/style.css", "text/css"), logo_png("logo.png", "/static/logo.png", "image/png"), cubes_png("cubes.png", "/static/cubes.png", "image/png"), favicon_ico("favicon.ico", "/favicon.ico", "image/x-icon"), @@ -70,7 +71,7 @@ public: template_base = Anope::ExpandData(block.Get("template_dir", "webcpanel/templates/default")); page_title = block.Get("title", "Anope IRC Services"); - provider = ServiceReference(HTTP_PROVIDER, provider_name); + provider.SetServiceName(block.Get("server", "httpd/main")); if (!provider) throw ModuleException("Unable to find HTTPD provider. Is httpd loaded?");