Line data Source code
1 : /*
2 : * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 : #include "account_config.h"
18 : #include "account_const.h"
19 : #include "account_schema.h"
20 : #include "string_utils.h"
21 : #include "fileutils.h"
22 : #include "config/account_config_utils.h"
23 :
24 : #include <fmt/compile.h>
25 : #include <fmt/ranges.h>
26 :
27 : namespace jami {
28 :
29 : constexpr const char* RINGTONE_PATH_KEY = "ringtonePath";
30 : constexpr const char* RINGTONE_ENABLED_KEY = "ringtoneEnabled";
31 : constexpr const char* VIDEO_ENABLED_KEY = "videoEnabled";
32 : constexpr const char* DISPLAY_NAME_KEY = "displayName";
33 : constexpr const char* ALIAS_KEY = "alias";
34 : constexpr const char* TYPE_KEY = "type";
35 : constexpr const char* USERNAME_KEY = "username";
36 : constexpr const char* HOSTNAME_KEY = "hostname";
37 : constexpr const char* ACCOUNT_ENABLE_KEY = "enable";
38 : constexpr const char* ACCOUNT_AUTOANSWER_KEY = "autoAnswer";
39 : constexpr const char* DENY_SECOND_CALL_KEY = "denySecondCall";
40 : constexpr const char* ACCOUNT_READRECEIPT_KEY = "sendReadReceipt";
41 : constexpr const char* ACCOUNT_COMPOSING_KEY = "sendComposing";
42 : constexpr const char* ACCOUNT_ISRENDEZVOUS_KEY = "rendezVous";
43 : constexpr const char* ACCOUNT_ACTIVE_CALL_LIMIT_KEY = "activeCallLimit";
44 : constexpr const char* MAILBOX_KEY = "mailbox";
45 : constexpr const char* USER_AGENT_KEY = "useragent";
46 : constexpr const char* UPNP_ENABLED_KEY = "upnpEnabled";
47 : constexpr const char* ACTIVE_CODEC_KEY = "activeCodecs";
48 : constexpr const char* DEFAULT_MODERATORS_KEY = "defaultModerators";
49 : constexpr const char* LOCAL_MODERATORS_ENABLED_KEY = "localModeratorsEnabled";
50 : constexpr const char* ALL_MODERATORS_ENABLED_KEY = "allModeratorsEnabled";
51 : constexpr const char* PROXY_PUSH_TOKEN_KEY = "proxyPushToken";
52 : constexpr const char* PROXY_PUSH_PLATFORM_KEY = "proxyPushPlatform";
53 : constexpr const char* PROXY_PUSH_TOPIC_KEY = "proxyPushiOSTopic";
54 : constexpr const char* UI_CUSTOMIZATION = "uiCustomization";
55 :
56 : using yaml_utils::parseValueOptional;
57 :
58 : void
59 2867 : AccountConfig::serializeDiff(YAML::Emitter& out, const AccountConfig& DEFAULT_CONFIG) const
60 : {
61 2867 : SERIALIZE_CONFIG(ACCOUNT_ENABLE_KEY, enabled);
62 2867 : SERIALIZE_CONFIG(TYPE_KEY, type);
63 2867 : SERIALIZE_CONFIG(ALIAS_KEY, alias);
64 2867 : SERIALIZE_CONFIG(HOSTNAME_KEY, hostname);
65 2867 : SERIALIZE_CONFIG(USERNAME_KEY, username);
66 2867 : SERIALIZE_CONFIG(MAILBOX_KEY, mailbox);
67 : out << YAML::Key << ACTIVE_CODEC_KEY << YAML::Value
68 11468 : << fmt::format(FMT_COMPILE("{}"), fmt::join(activeCodecs, "/"));
69 2867 : SERIALIZE_CONFIG(ACCOUNT_AUTOANSWER_KEY, autoAnswerEnabled);
70 2867 : SERIALIZE_CONFIG(DENY_SECOND_CALL_KEY, denySecondCallEnabled);
71 2867 : SERIALIZE_CONFIG(ACCOUNT_READRECEIPT_KEY, sendReadReceipt);
72 2867 : SERIALIZE_CONFIG(ACCOUNT_COMPOSING_KEY, sendComposing);
73 2867 : SERIALIZE_CONFIG(ACCOUNT_ISRENDEZVOUS_KEY, isRendezVous);
74 2867 : SERIALIZE_CONFIG(ACCOUNT_ACTIVE_CALL_LIMIT_KEY, activeCallLimit);
75 2867 : SERIALIZE_CONFIG(RINGTONE_ENABLED_KEY, ringtoneEnabled);
76 2867 : SERIALIZE_CONFIG(RINGTONE_PATH_KEY, ringtonePath);
77 2867 : SERIALIZE_CONFIG(USER_AGENT_KEY, customUserAgent);
78 2867 : SERIALIZE_CONFIG(DISPLAY_NAME_KEY, displayName);
79 2867 : SERIALIZE_CONFIG(UPNP_ENABLED_KEY, upnpEnabled);
80 : out << YAML::Key << DEFAULT_MODERATORS_KEY << YAML::Value
81 11468 : << fmt::format(FMT_COMPILE("{}"), fmt::join(defaultModerators, "/"));
82 2867 : SERIALIZE_CONFIG(LOCAL_MODERATORS_ENABLED_KEY, localModeratorsEnabled);
83 2867 : SERIALIZE_CONFIG(ALL_MODERATORS_ENABLED_KEY, allModeratorsEnabled);
84 2867 : SERIALIZE_CONFIG(PROXY_PUSH_TOKEN_KEY, deviceKey);
85 2867 : SERIALIZE_CONFIG(PROXY_PUSH_PLATFORM_KEY, platform);
86 2867 : SERIALIZE_CONFIG(PROXY_PUSH_TOPIC_KEY, notificationTopic);
87 2867 : SERIALIZE_CONFIG(VIDEO_ENABLED_KEY, videoEnabled);
88 2867 : SERIALIZE_CONFIG(UI_CUSTOMIZATION, uiCustomization);
89 2867 : }
90 :
91 : void
92 0 : AccountConfig::unserialize(const YAML::Node& node)
93 : {
94 0 : parseValueOptional(node, ALIAS_KEY, alias);
95 : // parseValueOptional(node, TYPE_KEY, type);
96 0 : parseValueOptional(node, ACCOUNT_ENABLE_KEY, enabled);
97 0 : parseValueOptional(node, HOSTNAME_KEY, hostname);
98 0 : parseValueOptional(node, ACCOUNT_AUTOANSWER_KEY, autoAnswerEnabled);
99 0 : parseValueOptional(node, DENY_SECOND_CALL_KEY, denySecondCallEnabled);
100 0 : parseValueOptional(node, ACCOUNT_READRECEIPT_KEY, sendReadReceipt);
101 0 : parseValueOptional(node, ACCOUNT_COMPOSING_KEY, sendComposing);
102 0 : parseValueOptional(node, ACCOUNT_ISRENDEZVOUS_KEY, isRendezVous);
103 0 : parseValueOptional(node, ACCOUNT_ACTIVE_CALL_LIMIT_KEY, activeCallLimit);
104 0 : parseValueOptional(node, MAILBOX_KEY, mailbox);
105 :
106 0 : std::string codecs;
107 0 : if (parseValueOptional(node, ACTIVE_CODEC_KEY, codecs))
108 0 : activeCodecs = split_string_to_unsigned(codecs, '/');
109 :
110 0 : parseValueOptional(node, DISPLAY_NAME_KEY, displayName);
111 :
112 0 : parseValueOptional(node, USER_AGENT_KEY, customUserAgent);
113 0 : parseValueOptional(node, RINGTONE_PATH_KEY, ringtonePath);
114 0 : parseValueOptional(node, RINGTONE_ENABLED_KEY, ringtoneEnabled);
115 0 : parseValueOptional(node, VIDEO_ENABLED_KEY, videoEnabled);
116 :
117 0 : parseValueOptional(node, UPNP_ENABLED_KEY, upnpEnabled);
118 :
119 0 : std::string defMod;
120 0 : parseValueOptional(node, DEFAULT_MODERATORS_KEY, defMod);
121 0 : defaultModerators = string_split_set(defMod);
122 0 : parseValueOptional(node, LOCAL_MODERATORS_ENABLED_KEY, localModeratorsEnabled);
123 0 : parseValueOptional(node, ALL_MODERATORS_ENABLED_KEY, allModeratorsEnabled);
124 0 : parseValueOptional(node, PROXY_PUSH_TOKEN_KEY, deviceKey);
125 0 : parseValueOptional(node, PROXY_PUSH_PLATFORM_KEY, platform);
126 0 : parseValueOptional(node, PROXY_PUSH_TOPIC_KEY, notificationTopic);
127 0 : parseValueOptional(node, UI_CUSTOMIZATION, uiCustomization);
128 0 : }
129 :
130 : std::map<std::string, std::string>
131 705 : AccountConfig::toMap() const
132 : {
133 705 : return {{Conf::CONFIG_ACCOUNT_ALIAS, alias},
134 705 : {Conf::CONFIG_ACCOUNT_DISPLAYNAME, displayName},
135 705 : {Conf::CONFIG_ACCOUNT_ENABLE, enabled ? TRUE_STR : FALSE_STR},
136 705 : {Conf::CONFIG_ACCOUNT_TYPE, type},
137 705 : {Conf::CONFIG_ACCOUNT_USERNAME, username},
138 705 : {Conf::CONFIG_ACCOUNT_HOSTNAME, hostname},
139 705 : {Conf::CONFIG_ACCOUNT_MAILBOX, mailbox},
140 705 : {Conf::CONFIG_ACCOUNT_USERAGENT, customUserAgent},
141 705 : {Conf::CONFIG_ACCOUNT_AUTOANSWER, autoAnswerEnabled ? TRUE_STR : FALSE_STR},
142 705 : {Conf::CONFIG_ACCOUNT_DENY_SECOND_CALL, denySecondCallEnabled ? TRUE_STR : FALSE_STR},
143 705 : {Conf::CONFIG_ACCOUNT_SENDREADRECEIPT, sendReadReceipt ? TRUE_STR : FALSE_STR},
144 705 : {Conf::CONFIG_ACCOUNT_SENDCOMPOSING, sendComposing ? TRUE_STR : FALSE_STR},
145 705 : {Conf::CONFIG_ACCOUNT_ISRENDEZVOUS, isRendezVous ? TRUE_STR : FALSE_STR},
146 1410 : {libjami::Account::ConfProperties::ACTIVE_CALL_LIMIT, std::to_string(activeCallLimit)},
147 705 : {Conf::CONFIG_RINGTONE_ENABLED, ringtoneEnabled ? TRUE_STR : FALSE_STR},
148 705 : {Conf::CONFIG_RINGTONE_PATH, ringtonePath},
149 705 : {Conf::CONFIG_VIDEO_ENABLED, videoEnabled ? TRUE_STR : FALSE_STR},
150 705 : {Conf::CONFIG_UPNP_ENABLED, upnpEnabled ? TRUE_STR : FALSE_STR},
151 1410 : {Conf::CONFIG_DEFAULT_MODERATORS, string_join(defaultModerators)},
152 705 : {Conf::CONFIG_LOCAL_MODERATORS_ENABLED, localModeratorsEnabled ? TRUE_STR : FALSE_STR},
153 705 : {Conf::CONFIG_ALL_MODERATORS_ENABLED, allModeratorsEnabled ? TRUE_STR : FALSE_STR},
154 26085 : {Conf::CONFIG_ACCOUNT_UICUSTOMIZATION, uiCustomization}};
155 : }
156 :
157 : void
158 1647 : AccountConfig::fromMap(const std::map<std::string, std::string>& details)
159 : {
160 1647 : parseString(details, Conf::CONFIG_ACCOUNT_ALIAS, alias);
161 1647 : parseString(details, Conf::CONFIG_ACCOUNT_DISPLAYNAME, displayName);
162 1647 : parseBool(details, Conf::CONFIG_ACCOUNT_ENABLE, enabled);
163 1647 : parseBool(details, Conf::CONFIG_VIDEO_ENABLED, videoEnabled);
164 1647 : parseString(details, Conf::CONFIG_ACCOUNT_HOSTNAME, hostname);
165 1647 : parseString(details, Conf::CONFIG_ACCOUNT_MAILBOX, mailbox);
166 1647 : parseBool(details, Conf::CONFIG_ACCOUNT_AUTOANSWER, autoAnswerEnabled);
167 1647 : parseBool(details, Conf::CONFIG_ACCOUNT_DENY_SECOND_CALL, denySecondCallEnabled);
168 1647 : parseBool(details, Conf::CONFIG_ACCOUNT_SENDREADRECEIPT, sendReadReceipt);
169 1647 : parseBool(details, Conf::CONFIG_ACCOUNT_SENDCOMPOSING, sendComposing);
170 1647 : parseBool(details, Conf::CONFIG_ACCOUNT_ISRENDEZVOUS, isRendezVous);
171 1647 : parseInt(details, libjami::Account::ConfProperties::ACTIVE_CALL_LIMIT, activeCallLimit);
172 1647 : parseBool(details, Conf::CONFIG_RINGTONE_ENABLED, ringtoneEnabled);
173 1647 : parseString(details, Conf::CONFIG_RINGTONE_PATH, ringtonePath);
174 1647 : parseString(details, Conf::CONFIG_ACCOUNT_USERAGENT, customUserAgent);
175 1647 : parseBool(details, Conf::CONFIG_UPNP_ENABLED, upnpEnabled);
176 1647 : std::string defMod;
177 1647 : parseString(details, Conf::CONFIG_DEFAULT_MODERATORS, defMod);
178 1647 : defaultModerators = string_split_set(defMod);
179 1647 : parseBool(details, Conf::CONFIG_LOCAL_MODERATORS_ENABLED, localModeratorsEnabled);
180 1647 : parseBool(details, Conf::CONFIG_ALL_MODERATORS_ENABLED, allModeratorsEnabled);
181 :
182 1647 : parseString(details, libjami::Account::ConfProperties::PROXY_PUSH_TOKEN, deviceKey);
183 1647 : parseString(details, PROXY_PUSH_PLATFORM_KEY, platform);
184 1647 : parseString(details, PROXY_PUSH_TOPIC_KEY, notificationTopic);
185 :
186 1647 : parseString(details, Conf::CONFIG_ACCOUNT_UICUSTOMIZATION, uiCustomization);
187 1647 : }
188 :
189 : void
190 4941 : parsePath(const std::map<std::string, std::string>& details,
191 : const char* key,
192 : std::string& s,
193 : const std::filesystem::path& base)
194 : {
195 4941 : auto it = details.find(key);
196 4941 : if (it != details.end())
197 2497 : s = fileutils::getFullPath(base, it->second).string();
198 4941 : }
199 :
200 : } // namespace jami
|