Skip to content

Commit

Permalink
feat: enable bcc-self automatically when doing Autocrypt Setup Message
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Feb 22, 2025
1 parent 3d7ac9d commit b916937
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
16 changes: 1 addition & 15 deletions src/imex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio_tar::Archive;

use crate::blob::BlobDirContents;
use crate::chat::{self, delete_and_reset_all_device_msgs};
use crate::chat::delete_and_reset_all_device_msgs;
use crate::config::Config;
use crate::context::Context;
use crate::e2ee;
use crate::events::EventType;
use crate::key::{self, DcKey, DcSecretKey, SignedPublicKey, SignedSecretKey};
use crate::log::LogExt;
use crate::message::{Message, Viewtype};
use crate::pgp;
use crate::sql;
use crate::tools::{
Expand Down Expand Up @@ -139,19 +138,6 @@ pub async fn has_backup(_context: &Context, dir_name: &Path) -> Result<String> {
}
}

async fn maybe_add_bcc_self_device_msg(context: &Context) -> Result<()> {
if !context.sql.get_raw_config_bool("bcc_self").await? {
let mut msg = Message::new(Viewtype::Text);
// TODO: define this as a stockstring once the wording is settled.
msg.text = "It seems you are using multiple devices with Delta Chat. Great!\n\n\
If you also want to synchronize outgoing messages across all devices, \
go to \"Settings → Advanced\" and enable \"Send Copy to Self\"."
.to_string();
chat::add_device_msg(context, Some("bcc-self-hint"), Some(&mut msg)).await?;
}
Ok(())
}

async fn set_self_key(context: &Context, armored: &str, set_default: bool) -> Result<()> {
// try hard to only modify key-state
let (private_key, header) = SignedSecretKey::from_asc(armored)?;
Expand Down
22 changes: 13 additions & 9 deletions src/imex/key_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::chat::{self, ChatId};
use crate::config::Config;
use crate::contact::ContactId;
use crate::context::Context;
use crate::imex::maybe_add_bcc_self_device_msg;
use crate::imex::set_self_key;
use crate::key::{load_self_secret_key, DcKey};
use crate::message::{Message, MsgId, Viewtype};
Expand Down Expand Up @@ -48,11 +47,10 @@ pub async fn initiate_key_transfer(context: &Context) -> Result<String> {
msg.param.set_int(Param::SkipAutocrypt, 1);

chat::send_msg(context, chat_id, &mut msg).await?;
// no maybe_add_bcc_self_device_msg() here.
// the ui shows the dialog with the setup code on this device,
// it would be too much noise to have two things popping up at the same time.
// maybe_add_bcc_self_device_msg() is called on the other device
// once the transfer is completed.

// Enable BCC-self, because transferring a key
// means we have a multi-device setup.
context.set_config_bool(Config::BccSelf, true).await?;
Ok(setup_code)
}

Expand All @@ -78,7 +76,7 @@ pub async fn continue_key_transfer(
let sc = normalize_setup_code(setup_code);
let armored_key = decrypt_setup_file(&sc, file).await?;
set_self_key(context, &armored_key, true).await?;
maybe_add_bcc_self_device_msg(context).await?;
context.set_config_bool(Config::BccSelf, true).await?;

Ok(())
} else {
Expand Down Expand Up @@ -301,8 +299,12 @@ mod tests {
async fn test_key_transfer() -> Result<()> {
let alice = TestContext::new_alice().await;

alice.set_config(Config::BccSelf, Some("0")).await?;
let setup_code = initiate_key_transfer(&alice).await?;

// Test that sending Autocrypt Setup Message enables `bcc_self`.
assert_eq!(alice.get_config_bool(Config::BccSelf).await?, true);

// Get Autocrypt Setup Message.
let sent = alice.pop_sent_msg().await;

Expand All @@ -322,12 +324,14 @@ mod tests {
assert_ne!(alice.get_last_msg().await.get_text(), "Test");

// Transfer the key.
alice2.set_config(Config::BccSelf, Some("0")).await?;
continue_key_transfer(&alice2, msg.id, &setup_code).await?;
assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true);

// Alice sends a message to self from the new device.
let sent = alice2.send_text(msg.chat_id, "Test").await;
alice.recv_msg(&sent).await;
assert_eq!(alice.get_last_msg().await.get_text(), "Test");
let rcvd_msg = alice.recv_msg(&sent).await;
assert_eq!(rcvd_msg.get_text(), "Test");

Ok(())
}
Expand Down

0 comments on commit b916937

Please sign in to comment.