src/config.rs

ada9f8dba85462219a27d18f678d97e57bac8f86
weechat-matrix-rs/src/config.rs history · blame · raw

188 lines · 4967 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::EnumVariantNames;
 24use weechat::{
 25    config,
 26    config::{
 27        Conf, ConfigSection, ConfigSectionSettings, OptionChanged,
 28        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
 60config!(
 61    "matrix-rust",
 62    Section look {
 63        encrypted_room_sign: String {
 64            // Description.
 65            "A sign that is used to show that the current room is encrypted",
 66            // Default value.
 67            "🔒",
 68        },
 69
 70        local_echo: bool {
 71            // Description
 72            "Should the sending message be printed out before the server \
 73             confirms the reception of the message",
 74             // Default value
 75             true,
 76        },
 77
 78        redaction_style: Enum {
 79            // Description
 80            "The style that should be used when a message needs to be redacted",
 81            RedactionStyle,
 82        },
 83    },
 84    Section network {
 85        debug_buffer: bool {
 86            // Description
 87            "Use a separate buffer for debug logs",
 88            // Default value.
 89            false,
 90        },
 91    }
 92);
 93
 94/// A wrapper for our config struct that can be cloned around.
 95#[derive(Clone)]
 96pub struct ConfigHandle {
 97    pub inner: Rc<RefCell<Config>>,
 98    servers: Servers,
 99}
100
101impl ConfigHandle {
102    /// Create a new config and wrap it in our config handle.
103    pub fn new(servers: &Servers) -> ConfigHandle {
104        let config = Config::new().expect("Can't create new config");
105
106        let config = ConfigHandle {
107            inner: Rc::new(RefCell::new(config)),
108            servers: servers.clone(),
109        };
110
111        // The server section is special since it has a custom section read and
112        // write implementations to support subsections for every configured
113        // server.
114        let server_section_options = ConfigSectionSettings::new("server")
115            .set_write_callback(
116                |_weechat: &Weechat,
117                 config: &Conf,
118                 section: &mut ConfigSection| {
119                    config.write_section(section.name());
120                    for option in section.options() {
121                        config.write_option(option);
122                    }
123                },
124            )
125            .set_read_callback(config.clone());
126
127        {
128            let mut config_borrow = config.borrow_mut();
129
130            config_borrow
131                .new_section(server_section_options)
132                .expect("Can't create server section");
133        }
134
135        config
136    }
137
138    pub fn borrow(&self) -> Ref<'_, Config> {
139        self.inner.borrow()
140    }
141
142    pub fn borrow_mut(&self) -> RefMut<'_, Config> {
143        self.inner.borrow_mut()
144    }
145}
146
147impl SectionReadCallback for ConfigHandle {
148    fn callback(
149        &mut self,
150        _: &Weechat,
151        _: &Conf,
152        section: &mut ConfigSection,
153        option_name: &str,
154        option_value: &str,
155    ) -> OptionChanged {
156        if option_name.is_empty() {
157            return OptionChanged::Error;
158        }
159
160        let option_args: Vec<&str> = option_name.splitn(2, '.').collect();
161
162        if option_args.len() != 2 {
163            return OptionChanged::Error;
164        }
165
166        let server_name = option_args[0];
167
168        {
169            let mut servers_borrow = self.servers.borrow_mut();
170
171            // We are reading the config, if the server doesn't yet exists
172            // we need to create it before setting the option and running
173            // the option change callback.
174            if !servers_borrow.contains_key(server_name) {
175                let server = MatrixServer::new(server_name, &self, section);
176                servers_borrow.insert(server_name.to_owned(), server);
177            }
178        }
179
180        let option = section.search_option(option_name);
181
182        if let Some(o) = option {
183            o.set(option_value, true)
184        } else {
185            OptionChanged::NotFound
186        }
187    }
188}