diff --git a/LICENSE b/LICENSE index 48aedecc..160bea89 100644 --- a/LICENSE +++ b/LICENSE @@ -18,4 +18,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/examples/login.rs b/examples/login.rs index 91b4e71e..b7e8e384 100644 --- a/examples/login.rs +++ b/examples/login.rs @@ -1,10 +1,11 @@ #![feature(async_closure)] -use std::{env, process::exit}; -use std::pin::Pin; use std::future::Future; -use std::sync::{Arc, Mutex}; +use std::pin::Pin; use std::rc::Rc; +use std::sync::{Arc, Mutex}; +use std::{env, process::exit}; +use url::Url; use matrix_nio::{ self, @@ -13,7 +14,7 @@ use matrix_nio::{ room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, EventType, }, - AsyncClient, AsyncClientConfig, SyncSettings, Room + AsyncClient, AsyncClientConfig, Room, SyncSettings, }; async fn async_helper(room: Arc>, event: Arc) { @@ -25,11 +26,18 @@ async fn async_helper(room: Arc>, event: Arc) { }) = &*event { let user = room.members.get(&sender.to_string()).unwrap(); - println!("{}: {}", user.display_name.as_ref().unwrap_or(&sender.to_string()), msg_body); + println!( + "{}: {}", + user.display_name.as_ref().unwrap_or(&sender.to_string()), + msg_body + ); } } -fn async_callback(room: Arc>, event: Arc) -> Pin + Send + Sync >> { +fn async_callback( + room: Arc>, + event: Arc, +) -> Pin + Send + Sync>> { Box::pin(async_helper(room, event)) } @@ -41,9 +49,10 @@ async fn login( let client_config = AsyncClientConfig::new() .proxy("http://localhost:8080")? .disable_ssl_verification(); - let mut client = AsyncClient::new_with_config(&homeserver_url, None, client_config).unwrap(); + let homeserver_url = Url::parse(&homeserver_url)?; + let mut client = AsyncClient::new_with_config(homeserver_url, None, client_config).unwrap(); - client.add_event_future(EventType::RoomMessage, Box::new(async_callback)); + // client.add_event_future(EventType::RoomMessage, Box::new(async_callback)); client.login(username, password, None).await?; let response = client.sync(SyncSettings::new()).await?; diff --git a/nio-olm/nio-olm-rs/src/account.rs b/nio-olm/nio-olm-rs/src/account.rs index a71e48c7..85bb759c 100644 --- a/nio-olm/nio-olm-rs/src/account.rs +++ b/nio-olm/nio-olm-rs/src/account.rs @@ -17,7 +17,21 @@ impl Account { Account { account: acc_ptr, - buffer: account_data + buffer: account_data, + } + } + + pub fn identity_keys(&self) { + let keys_length = unsafe { nio_olm_sys::olm_account_identity_keys_length(self.account) }; + + let out_buffer: Vec = vec![0; keys_length]; + } +} + +impl Drop for Account { + fn drop(&mut self) { + unsafe { + nio_olm_sys::olm_clear_account(self.account); } } } diff --git a/src/async_client.rs b/src/async_client.rs index 0a0d435a..d02b0533 100644 --- a/src/async_client.rs +++ b/src/async_client.rs @@ -2,8 +2,8 @@ use std::convert::{TryFrom, TryInto}; use std::future::Future; use std::pin::Pin; use std::rc::Rc; -use std::sync::{Arc, RwLock}; use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, RwLock}; use http::Method as HttpMethod; use http::Response as HttpResponse; @@ -16,8 +16,8 @@ use ruma_events::collections::all::RoomEvent; use ruma_events::room::message::MessageEvent; use ruma_events::room::message::MessageEventContent; use ruma_events::Event; -use ruma_identifiers::RoomId; pub use ruma_events::EventType; +use ruma_identifiers::RoomId; use crate::api; use crate::base_client::Client as BaseClient; @@ -25,8 +25,12 @@ use crate::base_client::Room; use crate::error::{Error, InnerError}; use crate::session::Session; -type RoomEventCallback = Box::; -type RoomEventCallbackF = Box::>, Arc) -> Pin + Send + Sync>> + Send + Sync>; +type RoomEventCallback = Box; +type RoomEventCallbackF = Box< + dyn FnMut(Arc>, Arc) -> Pin + Send + Sync>> + + Send + + Sync, +>; #[derive(Clone)] pub struct AsyncClient { @@ -120,13 +124,16 @@ impl SyncSettings { } } +use api::r0::send::send_message_event; use api::r0::session::login; use api::r0::sync::sync_events; -use api::r0::send::send_message_event; impl AsyncClient { /// Creates a new client for making HTTP requests to the given homeserver. - pub fn new>(homeserver_url: U, session: Option) -> Result { + pub fn new>( + homeserver_url: U, + session: Option, + ) -> Result { let config = AsyncClientConfig::new(); AsyncClient::new_with_config(homeserver_url, session, config) } @@ -138,7 +145,7 @@ impl AsyncClient { ) -> Result { let homeserver: Url = match homeserver_url.try_into() { Ok(u) => u, - Err(e) => panic!("Error parsing homeserver url") + Err(e) => panic!("Error parsing homeserver url"), }; let http_client = reqwest::Client::builder(); @@ -167,10 +174,7 @@ impl AsyncClient { None => HeaderValue::from_static("nio-rust"), }; - headers.insert( - reqwest::header::USER_AGENT, - user_agent, - ); + headers.insert(reqwest::header::USER_AGENT, user_agent); let http_client = http_client.default_headers(headers).build().unwrap(); @@ -244,7 +248,7 @@ impl AsyncClient { for event in &room.state.events { let event = match event.clone().into_result() { Ok(e) => e, - Err(e) => continue + Err(e) => continue, }; client.receive_joined_state_event(&room_id, &event); @@ -253,7 +257,7 @@ impl AsyncClient { for event in &room.timeline.events { let event = match event.clone().into_result() { Ok(e) => e, - Err(e) => continue + Err(e) => continue, }; client.receive_joined_timeline_event(&room_id, &event); @@ -276,7 +280,10 @@ impl AsyncClient { async fn send(&self, request: Request) -> Result { let request: http::Request> = request.try_into()?; let url = request.uri(); - let url = self.homeserver.join(url.path_and_query().unwrap().as_str()).unwrap(); + let url = self + .homeserver + .join(url.path_and_query().unwrap().as_str()) + .unwrap(); let request_builder = match Request::METADATA.method { HttpMethod::GET => self.http_client.get(url), @@ -324,7 +331,11 @@ impl AsyncClient { self.transaction_id.fetch_add(1, Ordering::SeqCst) } - pub async fn room_send(&mut self, room_id: &str, data: MessageEventContent) -> Result { + pub async fn room_send( + &mut self, + room_id: &str, + data: MessageEventContent, + ) -> Result { let request = send_message_event::Request { room_id: RoomId::try_from(room_id).unwrap(), event_type: EventType::RoomMessage, diff --git a/src/base_client.rs b/src/base_client.rs index 6b77e0b8..4cbafe8c 100644 --- a/src/base_client.rs +++ b/src/base_client.rs @@ -188,15 +188,14 @@ impl Client { fn get_or_create_room(&mut self, room_id: &RoomId) -> &mut Arc> { self.joined_rooms .entry(room_id.to_string()) - .or_insert( - Arc::new(RwLock::new(Room::new( - room_id, - &self - .session - .as_ref() - .expect("Receiving events while not being logged in") - .user_id - .to_string(), + .or_insert(Arc::new(RwLock::new(Room::new( + room_id, + &self + .session + .as_ref() + .expect("Receiving events while not being logged in") + .user_id + .to_string(), )))) } diff --git a/src/error.rs b/src/error.rs index 61a5e80e..c97e1c6d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,11 +3,11 @@ use std::error::Error as StdError; use std::fmt::{Display, Formatter, Result as FmtResult}; -use url::ParseError; use reqwest::Error as ReqwestError; use ruma_api::Error as RumaApiError; use serde_json::Error as SerdeJsonError; use serde_urlencoded::ser::Error as SerdeUrlEncodedSerializeError; +use url::ParseError; /// An error that can occur during client operations. #[derive(Debug)] diff --git a/src/lib.rs b/src/lib.rs index 16c8b7af..ecee76fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,9 @@ #![warn(missing_docs)] pub use crate::{error::Error, session::Session}; +pub use reqwest::header::InvalidHeaderValue; pub use ruma_client_api as api; pub use ruma_events as events; -pub use reqwest::header::InvalidHeaderValue; mod async_client; mod base_client;