src/config.rs
279 lines · 8979 bytes
1//! Configuration module
2//!
3//! This is the global plugin configuration.
4//!
5//! Configuration options should be split out into different sections:
6//!
7//! * network
8//! * look
9//! * color
10//! * server
11//!
12//! The server config options are added in the server.rs file.
13//!
14//! The config options created here will be alive as long as the plugin is
15//! loaded so they don't need to be freed manually. The drop implementation of
16//! the section will do so.
17use crate::{MatrixServer, Servers};
18use weechat::config::{
19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged,
20 SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings,
21};
22use weechat::Weechat;
23
24use std::cell::{Ref, RefCell, RefMut};
25use std::ops::{Deref, DerefMut};
26use std::rc::Rc;
27
28#[derive(Clone)]
29pub struct ConfigHandle {
30 inner: Rc<RefCell<Config>>,
31 servers: Servers,
32}
33
34macro_rules! option {
35 (StringOption, $option_name:ident, $description:literal, $default:literal) => {
36 paste::item! {
37 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
38 let option_name = stringify!($option_name);
39 let option_settings = StringOptionSettings::new(option_name)
40 .description($description)
41 .default_value($default);
42
43 section.new_string_option(option_settings)
44 .expect(&format!("Can't create option {}", option_name));
45 }
46 }
47
48 paste::item! {
49 pub fn [<$option_name>](&self) -> String {
50 let option_name = stringify!($option_name);
51
52 let option = self.0.search_option(option_name)
53 .expect(&format!("Couldn't find option {} in section {}",
54 option_name, self.0.name()));
55
56 if let ConfigOption::String(o) = option {
57 o.value().to_string()
58 } else {
59 panic!("Incorect option type for option {} in section {}",
60 option_name, self.0.name());
61 }
62 }
63 }
64 };
65
66 (EvaluatedStringOption, $option_name:ident, $description:literal, $default:literal) => {
67 paste::item! {
68 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
69 let option_name = stringify!($option_name);
70 let option_settings = StringOptionSettings::new(option_name)
71 .description(&format!("{} (note: the content is evaluated, see /help eval)", $description))
72 .default_value($default);
73
74 section.new_string_option(option_settings)
75 .expect(&format!("Can't create option {}", option_name));
76 }
77 }
78
79 paste::item! {
80 pub fn [<$option_name>](&self) -> String {
81 let option_name = stringify!($option_name);
82
83 let option = self.0.search_option(option_name)
84 .expect(&format!("Couldn't find option {} in section {}",
85 option_name, self.0.name()));
86
87 if let ConfigOption::String(o) = option {
88 Weechat::eval_string_expression(&o.value())
89 .expect(&format!(
90 "Can't evaluate string expression for option {} in section {}",
91 option_name,
92 self.0.name())
93 )
94 } else {
95 panic!("Incorect option type for option {} in section {}",
96 option_name, self.0.name());
97 }
98 }
99 }
100 };
101}
102
103macro_rules! section {
104 ($section:ident { $({$option_type:ident, $option_name:ident, $($option:tt)*}), * }) => {
105 paste::item! {
106 pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>);
107
108 impl<'a> [<$section:camel Section>]<'a> {
109 pub fn create(config: &mut Config) {
110 let section_settings = ConfigSectionSettings::new(stringify!($section));
111
112 let mut $section = config.new_section(section_settings)
113 .expect(&format!("Can't create config section {}", stringify!($section)));
114
115 [<$section:camel Section>]::create_options(&mut $section);
116 }
117
118 pub fn create_options(section: &mut SectionHandleMut) {
119 $(
120 [<$section:camel Section>]::[<create_option_ $option_name>](section);
121 )*
122 }
123
124 $(
125 option!($option_type, $option_name, $($option)*);
126 )*
127 }
128 }
129 }
130}
131
132macro_rules! config {
133 ($(Section $section:ident { $({$($option:tt)*}), * }), *) => {
134 pub struct Config(weechat::config::Config);
135
136 impl Deref for Config {
137 type Target = weechat::config::Config;
138
139 fn deref(&self) -> &Self::Target {
140 &self.0
141 }
142 }
143
144 impl DerefMut for Config {
145 fn deref_mut(&mut self) -> &mut Self::Target {
146 &mut self.0
147 }
148 }
149
150 impl Config {
151 fn new(config: weechat::config::Config) -> Self {
152 let mut config = Config(config);
153 config.create_sections();
154
155 config
156 }
157
158 paste::item! {
159 fn create_sections(&mut self) {
160 $(
161 paste::expr! { [<$section:camel Section>]::create(self) };
162 )*
163 }
164 }
165
166 paste::item! {
167 $(
168 pub fn $section(&self) -> [<$section:camel Section>] {
169 let section_name = stringify!($section);
170 let section = self.0.search_section(section_name)
171 .expect(&format!("Couldn't find section {}", section_name));
172
173 paste::item! { [<$section:camel Section>](section) }
174 }
175 )*
176 }
177 }
178
179 $(
180 section!($section { $({$($option)*}), * });
181 )*
182 }
183}
184
185config!(
186 Section look {
187 {StringOption, encrypted_room_sign,
188 "A sign that is used to show that the current room is encrypted",
189 "🔒"}
190 }
191);
192
193impl ConfigHandle {
194 pub fn new(_weechat: &Weechat, servers: &Servers) -> ConfigHandle {
195 let config = Weechat::config_new("matrix-rust")
196 .expect("Can't create new config");
197
198 let config = Config::new(config);
199
200 let config = ConfigHandle {
201 inner: Rc::new(RefCell::new(config)),
202 servers: servers.clone(),
203 };
204
205 let server_section_options = ConfigSectionSettings::new("server")
206 .set_write_callback(
207 |_weechat: &Weechat,
208 config: &Conf,
209 section: &mut ConfigSection| {
210 config.write_section(section.name());
211 for option in section.options() {
212 config.write_option(option);
213 }
214 },
215 )
216 .set_read_callback(config.clone());
217
218 {
219 let mut config_borrow = config.borrow_mut();
220
221 config_borrow
222 .new_section(server_section_options)
223 .expect("Can't create server section");
224 }
225
226 config
227 }
228
229 pub fn borrow(&self) -> Ref<'_, Config> {
230 self.inner.borrow()
231 }
232
233 pub fn borrow_mut(&self) -> RefMut<'_, Config> {
234 self.inner.borrow_mut()
235 }
236}
237
238impl SectionReadCallback for ConfigHandle {
239 fn callback(
240 &mut self,
241 _: &Weechat,
242 _: &Conf,
243 section: &mut ConfigSection,
244 option_name: &str,
245 option_value: &str,
246 ) -> OptionChanged {
247 if option_name.is_empty() {
248 return OptionChanged::Error;
249 }
250
251 let option_args: Vec<&str> = option_name.splitn(2, '.').collect();
252
253 if option_args.len() != 2 {
254 return OptionChanged::Error;
255 }
256
257 let server_name = option_args[0];
258
259 {
260 let mut servers_borrow = self.servers.borrow_mut();
261
262 // We are reading the config, if the server doesn't yet exists
263 // we need to create it before setting the option and running
264 // the option change callback.
265 if !servers_borrow.contains_key(server_name) {
266 let server = MatrixServer::new(server_name, &self, section);
267 servers_borrow.insert(server_name.to_owned(), server);
268 }
269 }
270
271 let option = section.search_option(option_name);
272
273 if let Some(o) = option {
274 o.set(option_value, true)
275 } else {
276 OptionChanged::NotFound
277 }
278 }
279}