Commit 7ef7a0d842
Unsigned
Layout: unified · split
src/server.rs +148 −150
| @@ -384,157 +384,7 @@ impl MatrixServer { | ||
| 384 | 384 | Ok(Some(device_id)) |
| 385 | 385 | } |
| 386 | 386 | } |
| 387 | } | |
| 388 | ||
| 389 | impl Drop for MatrixServer { | |
| 390 | fn drop(&mut self) { | |
| 391 | // TODO close all the server buffers. | |
| 392 | let config = &self.inner.borrow().config; | |
| 393 | let mut config_borrow = config.borrow_mut(); | |
| 394 | ||
| 395 | let mut section = config_borrow | |
| 396 | .search_section_mut("server") | |
| 397 | .expect("Can't get server section"); | |
| 398 | ||
| 399 | for option_name in &["homeserver", "autoconnect"] { | |
| 400 | let option_name = &format!("{}.{}", self.name(), option_name); | |
| 401 | section.free_option(option_name).unwrap_or_else(|_| { | |
| 402 | panic!(format!("Can't free option {}", option_name)) | |
| 403 | }); | |
| 404 | } | |
| 405 | } | |
| 406 | } | |
| 407 | ||
| 408 | impl InnerServer { | |
| 409 | pub(crate) fn get_or_create_room( | |
| 410 | &mut self, | |
| 411 | room_id: &RoomId, | |
| 412 | ) -> &mut RoomBuffer { | |
| 413 | if !self.room_buffers.contains_key(room_id) { | |
| 414 | let homeserver = self | |
| 415 | .settings | |
| 416 | .homeserver | |
| 417 | .as_ref() | |
| 418 | .expect("Creating room buffer while no homeserver"); | |
| 419 | let login_state = self | |
| 420 | .login_state | |
| 421 | .as_ref() | |
| 422 | .expect("Receiving events while not being logged in"); | |
| 423 | let buffer = RoomBuffer::new( | |
| 424 | &self.server_name, | |
| 425 | &self.connected_state, | |
| 426 | homeserver, | |
| 427 | room_id.clone(), | |
| 428 | &login_state.user_id, | |
| 429 | ); | |
| 430 | self.room_buffers.insert(room_id.clone(), buffer); | |
| 431 | } | |
| 432 | ||
| 433 | self.room_buffers.get_mut(room_id).unwrap() | |
| 434 | } | |
| 435 | ||
| 436 | pub fn room_buffers(&self) -> &HashMap<RoomId, RoomBuffer> { | |
| 437 | &self.room_buffers | |
| 438 | } | |
| 439 | ||
| 440 | pub fn config(&self) -> Ref<Config> { | |
| 441 | self.config.borrow() | |
| 442 | } | |
| 443 | ||
| 444 | fn restore_room(&mut self, room: Room) { | |
| 445 | let homeserver = self | |
| 446 | .settings | |
| 447 | .homeserver | |
| 448 | .as_ref() | |
| 449 | .expect("Creating room buffer while no homeserver"); | |
| 450 | ||
| 451 | let room_id = room.room_id.clone(); | |
| 452 | ||
| 453 | let buffer = RoomBuffer::restore( | |
| 454 | room, | |
| 455 | &self.server_name, | |
| 456 | &self.connected_state, | |
| 457 | homeserver, | |
| 458 | ); | |
| 459 | ||
| 460 | self.room_buffers.insert(room_id, buffer); | |
| 461 | } | |
| 462 | ||
| 463 | /// Is the server connected. | |
| 464 | pub fn connected(&self) -> bool { | |
| 465 | self.connected_state.borrow().is_some() | |
| 466 | } | |
| 467 | ||
| 468 | pub(crate) fn receive_joined_state_event( | |
| 469 | &mut self, | |
| 470 | room_id: &RoomId, | |
| 471 | event: StateEvent, | |
| 472 | ) { | |
| 473 | let room = self.get_or_create_room(room_id); | |
| 474 | room.handle_state_event(event) | |
| 475 | } | |
| 476 | ||
| 477 | pub(crate) fn receive_joined_timeline_event( | |
| 478 | &mut self, | |
| 479 | room_id: &RoomId, | |
| 480 | event: RoomEvent, | |
| 481 | ) { | |
| 482 | let room = self.get_or_create_room(room_id); | |
| 483 | room.handle_room_event(event) | |
| 484 | } | |
| 485 | ||
| 486 | pub fn receive_login(&mut self, response: LoginResponse) { | |
| 487 | let login_state = LoginState { | |
| 488 | user_id: response.user_id, | |
| 489 | }; | |
| 490 | self.login_state = Some(login_state); | |
| 491 | } | |
| 492 | ||
| 493 | fn create_server_dir(&self) -> std::io::Result<()> { | |
| 494 | let path = self.get_server_path(); | |
| 495 | std::fs::create_dir_all(path) | |
| 496 | } | |
| 497 | ||
| 498 | fn get_server_path(&self) -> PathBuf { | |
| 499 | let mut path = Weechat::home_dir(); | |
| 500 | let server_name: &str = &self.server_name; | |
| 501 | path.push("matrix-rust"); | |
| 502 | path.push(server_name); | |
| 503 | ||
| 504 | path | |
| 505 | } | |
| 506 | ||
| 507 | pub fn create_client(&mut self) -> Result<Client, ServerError> { | |
| 508 | let homeserver = | |
| 509 | self.settings.homeserver.as_ref().ok_or_else(|| { | |
| 510 | ServerError::StartError("Homeserver not configured".to_owned()) | |
| 511 | })?; | |
| 512 | ||
| 513 | self.create_server_dir().map_err(|e| { | |
| 514 | ServerError::IoError(format!( | |
| 515 | "Error creating the session dir: {}", | |
| 516 | e | |
| 517 | )) | |
| 518 | })?; | |
| 519 | ||
| 520 | let mut client_config = | |
| 521 | ClientConfig::new().store_path(self.get_server_path()); | |
| 522 | ||
| 523 | if let Some(proxy) = &self.settings.proxy { | |
| 524 | client_config = client_config | |
| 525 | .proxy(proxy.as_str()) | |
| 526 | .unwrap() | |
| 527 | .disable_ssl_verification(); | |
| 528 | } | |
| 529 | ||
| 530 | let client = | |
| 531 | Client::new_with_config(homeserver.clone(), client_config).unwrap(); | |
| 532 | self.client = Some(client.clone()); | |
| 533 | Ok(client) | |
| 534 | } | |
| 535 | } | |
| 536 | 387 | |
| 537 | impl MatrixServer { | |
| 538 | 388 | pub fn connect(&self) -> Result<(), ServerError> { |
| 539 | 389 | if self.connected() { |
| 540 | 390 | return Ok(()); |
| @@ -807,3 +657,151 @@ impl MatrixServer { | ||
| 807 | 657 | } |
| 808 | 658 | } |
| 809 | 659 | } |
| 660 | ||
| 661 | impl Drop for MatrixServer { | |
| 662 | fn drop(&mut self) { | |
| 663 | // TODO close all the server buffers. | |
| 664 | let config = &self.inner.borrow().config; | |
| 665 | let mut config_borrow = config.borrow_mut(); | |
| 666 | ||
| 667 | let mut section = config_borrow | |
| 668 | .search_section_mut("server") | |
| 669 | .expect("Can't get server section"); | |
| 670 | ||
| 671 | for option_name in &["homeserver", "autoconnect"] { | |
| 672 | let option_name = &format!("{}.{}", self.name(), option_name); | |
| 673 | section.free_option(option_name).unwrap_or_else(|_| { | |
| 674 | panic!(format!("Can't free option {}", option_name)) | |
| 675 | }); | |
| 676 | } | |
| 677 | } | |
| 678 | } | |
| 679 | ||
| 680 | impl InnerServer { | |
| 681 | pub(crate) fn get_or_create_room( | |
| 682 | &mut self, | |
| 683 | room_id: &RoomId, | |
| 684 | ) -> &mut RoomBuffer { | |
| 685 | if !self.room_buffers.contains_key(room_id) { | |
| 686 | let homeserver = self | |
| 687 | .settings | |
| 688 | .homeserver | |
| 689 | .as_ref() | |
| 690 | .expect("Creating room buffer while no homeserver"); | |
| 691 | let login_state = self | |
| 692 | .login_state | |
| 693 | .as_ref() | |
| 694 | .expect("Receiving events while not being logged in"); | |
| 695 | let buffer = RoomBuffer::new( | |
| 696 | &self.server_name, | |
| 697 | &self.connected_state, | |
| 698 | homeserver, | |
| 699 | room_id.clone(), | |
| 700 | &login_state.user_id, | |
| 701 | ); | |
| 702 | self.room_buffers.insert(room_id.clone(), buffer); | |
| 703 | } | |
| 704 | ||
| 705 | self.room_buffers.get_mut(room_id).unwrap() | |
| 706 | } | |
| 707 | ||
| 708 | pub fn room_buffers(&self) -> &HashMap<RoomId, RoomBuffer> { | |
| 709 | &self.room_buffers | |
| 710 | } | |
| 711 | ||
| 712 | pub fn config(&self) -> Ref<Config> { | |
| 713 | self.config.borrow() | |
| 714 | } | |
| 715 | ||
| 716 | fn restore_room(&mut self, room: Room) { | |
| 717 | let homeserver = self | |
| 718 | .settings | |
| 719 | .homeserver | |
| 720 | .as_ref() | |
| 721 | .expect("Creating room buffer while no homeserver"); | |
| 722 | ||
| 723 | let room_id = room.room_id.clone(); | |
| 724 | ||
| 725 | let buffer = RoomBuffer::restore( | |
| 726 | room, | |
| 727 | &self.server_name, | |
| 728 | &self.connected_state, | |
| 729 | homeserver, | |
| 730 | ); | |
| 731 | ||
| 732 | self.room_buffers.insert(room_id, buffer); | |
| 733 | } | |
| 734 | ||
| 735 | /// Is the server connected. | |
| 736 | pub fn connected(&self) -> bool { | |
| 737 | self.connected_state.borrow().is_some() | |
| 738 | } | |
| 739 | ||
| 740 | pub(crate) fn receive_joined_state_event( | |
| 741 | &mut self, | |
| 742 | room_id: &RoomId, | |
| 743 | event: StateEvent, | |
| 744 | ) { | |
| 745 | let room = self.get_or_create_room(room_id); | |
| 746 | room.handle_state_event(event) | |
| 747 | } | |
| 748 | ||
| 749 | pub(crate) fn receive_joined_timeline_event( | |
| 750 | &mut self, | |
| 751 | room_id: &RoomId, | |
| 752 | event: RoomEvent, | |
| 753 | ) { | |
| 754 | let room = self.get_or_create_room(room_id); | |
| 755 | room.handle_room_event(event) | |
| 756 | } | |
| 757 | ||
| 758 | pub fn receive_login(&mut self, response: LoginResponse) { | |
| 759 | let login_state = LoginState { | |
| 760 | user_id: response.user_id, | |
| 761 | }; | |
| 762 | self.login_state = Some(login_state); | |
| 763 | } | |
| 764 | ||
| 765 | fn create_server_dir(&self) -> std::io::Result<()> { | |
| 766 | let path = self.get_server_path(); | |
| 767 | std::fs::create_dir_all(path) | |
| 768 | } | |
| 769 | ||
| 770 | fn get_server_path(&self) -> PathBuf { | |
| 771 | let mut path = Weechat::home_dir(); | |
| 772 | let server_name: &str = &self.server_name; | |
| 773 | path.push("matrix-rust"); | |
| 774 | path.push(server_name); | |
| 775 | ||
| 776 | path | |
| 777 | } | |
| 778 | ||
| 779 | pub fn create_client(&mut self) -> Result<Client, ServerError> { | |
| 780 | let homeserver = | |
| 781 | self.settings.homeserver.as_ref().ok_or_else(|| { | |
| 782 | ServerError::StartError("Homeserver not configured".to_owned()) | |
| 783 | })?; | |
| 784 | ||
| 785 | self.create_server_dir().map_err(|e| { | |
| 786 | ServerError::IoError(format!( | |
| 787 | "Error creating the session dir: {}", | |
| 788 | e | |
| 789 | )) | |
| 790 | })?; | |
| 791 | ||
| 792 | let mut client_config = | |
| 793 | ClientConfig::new().store_path(self.get_server_path()); | |
| 794 | ||
| 795 | if let Some(proxy) = &self.settings.proxy { | |
| 796 | client_config = client_config | |
| 797 | .proxy(proxy.as_str()) | |
| 798 | .unwrap() | |
| 799 | .disable_ssl_verification(); | |
| 800 | } | |
| 801 | ||
| 802 | let client = | |
| 803 | Client::new_with_config(homeserver.clone(), client_config).unwrap(); | |
| 804 | self.client = Some(client.clone()); | |
| 805 | Ok(client) | |
| 806 | } | |
| 807 | } | |