src/config.rs

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

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