From 7ecbda160017d42a9de938746a9ec9cdfb2afb63 Mon Sep 17 00:00:00 2001 From: Evin Yulo Date: Fri, 16 Dec 2022 13:39:07 -0500 Subject: [PATCH] support contact sync messages --- src/contact.rs | 8 ++++++++ src/lib.rs | 17 ++++++----------- src/message.rs | 18 ++++++++++++++++-- 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 src/contact.rs diff --git a/src/contact.rs b/src/contact.rs new file mode 100644 index 0000000..0e42772 --- /dev/null +++ b/src/contact.rs @@ -0,0 +1,8 @@ +use crate::prelude::*; +use crate::addr::*; + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct Contact { + pub name: String, + pub addr: SufecAddr, +} diff --git a/src/lib.rs b/src/lib.rs index a1607f6..9ed30e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,23 +2,18 @@ mod prelude; mod addr; mod account; mod message; +mod contact; +mod room; mod crypto; -mod error; mod server; +mod error; mod store; -mod room; pub use account::*; pub use addr::*; pub use message::*; -pub use error::*; -pub use server::*; +pub use contact::*; pub use room::*; +pub use server::*; +pub use error::*; pub use store::*; -use prelude::*; - -#[derive(Serialize, Deserialize, Clone)] -pub struct Contact { - pub name: String, - pub addr: SufecAddr, -} diff --git a/src/message.rs b/src/message.rs index 41c002e..b8fe8ab 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,5 +1,6 @@ use crate::prelude::*; use crate::addr::*; +use crate::contact::*; #[derive(Debug)] pub struct Message { @@ -13,12 +14,19 @@ pub struct Message { pub enum MessageContent { Text(String), File(String, Vec), + Contact(Contact), } impl MessageContent { pub fn to_bytes(&self) -> Vec { match self { MessageContent::Text(text) => [&[0], text.as_bytes()].concat(), MessageContent::File(name, content) => [&[1], &[name.len() as u8], name.as_bytes(), content].concat(), + MessageContent::Contact(contact) => [ + [2].as_slice(), + &[contact.addr.to_bytes().len() as u8], + &contact.addr.to_bytes(), + contact.name.as_bytes(), + ].concat(), } } pub fn from_bytes(bytes: &[u8]) -> Option { @@ -29,8 +37,14 @@ impl MessageContent { } 1 => { let name_len = *bytes.get(1)? as usize; - let name = String::from_utf8(bytes[1..1+name_len].to_vec()).ok()?; - Some(MessageContent::File(name, bytes[1+name_len..].to_vec())) + let name = String::from_utf8(bytes[2..2+name_len].to_vec()).ok()?; + Some(MessageContent::File(name, bytes[2+name_len..].to_vec())) + }, + 2 => { + let addr_len = *bytes.get(1)? as usize; + let addr = SufecAddr::from_bytes(&bytes[2..2+addr_len])?; + let name = String::from_utf8(bytes[2+addr_len+PUBLICKEYBYTES..].to_vec()).ok()?; + Some(MessageContent::Contact(Contact{addr, name})) }, _ => None, } -- 2.45.2