src/config.rs

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

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