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