Commit 87041345e6
Unsigned
Layout: unified · split
src/server.rs +28 −29
| @@ -54,7 +54,6 @@ pub struct LoginState { | ||
| 54 | 54 | struct ConnectedState { |
| 55 | 55 | client_channel: Sender<ServerMessage>, |
| 56 | 56 | sync_receiver: JoinHandle<(), ()>, |
| 57 | homeserver: Url, | |
| 58 | 57 | runtime: Runtime, |
| 59 | 58 | } |
| 60 | 59 | |
| @@ -63,17 +62,32 @@ pub(crate) struct MatrixServer { | ||
| 63 | 62 | connected: bool, |
| 64 | 63 | room_buffers: HashMap<String, RoomBuffer>, |
| 65 | 64 | config: ServerConfig, |
| 65 | client: AsyncClient, | |
| 66 | homeserver: Url, | |
| 66 | 67 | login_state: Option<LoginState>, |
| 67 | 68 | connected_state: Option<ConnectedState>, |
| 68 | 69 | } |
| 69 | 70 | |
| 70 | 71 | impl MatrixServer { |
| 71 | 72 | pub fn new(name: &str) -> Self { |
| 73 | let homeserver = Url::parse("http://localhost:8008").unwrap(); | |
| 74 | ||
| 75 | let config = AsyncClientConfig::new() | |
| 76 | .proxy("http://localhost:8080") | |
| 77 | .unwrap() | |
| 78 | .disable_ssl_verification(); | |
| 79 | ||
| 80 | let client = | |
| 81 | AsyncClient::new_with_config(homeserver.clone(), None, config) | |
| 82 | .unwrap(); | |
| 83 | ||
| 72 | 84 | MatrixServer { |
| 73 | 85 | server_name: name.to_owned(), |
| 74 | 86 | connected: false, |
| 75 | 87 | room_buffers: HashMap::new(), |
| 76 | 88 | config: ServerConfig { homeserver: None }, |
| 89 | client, | |
| 90 | homeserver, | |
| 77 | 91 | login_state: None, |
| 78 | 92 | connected_state: None, |
| 79 | 93 | } |
| @@ -84,21 +98,12 @@ impl MatrixServer { | ||
| 84 | 98 | } |
| 85 | 99 | |
| 86 | 100 | pub fn connect(&mut self) { |
| 87 | let homeserver = Url::parse("http://localhost:8008").unwrap(); | |
| 88 | 101 | let runtime = Runtime::new().unwrap(); |
| 89 | 102 | |
| 90 | let config = AsyncClientConfig::new() | |
| 91 | .proxy("http://localhost:8080") | |
| 92 | .unwrap() | |
| 93 | .disable_ssl_verification(); | |
| 94 | ||
| 95 | let client = | |
| 96 | AsyncClient::new_with_config(homeserver.clone(), None, config) | |
| 97 | .unwrap(); | |
| 98 | let send_client = client.clone(); | |
| 103 | let send_client = self.client.clone(); | |
| 99 | 104 | |
| 100 | 105 | let (tx, rx) = async_channel(1000); |
| 101 | runtime.spawn(MatrixServer::sync_loop(client, tx)); | |
| 106 | runtime.spawn(MatrixServer::sync_loop(self.client.clone(), tx)); | |
| 102 | 107 | let sync_receiver_handle = |
| 103 | 108 | spawn_weechat(MatrixServer::sync_receiver(rx)); |
| 104 | 109 | |
| @@ -108,7 +113,6 @@ impl MatrixServer { | ||
| 108 | 113 | self.connected_state = Some(ConnectedState { |
| 109 | 114 | sync_receiver: sync_receiver_handle, |
| 110 | 115 | client_channel: client_sender, |
| 111 | homeserver, | |
| 112 | 116 | runtime, |
| 113 | 117 | }); |
| 114 | 118 | } |
| @@ -133,13 +137,13 @@ impl MatrixServer { | ||
| 133 | 137 | .send(Ok(ThreadMessage::LoginMessage(response))) |
| 134 | 138 | .await |
| 135 | 139 | } |
| 136 | Err(e) => { | |
| 140 | Err(_e) => { | |
| 137 | 141 | channel.send(Err("No logging in".to_string())).await; |
| 138 | 142 | return; |
| 139 | 143 | } |
| 140 | 144 | } |
| 141 | 145 | |
| 142 | let mut sync_token = None; | |
| 146 | let mut sync_token = client.sync_token(); | |
| 143 | 147 | |
| 144 | 148 | loop { |
| 145 | 149 | let sync_settings = SyncSettings::new() |
| @@ -162,7 +166,7 @@ impl MatrixServer { | ||
| 162 | 166 | for event in room.state.events { |
| 163 | 167 | let event = match event.into_result() { |
| 164 | 168 | Ok(e) => e, |
| 165 | Err(e) => continue, | |
| 169 | Err(_e) => continue, | |
| 166 | 170 | }; |
| 167 | 171 | channel |
| 168 | 172 | .send(Ok(ThreadMessage::SyncState( |
| @@ -175,7 +179,7 @@ impl MatrixServer { | ||
| 175 | 179 | for event in room.timeline.events { |
| 176 | 180 | let event = match event.into_result() { |
| 177 | 181 | Ok(e) => e, |
| 178 | Err(e) => continue, | |
| 182 | Err(_e) => continue, | |
| 179 | 183 | }; |
| 180 | 184 | channel |
| 181 | 185 | .send(Ok(ThreadMessage::SyncEvent( |
| @@ -201,7 +205,7 @@ impl MatrixServer { | ||
| 201 | 205 | let weechat = unsafe { Weechat::weechat() }; |
| 202 | 206 | let plugin = plugin(); |
| 203 | 207 | |
| 204 | let mut server = match plugin.servers.get_mut("localhost") { | |
| 208 | let server = match plugin.servers.get_mut("localhost") { | |
| 205 | 209 | Some(s) => s, |
| 206 | 210 | None => return, |
| 207 | 211 | }; |
| @@ -224,7 +228,6 @@ impl MatrixServer { | ||
| 224 | 228 | ThreadMessage::SyncState(r, e) => { |
| 225 | 229 | server.receive_joined_state_event(&r, e) |
| 226 | 230 | } |
| 227 | _ => (), | |
| 228 | 231 | }, |
| 229 | 232 | Err(e) => weechat.print(&format!("Ruma error {}", e)), |
| 230 | 233 | }; |
| @@ -250,8 +253,8 @@ impl MatrixServer { | ||
| 250 | 253 | let ret = client.room_send(&room_id, content).await; |
| 251 | 254 | |
| 252 | 255 | match ret { |
| 253 | Ok(r) => (), | |
| 254 | Err(e) => (), | |
| 256 | Ok(_r) => (), | |
| 257 | Err(_e) => (), | |
| 255 | 258 | } |
| 256 | 259 | } |
| 257 | 260 | } |
| @@ -261,7 +264,7 @@ impl MatrixServer { | ||
| 261 | 264 | pub fn receive_login(&mut self, response: LoginResponse) { |
| 262 | 265 | let login_state = LoginState { |
| 263 | 266 | user_id: response.user_id.to_string(), |
| 264 | device_id: response.device_id.clone(), | |
| 267 | device_id: response.device_id, | |
| 265 | 268 | }; |
| 266 | 269 | self.login_state = Some(login_state); |
| 267 | 270 | } |
| @@ -287,13 +290,9 @@ impl MatrixServer { | ||
| 287 | 290 | .login_state |
| 288 | 291 | .as_ref() |
| 289 | 292 | .expect("Receiving events while not being logged in"); |
| 290 | let connected_state = self | |
| 291 | .connected_state | |
| 292 | .as_ref() | |
| 293 | .expect("Receiving events while not being connected"); | |
| 294 | 293 | let buffer = RoomBuffer::new( |
| 295 | 294 | &self.server_name, |
| 296 | &connected_state.homeserver, | |
| 295 | &self.homeserver, | |
| 297 | 296 | room_id, |
| 298 | 297 | &login_state.user_id, |
| 299 | 298 | ); |
| @@ -308,7 +307,7 @@ impl MatrixServer { | ||
| 308 | 307 | room_id: &str, |
| 309 | 308 | event: StateEvent, |
| 310 | 309 | ) { |
| 311 | let mut room = self.get_or_create_room(room_id); | |
| 310 | let room = self.get_or_create_room(room_id); | |
| 312 | 311 | room.handle_state_event(event) |
| 313 | 312 | } |
| 314 | 313 | |
| @@ -317,7 +316,7 @@ impl MatrixServer { | ||
| 317 | 316 | room_id: &str, |
| 318 | 317 | event: RoomEvent, |
| 319 | 318 | ) { |
| 320 | let mut room = self.get_or_create_room(room_id); | |
| 319 | let room = self.get_or_create_room(room_id); | |
| 321 | 320 | room.handle_room_event(event) |
| 322 | 321 | } |
| 323 | 322 | } |