Commit e26032e9a7
Unsigned
Layout: unified · split
src/config.rs +16 −62
| @@ -16,85 +16,36 @@ | ||
| 16 | 16 | //! the section will do so. |
| 17 | 17 | use crate::{MatrixServer, Servers}; |
| 18 | 18 | use weechat::config::{ |
| 19 | BooleanOptionSettings, Conf, ConfigOption, ConfigSection, | |
| 20 | ConfigSectionSettings, IntegerOptionSettings, OptionChanged, SectionHandle, | |
| 21 | SectionHandleMut, SectionReadCallback, StringOptionSettings, ColorOptionSettings, | |
| 19 | Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged, | |
| 20 | SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings, | |
| 22 | 21 | }; |
| 23 | 22 | use weechat::Weechat; |
| 24 | 23 | |
| 25 | use strum::EnumVariantNames; | |
| 26 | use strum::VariantNames; | |
| 27 | ||
| 28 | 24 | use std::cell::{Ref, RefCell, RefMut}; |
| 29 | 25 | use std::ops::{Deref, DerefMut}; |
| 30 | 26 | use std::rc::Rc; |
| 31 | 27 | |
| 32 | #[derive(Clone)] | |
| 33 | pub struct ConfigHandle { | |
| 34 | inner: Rc<RefCell<Config>>, | |
| 35 | servers: Servers, | |
| 36 | } | |
| 37 | ||
| 38 | #[derive(EnumVariantNames)] | |
| 39 | #[strum(serialize_all = "kebab_case")] | |
| 40 | pub enum Test { | |
| 41 | First, | |
| 42 | Second, | |
| 43 | } | |
| 44 | ||
| 45 | impl Default for Test { | |
| 46 | fn default() -> Self { | |
| 47 | Test::First | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | impl From<i32> for Test { | |
| 52 | fn from(value: i32) -> Self { | |
| 53 | match value { | |
| 54 | 0 => Test::First, | |
| 55 | 1 => Test::Second, | |
| 56 | _ => unreachable!(), | |
| 57 | } | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | 28 | config!( |
| 62 | 29 | Section look { |
| 63 | 30 | encrypted_room_sign: String { |
| 31 | // Description. | |
| 64 | 32 | "A sign that is used to show that the current room is encrypted", |
| 33 | // Default value. | |
| 65 | 34 | "🔒", |
| 66 | 35 | }, |
| 67 | ||
| 68 | what: EvaluatedString { | |
| 69 | "test", | |
| 70 | "test2", | |
| 71 | }, | |
| 72 | ||
| 73 | int_test: Integer { | |
| 74 | "test", | |
| 75 | 5, | |
| 76 | 0..10, | |
| 77 | }, | |
| 78 | ||
| 79 | enum_test: Enum { | |
| 80 | "test", | |
| 81 | Test, | |
| 82 | }, | |
| 83 | ||
| 84 | bool_test: bool { | |
| 85 | "description", | |
| 86 | false, | |
| 87 | }, | |
| 88 | ||
| 89 | color_test: Color { | |
| 90 | "color", | |
| 91 | "darkgray", | |
| 92 | }, | |
| 93 | 36 | }, |
| 94 | 37 | ); |
| 95 | 38 | |
| 39 | /// A wrapper for our config struct that can be cloned around. | |
| 40 | #[derive(Clone)] | |
| 41 | pub struct ConfigHandle { | |
| 42 | inner: Rc<RefCell<Config>>, | |
| 43 | servers: Servers, | |
| 44 | } | |
| 45 | ||
| 96 | 46 | impl ConfigHandle { |
| 97 | pub fn new(_weechat: &Weechat, servers: &Servers) -> ConfigHandle { | |
| 47 | /// Create a new config and wrap it in our config handle. | |
| 48 | pub fn new(servers: &Servers) -> ConfigHandle { | |
| 98 | 49 | let config = Weechat::config_new("matrix-rust") |
| 99 | 50 | .expect("Can't create new config"); |
| 100 | 51 | |
| @@ -105,6 +56,9 @@ impl ConfigHandle { | ||
| 105 | 56 | servers: servers.clone(), |
| 106 | 57 | }; |
| 107 | 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. | |
| 108 | 62 | let server_section_options = ConfigSectionSettings::new("server") |
| 109 | 63 | .set_write_callback( |
| 110 | 64 | |_weechat: &Weechat, |
src/config_macros.rs +114 −9
| @@ -1,12 +1,4 @@ | ||
| 1 | use weechat::config::{ | |
| 2 | BooleanOptionSettings, Conf, ConfigOption, ConfigSection, | |
| 3 | ConfigSectionSettings, IntegerOptionSettings, OptionChanged, SectionHandle, | |
| 4 | SectionHandleMut, SectionReadCallback, StringOptionSettings, ColorOptionSettings, | |
| 5 | }; | |
| 6 | use weechat::Weechat; | |
| 7 | ||
| 8 | use strum::VariantNames; | |
| 9 | ||
| 1 | #[allow(unused_macros)] | |
| 10 | 2 | macro_rules! string_create { |
| 11 | 3 | ($option_name:ident, $description:literal, $default:literal) => { |
| 12 | 4 | paste::item! { |
| @@ -23,6 +15,7 @@ macro_rules! string_create { | ||
| 23 | 15 | }; |
| 24 | 16 | } |
| 25 | 17 | |
| 18 | #[allow(unused_macros)] | |
| 26 | 19 | macro_rules! color_create { |
| 27 | 20 | ($option_name:ident, $description:literal, $default:literal) => { |
| 28 | 21 | paste::item! { |
| @@ -39,6 +32,7 @@ macro_rules! color_create { | ||
| 39 | 32 | }; |
| 40 | 33 | } |
| 41 | 34 | |
| 35 | #[allow(unused_macros)] | |
| 42 | 36 | macro_rules! bool_create { |
| 43 | 37 | ($option_name:ident, $description:literal, $default:literal) => { |
| 44 | 38 | paste::item! { |
| @@ -55,6 +49,7 @@ macro_rules! bool_create { | ||
| 55 | 49 | }; |
| 56 | 50 | } |
| 57 | 51 | |
| 52 | #[allow(unused_macros)] | |
| 58 | 53 | macro_rules! integer_create { |
| 59 | 54 | ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => { |
| 60 | 55 | paste::item! { |
| @@ -74,6 +69,7 @@ macro_rules! integer_create { | ||
| 74 | 69 | }; |
| 75 | 70 | } |
| 76 | 71 | |
| 72 | #[allow(unused_macros)] | |
| 77 | 73 | macro_rules! enum_create { |
| 78 | 74 | ($option_name:ident, $description:literal, $out_type:ty) => { |
| 79 | 75 | paste::item! { |
| @@ -201,8 +197,117 @@ macro_rules! section { | ||
| 201 | 197 | } |
| 202 | 198 | } |
| 203 | 199 | |
| 200 | /// Declare a Weechat configuration file. | |
| 201 | /// | |
| 202 | /// This will generate a struct called `Config` which wraps the Weechat struct | |
| 203 | /// of the same name. The generated struct will have accessors for every | |
| 204 | /// section and option that is declared. | |
| 205 | /// | |
| 206 | /// The generated struct dereferences into the Weechat `Config` struct so | |
| 207 | /// additional sections and options can be created the usual way as well. | |
| 208 | /// | |
| 209 | /// The config still needs to be created in the `init()` method of the plugin. | |
| 210 | /// | |
| 211 | /// # Example | |
| 212 | /// ``` | |
| 213 | /// | |
| 214 | /// #[derive(EnumVariantNames)] | |
| 215 | /// #[strum(serialize_all = "kebab_case")] | |
| 216 | /// pub enum ServerBufferMerge { | |
| 217 | /// MergeWithCore, | |
| 218 | /// MergeWithoutCore, | |
| 219 | /// Independent, | |
| 220 | /// } | |
| 221 | /// | |
| 222 | /// impl Default for ServerBufferMerge { | |
| 223 | /// fn default() -> Self { | |
| 224 | /// ServerBufferMerge::MergeWithCore | |
| 225 | /// } | |
| 226 | /// } | |
| 227 | /// | |
| 228 | /// impl From<i32> for ServerBufferMerge { | |
| 229 | /// fn from(value: i32) -> Self { | |
| 230 | /// match value { | |
| 231 | /// 0 => ServerBufferMerge::MergeWithCore, | |
| 232 | /// 1 => ServerBufferMerge::MergeWithoutCore, | |
| 233 | /// 2 => ServerBufferMerge::Independent, | |
| 234 | /// _ => unreachable!(), | |
| 235 | /// } | |
| 236 | /// } | |
| 237 | /// } | |
| 238 | /// | |
| 239 | /// config!( | |
| 240 | /// Section look { | |
| 241 | /// encrypted_room_sign: String { | |
| 242 | /// // Description. | |
| 243 | /// "A sign that is used to show that the current room is encrypted", | |
| 244 | /// | |
| 245 | /// // Default value. | |
| 246 | /// "🔒", | |
| 247 | /// }, | |
| 248 | /// | |
| 249 | /// server_buffer: Enum { | |
| 250 | /// // Description. | |
| 251 | /// "Merge server buffers", | |
| 252 | /// | |
| 253 | /// // This is an enum that needs to have the following traits | |
| 254 | /// // implemented: | |
| 255 | /// // * Default - To define the default value of the option. | |
| 256 | /// // * From<i32> - To convert the internal Weechat integer option | |
| 257 | /// // to the enum. | |
| 258 | /// // * VariantNames - To get the string representation of the | |
| 259 | /// // enum variants. This is a trait defined in the strum library, | |
| 260 | /// // a simple macro that derives an implementation is provided by | |
| 261 | /// // strum. | |
| 262 | /// ServerBufferMerge, | |
| 263 | /// }, | |
| 264 | /// | |
| 265 | /// quote_fg: Color { | |
| 266 | /// // Description. | |
| 267 | /// "Foreground color for Matrix style blockquotes", | |
| 268 | /// | |
| 269 | /// // Default value. | |
| 270 | /// "lightgreen", | |
| 271 | /// }, | |
| 272 | /// }, | |
| 273 | /// | |
| 274 | /// Section network { | |
| 275 | /// username: EvaluatedString { | |
| 276 | /// // Description. | |
| 277 | /// "The username that will be used to log in to the server \ | |
| 278 | /// (note: content is evaluated, see /help eval)", | |
| 279 | /// | |
| 280 | /// // Default value. | |
| 281 | /// "", | |
| 282 | /// }, | |
| 283 | /// | |
| 284 | /// timeout: Integer { | |
| 285 | /// // Description. | |
| 286 | /// "A timeout (in seconds) that determines how long we should wait \ | |
| 287 | /// for a request to finish before aborting.", | |
| 288 | /// | |
| 289 | /// // Default value. | |
| 290 | /// 30, | |
| 291 | /// | |
| 292 | /// // The range that the value is allowed to have, note that both of | |
| 293 | /// // those are inclusive. | |
| 294 | /// 0..100, | |
| 295 | /// }, | |
| 296 | /// | |
| 297 | /// autoconnect: bool { | |
| 298 | /// // Description. | |
| 299 | /// "Automatically connect to the server when Weechat is starting", | |
| 300 | /// | |
| 301 | /// // Default value. | |
| 302 | /// false, | |
| 303 | /// }, | |
| 304 | /// } | |
| 305 | /// ); | |
| 306 | /// ``` | |
| 204 | 307 | macro_rules! config { |
| 205 | 308 | ($(Section $section:ident { $($option:tt)* }), * $(,)?) => { |
| 309 | #[allow(unused_imports)] | |
| 310 | use strum::VariantNames; | |
| 206 | 311 | pub struct Config(weechat::config::Config); |
| 207 | 312 | |
| 208 | 313 | impl Deref for Config { |
src/lib.rs +26 −10
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | #![feature(async_closure)] |
| 2 | 2 | |
| 3 | #[macro_use] mod config_macros; | |
| 3 | #[macro_use] | |
| 4 | mod config_macros; | |
| 4 | 5 | |
| 5 | 6 | mod commands; |
| 6 | 7 | mod config; |
| @@ -68,11 +69,14 @@ impl Matrix { | ||
| 68 | 69 | servers: &mut HashMap<String, MatrixServer>, |
| 69 | 70 | config: &ConfigHandle, |
| 70 | 71 | ) { |
| 72 | // TODO change this to matrix.org. | |
| 71 | 73 | let server_name = "localhost"; |
| 74 | ||
| 72 | 75 | let mut config_borrow = config.borrow_mut(); |
| 73 | 76 | let mut section = config_borrow |
| 74 | 77 | .search_section_mut("server") |
| 75 | 78 | .expect("Can't get server section"); |
| 79 | ||
| 76 | 80 | let server = MatrixServer::new(server_name, config, &mut section); |
| 77 | 81 | servers.insert(server_name.to_owned(), server); |
| 78 | 82 | } |
| @@ -84,8 +88,10 @@ impl BarItemCallback for Servers { | ||
| 84 | 88 | |
| 85 | 89 | for server in servers.values() { |
| 86 | 90 | let server = server.inner(); |
| 91 | ||
| 87 | 92 | for room in server.room_buffers().values() { |
| 88 | 93 | let room_buffer = room.weechat_buffer(); |
| 94 | ||
| 89 | 95 | if &room_buffer == buffer { |
| 90 | 96 | if room.room().is_encrypted() { |
| 91 | 97 | return server.config().look().encrypted_room_sign(); |
| @@ -101,20 +107,16 @@ impl BarItemCallback for Servers { | ||
| 101 | 107 | impl WeechatPlugin for Matrix { |
| 102 | 108 | fn init(weechat: &Weechat, _args: ArgsWeechat) -> Result<Self, ()> { |
| 103 | 109 | let servers = Servers::new(); |
| 104 | let config = ConfigHandle::new(weechat, &servers); | |
| 110 | let config = ConfigHandle::new(&servers); | |
| 105 | 111 | let commands = Commands::hook_all(weechat, &servers, &config); |
| 112 | ||
| 113 | // TODO move the bar creation into a separate file. | |
| 106 | 114 | let status_bar = |
| 107 | 115 | Weechat::new_bar_item("matrix_modes", servers.clone())?; |
| 108 | 116 | |
| 117 | // TODO make this configurable. | |
| 109 | 118 | tracing_subscriber::fmt().with_writer(debug::Debug).init(); |
| 110 | 119 | |
| 111 | let matrix = Matrix { | |
| 112 | servers: servers.clone(), | |
| 113 | commands, | |
| 114 | config: config.clone(), | |
| 115 | status_bar, | |
| 116 | }; | |
| 117 | ||
| 118 | 120 | { |
| 119 | 121 | let config_borrow = config.borrow(); |
| 120 | 122 | if config_borrow.read().is_err() { |
| @@ -129,18 +131,32 @@ impl WeechatPlugin for Matrix { | ||
| 129 | 131 | } |
| 130 | 132 | } |
| 131 | 133 | |
| 134 | let plugin = Matrix { | |
| 135 | servers: servers.clone(), | |
| 136 | commands, | |
| 137 | config: config.clone(), | |
| 138 | status_bar, | |
| 139 | }; | |
| 140 | ||
| 132 | 141 | Weechat::spawn(async move { |
| 133 | 142 | let mut servers = servers.borrow_mut(); |
| 134 | 143 | Matrix::autoconnect(&mut servers); |
| 135 | 144 | }); |
| 136 | 145 | |
| 137 | Ok(matrix) | |
| 146 | Ok(plugin) | |
| 138 | 147 | } |
| 139 | 148 | } |
| 140 | 149 | |
| 141 | 150 | impl Drop for Matrix { |
| 142 | 151 | fn drop(&mut self) { |
| 143 | 152 | let mut servers = self.servers.borrow_mut(); |
| 153 | ||
| 154 | // Buffer close callbacks get called after this, so disconnect here so | |
| 155 | // we don't leave all our rooms. | |
| 156 | // | |
| 157 | // TODO set a flag on the server as well so we don't even try to leave | |
| 158 | // the rooms, once leaving the rooms is implemented when the buffer gets | |
| 159 | // closed. | |
| 144 | 160 | for server in servers.values_mut() { |
| 145 | 161 | server.disconnect(); |
| 146 | 162 | } |