src/lib.rs

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

78 lines · 1733 bytes

 1#![feature(async_closure)]
 2
 3mod commands;
 4mod config;
 5mod executor;
 6mod room_buffer;
 7mod server;
 8
 9use std::collections::HashMap;
10use std::time::Duration;
11
12use weechat::{
13    weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult,
14};
15
16use crate::commands::Commands;
17use crate::config::Config;
18use crate::executor::{cleanup_executor, spawn_weechat};
19use crate::server::MatrixServer;
20
21const PLUGIN_NAME: &str = "matrix";
22
23struct Matrix {
24    servers: HashMap<String, MatrixServer>,
25    commands: Commands,
26    config: Config,
27}
28
29impl Matrix {
30    fn autoconnect(&mut self) {
31        for server in self.servers.values_mut() {
32            server.connect();
33        }
34    }
35}
36
37impl WeechatPlugin for Matrix {
38    fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> {
39        let commands = Commands::hook_all(weechat);
40        let config = Config::new(weechat);
41
42        let server_name = "localhost";
43        let server = MatrixServer::new(server_name, &config);
44        let mut servers = HashMap::new();
45        servers.insert(server_name.to_owned(), server);
46
47        spawn_weechat(async move {
48            async_std::task::sleep(Duration::from_secs(1)).await;
49            let matrix = plugin();
50            matrix.autoconnect();
51        });
52
53        Ok(Matrix {
54            servers,
55            commands,
56            config,
57        })
58    }
59}
60
61impl Drop for Matrix {
62    fn drop(&mut self) {
63        for server in self.servers.values_mut() {
64            server.disconnect();
65        }
66
67        cleanup_executor();
68    }
69}
70
71weechat_plugin!(
72    Matrix,
73    name: "matrix",
74    author: "Damir Jelić <poljar@termina.org.uk>",
75    description: "Matrix protocol",
76    version: "0.1.0",
77    license: "ISC"
78);