src/config.rs

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

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