src/lib.rs
88 lines · 2125 bytes
1mod executor;
2mod room_buffer;
3mod server;
4
5use url::Url;
6
7use tokio::runtime::Runtime;
8
9use async_std;
10use async_std::sync::channel as async_channel;
11use std::collections::HashMap;
12
13use matrix_nio::{AsyncClient, AsyncClientConfig};
14use server::MatrixServer;
15
16use weechat::{
17 weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult,
18};
19
20use crate::executor::{cleanup_executor, spawn_weechat};
21
22const PLUGIN_NAME: &str = "matrix";
23
24struct Matrix {
25 tokio: Option<Runtime>,
26 servers: HashMap<String, MatrixServer>,
27}
28
29impl WeechatPlugin for Matrix {
30 fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> {
31 let runtime = Runtime::new().unwrap();
32
33 let homeserver = Url::parse("http://localhost:8008").unwrap();
34
35 let config = AsyncClientConfig::new()
36 .proxy("http://localhost:8080")
37 .unwrap()
38 .disable_ssl_verification();
39 let client =
40 AsyncClient::new_with_config(homeserver.clone(), None, config)
41 .unwrap();
42 let send_client = client.clone();
43
44 let (tx, rx) = async_channel(10);
45
46 let server_name = "localhost";
47
48 let server = MatrixServer::new(server_name, &homeserver, tx);
49 let mut servers = HashMap::new();
50 servers.insert(server_name.to_owned(), server);
51 runtime.spawn(async move {
52 MatrixServer::send_loop(send_client, rx).await;
53 });
54
55 let (tx, rx) = async_channel(1000);
56
57 runtime.spawn(async move {
58 MatrixServer::sync_loop(client, tx).await;
59 });
60
61 spawn_weechat(MatrixServer::sync_receiver(rx));
62
63 Ok(Matrix {
64 tokio: Some(runtime),
65 servers,
66 })
67 }
68}
69
70impl Drop for Matrix {
71 fn drop(&mut self) {
72 let runtime = self.tokio.take();
73
74 if let Some(r) = runtime {
75 r.shutdown_now();
76 }
77 cleanup_executor();
78 }
79}
80
81weechat_plugin!(
82 Matrix,
83 name: "matrix",
84 author: "Damir Jelić <poljar@termina.org.uk>",
85 description: "Matrix protocol",
86 version: "0.1.0",
87 license: "ISC"
88);