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