-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include the bot's public key in Delta Chat
- Loading branch information
Showing
2 changed files
with
78 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,12 @@ use std::time::{Duration, Instant, SystemTime}; | |
|
||
use anyhow::{bail, ensure, Context as _, Result}; | ||
use async_channel::{self as channel, Receiver, Sender}; | ||
use pgp::SignedPublicKey; | ||
use ratelimit::Ratelimit; | ||
use tokio::sync::{Mutex, Notify, RwLock}; | ||
|
||
use crate::chat::{get_chat_cnt, ChatId}; | ||
use crate::aheader::EncryptPreference; | ||
use crate::chat::{get_chat_cnt, ChatId, ProtectionStatus}; | ||
use crate::config::Config; | ||
use crate::constants::{DC_CHAT_ID_TRASH, DC_VERSION_STR}; | ||
use crate::contact::Contact; | ||
|
@@ -22,6 +24,7 @@ use crate::events::{Event, EventEmitter, EventType, Events}; | |
use crate::key::{load_self_public_key, load_self_secret_key, DcKey as _}; | ||
use crate::login_param::LoginParam; | ||
use crate::message::{self, Message, MessageState, MsgId, Viewtype}; | ||
use crate::peerstate::{Peerstate, PeerstateKeyType}; | ||
use crate::quota::QuotaInfo; | ||
use crate::scheduler::SchedulerState; | ||
use crate::sql::Sql; | ||
|
@@ -831,7 +834,7 @@ impl Context { | |
id | ||
} | ||
}; | ||
res += &format!("self_reporting_id {}\n", self_reporting_id); | ||
res += &format!("self_reporting_id {}", self_reporting_id); | ||
|
||
Ok(res) | ||
} | ||
|
@@ -844,8 +847,36 @@ impl Context { | |
pub async fn draft_self_report(&self) -> Result<ChatId> { | ||
const SELF_REPORTING_BOT: &str = "[email protected]"; | ||
|
||
let contact = Contact::create(self, "Statistics bot", SELF_REPORTING_BOT).await?; | ||
let chat_id = ChatId::create_for_contact(self, contact).await?; | ||
let contact_id = Contact::create(self, "Statistics bot", SELF_REPORTING_BOT).await?; | ||
let chat_id = ChatId::create_for_contact(self, contact_id).await?; | ||
|
||
// We're including the bot's public key in Delta Chat | ||
// so that the first message to the bot can directly be encrypted: | ||
let public_key = SignedPublicKey::from_base64( | ||
"xjMEZbfBlBYJKwYBBAHaRw8BAQdABpLWS2PUIGGo4pslVt4R8sylP5wZihmhf1DTDr3oCM\ | ||
PNHDxzZWxmX3JlcG9ydGluZ0B0ZXN0cnVuLm9yZz7CiwQQFggAMwIZAQUCZbfBlAIbAwQLCQgHBhUI\ | ||
CQoLAgMWAgEWIQTS2i16sHeYTckGn284K3M5Z4oohAAKCRA4K3M5Z4oohD8dAQCQV7CoH6UP4PD+Nq\ | ||
I4kW5tbbqdh2AnDROg60qotmLExAEAxDfd3QHAK9f8b9qQUbLmHIztCLxhEuVbWPBEYeVW0gvOOARl\ | ||
t8GUEgorBgEEAZdVAQUBAQdAMBUhYoAAcI625vGZqnM5maPX4sGJ7qvJxPAFILPy6AcDAQgHwngEGB\ | ||
YIACAFAmW3wZQCGwwWIQTS2i16sHeYTckGn284K3M5Z4oohAAKCRA4K3M5Z4oohPwCAQCvzk1ObIkj\ | ||
2GqsuIfaULlgdnfdZY8LNary425CEfHZDQD5AblXVrlMO1frdlc/Vo9z3pEeCrfYdD7ITD3/OeVoiQ\ | ||
4=", | ||
)?; | ||
let mut peerstate = Peerstate::from_public_key( | ||
SELF_REPORTING_BOT, | ||
0, | ||
EncryptPreference::Mutual, | ||
&public_key, | ||
); | ||
peerstate.set_verified( | ||
PeerstateKeyType::PublicKey, | ||
public_key.fingerprint(), | ||
"".to_string(), | ||
)?; | ||
peerstate.save_to_db(&self.sql).await?; | ||
chat_id | ||
.set_protection(self, ProtectionStatus::Protected, time(), Some(contact_id)) | ||
.await?; | ||
|
||
let mut msg = Message::new(Viewtype::Text); | ||
msg.text = self.get_self_report().await?; | ||
|
@@ -1125,8 +1156,9 @@ mod tests { | |
use crate::constants::Chattype; | ||
use crate::contact::ContactId; | ||
use crate::message::{Message, Viewtype}; | ||
use crate::mimeparser::SystemMessage; | ||
use crate::receive_imf::receive_imf; | ||
use crate::test_utils::TestContext; | ||
use crate::test_utils::{get_chat_msg, TestContext}; | ||
use crate::tools::create_outgoing_rfc724_mid; | ||
|
||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
|
@@ -1665,4 +1697,24 @@ mod tests { | |
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
async fn test_draft_self_report() -> Result<()> { | ||
let alice = TestContext::new_alice().await; | ||
|
||
let chat_id = alice.draft_self_report().await?; | ||
let msg = get_chat_msg(&alice, chat_id, 0, 1).await; | ||
assert_eq!(msg.get_info_type(), SystemMessage::ChatProtectionEnabled); | ||
|
||
let chat = Chat::load_from_db(&alice, chat_id).await?; | ||
assert!(chat.is_protected()); | ||
|
||
let mut draft = chat_id.get_draft(&alice).await?.unwrap(); | ||
assert!(draft.text.starts_with("core_version")); | ||
|
||
// Test that sending into the protected chat works: | ||
let sent = alice.send_msg(chat_id, &mut draft).await; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters