diff --git a/src/account_config.cpp b/src/account_config.cpp index ef0144bb80ceab0f93f7dcc5eed9fbb24d81ee33..18f373449f2d32180a3c47df5be9def956a9b95c 100644 --- a/src/account_config.cpp +++ b/src/account_config.cpp @@ -17,9 +17,9 @@ #include "account_config.h" #include "account_const.h" #include "account_schema.h" -#include "config/yamlparser.h" #include "string_utils.h" #include "fileutils.h" +#include "config/account_config_utils.h" #include <fmt/compile.h> @@ -54,30 +54,30 @@ constexpr const char* PROXY_PUSH_TOPIC_KEY = "proxyPushiOSTopic"; using yaml_utils::parseValueOptional; void -AccountConfig::serialize(YAML::Emitter& out) const +AccountConfig::serializeDiff(YAML::Emitter& out, const AccountConfig& DEFAULT_CONFIG) const { - out << YAML::Key << ACCOUNT_ENABLE_KEY << YAML::Value << enabled; - out << YAML::Key << TYPE_KEY << YAML::Value << type; - out << YAML::Key << ALIAS_KEY << YAML::Value << alias; - out << YAML::Key << HOSTNAME_KEY << YAML::Value << hostname; - out << YAML::Key << USERNAME_KEY << YAML::Value << username; + SERIALIZE_CONFIG(ACCOUNT_ENABLE_KEY, enabled); + SERIALIZE_CONFIG(TYPE_KEY, type); + SERIALIZE_CONFIG(ALIAS_KEY, alias); + SERIALIZE_CONFIG(HOSTNAME_KEY, hostname); + SERIALIZE_CONFIG(USERNAME_KEY, username); + SERIALIZE_CONFIG(MAILBOX_KEY, mailbox); out << YAML::Key << ACTIVE_CODEC_KEY << YAML::Value << fmt::format(FMT_COMPILE("{}"), fmt::join(activeCodecs, "/"sv)); - out << YAML::Key << MAILBOX_KEY << YAML::Value << mailbox; - out << YAML::Key << ACCOUNT_AUTOANSWER_KEY << YAML::Value << autoAnswerEnabled; - out << YAML::Key << ACCOUNT_READRECEIPT_KEY << YAML::Value << sendReadReceipt; - out << YAML::Key << ACCOUNT_ISRENDEZVOUS_KEY << YAML::Value << isRendezVous; - out << YAML::Key << ACCOUNT_ACTIVE_CALL_LIMIT_KEY << YAML::Value << activeCallLimit; - out << YAML::Key << RINGTONE_ENABLED_KEY << YAML::Value << ringtoneEnabled; - out << YAML::Key << RINGTONE_PATH_KEY << YAML::Value << ringtonePath; - out << YAML::Key << USER_AGENT_KEY << YAML::Value << customUserAgent; - out << YAML::Key << DISPLAY_NAME_KEY << YAML::Value << displayName; - out << YAML::Key << UPNP_ENABLED_KEY << YAML::Value << upnpEnabled; - out << YAML::Key << DEFAULT_MODERATORS_KEY << YAML::Value << defaultModerators; - out << YAML::Key << LOCAL_MODERATORS_ENABLED_KEY << YAML::Value << localModeratorsEnabled; - out << YAML::Key << ALL_MODERATORS_ENABLED_KEY << YAML::Value << allModeratorsEnabled; - out << YAML::Key << PROXY_PUSH_TOKEN_KEY << YAML::Value << deviceKey; - out << YAML::Key << PROXY_PUSH_TOPIC_KEY << YAML::Value << notificationTopic; - out << YAML::Key << VIDEO_ENABLED_KEY << YAML::Value << videoEnabled; + SERIALIZE_CONFIG(ACCOUNT_AUTOANSWER_KEY, autoAnswerEnabled); + SERIALIZE_CONFIG(ACCOUNT_READRECEIPT_KEY, sendReadReceipt); + SERIALIZE_CONFIG(ACCOUNT_ISRENDEZVOUS_KEY, isRendezVous); + SERIALIZE_CONFIG(ACCOUNT_ACTIVE_CALL_LIMIT_KEY, activeCallLimit); + SERIALIZE_CONFIG(RINGTONE_ENABLED_KEY, ringtoneEnabled); + SERIALIZE_CONFIG(RINGTONE_PATH_KEY, ringtonePath); + SERIALIZE_CONFIG(USER_AGENT_KEY, customUserAgent); + SERIALIZE_CONFIG(DISPLAY_NAME_KEY, displayName); + SERIALIZE_CONFIG(UPNP_ENABLED_KEY, upnpEnabled); + SERIALIZE_CONFIG(DEFAULT_MODERATORS_KEY, defaultModerators); + SERIALIZE_CONFIG(LOCAL_MODERATORS_ENABLED_KEY, localModeratorsEnabled); + SERIALIZE_CONFIG(ALL_MODERATORS_ENABLED_KEY, allModeratorsEnabled); + SERIALIZE_CONFIG(PROXY_PUSH_TOKEN_KEY, deviceKey); + SERIALIZE_CONFIG(PROXY_PUSH_TOPIC_KEY, notificationTopic); + SERIALIZE_CONFIG(VIDEO_ENABLED_KEY, videoEnabled); } void diff --git a/src/account_config.h b/src/account_config.h index 5b6c34e749ff06741a789101c1df238c5b65c3b1..6bc75737ca2b90cfb6af5e89ef245d60f7cc7527 100644 --- a/src/account_config.h +++ b/src/account_config.h @@ -30,7 +30,9 @@ constexpr const char* const DEFAULT_RINGTONE_PATH = "default.opus"; struct AccountConfig: public Serializable { AccountConfig(const std::string& type_, const std::string& id_, const std::string& path_ = {}): type(type_), id(id_), path(path_) {} - virtual void serialize(YAML::Emitter& out) const; + void serializeDiff(YAML::Emitter& out, const AccountConfig& def) const; + + virtual void serialize(YAML::Emitter& out) const = 0; virtual void unserialize(const YAML::Node& node); virtual std::map<std::string, std::string> toMap() const; diff --git a/src/config/account_config_utils.h b/src/config/account_config_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..2af4689c09d6ee70eac030c4208aa8e3e1b06c0b --- /dev/null +++ b/src/config/account_config_utils.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2004-2022 Savoir-faire Linux Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <https://www.gnu.org/licenses/>. + */ +#pragma once +#include "yamlparser.h" + +template<typename T> +inline void serializeValue(YAML::Emitter& out, const char* key, const T& value, const T& def) { + if (value != def) + out << YAML::Key << key << YAML::Value << value; +} + +#define SERIALIZE_CONFIG(key, name) serializeValue(out, key, name, DEFAULT_CONFIG.name) +#define SERIALIZE_PATH(key, name) serializeValue(out, key, fileutils::getCleanPath(path, name), DEFAULT_CONFIG.name) diff --git a/src/jamidht/jamiaccount_config.cpp b/src/jamidht/jamiaccount_config.cpp index 905a2d7842e7096a085c7382f475090d81ddcb2a..534389e6ee42d908f319a631aec09238542c8b77 100644 --- a/src/jamidht/jamiaccount_config.cpp +++ b/src/jamidht/jamiaccount_config.cpp @@ -18,8 +18,8 @@ #include "account_const.h" #include "account_schema.h" #include "configkeys.h" -#include "config/yamlparser.h" #include "fileutils.h" +#include "config/account_config_utils.h" namespace jami { @@ -31,53 +31,43 @@ const char* const TLS_PASSWORD_KEY = "password"; const char* const PRIVATE_KEY_KEY = "privateKey"; } // namespace Conf +static const JamiAccountConfig DEFAULT_CONFIG {}; + void JamiAccountConfig::serialize(YAML::Emitter& out) const { out << YAML::BeginMap; - SipAccountBaseConfig::serialize(out); - out << YAML::Key << Conf::DHT_PORT_KEY << YAML::Value << dhtPort; - out << YAML::Key << Conf::DHT_PUBLIC_IN_CALLS << YAML::Value << allowPublicIncoming; - out << YAML::Key << Conf::DHT_ALLOW_PEERS_FROM_HISTORY << YAML::Value << allowPeersFromHistory; - out << YAML::Key << Conf::DHT_ALLOW_PEERS_FROM_CONTACT << YAML::Value << allowPeersFromContact; - out << YAML::Key << Conf::DHT_ALLOW_PEERS_FROM_TRUSTED << YAML::Value << allowPeersFromTrusted; - out << YAML::Key << libjami::Account::ConfProperties::DHT_PEER_DISCOVERY << YAML::Value - << dhtPeerDiscovery; - out << YAML::Key << libjami::Account::ConfProperties::ACCOUNT_PEER_DISCOVERY << YAML::Value - << accountPeerDiscovery; - out << YAML::Key << libjami::Account::ConfProperties::ACCOUNT_PUBLISH << YAML::Value - << accountPublish; - - out << YAML::Key << Conf::PROXY_ENABLED_KEY << YAML::Value << proxyEnabled; - out << YAML::Key << Conf::PROXY_SERVER_KEY << YAML::Value << proxyServer; - out << YAML::Key << libjami::Account::ConfProperties::DHT_PROXY_LIST_URL << YAML::Value - << proxyListUrl; - -#if HAVE_RINGNS - out << YAML::Key << libjami::Account::ConfProperties::RingNS::URI << YAML::Value << nameServer; - if (not registeredName.empty()) - out << YAML::Key << libjami::Account::VolatileProperties::REGISTERED_NAME << YAML::Value - << registeredName; -#endif + SipAccountBaseConfig::serializeDiff(out, DEFAULT_CONFIG); + SERIALIZE_CONFIG(Conf::DHT_PORT_KEY, dhtPort); + SERIALIZE_CONFIG(Conf::DHT_PUBLIC_IN_CALLS, allowPublicIncoming); + SERIALIZE_CONFIG(Conf::DHT_ALLOW_PEERS_FROM_HISTORY, allowPeersFromHistory); + SERIALIZE_CONFIG(Conf::DHT_ALLOW_PEERS_FROM_CONTACT, allowPeersFromContact); + SERIALIZE_CONFIG(Conf::DHT_ALLOW_PEERS_FROM_TRUSTED, allowPeersFromTrusted); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::DHT_PEER_DISCOVERY, dhtPeerDiscovery); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::ACCOUNT_PEER_DISCOVERY, accountPeerDiscovery); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::ACCOUNT_PUBLISH, accountPublish); + SERIALIZE_CONFIG(Conf::PROXY_ENABLED_KEY, proxyEnabled); + SERIALIZE_CONFIG(Conf::PROXY_SERVER_KEY, proxyServer); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::DHT_PROXY_LIST_URL, proxyListUrl); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::RingNS::URI, nameServer); + SERIALIZE_CONFIG(libjami::Account::VolatileProperties::REGISTERED_NAME, registeredName); + SERIALIZE_PATH(libjami::Account::ConfProperties::ARCHIVE_PATH, archivePath); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::ARCHIVE_HAS_PASSWORD, archiveHasPassword); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::DEVICE_NAME, deviceName); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::MANAGER_URI, managerUri); + SERIALIZE_CONFIG(libjami::Account::ConfProperties::MANAGER_USERNAME, managerUsername); - out << YAML::Key << libjami::Account::ConfProperties::ARCHIVE_PATH << YAML::Value << fileutils::getCleanPath(path, archivePath); - out << YAML::Key << libjami::Account::ConfProperties::ARCHIVE_HAS_PASSWORD << YAML::Value - << archiveHasPassword; out << YAML::Key << Conf::RING_ACCOUNT_RECEIPT << YAML::Value << receipt; if (receiptSignature.size() > 0) out << YAML::Key << Conf::RING_ACCOUNT_RECEIPT_SIG << YAML::Value << YAML::Binary(receiptSignature.data(), receiptSignature.size()); - out << YAML::Key << libjami::Account::ConfProperties::DEVICE_NAME << YAML::Value << deviceName; - out << YAML::Key << libjami::Account::ConfProperties::MANAGER_URI << YAML::Value << managerUri; - out << YAML::Key << libjami::Account::ConfProperties::MANAGER_USERNAME << YAML::Value - << managerUsername; // tls submap out << YAML::Key << Conf::TLS_KEY << YAML::Value << YAML::BeginMap; - out << YAML::Key << Conf::CALIST_KEY << YAML::Value << fileutils::getCleanPath(path, tlsCaListFile); - out << YAML::Key << Conf::CERTIFICATE_KEY << YAML::Value << fileutils::getCleanPath(path, tlsCertificateFile);; - out << YAML::Key << Conf::TLS_PASSWORD_KEY << YAML::Value << tlsPassword; - out << YAML::Key << Conf::PRIVATE_KEY_KEY << YAML::Value << fileutils::getCleanPath(path, tlsPrivateKeyFile);; + SERIALIZE_PATH(Conf::CALIST_KEY, tlsCaListFile); + SERIALIZE_PATH(Conf::CERTIFICATE_KEY, tlsCertificateFile); + SERIALIZE_CONFIG(Conf::TLS_PASSWORD_KEY, tlsPassword); + SERIALIZE_PATH(Conf::PRIVATE_KEY_KEY, tlsPrivateKeyFile); out << YAML::EndMap; out << YAML::EndMap; @@ -87,7 +77,6 @@ void JamiAccountConfig::unserialize(const YAML::Node& node) { using yaml_utils::parseValueOptional; - using yaml_utils::parsePath; using yaml_utils::parsePathOptional; SipAccountBaseConfig::unserialize(node); diff --git a/src/sip/sipaccount_config.cpp b/src/sip/sipaccount_config.cpp index ebf03f7459ff58b11a1f4d0454c99ece104b4783..9f57f5d66e1218d13528ea9d73f59b37e5e02d6c 100644 --- a/src/sip/sipaccount_config.cpp +++ b/src/sip/sipaccount_config.cpp @@ -70,6 +70,7 @@ constexpr const char* KEY_EXCHANGE_KEY = "keyExchange"; constexpr const char* RTP_FALLBACK_KEY = "rtpFallback"; } // namespace Conf +static const SipAccountConfig DEFAULT_CONFIG {}; static constexpr unsigned MIN_REGISTRATION_TIME = 60; // seconds using yaml_utils::parseValueOptional; @@ -90,7 +91,7 @@ SipAccountConfig::serialize(YAML::Emitter& out) const { out << YAML::BeginMap; out << YAML::Key << Conf::ID_KEY << YAML::Value << id; - SipAccountBaseConfig::serialize(out); + SipAccountBaseConfig::serializeDiff(out, DEFAULT_CONFIG); out << YAML::Key << Conf::BIND_ADDRESS_KEY << YAML::Value << bindAddress; out << YAML::Key << Conf::PORT_KEY << YAML::Value << localPort; diff --git a/src/sip/sipaccountbase_config.cpp b/src/sip/sipaccountbase_config.cpp index a8a668b0ba297450b3a1442764899e7b65ec2c38..e735ace5e43b6a8691af3da740614ce94cea2cb0 100644 --- a/src/sip/sipaccountbase_config.cpp +++ b/src/sip/sipaccountbase_config.cpp @@ -17,7 +17,7 @@ #include "sipaccountbase_config.h" #include "account_const.h" #include "account_schema.h" -#include "config/yamlparser.h" +#include "config/account_config_utils.h" namespace jami { @@ -75,33 +75,24 @@ addRangeToDetails(std::map<std::string, std::string>& a, } void -SipAccountBaseConfig::serialize(YAML::Emitter& out) const +SipAccountBaseConfig::serializeDiff(YAML::Emitter& out, const SipAccountBaseConfig& DEFAULT_CONFIG) const { - AccountConfig::serialize(out); - out << YAML::Key << Conf::AUDIO_PORT_MAX_KEY << YAML::Value << audioPortRange.second; - out << YAML::Key << Conf::AUDIO_PORT_MIN_KEY << YAML::Value << audioPortRange.first; - out << YAML::Key << Conf::DTMF_TYPE_KEY << YAML::Value << dtmfType; - out << YAML::Key << Conf::INTERFACE_KEY << YAML::Value << interface; - out << YAML::Key << Conf::PUBLISH_ADDR_KEY << YAML::Value << publishedIp; - //out << YAML::Key << Conf::PUBLISH_PORT_KEY << YAML::Value << publishedPort; - out << YAML::Key << Conf::SAME_AS_LOCAL_KEY << YAML::Value << publishedSameasLocal; - - out << YAML::Key << Conf::VIDEO_PORT_MAX_KEY << YAML::Value << videoPortRange.second; - out << YAML::Key << Conf::VIDEO_PORT_MIN_KEY << YAML::Value << videoPortRange.first; - - out << YAML::Key << Conf::TURN_ENABLED_KEY << YAML::Value << turnEnabled; - out << YAML::Key << Conf::TURN_SERVER_KEY << YAML::Value << turnServer; - out << YAML::Key << Conf::TURN_SERVER_UNAME_KEY << YAML::Value << turnServerUserName; - out << YAML::Key << Conf::TURN_SERVER_PWD_KEY << YAML::Value << turnServerPwd; - out << YAML::Key << Conf::TURN_SERVER_REALM_KEY << YAML::Value << turnServerRealm; - - /*out << YAML::Key << Conf::CALIST_KEY << YAML::Value << tlsCaListFile; - out << YAML::Key << Conf::CERTIFICATE_KEY << YAML::Value << tlsCertificateFile; - out << YAML::Key << Conf::TLS_PASSWORD_KEY << YAML::Value << tlsPassword; - out << YAML::Key << Conf::PRIVATE_KEY_KEY << YAML::Value << tlsPrivateKeyFile;*/ + AccountConfig::serializeDiff(out, DEFAULT_CONFIG); + SERIALIZE_CONFIG(Conf::DTMF_TYPE_KEY, dtmfType); + SERIALIZE_CONFIG(Conf::INTERFACE_KEY, interface); + SERIALIZE_CONFIG(Conf::PUBLISH_ADDR_KEY, publishedIp); + SERIALIZE_CONFIG(Conf::SAME_AS_LOCAL_KEY, publishedSameasLocal); + SERIALIZE_CONFIG(Conf::AUDIO_PORT_MAX_KEY, audioPortRange.second); + SERIALIZE_CONFIG(Conf::AUDIO_PORT_MAX_KEY, audioPortRange.first); + SERIALIZE_CONFIG(Conf::VIDEO_PORT_MAX_KEY, videoPortRange.second); + SERIALIZE_CONFIG(Conf::VIDEO_PORT_MIN_KEY, videoPortRange.first); + SERIALIZE_CONFIG(Conf::TURN_ENABLED_KEY, turnEnabled); + SERIALIZE_CONFIG(Conf::TURN_SERVER_KEY, turnServer); + SERIALIZE_CONFIG(Conf::TURN_SERVER_UNAME_KEY, turnServerUserName); + SERIALIZE_CONFIG(Conf::TURN_SERVER_PWD_KEY, turnServerPwd); + SERIALIZE_CONFIG(Conf::TURN_SERVER_REALM_KEY, turnServerRealm); } - void SipAccountBaseConfig::unserialize(const YAML::Node& node) { diff --git a/src/sip/sipaccountbase_config.h b/src/sip/sipaccountbase_config.h index 2a60efa4e1c1cc6a8d8a2609c2ae96333b484a7e..a5553e411eefffcfe609fd518cb63e92f9ad5d5c 100644 --- a/src/sip/sipaccountbase_config.h +++ b/src/sip/sipaccountbase_config.h @@ -26,7 +26,7 @@ constexpr static unsigned HALF_MAX_PORT {MAX_PORT / 2}; struct SipAccountBaseConfig: public AccountConfig { SipAccountBaseConfig(const std::string& type, const std::string& id, const std::string& path): AccountConfig(type, id, path) {} - void serialize(YAML::Emitter& out) const override; + void serializeDiff(YAML::Emitter& out, const SipAccountBaseConfig& def) const; void unserialize(const YAML::Node& node) override; std::map<std::string, std::string> toMap() const override;