src/config.rs

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

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