src/lib.rs

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

135 lines · 3331 bytes

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