diff --git a/apps/sigchat/src/account.rs b/apps/sigchat/src/account.rs index b8fc5f16b..1a301969b 100644 --- a/apps/sigchat/src/account.rs +++ b/apps/sigchat/src/account.rs @@ -54,6 +54,18 @@ impl Account { } } + /// Delete this Account key/value from the pddb + /// + /// While this Account struct will persist in memory, a subsequent Account.read() will fail + /// + pub fn delete(pddb_dict: &str) -> Result<(), Error> { + let pddb = pddb::Pddb::new(); + pddb.try_mount(); + pddb.delete_dict(pddb_dict, None)?; + log::info!("deleted Signal Account from pddb"); + Ok(()) + } + #[allow(dead_code)] pub fn number(&self) -> Option<&str> { match &self.number { diff --git a/apps/sigchat/src/api.rs b/apps/sigchat/src/api.rs index aabb47cb6..01522cc62 100644 --- a/apps/sigchat/src/api.rs +++ b/apps/sigchat/src/api.rs @@ -1,5 +1,6 @@ #[allow(dead_code)] pub(crate) const SERVER_NAME_SIGCHAT: &str = "_Signal chat_"; +pub(crate) const DEFAULT_DEVICE_NAME: &str = "xous"; pub(crate) const DEFAULT_HOST_NAME: &str = "signal.org"; #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] diff --git a/apps/sigchat/src/lib.rs b/apps/sigchat/src/lib.rs index 97667ec22..c5bad46ff 100644 --- a/apps/sigchat/src/lib.rs +++ b/apps/sigchat/src/lib.rs @@ -178,9 +178,40 @@ impl<'a> SigChat<'a> { /// Note that currently Signal allows up to three linked devices per primary. /// pub fn account_link(&mut self, host: &str) -> Result { - self.modals - .show_notification(&"sorry - linking is not implemented yet", None) - .expect("notification failed"); + log::info!("Attempting to Link to an existing Signal Account"); + match Account::new(SIGCHAT_ACCOUNT) { + Ok(account) => { + let mut manager = Manager::new(account, TrustMode::OnFirstUse); + let name = self.name_modal( + DEFAULT_DEVICE_NAME, + t!("sigchat.account.link.name", locales::LANG), + ); + match manager.link(Some(&name), Some(&host)) { + Ok(true) => { + log::info!("Linked Signal Account"); + } + Ok(false) => { + log::info!("failed to link Signal Account"); + } + Err(e) => { + log::warn!("error while linking Signal Account: {e}"); + Account::delete(SIGCHAT_ACCOUNT).unwrap_or_else(|e| { + log::warn!("failed to delete unregistered account from pddb: {e}") + }); + self.modals + .show_notification(&format!("{}", e), None) + .expect("notification failed"); + } + } + } + Err(_) => { + log::warn!("failed to create new Account in pddb: {}", SIGCHAT_ACCOUNT); + self.modals + .show_notification(t!("sigchat.account.failed", locales::LANG), None) + .expect("notification failed"); + } + }; + Ok(Account::read(SIGCHAT_ACCOUNT)?) } /// Prompt a name from the user @@ -210,7 +241,7 @@ impl<'a> SigChat<'a> { /// The phone number must include the country calling code, i.e. the number must start with a "+" sign. /// Warning: this will disable the authentication of your phone as a primary device. /// - pub fn account_register(&mut self, host: &str) -> Result { + pub fn account_register(&mut self, _host: &str) -> Result { log::info!("Attempting to Register a new Signal Account"); self.modals .show_notification(&"sorry - registration is not implemented yet", None) diff --git a/apps/sigchat/src/manager.rs b/apps/sigchat/src/manager.rs index 04c723b97..f9e06f3c9 100644 --- a/apps/sigchat/src/manager.rs +++ b/apps/sigchat/src/manager.rs @@ -123,10 +123,11 @@ impl Manager { /// Link to an existing device, instead of registering a new number. This shows a "sgnl://linkdevice?uuid=…​" URI. If you want to connect to another signal-cli instance, you can just use this URI. If you want to link to an Android/iOS device, create a QR code with the URI (e.g. with qrencode) and scan that in the Signal app. /// /// # Arguments - /// * `name` - Optionally specify a name to describe this new device. By default "cli" will be used. + /// * `name` - Optionally specify a name to describe this new device (defaults to "xous"). + /// * `host` - Optionally specify a host to connect to (defaults to "signal.org"). /// #[allow(dead_code)] - pub fn link(_name: &str) -> Result<(), Error> { + pub fn link(&mut self, name: Option<&str>, host: Option<&str>) -> Result { todo!(); }