core: Replace legacy INI config system with Pydantic-based TOML configuration.

This is a huge configuration refactoring.

The configuration system now uses Pydantic settings, with better validation, better
support for environment variables and other goodies.

The main config files now use TOML instead of INI, this allow for better handling of
complex types, notably dict.

Plugins, components and frontends can declare their config settings in a class which will
be imported dynamically.
This commit is contained in:
Goffi 2025-11-30 12:27:35 +01:00
commit 6a7b7bc36a

View file

@ -16,6 +16,9 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import cast
from pydantic import Field
from libervia.backend import G
from libervia.backend.core.i18n import _, D_
from libervia.backend.core.log import getLogger
@ -24,11 +27,11 @@ from libervia.backend.tools import xml_tools
from libervia.backend.memory.memory import Sessions
from libervia.backend.memory.crypto import PasswordHasher
from libervia.backend.core.constants import Const as C
import configparser
from twisted.internet import defer
from twisted.python.failure import Failure
from twisted.words.protocols.jabber import jid
from libervia.backend.tools.common import email as sat_email
from libervia.backend.tools.config import EmailSettings, PluginSettings
log = getLogger(__name__)
@ -40,7 +43,7 @@ log = getLogger(__name__)
PLUGIN_INFO = {
C.PI_NAME: "Account Plugin",
C.PI_IMPORT_NAME: "MISC-ACCOUNT",
C.PI_IMPORT_NAME: "account",
C.PI_TYPE: "MISC",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["XEP-0077"],
@ -69,11 +72,14 @@ default_conf = {
"admin_email": "",
"new_account_server": "localhost",
"new_account_domain": "", #  use xmpp_domain if not found
"reserved_list": ["libervia"], # profiles which can't be used
}
# profiles which can't be used for accound creation. Must be lowercase.
RESERVED_LIST: list[str] = ["libervia"]
WELCOME_MSG = D_(
"""Welcome to Libervia, the web interface of Salut à Toi.
"""Welcome to Libervia.
Your account on {domain} has been successfully created.
This is a demonstration version to show you the current status of the project.
@ -91,16 +97,23 @@ In the beginning, you have nobody to talk to. To find some contacts, you may use
Any feedback welcome. Thank you!
Salut à Toi association
https://www.salut-a-toi.org
The Libervia team.
https://www.libervia.org
"""
)
DEFAULT_DOMAIN = "example.net"
class PluginsAccountSettings(PluginSettings, plugin_info=PLUGIN_INFO):
# Use global email settings by default.
email: EmailSettings | None = Field(
default=None,
description="Email settings, default to settings from ``email`` section, use "
"this if you want different ones for account creation.",
)
class MiscAccount(object):
"""Account plugin: create a SàT + XMPP account, used by Libervia"""
"""Account plugin: create a Libervia + XMPP account."""
# XXX: This plugin was initialy a Q&D one used for the demo.
# TODO: cleaning, separate email handling, more configuration/tests, fixes
@ -121,7 +134,7 @@ class MiscAccount(object):
".plugin",
in_sign="",
out_sign="s",
method=self.account_domain_new_get,
method=lambda: G.config.common.new_account_domain,
async_=False,
)
host.bridge.add_method(
@ -141,7 +154,6 @@ class MiscAccount(object):
async_=True,
)
self.fix_email_admins()
self._sessions = Sessions()
self.__account_cb_id = host.register_callback(
@ -170,46 +182,16 @@ class MiscAccount(object):
self.__delete_account_cb, with_data=True
)
# FIXME: remove this after some time, when the deprecated parameter is really abandoned
def fix_email_admins(self):
"""Handle deprecated config option "admin_email" to fix the admin emails list"""
admin_email = self.config_get("admin_email")
if not admin_email:
return
log.warning(
"admin_email parameter is deprecated, please use email_admins_list instead"
)
param_name = "email_admins_list"
try:
section = ""
value = self.host.memory.config_get(section, param_name, Exception)
except (configparser.NoOptionError, configparser.NoSectionError):
section = CONFIG_SECTION
value = self.host.memory.config_get(
section, param_name, default_conf[param_name]
)
def get_email_conf(self) -> EmailSettings:
"""Return settings to send an email.
value = set(value)
value.add(admin_email)
self.host.memory.config.set(section, param_name, ",".join(value))
def config_get(self, name, section=CONFIG_SECTION):
if name.startswith("email_"):
# XXX: email_ parameters were first in [plugin account] section
# but as it make more sense to have them in common with other plugins,
# they can now be in [DEFAULT] section
try:
value = self.host.memory.config_get(None, name, Exception)
except (configparser.NoOptionError, configparser.NoSectionError):
pass
else:
return value
if section == CONFIG_SECTION:
default = default_conf[name]
Will use settings of this plugins is set, otherwise global ones.
"""
plugin_settings = G.config.get_settings(PluginsAccountSettings)
if plugin_settings.email is not None:
return plugin_settings.email
else:
default = None
return self.host.memory.config_get(section, name, default)
return G.config.email
def _register_account(self, email, password, profile):
return self.registerAccount(email, password, None, profile)
@ -244,7 +226,7 @@ class MiscAccount(object):
if not password or not profile:
raise exceptions.DataError
if profile.lower() in self.config_get("reserved_list"):
if profile.lower() in RESERVED_LIST:
return defer.fail(Failure(exceptions.ConflictError))
d = self.host.memory.create_profile(profile, password)
@ -265,7 +247,7 @@ class MiscAccount(object):
d = defer.succeed(None)
jid_ = jid.JID(jid_s)
else:
jid_s = profile + "@" + self.account_domain_new_get()
jid_s = f"{profile}@{G.config.common.new_account_domain}"
jid_ = jid.JID(jid_s)
d = self.host.plugins["XEP-0077"].register_new_account(jid_, password)
@ -299,10 +281,10 @@ class MiscAccount(object):
def send_emails(self, email, profile):
# time to send the email
domain = self.account_domain_new_get()
domain = G.config.common.new_account_domain
# email to the administrators
admins_emails = self.config_get("email_admins_list")
admins_emails = G.config.common.email_admins_list
if not admins_emails:
log.warning(
"No known admin email, we can't send email to administrator(s).\n"
@ -314,7 +296,11 @@ class MiscAccount(object):
# there is no email when an existing XMPP account is used
body = f"New account created on {domain}: {profile} [{email or '<no email>'}]"
d_admin = sat_email.send_email(
self.host.memory.config, admins_emails, subject, body
self.get_email_conf(),
self.host.memory.config,
admins_emails,
subject,
body,
)
admins_emails_txt = ", ".join(["<" + addr + ">" for addr in admins_emails])
@ -343,7 +329,7 @@ class MiscAccount(object):
# XXX: this will not fail when the email address doesn't exist
# FIXME: check email reception to validate email given by the user
# FIXME: delete the profile if the email could not been sent?
d_user = sat_email.send_email(self.host.memory.config, [email], subject, body)
d_user = sat_email.send_email(self.get_email_conf, [email], subject, body)
d_user.addCallbacks(
lambda __: log.debug(
"Account creation confirmation sent to <{}>".format(email)
@ -353,21 +339,6 @@ class MiscAccount(object):
)
return defer.DeferredList([d_user, d_admin])
def account_domain_new_get(self):
"""get the domain that will be set to new account"""
domain = self.config_get("new_account_domain") or self.config_get(
"xmpp_domain", None
)
if not domain:
log.warning(
_(
'xmpp_domain needs to be set in sat.conf. Using "{default}" meanwhile'
).format(default=DEFAULT_DOMAIN)
)
return DEFAULT_DOMAIN
return domain
def _get_account_dialog_ui(self, profile):
"""Get the main dialog to manage your account
@param menu_data
@ -540,7 +511,7 @@ class MiscAccount(object):
return d
def __delete_account(self, profile):
"""Ask for a confirmation before deleting the XMPP account and SàT profile
"""Ask for a confirmation before deleting the XMPP account and Libervia profile
@param profile
"""
form_ui = xml_tools.XMLUI(
@ -558,9 +529,8 @@ class MiscAccount(object):
)
form_ui.addText(
D_(
"All your data stored on %(server)s, including your %(target)s will be erased."
)
% {"server": self.account_domain_new_get(), "target": target}
"All your data stored on {server}, including your {target} will be erased."
).format(server=G.config.common.new_account_domain, target=target)
)
form_ui.addText(
D_(