src/config.rs
139 lines · 3911 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 crate::{MatrixServer, Servers};
19use weechat::config::{Conf, OptionChanged, SectionReadCallback};
20use weechat::Weechat;
21
22use std::cell::{Ref, RefCell, RefMut};
23use std::rc::Rc;
24
25config!(
26 "matrix-rust",
27 Section look {
28 encrypted_room_sign: String {
29 // Description.
30 "A sign that is used to show that the current room is encrypted",
31 // Default value.
32 "🔒",
33 },
34 },
35 Section network {
36 debug_buffer: bool {
37 // Description
38 "Use a separate buffer for debug logs",
39 // Default value.
40 false,
41 }
42 }
43);
44
45/// A wrapper for our config struct that can be cloned around.
46#[derive(Clone)]
47pub struct ConfigHandle {
48 inner: Rc<RefCell<Config>>,
49 servers: Servers,
50}
51
52impl ConfigHandle {
53 /// Create a new config and wrap it in our config handle.
54 pub fn new(servers: &Servers) -> ConfigHandle {
55 let config = Config::new().expect("Can't create new config");
56
57 let config = ConfigHandle {
58 inner: Rc::new(RefCell::new(config)),
59 servers: servers.clone(),
60 };
61
62 // The server section is special since it has a custom section read and
63 // write implementations to support subsections for every configured
64 // server.
65 let server_section_options = ConfigSectionSettings::new("server")
66 .set_write_callback(
67 |_weechat: &Weechat,
68 config: &Conf,
69 section: &mut ConfigSection| {
70 config.write_section(section.name());
71 for option in section.options() {
72 config.write_option(option);
73 }
74 },
75 )
76 .set_read_callback(config.clone());
77
78 {
79 let mut config_borrow = config.borrow_mut();
80
81 config_borrow
82 .new_section(server_section_options)
83 .expect("Can't create server section");
84 }
85
86 config
87 }
88
89 pub fn borrow(&self) -> Ref<'_, Config> {
90 self.inner.borrow()
91 }
92
93 pub fn borrow_mut(&self) -> RefMut<'_, Config> {
94 self.inner.borrow_mut()
95 }
96}
97
98impl SectionReadCallback for ConfigHandle {
99 fn callback(
100 &mut self,
101 _: &Weechat,
102 _: &Conf,
103 section: &mut ConfigSection,
104 option_name: &str,
105 option_value: &str,
106 ) -> OptionChanged {
107 if option_name.is_empty() {
108 return OptionChanged::Error;
109 }
110
111 let option_args: Vec<&str> = option_name.splitn(2, '.').collect();
112
113 if option_args.len() != 2 {
114 return OptionChanged::Error;
115 }
116
117 let server_name = option_args[0];
118
119 {
120 let mut servers_borrow = self.servers.borrow_mut();
121
122 // We are reading the config, if the server doesn't yet exists
123 // we need to create it before setting the option and running
124 // the option change callback.
125 if !servers_borrow.contains_key(server_name) {
126 let server = MatrixServer::new(server_name, &self, section);
127 servers_borrow.insert(server_name.to_owned(), server);
128 }
129 }
130
131 let option = section.search_option(option_name);
132
133 if let Some(o) = option {
134 o.set(option_value, true)
135 } else {
136 OptionChanged::NotFound
137 }
138 }
139}