src/config.rs

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

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