| @@ -1,6 +1,8 @@ |
| 1 | use async_std::sync::channel as async_channel; |
| 1 | 2 | use async_std::sync::{Receiver, Sender}; |
| 2 | 3 | use std::collections::HashMap; |
| 3 | 4 | use std::time::Duration; |
| 5 | use tokio::runtime::Runtime; |
| 4 | 6 | use url::Url; |
| 5 | 7 | |
| 6 | 8 | use matrix_nio::api::r0::session::login::Response as LoginResponse; |
| @@ -11,10 +13,12 @@ use matrix_nio::{ |
| 11 | 13 | collections::all::{RoomEvent, StateEvent}, |
| 12 | 14 | room::message::{MessageEventContent, TextMessageEventContent}, |
| 13 | 15 | }, |
| 14 | | AsyncClient, SyncSettings, |
| 16 | AsyncClient, AsyncClientConfig, SyncSettings, |
| 15 | 17 | }; |
| 18 | |
| 16 | 19 | use weechat::Weechat; |
| 17 | 20 | |
| 21 | use crate::executor::spawn_weechat; |
| 18 | 22 | use crate::plugin; |
| 19 | 23 | use crate::room_buffer::RoomBuffer; |
| 20 | 24 | |
| @@ -24,52 +28,87 @@ pub enum ThreadMessage { |
| 24 | 28 | SyncEvent(String, RoomEvent), |
| 25 | 29 | } |
| 26 | 30 | |
| 31 | #[derive(Debug)] |
| 32 | pub enum ServerError { |
| 33 | StartError(String), |
| 34 | } |
| 35 | |
| 27 | 36 | pub enum ServerMessage { |
| 28 | 37 | ShutDown, |
| 29 | 38 | RoomSend(String, String), |
| 30 | 39 | } |
| 31 | 40 | |
| 32 | | pub(crate) struct ServerConfig {} |
| 33 | | |
| 34 | 41 | #[derive(Default)] |
| 35 | | pub(crate) struct ServerUser { |
| 42 | pub struct ServerConfig { |
| 43 | homeserver: Option<Url>, |
| 44 | } |
| 45 | |
| 46 | pub struct LoginState { |
| 36 | 47 | user_id: String, |
| 37 | 48 | device_id: String, |
| 38 | 49 | } |
| 39 | 50 | |
| 51 | struct ConnectedState { |
| 52 | client_channel: Sender<ServerMessage>, |
| 53 | homeserver: Url, |
| 54 | } |
| 55 | |
| 40 | 56 | pub(crate) struct MatrixServer { |
| 41 | 57 | server_name: String, |
| 42 | 58 | connected: bool, |
| 43 | | homeserver: Url, |
| 44 | 59 | room_buffers: HashMap<String, RoomBuffer>, |
| 45 | 60 | config: ServerConfig, |
| 46 | | server_user: Option<ServerUser>, |
| 47 | | client_channel: Sender<ServerMessage>, |
| 61 | login_state: Option<LoginState>, |
| 62 | connected_state: Option<ConnectedState>, |
| 48 | 63 | } |
| 49 | 64 | |
| 50 | 65 | impl MatrixServer { |
| 51 | | pub fn new( |
| 52 | | name: &str, |
| 53 | | homeserver: &Url, |
| 54 | | channel: Sender<ServerMessage>, |
| 55 | | ) -> Self { |
| 66 | pub fn new(name: &str) -> Self { |
| 56 | 67 | MatrixServer { |
| 57 | 68 | server_name: name.to_owned(), |
| 58 | 69 | connected: false, |
| 59 | | homeserver: homeserver.clone(), |
| 60 | 70 | room_buffers: HashMap::new(), |
| 61 | | config: ServerConfig {}, |
| 62 | | server_user: None, |
| 63 | | client_channel: channel, |
| 71 | config: ServerConfig { homeserver: None }, |
| 72 | login_state: None, |
| 73 | connected_state: None, |
| 64 | 74 | } |
| 65 | 75 | } |
| 66 | 76 | |
| 77 | pub fn name(&self) -> &str { |
| 78 | &self.server_name |
| 79 | } |
| 80 | |
| 81 | pub fn connect(&mut self, runtime: &Runtime) { |
| 82 | let homeserver = Url::parse("http://localhost:8008").unwrap(); |
| 83 | |
| 84 | let config = AsyncClientConfig::new() |
| 85 | .proxy("http://localhost:8080") |
| 86 | .unwrap() |
| 87 | .disable_ssl_verification(); |
| 88 | |
| 89 | let client = |
| 90 | AsyncClient::new_with_config(homeserver.clone(), None, config) |
| 91 | .unwrap(); |
| 92 | let send_client = client.clone(); |
| 93 | |
| 94 | let (tx, rx) = async_channel(1000); |
| 95 | runtime.spawn(MatrixServer::sync_loop(client, tx)); |
| 96 | spawn_weechat(MatrixServer::sync_receiver(rx)); |
| 97 | |
| 98 | let (client_sender, client_receiver) = async_channel(10); |
| 99 | |
| 100 | runtime.spawn(MatrixServer::send_loop(send_client, client_receiver)); |
| 101 | |
| 102 | self.connected_state = Some(ConnectedState { |
| 103 | client_channel: client_sender, |
| 104 | homeserver, |
| 105 | }); |
| 106 | } |
| 107 | |
| 67 | 108 | pub async fn sync_loop( |
| 68 | 109 | mut client: AsyncClient, |
| 69 | 110 | channel: Sender<Result<ThreadMessage, String>>, |
| 70 | 111 | ) { |
| 71 | | let sender_client = client.clone(); |
| 72 | | |
| 73 | 112 | let ret = client.login("example", "wordpass", None).await; |
| 74 | 113 | |
| 75 | 114 | match ret { |
| @@ -83,6 +122,7 @@ impl MatrixServer { |
| 83 | 122 | return; |
| 84 | 123 | } |
| 85 | 124 | } |
| 125 | |
| 86 | 126 | let mut sync_token = None; |
| 87 | 127 | |
| 88 | 128 | loop { |
| @@ -142,7 +182,10 @@ impl MatrixServer { |
| 142 | 182 | let weechat = unsafe { Weechat::weechat() }; |
| 143 | 183 | let plugin = plugin(); |
| 144 | 184 | |
| 145 | | let mut server = plugin.servers.get_mut("localhost").unwrap(); |
| 185 | let mut server = match plugin.servers.get_mut("localhost") { |
| 186 | Some(s) => s, |
| 187 | None => return, |
| 188 | }; |
| 146 | 189 | |
| 147 | 190 | loop { |
| 148 | 191 | let ret = match receiver.recv().await { |
| @@ -197,15 +240,18 @@ impl MatrixServer { |
| 197 | 240 | } |
| 198 | 241 | |
| 199 | 242 | pub fn receive_login(&mut self, response: LoginResponse) { |
| 200 | | let server_user = ServerUser { |
| 243 | let login_state = LoginState { |
| 201 | 244 | user_id: response.user_id.to_string(), |
| 202 | 245 | device_id: response.device_id.clone(), |
| 203 | 246 | }; |
| 204 | | self.server_user = Some(server_user); |
| 247 | self.login_state = Some(login_state); |
| 205 | 248 | } |
| 206 | 249 | |
| 207 | 250 | pub async fn send_message(&self, room_id: &str, message: &str) { |
| 208 | | self.client_channel |
| 251 | self.connected_state |
| 252 | .as_ref() |
| 253 | .expect("Sending a message while not connected") |
| 254 | .client_channel |
| 209 | 255 | .send(ServerMessage::RoomSend( |
| 210 | 256 | room_id.to_owned(), |
| 211 | 257 | message.to_owned(), |
| @@ -218,16 +264,19 @@ impl MatrixServer { |
| 218 | 264 | room_id: &str, |
| 219 | 265 | ) -> &mut RoomBuffer { |
| 220 | 266 | if !self.room_buffers.contains_key(room_id) { |
| 267 | let login_state = self |
| 268 | .login_state |
| 269 | .as_ref() |
| 270 | .expect("Receiving events while not being logged in"); |
| 271 | let connected_state = self |
| 272 | .connected_state |
| 273 | .as_ref() |
| 274 | .expect("Receiving events while not being connected"); |
| 221 | 275 | let buffer = RoomBuffer::new( |
| 222 | 276 | &self.server_name, |
| 223 | | &self.homeserver, |
| 277 | &connected_state.homeserver, |
| 224 | 278 | room_id, |
| 225 | | &self |
| 226 | | .server_user |
| 227 | | .as_ref() |
| 228 | | .expect("Receiving events while not being logged in") |
| 229 | | .user_id |
| 230 | | .to_string(), |
| 279 | &login_state.user_id, |
| 231 | 280 | ); |
| 232 | 281 | self.room_buffers.insert(room_id.to_string(), buffer); |
| 233 | 282 | } |