Commit e7dee5c19c
Unsigned
Layout: unified · split
src/room_buffer.rs +33
| @@ -132,6 +132,39 @@ impl RoomBuffer { | |||
| 132 | } | 132 | } |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | pub fn restore( | ||
| 136 | room: Room, | ||
| 137 | server_name: &str, | ||
| 138 | connected_state: &Rc<RefCell<Option<Connection>>>, | ||
| 139 | homeserver: &Url, | ||
| 140 | config: &Config, | ||
| 141 | ) -> Self { | ||
| 142 | let mut room_buffer = RoomBuffer::new( | ||
| 143 | server_name, | ||
| 144 | connected_state, | ||
| 145 | homeserver, | ||
| 146 | config, | ||
| 147 | room.room_id.clone(), | ||
| 148 | &room.own_user_id, | ||
| 149 | ); | ||
| 150 | |||
| 151 | let buffer = room_buffer.weechat_buffer(); | ||
| 152 | |||
| 153 | for user in room.members.values() { | ||
| 154 | let user_id = user.user_id.to_string(); | ||
| 155 | // TODO use display names here | ||
| 156 | let settings = NickSettings::new(&user_id); | ||
| 157 | buffer | ||
| 158 | .add_nick(settings) | ||
| 159 | .expect("Can't add nick to nicklist"); | ||
| 160 | } | ||
| 161 | |||
| 162 | room_buffer.inner.room = Rc::new(RefCell::new(room)); | ||
| 163 | room_buffer.update_buffer_name(); | ||
| 164 | |||
| 165 | room_buffer | ||
| 166 | } | ||
| 167 | |||
| 135 | pub fn room_mut(&mut self) -> RefMut<'_, Room> { | 168 | pub fn room_mut(&mut self) -> RefMut<'_, Room> { |
| 136 | self.inner.room.borrow_mut() | 169 | self.inner.room.borrow_mut() |
| 137 | } | 170 | } |
src/server.rs +136 −9
| @@ -54,6 +54,7 @@ use async_std::sync::channel as async_channel; | |||
| 54 | use async_std::sync::{Receiver, Sender}; | 54 | use async_std::sync::{Receiver, Sender}; |
| 55 | use std::cell::RefCell; | 55 | use std::cell::RefCell; |
| 56 | use std::collections::HashMap; | 56 | use std::collections::HashMap; |
| 57 | use std::path::PathBuf; | ||
| 57 | use std::rc::{Rc, Weak}; | 58 | use std::rc::{Rc, Weak}; |
| 58 | use std::time::Duration; | 59 | use std::time::Duration; |
| 59 | use tokio::runtime::Runtime; | 60 | use tokio::runtime::Runtime; |
| @@ -69,7 +70,7 @@ use matrix_sdk::{ | |||
| 69 | room::message::{MessageEventContent, TextMessageEventContent}, | 70 | room::message::{MessageEventContent, TextMessageEventContent}, |
| 70 | }, | 71 | }, |
| 71 | identifiers::{RoomId, UserId}, | 72 | identifiers::{RoomId, UserId}, |
| 72 | Client, ClientConfig, SyncSettings, | 73 | Client, ClientConfig, Room, SyncSettings, |
| 73 | }; | 74 | }; |
| 74 | 75 | ||
| 75 | use weechat::config::{ | 76 | use weechat::config::{ |
| @@ -88,11 +89,13 @@ pub enum ThreadMessage { | |||
| 88 | LoginMessage(LoginResponse), | 89 | LoginMessage(LoginResponse), |
| 89 | SyncState(RoomId, StateEvent), | 90 | SyncState(RoomId, StateEvent), |
| 90 | SyncEvent(RoomId, RoomEvent), | 91 | SyncEvent(RoomId, RoomEvent), |
| 92 | RestoredRoom(Room), | ||
| 91 | } | 93 | } |
| 92 | 94 | ||
| 93 | #[derive(Debug)] | 95 | #[derive(Debug)] |
| 94 | pub enum ServerError { | 96 | pub enum ServerError { |
| 95 | StartError(String), | 97 | StartError(String), |
| 98 | IoError(String), | ||
| 96 | } | 99 | } |
| 97 | 100 | ||
| 98 | pub enum ServerMessage { | 101 | pub enum ServerMessage { |
| @@ -116,7 +119,6 @@ impl ServerSettings { | |||
| 116 | 119 | ||
| 117 | pub struct LoginState { | 120 | pub struct LoginState { |
| 118 | user_id: UserId, | 121 | user_id: UserId, |
| 119 | device_id: String, | ||
| 120 | } | 122 | } |
| 121 | 123 | ||
| 122 | pub struct Connection { | 124 | pub struct Connection { |
| @@ -333,6 +335,42 @@ impl MatrixServer { | |||
| 333 | pub fn autoconnect(&self) -> bool { | 335 | pub fn autoconnect(&self) -> bool { |
| 334 | self.inner.borrow().settings.autoconnect | 336 | self.inner.borrow().settings.autoconnect |
| 335 | } | 337 | } |
| 338 | |||
| 339 | fn save_device_id( | ||
| 340 | user_name: &str, | ||
| 341 | mut server_path: PathBuf, | ||
| 342 | response: &LoginResponse, | ||
| 343 | ) -> std::io::Result<()> { | ||
| 344 | server_path.push(user_name); | ||
| 345 | server_path.set_extension("device_id"); | ||
| 346 | std::fs::write(&server_path, &response.device_id) | ||
| 347 | } | ||
| 348 | |||
| 349 | fn load_device_id( | ||
| 350 | user_name: &str, | ||
| 351 | mut server_path: PathBuf, | ||
| 352 | ) -> std::io::Result<Option<String>> { | ||
| 353 | server_path.push(user_name); | ||
| 354 | server_path.set_extension("device_id"); | ||
| 355 | |||
| 356 | let device_id = std::fs::read_to_string(server_path); | ||
| 357 | |||
| 358 | if let Err(e) = device_id { | ||
| 359 | // A file not found error is ok, report the rest. | ||
| 360 | if e.kind() != std::io::ErrorKind::NotFound { | ||
| 361 | return Err(e); | ||
| 362 | } | ||
| 363 | return Ok(None); | ||
| 364 | }; | ||
| 365 | |||
| 366 | let device_id = device_id.unwrap_or_default(); | ||
| 367 | |||
| 368 | if device_id.is_empty() { | ||
| 369 | Ok(None) | ||
| 370 | } else { | ||
| 371 | Ok(Some(device_id)) | ||
| 372 | } | ||
| 373 | } | ||
| 336 | } | 374 | } |
| 337 | 375 | ||
| 338 | impl Drop for MatrixServer { | 376 | impl Drop for MatrixServer { |
| @@ -383,6 +421,26 @@ impl InnerServer { | |||
| 383 | self.room_buffers.get_mut(room_id).unwrap() | 421 | self.room_buffers.get_mut(room_id).unwrap() |
| 384 | } | 422 | } |
| 385 | 423 | ||
| 424 | fn restore_room(&mut self, room: Room) { | ||
| 425 | let homeserver = self | ||
| 426 | .settings | ||
| 427 | .homeserver | ||
| 428 | .as_ref() | ||
| 429 | .expect("Creating room buffer while no homeserver"); | ||
| 430 | |||
| 431 | let room_id = room.room_id.clone(); | ||
| 432 | |||
| 433 | let buffer = RoomBuffer::restore( | ||
| 434 | room, | ||
| 435 | &self.server_name, | ||
| 436 | &self.connected_state, | ||
| 437 | homeserver, | ||
| 438 | &self.config, | ||
| 439 | ); | ||
| 440 | |||
| 441 | self.room_buffers.insert(room_id, buffer); | ||
| 442 | } | ||
| 443 | |||
| 386 | /// Is the server connected. | 444 | /// Is the server connected. |
| 387 | pub fn connected(&self) -> bool { | 445 | pub fn connected(&self) -> bool { |
| 388 | self.connected_state.borrow().is_some() | 446 | self.connected_state.borrow().is_some() |
| @@ -409,18 +467,39 @@ impl InnerServer { | |||
| 409 | pub fn receive_login(&mut self, response: LoginResponse) { | 467 | pub fn receive_login(&mut self, response: LoginResponse) { |
| 410 | let login_state = LoginState { | 468 | let login_state = LoginState { |
| 411 | user_id: response.user_id, | 469 | user_id: response.user_id, |
| 412 | device_id: response.device_id, | ||
| 413 | }; | 470 | }; |
| 414 | self.login_state = Some(login_state); | 471 | self.login_state = Some(login_state); |
| 415 | } | 472 | } |
| 416 | 473 | ||
| 474 | fn create_server_dir(&self) -> std::io::Result<()> { | ||
| 475 | let path = self.get_server_path(); | ||
| 476 | std::fs::create_dir_all(path) | ||
| 477 | } | ||
| 478 | |||
| 479 | fn get_server_path(&self) -> PathBuf { | ||
| 480 | let mut path = Weechat::home_dir(); | ||
| 481 | let server_name: &str = &self.server_name; | ||
| 482 | path.push("matrix-rust"); | ||
| 483 | path.push(server_name); | ||
| 484 | |||
| 485 | path | ||
| 486 | } | ||
| 487 | |||
| 417 | pub fn create_client(&mut self) -> Result<Client, ServerError> { | 488 | pub fn create_client(&mut self) -> Result<Client, ServerError> { |
| 418 | let homeserver = | 489 | let homeserver = |
| 419 | self.settings.homeserver.as_ref().ok_or_else(|| { | 490 | self.settings.homeserver.as_ref().ok_or_else(|| { |
| 420 | ServerError::StartError("Homeserver not configured".to_owned()) | 491 | ServerError::StartError("Homeserver not configured".to_owned()) |
| 421 | })?; | 492 | })?; |
| 422 | 493 | ||
| 423 | let mut client_config = ClientConfig::new(); | 494 | self.create_server_dir().map_err(|e| { |
| 495 | ServerError::IoError(format!( | ||
| 496 | "Error creating the session dir: {}", | ||
| 497 | e | ||
| 498 | )) | ||
| 499 | })?; | ||
| 500 | |||
| 501 | let mut client_config = | ||
| 502 | ClientConfig::new().store_path(self.get_server_path()); | ||
| 424 | 503 | ||
| 425 | if let Some(proxy) = &self.settings.proxy { | 504 | if let Some(proxy) = &self.settings.proxy { |
| 426 | client_config = client_config | 505 | client_config = client_config |
| @@ -430,8 +509,7 @@ impl InnerServer { | |||
| 430 | } | 509 | } |
| 431 | 510 | ||
| 432 | let client = | 511 | let client = |
| 433 | Client::new_with_config(homeserver.clone(), None, client_config) | 512 | Client::new_with_config(homeserver.clone(), client_config).unwrap(); |
| 434 | .unwrap(); | ||
| 435 | self.client = Some(client.clone()); | 513 | self.client = Some(client.clone()); |
| 436 | Ok(client) | 514 | Ok(client) |
| 437 | } | 515 | } |
| @@ -470,6 +548,8 @@ impl MatrixServer { | |||
| 470 | tx, | 548 | tx, |
| 471 | server.settings.username.to_string(), | 549 | server.settings.username.to_string(), |
| 472 | server.settings.password.to_string(), | 550 | server.settings.password.to_string(), |
| 551 | server.server_name.to_string(), | ||
| 552 | server.get_server_path(), | ||
| 473 | )); | 553 | )); |
| 474 | let response_receiver = Weechat::spawn( | 554 | let response_receiver = Weechat::spawn( |
| 475 | MatrixServer::response_receiver(rx, Rc::downgrade(&self.inner)), | 555 | MatrixServer::response_receiver(rx, Rc::downgrade(&self.inner)), |
| @@ -523,19 +603,52 @@ impl MatrixServer { | |||
| 523 | channel: Sender<Result<ThreadMessage, String>>, | 603 | channel: Sender<Result<ThreadMessage, String>>, |
| 524 | username: String, | 604 | username: String, |
| 525 | password: String, | 605 | password: String, |
| 606 | server_name: String, | ||
| 607 | server_path: PathBuf, | ||
| 526 | ) { | 608 | ) { |
| 527 | if !client.logged_in().await { | 609 | if !client.logged_in().await { |
| 610 | let device_id = | ||
| 611 | MatrixServer::load_device_id(&username, server_path.clone()); | ||
| 612 | |||
| 613 | let device_id = match device_id { | ||
| 614 | Err(e) => { | ||
| 615 | channel | ||
| 616 | .send(Err(format!( | ||
| 617 | "Error while reading the device id for server {}: {:?}", | ||
| 618 | server_name, e | ||
| 619 | ))) | ||
| 620 | .await; | ||
| 621 | return; | ||
| 622 | } | ||
| 623 | Ok(d) => d, | ||
| 624 | }; | ||
| 625 | |||
| 626 | let first_login = device_id.is_none(); | ||
| 627 | |||
| 528 | let ret = client | 628 | let ret = client |
| 529 | .login( | 629 | .login( |
| 530 | username, | 630 | username.clone(), |
| 531 | password, | 631 | password, |
| 532 | None, | 632 | device_id, |
| 533 | Some("Weechat-Matrix".to_owned()), | 633 | Some("Weechat-Matrix-rs".to_owned()), |
| 534 | ) | 634 | ) |
| 535 | .await; | 635 | .await; |
| 536 | 636 | ||
| 537 | match ret { | 637 | match ret { |
| 538 | Ok(response) => { | 638 | Ok(response) => { |
| 639 | if let Err(e) = MatrixServer::save_device_id( | ||
| 640 | &username, | ||
| 641 | server_path.clone(), | ||
| 642 | &response, | ||
| 643 | ) { | ||
| 644 | channel | ||
| 645 | .send(Err(format!( | ||
| 646 | "Error while writing the device id for server {}: {:?}", | ||
| 647 | server_name, e | ||
| 648 | ))).await; | ||
| 649 | return; | ||
| 650 | } | ||
| 651 | |||
| 539 | channel | 652 | channel |
| 540 | .send(Ok(ThreadMessage::LoginMessage(response))) | 653 | .send(Ok(ThreadMessage::LoginMessage(response))) |
| 541 | .await | 654 | .await |
| @@ -545,6 +658,17 @@ impl MatrixServer { | |||
| 545 | return; | 658 | return; |
| 546 | } | 659 | } |
| 547 | } | 660 | } |
| 661 | |||
| 662 | if !first_login { | ||
| 663 | let joined_rooms = client.joined_rooms(); | ||
| 664 | for room in joined_rooms.read().await.values() { | ||
| 665 | let room = room.read().await; | ||
| 666 | let room: &Room = &*room; | ||
| 667 | channel | ||
| 668 | .send(Ok(ThreadMessage::RestoredRoom(room.clone()))) | ||
| 669 | .await | ||
| 670 | } | ||
| 671 | } | ||
| 548 | } | 672 | } |
| 549 | 673 | ||
| 550 | let sync_token = client.sync_token().await; | 674 | let sync_token = client.sync_token().await; |
| @@ -620,6 +744,9 @@ impl MatrixServer { | |||
| 620 | ThreadMessage::SyncState(r, e) => { | 744 | ThreadMessage::SyncState(r, e) => { |
| 621 | server.receive_joined_state_event(&r, e) | 745 | server.receive_joined_state_event(&r, e) |
| 622 | } | 746 | } |
| 747 | ThreadMessage::RestoredRoom(room) => { | ||
| 748 | server.restore_room(room) | ||
| 749 | } | ||
| 623 | }, | 750 | }, |
| 624 | Err(e) => Weechat::print(&format!("Ruma error {}", e)), | 751 | Err(e) => Weechat::print(&format!("Ruma error {}", e)), |
| 625 | }; | 752 | }; |