src/lib.rs
137 lines · 3396 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 weechat.print(&format!("Autconnecting servers {:?}", servers.keys()));
65
66 for server in servers.values_mut() {
67 server.connect();
68 }
69 }
70
71 fn create_default_server(
72 servers: &mut HashMap<String, MatrixServer>,
73 config: &Config,
74 ) {
75 let weechat = unsafe { Weechat::weechat() };
76 weechat.print("Creating localhost server");
77
78 let server_name = "localhost";
79 let mut config_borrow = config.borrow_mut();
80 let mut section = config_borrow
81 .search_section_mut("server")
82 .expect("Can't get server section");
83 let server = MatrixServer::new(server_name, config, &mut section);
84 servers.insert(server_name.to_owned(), server);
85 }
86}
87
88impl WeechatPlugin for Matrix {
89 fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> {
90 let servers = Servers::new();
91 let commands = Commands::hook_all(weechat, &servers);
92 let config = Config::new(weechat, &servers);
93
94 let matrix = Matrix {
95 servers: servers.clone(),
96 commands,
97 config: config.clone(),
98 };
99
100 spawn_weechat(async move {
101 {
102 let config = config.borrow();
103 config.read().expect("Can't read config");
104 }
105
106 let mut servers = servers.borrow_mut();
107
108 if servers.is_empty() {
109 Matrix::create_default_server(&mut servers, &config)
110 }
111
112 Matrix::autoconnect(&mut servers);
113 });
114
115 Ok(matrix)
116 }
117}
118
119impl Drop for Matrix {
120 fn drop(&mut self) {
121 let mut servers = self.servers.borrow_mut();
122 for server in servers.values_mut() {
123 server.disconnect();
124 }
125
126 cleanup_executor();
127 }
128}
129
130weechat_plugin!(
131 Matrix,
132 name: "matrix",
133 author: "Damir Jelić <poljar@termina.org.uk>",
134 description: "Matrix protocol",
135 version: "0.1.0",
136 license: "ISC"
137);