src/lib.rs

d99044c80d3e75fc165cd081cf4d874e236f6b34
weechat-matrix-rs/src/lib.rs history · blame · raw

133 lines · 3344 bytes

  1#![feature(async_closure)]
  2
  3mod commands;
  4mod config;
  5mod room_buffer;
  6mod server;
  7
  8use std::cell::{Ref, RefCell, RefMut};
  9use std::collections::HashMap;
 10use std::rc::{Rc, Weak};
 11
 12use weechat::{weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin};
 13
 14use crate::commands::Commands;
 15use crate::config::Config;
 16use crate::server::MatrixServer;
 17
 18const PLUGIN_NAME: &str = "matrix";
 19
 20#[derive(Clone)]
 21pub struct Servers(Rc<RefCell<HashMap<String, MatrixServer>>>);
 22
 23#[derive(Clone, Default)]
 24pub struct ServersHandle(Weak<RefCell<HashMap<String, MatrixServer>>>);
 25
 26impl ServersHandle {
 27    fn upgrade(&self) -> Servers {
 28        Servers(self.0.upgrade().expect("Severs are dropped?"))
 29    }
 30}
 31
 32impl Servers {
 33    fn new() -> Self {
 34        Servers(Rc::new(RefCell::new(HashMap::new())))
 35    }
 36
 37    fn borrow(&self) -> Ref<'_, HashMap<String, MatrixServer>> {
 38        self.0.borrow()
 39    }
 40
 41    fn borrow_mut(&self) -> RefMut<'_, HashMap<String, MatrixServer>> {
 42        self.0.borrow_mut()
 43    }
 44
 45    fn clone_weak(&self) -> ServersHandle {
 46        ServersHandle(Rc::downgrade(&self.0))
 47    }
 48}
 49
 50struct Matrix {
 51    servers: Servers,
 52    commands: Commands,
 53    config: Config,
 54}
 55
 56impl Matrix {
 57    fn autoconnect(servers: &mut HashMap<String, MatrixServer>) {
 58        for server in servers.values_mut() {
 59            if server.autoconnect() {
 60                match server.connect() {
 61                    Ok(_) => (),
 62                    Err(e) => Weechat::print(&format!("{:?}", e)),
 63                }
 64            }
 65        }
 66    }
 67
 68    fn create_default_server(
 69        servers: &mut HashMap<String, MatrixServer>,
 70        config: &Config,
 71    ) {
 72        let server_name = "localhost";
 73        let mut config_borrow = config.borrow_mut();
 74        let mut section = config_borrow
 75            .search_section_mut("server")
 76            .expect("Can't get server section");
 77        let server = MatrixServer::new(server_name, config, &mut section);
 78        servers.insert(server_name.to_owned(), server);
 79    }
 80}
 81
 82impl WeechatPlugin for Matrix {
 83    fn init(weechat: &Weechat, _args: ArgsWeechat) -> Result<Self, ()> {
 84        let servers = Servers::new();
 85        let config = Config::new(weechat, &servers);
 86        let commands = Commands::hook_all(weechat, &servers, &config);
 87
 88        let matrix = Matrix {
 89            servers: servers.clone(),
 90            commands,
 91            config: config.clone(),
 92        };
 93
 94        {
 95            let config_borrow = config.borrow();
 96            if config_borrow.read().is_err() {
 97                return Err(());
 98            }
 99        }
100
101        {
102            let mut servers_borrow = servers.borrow_mut();
103            if servers_borrow.is_empty() {
104                Matrix::create_default_server(&mut servers_borrow, &config)
105            }
106        }
107
108        Weechat::spawn(async move {
109            let mut servers = servers.borrow_mut();
110            Matrix::autoconnect(&mut servers);
111        });
112
113        Ok(matrix)
114    }
115}
116
117impl Drop for Matrix {
118    fn drop(&mut self) {
119        let mut servers = self.servers.borrow_mut();
120        for server in servers.values_mut() {
121            server.disconnect();
122        }
123    }
124}
125
126weechat_plugin!(
127    Matrix,
128    name: "matrix",
129    author: "Damir Jelić <poljar@termina.org.uk>",
130    description: "Matrix protocol",
131    version: "0.1.0",
132    license: "ISC"
133);