src/config.rs
275 lines · 7376 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.
17
18use std::{
19 cell::{Ref, RefCell, RefMut},
20 rc::Rc,
21};
22
23use strum_macros::EnumVariantNames;
24use weechat::{
25 config,
26 config::{
27 Conf, ConfigOption, ConfigSection, ConfigSectionSettings,
28 IntegerOptionSettings, OptionChanged, SectionReadCallback,
29 },
30 Weechat,
31};
32
33use crate::{MatrixServer, Servers};
34
35#[derive(EnumVariantNames)]
36#[strum(serialize_all = "kebab_case")]
37pub enum RedactionStyle {
38 StrikeThrough,
39 Delete,
40 Notice,
41}
42
43impl Default for RedactionStyle {
44 fn default() -> Self {
45 RedactionStyle::StrikeThrough
46 }
47}
48
49impl From<i32> for RedactionStyle {
50 fn from(value: i32) -> Self {
51 match value {
52 0 => RedactionStyle::StrikeThrough,
53 1 => RedactionStyle::Delete,
54 2 => RedactionStyle::Notice,
55 _ => unreachable!(),
56 }
57 }
58}
59
60#[derive(EnumVariantNames)]
61#[strum(serialize_all = "kebab_case")]
62pub enum ServerBuffer {
63 MergeWithCore,
64 MergeWithoutCore,
65 Independent,
66}
67
68impl Default for ServerBuffer {
69 fn default() -> Self {
70 ServerBuffer::MergeWithCore
71 }
72}
73
74impl From<i32> for ServerBuffer {
75 fn from(value: i32) -> Self {
76 match value {
77 0 => ServerBuffer::MergeWithCore,
78 1 => ServerBuffer::MergeWithoutCore,
79 2 => ServerBuffer::Independent,
80 _ => unreachable!(),
81 }
82 }
83}
84
85config!(
86 "matrix-rust",
87
88 Section look {
89 encrypted_room_sign: String {
90 // Description.
91 "A sign that is used to show that the current room is encrypted",
92 // Default value.
93 "🔒",
94 },
95
96 public_room_sign: String {
97 // Description.
98 "A sign indicating that the current room is public",
99 // Default value.
100 "🌍",
101 },
102
103 busy_sign: String {
104 // Description.
105 "A sign that is used to show that the client is busy, \
106 e.g. when room history is being fetched",
107 // Default value.
108 "⏳",
109 },
110
111 local_echo: bool {
112 // Description
113 "Should the sending message be printed out before the server \
114 confirms the reception of the message",
115 // Default value
116 true,
117 },
118
119 redaction_style: Enum {
120 // Description
121 "The style that should be used when a message needs to be redacted",
122 RedactionStyle,
123 },
124 },
125
126 Section network {
127 debug_buffer: bool {
128 // Description
129 "Use a separate buffer for debug logs",
130 // Default value.
131 false,
132 },
133 },
134
135 Section input {
136 markdown_input: bool {
137 // Description
138 "Should the input be parsed as markdown",
139 // Default value.
140 true,
141 },
142 }
143);
144
145/// A wrapper for our config struct that can be cloned around.
146#[derive(Clone)]
147pub struct ConfigHandle {
148 pub inner: Rc<RefCell<Config>>,
149 servers: Servers,
150}
151
152impl ConfigHandle {
153 /// Create a new config and wrap it in our config handle.
154 pub fn new(servers: &Servers) -> ConfigHandle {
155 let config = Config::new().expect("Can't create new config");
156
157 let config = ConfigHandle {
158 inner: Rc::new(RefCell::new(config)),
159 servers: servers.clone(),
160 };
161
162 // The server section is special since it has a custom section read and
163 // write implementations to support subsections for every configured
164 // server.
165 let server_section_options = ConfigSectionSettings::new("server")
166 .set_write_callback(
167 |_weechat: &Weechat,
168 config: &Conf,
169 section: &mut ConfigSection| {
170 config.write_section(section.name());
171 for option in section.options() {
172 config.write_option(option);
173 }
174 },
175 )
176 .set_read_callback(config.clone());
177
178 {
179 let mut config_borrow = config.borrow_mut();
180
181 config_borrow
182 .new_section(server_section_options)
183 .expect("Can't create server section");
184
185 let mut look_section = config_borrow.look_mut();
186
187 let servers = servers.clone();
188
189 let settings = IntegerOptionSettings::new("server_buffer")
190 .description("Should the server buffer be merged with other buffers or independent")
191 .set_change_callback(move |_, _| {
192 for server in servers.borrow().values() {
193 server.merge_server_buffers();
194 }
195 })
196 .default_value(ServerBuffer::default() as i32)
197 .string_values(
198 ServerBuffer::VARIANTS
199 .iter()
200 .map(|v| v.to_string())
201 .collect::<Vec<String>>(),
202 );
203
204 look_section
205 .new_integer_option(settings)
206 .expect("Can't create server buffers option");
207 }
208
209 config
210 }
211
212 pub fn borrow(&self) -> Ref<'_, Config> {
213 self.inner.borrow()
214 }
215
216 pub fn borrow_mut(&self) -> RefMut<'_, Config> {
217 self.inner.borrow_mut()
218 }
219}
220
221impl<'a> LookSection<'a> {
222 pub fn server_buffer(&self) -> ServerBuffer {
223 if let ConfigOption::Integer(o) =
224 self.search_option("server_buffer").unwrap()
225 {
226 ServerBuffer::from(o.value())
227 } else {
228 panic!("Server buffer option has the wrong type");
229 }
230 }
231}
232
233impl SectionReadCallback for ConfigHandle {
234 fn callback(
235 &mut self,
236 _: &Weechat,
237 _: &Conf,
238 section: &mut ConfigSection,
239 option_name: &str,
240 option_value: &str,
241 ) -> OptionChanged {
242 if option_name.is_empty() {
243 return OptionChanged::Error;
244 }
245
246 let option_args: Vec<&str> = option_name.splitn(2, '.').collect();
247
248 if option_args.len() != 2 {
249 return OptionChanged::Error;
250 }
251
252 let server_name = option_args[0];
253
254 // We are reading the config, if the server doesn't yet exists
255 // we need to create it before setting the option and running
256 // the option change callback.
257 if !self.servers.contains(server_name) {
258 let server = MatrixServer::new(
259 server_name,
260 &self,
261 section,
262 self.servers.clone(),
263 );
264 self.servers.insert(server);
265 }
266
267 let option = section.search_option(option_name);
268
269 if let Some(o) = option {
270 o.set(option_value, true)
271 } else {
272 OptionChanged::NotFound
273 }
274 }
275}