src/lib.rs
137 lines · 3438 bytes
1#![feature(async_closure)]
2
3mod commands;
4mod config;
5mod room_buffer;
6mod server;
7
8use std::cell::{Ref, RefCell, RefMut};
9use std::collections::HashMap;
10use std::rc::{Rc, Weak};
11
12use weechat::{
13 weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult,
14};
15
16use crate::commands::Commands;
17use crate::config::Config;
18use crate::server::MatrixServer;
19
20const PLUGIN_NAME: &str = "matrix";
21
22#[derive(Clone)]
23pub struct Servers(Rc<RefCell<HashMap<String, MatrixServer>>>);
24
25#[derive(Clone, Default)]
26pub struct ServersHandle(Weak<RefCell<HashMap<String, MatrixServer>>>);
27
28impl ServersHandle {
29 fn upgrade(&self) -> Servers {
30 Servers(self.0.upgrade().expect("Severs are dropped?"))
31 }
32}
33
34impl Servers {
35 fn new() -> Self {
36 Servers(Rc::new(RefCell::new(HashMap::new())))
37 }
38
39 fn borrow(&self) -> Ref<'_, HashMap<String, MatrixServer>> {
40 self.0.borrow()
41 }
42
43 fn borrow_mut(&self) -> RefMut<'_, HashMap<String, MatrixServer>> {
44 self.0.borrow_mut()
45 }
46
47 fn clone_weak(&self) -> ServersHandle {
48 ServersHandle(Rc::downgrade(&self.0))
49 }
50}
51
52struct Matrix {
53 servers: Servers,
54 commands: Commands,
55 config: Config,
56}
57
58impl Matrix {
59 fn autoconnect(servers: &mut HashMap<String, MatrixServer>) {
60 let weechat = unsafe { Weechat::weechat() };
61
62 for server in servers.values_mut() {
63 if server.autoconnect() {
64 match server.connect() {
65 Ok(_) => (),
66 Err(e) => weechat.print(&format!("{:?}", e)),
67 }
68 }
69 }
70 }
71
72 fn create_default_server(
73 servers: &mut HashMap<String, MatrixServer>,
74 config: &Config,
75 ) {
76 let server_name = "localhost";
77 let mut config_borrow = config.borrow_mut();
78 let mut section = config_borrow
79 .search_section_mut("server")
80 .expect("Can't get server section");
81 let server = MatrixServer::new(server_name, config, &mut section);
82 servers.insert(server_name.to_owned(), server);
83 }
84}
85
86impl WeechatPlugin for Matrix {
87 fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> {
88 let servers = Servers::new();
89 let config = Config::new(weechat, &servers);
90 let commands = Commands::hook_all(weechat, &servers, &config);
91
92 let matrix = Matrix {
93 servers: servers.clone(),
94 commands,
95 config: config.clone(),
96 };
97
98 {
99 let config_borrow = config.borrow();
100 if config_borrow.read().is_err() {
101 return Err(weechat::Error(-1));
102 }
103 }
104
105 {
106 let mut servers_borrow = servers.borrow_mut();
107 if servers_borrow.is_empty() {
108 Matrix::create_default_server(&mut servers_borrow, &config)
109 }
110 }
111
112 Weechat::spawn(async move {
113 let mut servers = servers.borrow_mut();
114 Matrix::autoconnect(&mut servers);
115 });
116
117 Ok(matrix)
118 }
119}
120
121impl Drop for Matrix {
122 fn drop(&mut self) {
123 let mut servers = self.servers.borrow_mut();
124 for server in servers.values_mut() {
125 server.disconnect();
126 }
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);