|
|
|
|
|
|
|
|
use std::env; |
|
|
use std::path::Path; |
|
|
use std::str::FromStr; |
|
|
|
|
|
use anyhow::{ensure, Context as _, Result}; |
|
|
use base64::Engine as _; |
|
|
use deltachat_contact_tools::addr_cmp; |
|
|
use serde::{Deserialize, Serialize}; |
|
|
use strum::{EnumProperty, IntoEnumIterator}; |
|
|
use strum_macros::{AsRefStr, Display, EnumIter, EnumString}; |
|
|
use tokio::fs; |
|
|
|
|
|
use crate::blob::BlobObject; |
|
|
use crate::constants::{self, DC_VERSION_STR}; |
|
|
use crate::context::Context; |
|
|
use crate::events::EventType; |
|
|
use crate::log::LogExt; |
|
|
use crate::mimefactory::RECOMMENDED_FILE_SIZE; |
|
|
use crate::provider::{get_provider_by_id, Provider}; |
|
|
use crate::sync::{self, Sync::*, SyncData}; |
|
|
use crate::tools::{get_abs_path, improve_single_line_input}; |
|
|
|
|
|
|
|
|
#[derive( |
|
|
Debug, |
|
|
Clone, |
|
|
Copy, |
|
|
PartialEq, |
|
|
Eq, |
|
|
Display, |
|
|
EnumString, |
|
|
AsRefStr, |
|
|
EnumIter, |
|
|
EnumProperty, |
|
|
PartialOrd, |
|
|
Ord, |
|
|
Serialize, |
|
|
Deserialize, |
|
|
)] |
|
|
#[strum(serialize_all = "snake_case")] |
|
|
pub enum Config { |
|
|
|
|
|
Addr, |
|
|
|
|
|
|
|
|
MailServer, |
|
|
|
|
|
|
|
|
MailUser, |
|
|
|
|
|
|
|
|
MailPw, |
|
|
|
|
|
|
|
|
MailPort, |
|
|
|
|
|
|
|
|
MailSecurity, |
|
|
|
|
|
|
|
|
ImapCertificateChecks, |
|
|
|
|
|
|
|
|
SendServer, |
|
|
|
|
|
|
|
|
SendUser, |
|
|
|
|
|
|
|
|
SendPw, |
|
|
|
|
|
|
|
|
SendPort, |
|
|
|
|
|
|
|
|
SendSecurity, |
|
|
|
|
|
|
|
|
SmtpCertificateChecks, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ServerFlags, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socks5Enabled, |
|
|
|
|
|
|
|
|
Socks5Host, |
|
|
|
|
|
|
|
|
Socks5Port, |
|
|
|
|
|
|
|
|
Socks5User, |
|
|
|
|
|
|
|
|
Socks5Password, |
|
|
|
|
|
|
|
|
Displayname, |
|
|
|
|
|
|
|
|
Selfstatus, |
|
|
|
|
|
|
|
|
Selfavatar, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "1"))] |
|
|
BccSelf, |
|
|
|
|
|
|
|
|
#[strum(props(default = "1"))] |
|
|
E2eeEnabled, |
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "1"))] |
|
|
MdnsEnabled, |
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
SentboxWatch, |
|
|
|
|
|
|
|
|
#[strum(props(default = "1"))] |
|
|
MvboxMove, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
OnlyFetchMvbox, |
|
|
|
|
|
|
|
|
#[strum(props(default = "2"))] |
|
|
ShowEmails, |
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
MediaQuality, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
FetchExistingMsgs, |
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "1"))] |
|
|
FetchedExistingMsgs, |
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
KeyGenType, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
DeleteServerAfter, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
DeleteDeviceAfter, |
|
|
|
|
|
|
|
|
|
|
|
DeleteToTrash, |
|
|
|
|
|
|
|
|
SaveMimeHeaders, |
|
|
|
|
|
|
|
|
ConfiguredAddr, |
|
|
|
|
|
|
|
|
ConfiguredMailServer, |
|
|
|
|
|
|
|
|
ConfiguredMailUser, |
|
|
|
|
|
|
|
|
ConfiguredMailPw, |
|
|
|
|
|
|
|
|
ConfiguredMailPort, |
|
|
|
|
|
|
|
|
ConfiguredMailSecurity, |
|
|
|
|
|
|
|
|
ConfiguredImapCertificateChecks, |
|
|
|
|
|
|
|
|
ConfiguredSendServer, |
|
|
|
|
|
|
|
|
ConfiguredSendUser, |
|
|
|
|
|
|
|
|
ConfiguredSendPw, |
|
|
|
|
|
|
|
|
ConfiguredSendPort, |
|
|
|
|
|
|
|
|
ConfiguredSmtpCertificateChecks, |
|
|
|
|
|
|
|
|
ConfiguredServerFlags, |
|
|
|
|
|
|
|
|
ConfiguredSendSecurity, |
|
|
|
|
|
|
|
|
ConfiguredInboxFolder, |
|
|
|
|
|
|
|
|
ConfiguredMvboxFolder, |
|
|
|
|
|
|
|
|
ConfiguredSentboxFolder, |
|
|
|
|
|
|
|
|
ConfiguredTrashFolder, |
|
|
|
|
|
|
|
|
ConfiguredTimestamp, |
|
|
|
|
|
|
|
|
ConfiguredProvider, |
|
|
|
|
|
|
|
|
Configured, |
|
|
|
|
|
|
|
|
IsChatmail, |
|
|
|
|
|
|
|
|
|
|
|
SecondaryAddrs, |
|
|
|
|
|
|
|
|
#[strum(serialize = "sys.version")] |
|
|
SysVersion, |
|
|
|
|
|
|
|
|
#[strum(serialize = "sys.msgsize_max_recommended")] |
|
|
SysMsgsizeMaxRecommended, |
|
|
|
|
|
|
|
|
#[strum(serialize = "sys.config_keys")] |
|
|
SysConfigKeys, |
|
|
|
|
|
|
|
|
Bot, |
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
SkipStartMessages, |
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
NotifyAboutWrongPw, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QuotaExceeding, |
|
|
|
|
|
|
|
|
WebrtcInstance, |
|
|
|
|
|
|
|
|
LastHousekeeping, |
|
|
|
|
|
|
|
|
LastCantDecryptOutgoingMsgs, |
|
|
|
|
|
|
|
|
#[strum(props(default = "60"))] |
|
|
ScanAllFoldersDebounceSecs, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
DisableIdle, |
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
DownloadLimit, |
|
|
|
|
|
|
|
|
#[strum(props(default = "1"))] |
|
|
SyncMsgs, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AuthservIdCandidates, |
|
|
|
|
|
|
|
|
SignUnencrypted, |
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
DebugLogging, |
|
|
|
|
|
|
|
|
LastMsgId, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "172800"))] |
|
|
GossipPeriod, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[strum(props(default = "0"))] |
|
|
VerifiedOneOnOneChats, |
|
|
|
|
|
|
|
|
|
|
|
KeyId, |
|
|
|
|
|
|
|
|
|
|
|
SelfReportingId, |
|
|
|
|
|
|
|
|
WebxdcIntegration, |
|
|
|
|
|
|
|
|
IrohSecretKey, |
|
|
} |
|
|
|
|
|
impl Config { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) fn is_synced(&self) -> bool { |
|
|
|
|
|
if self.needs_io_restart() { |
|
|
return false; |
|
|
} |
|
|
matches!( |
|
|
self, |
|
|
Self::Displayname |
|
|
| Self::MdnsEnabled |
|
|
| Self::ShowEmails |
|
|
| Self::Selfavatar |
|
|
| Self::Selfstatus, |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
pub(crate) fn needs_io_restart(&self) -> bool { |
|
|
matches!( |
|
|
self, |
|
|
Config::MvboxMove | Config::OnlyFetchMvbox | Config::SentboxWatch |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
impl Context { |
|
|
|
|
|
pub async fn config_exists(&self, key: Config) -> Result<bool> { |
|
|
Ok(self.sql.get_raw_config(key.as_ref()).await?.is_some()) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn get_config(&self, key: Config) -> Result<Option<String>> { |
|
|
let env_key = format!("DELTACHAT_{}", key.as_ref().to_uppercase()); |
|
|
if let Ok(value) = env::var(env_key) { |
|
|
return Ok(Some(value)); |
|
|
} |
|
|
|
|
|
let value = match key { |
|
|
Config::Selfavatar => { |
|
|
let rel_path = self.sql.get_raw_config(key.as_ref()).await?; |
|
|
rel_path.map(|p| { |
|
|
get_abs_path(self, Path::new(&p)) |
|
|
.to_string_lossy() |
|
|
.into_owned() |
|
|
}) |
|
|
} |
|
|
Config::SysVersion => Some((*DC_VERSION_STR).clone()), |
|
|
Config::SysMsgsizeMaxRecommended => Some(format!("{RECOMMENDED_FILE_SIZE}")), |
|
|
Config::SysConfigKeys => Some(get_config_keys_string()), |
|
|
_ => self.sql.get_raw_config(key.as_ref()).await?, |
|
|
}; |
|
|
|
|
|
if value.is_some() { |
|
|
return Ok(value); |
|
|
} |
|
|
|
|
|
|
|
|
match key { |
|
|
Config::ConfiguredInboxFolder => Ok(Some("INBOX".to_owned())), |
|
|
_ => Ok(key.get_str("default").map(|s| s.to_string())), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_config_parsed<T: FromStr>(&self, key: Config) -> Result<Option<T>> { |
|
|
self.get_config(key) |
|
|
.await |
|
|
.map(|s: Option<String>| s.and_then(|s| s.parse().ok())) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn get_config_int(&self, key: Config) -> Result<i32> { |
|
|
Ok(self.get_config_parsed(key).await?.unwrap_or_default()) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn get_config_u32(&self, key: Config) -> Result<u32> { |
|
|
Ok(self.get_config_parsed(key).await?.unwrap_or_default()) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn get_config_i64(&self, key: Config) -> Result<i64> { |
|
|
Ok(self.get_config_parsed(key).await?.unwrap_or_default()) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn get_config_u64(&self, key: Config) -> Result<u64> { |
|
|
Ok(self.get_config_parsed(key).await?.unwrap_or_default()) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn get_config_bool_opt(&self, key: Config) -> Result<Option<bool>> { |
|
|
Ok(self.get_config_parsed::<i32>(key).await?.map(|x| x != 0)) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn get_config_bool(&self, key: Config) -> Result<bool> { |
|
|
Ok(self.get_config_bool_opt(key).await?.unwrap_or_default()) |
|
|
} |
|
|
|
|
|
|
|
|
pub(crate) async fn should_watch_mvbox(&self) -> Result<bool> { |
|
|
Ok(self.get_config_bool(Config::MvboxMove).await? |
|
|
|| self.get_config_bool(Config::OnlyFetchMvbox).await?) |
|
|
} |
|
|
|
|
|
|
|
|
pub(crate) async fn should_watch_sentbox(&self) -> Result<bool> { |
|
|
Ok(self.get_config_bool(Config::SentboxWatch).await? |
|
|
&& self |
|
|
.get_config(Config::ConfiguredSentboxFolder) |
|
|
.await? |
|
|
.is_some()) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_config_delete_server_after(&self) -> Result<Option<i64>> { |
|
|
match self.get_config_int(Config::DeleteServerAfter).await? { |
|
|
0 => Ok(None), |
|
|
1 => Ok(Some(0)), |
|
|
x => Ok(Some(i64::from(x))), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_configured_provider(&self) -> Result<Option<&'static Provider>> { |
|
|
if let Some(cfg) = self.get_config(Config::ConfiguredProvider).await? { |
|
|
return Ok(get_provider_by_id(&cfg)); |
|
|
} |
|
|
Ok(None) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_config_delete_device_after(&self) -> Result<Option<i64>> { |
|
|
match self.get_config_int(Config::DeleteDeviceAfter).await? { |
|
|
0 => Ok(None), |
|
|
x => Ok(Some(i64::from(x))), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub(crate) async fn sync_config(&self, key: &Config, value: &str) -> Result<()> { |
|
|
let config_value; |
|
|
let value = match key { |
|
|
Config::Selfavatar if value.is_empty() => None, |
|
|
Config::Selfavatar => { |
|
|
config_value = BlobObject::store_from_base64(self, value, "avatar").await?; |
|
|
Some(config_value.as_str()) |
|
|
} |
|
|
_ => Some(value), |
|
|
}; |
|
|
match key.is_synced() { |
|
|
true => self.set_config_ex(Nosync, *key, value).await, |
|
|
false => Ok(()), |
|
|
} |
|
|
} |
|
|
|
|
|
fn check_config(key: Config, value: Option<&str>) -> Result<()> { |
|
|
match key { |
|
|
Config::Socks5Enabled |
|
|
| Config::BccSelf |
|
|
| Config::E2eeEnabled |
|
|
| Config::MdnsEnabled |
|
|
| Config::SentboxWatch |
|
|
| Config::MvboxMove |
|
|
| Config::OnlyFetchMvbox |
|
|
| Config::FetchExistingMsgs |
|
|
| Config::DeleteToTrash |
|
|
| Config::SaveMimeHeaders |
|
|
| Config::Configured |
|
|
| Config::Bot |
|
|
| Config::NotifyAboutWrongPw |
|
|
| Config::SyncMsgs |
|
|
| Config::SignUnencrypted |
|
|
| Config::DisableIdle => { |
|
|
ensure!( |
|
|
matches!(value, None | Some("0") | Some("1")), |
|
|
"Boolean value must be either 0 or 1" |
|
|
); |
|
|
} |
|
|
_ => (), |
|
|
} |
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn set_config(&self, key: Config, value: Option<&str>) -> Result<()> { |
|
|
Self::check_config(key, value)?; |
|
|
|
|
|
let _pause = match key.needs_io_restart() { |
|
|
true => self.scheduler.pause(self.clone()).await?, |
|
|
_ => Default::default(), |
|
|
}; |
|
|
self.set_config_internal(key, value).await?; |
|
|
if key == Config::SentboxWatch { |
|
|
self.last_full_folder_scan.lock().await.take(); |
|
|
} |
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
pub(crate) async fn set_config_internal(&self, key: Config, value: Option<&str>) -> Result<()> { |
|
|
self.set_config_ex(Sync, key, value).await |
|
|
} |
|
|
|
|
|
pub(crate) async fn set_config_ex( |
|
|
&self, |
|
|
sync: sync::Sync, |
|
|
key: Config, |
|
|
mut value: Option<&str>, |
|
|
) -> Result<()> { |
|
|
Self::check_config(key, value)?; |
|
|
let sync = sync == Sync && key.is_synced(); |
|
|
let better_value; |
|
|
|
|
|
match key { |
|
|
Config::Selfavatar => { |
|
|
self.sql |
|
|
.execute("UPDATE contacts SET selfavatar_sent=0;", ()) |
|
|
.await?; |
|
|
match value { |
|
|
Some(path) => { |
|
|
let mut blob = BlobObject::new_from_path(self, path.as_ref()).await?; |
|
|
blob.recode_to_avatar_size(self).await?; |
|
|
self.sql |
|
|
.set_raw_config(key.as_ref(), Some(blob.as_name())) |
|
|
.await?; |
|
|
if sync { |
|
|
let buf = fs::read(blob.to_abs_path()).await?; |
|
|
better_value = base64::engine::general_purpose::STANDARD.encode(buf); |
|
|
value = Some(&better_value); |
|
|
} |
|
|
} |
|
|
None => { |
|
|
self.sql.set_raw_config(key.as_ref(), None).await?; |
|
|
if sync { |
|
|
better_value = String::new(); |
|
|
value = Some(&better_value); |
|
|
} |
|
|
} |
|
|
} |
|
|
self.emit_event(EventType::SelfavatarChanged); |
|
|
} |
|
|
Config::DeleteDeviceAfter => { |
|
|
let ret = self.sql.set_raw_config(key.as_ref(), value).await; |
|
|
|
|
|
self.scheduler.interrupt_ephemeral_task().await; |
|
|
ret? |
|
|
} |
|
|
Config::Displayname => { |
|
|
if let Some(v) = value { |
|
|
better_value = improve_single_line_input(v); |
|
|
value = Some(&better_value); |
|
|
} |
|
|
self.sql.set_raw_config(key.as_ref(), value).await?; |
|
|
} |
|
|
Config::Addr => { |
|
|
self.sql |
|
|
.set_raw_config(key.as_ref(), value.map(|s| s.to_lowercase()).as_deref()) |
|
|
.await?; |
|
|
} |
|
|
Config::MvboxMove => { |
|
|
self.sql.set_raw_config(key.as_ref(), value).await?; |
|
|
self.sql |
|
|
.set_raw_config(constants::DC_FOLDERS_CONFIGURED_KEY, None) |
|
|
.await?; |
|
|
} |
|
|
_ => { |
|
|
self.sql.set_raw_config(key.as_ref(), value).await?; |
|
|
} |
|
|
} |
|
|
if key.is_synced() { |
|
|
self.emit_event(EventType::ConfigSynced { key }); |
|
|
} |
|
|
if !sync { |
|
|
return Ok(()); |
|
|
} |
|
|
let Some(val) = value else { |
|
|
return Ok(()); |
|
|
}; |
|
|
let val = val.to_string(); |
|
|
if self |
|
|
.add_sync_item(SyncData::Config { key, val }) |
|
|
.await |
|
|
.log_err(self) |
|
|
.is_err() |
|
|
{ |
|
|
return Ok(()); |
|
|
} |
|
|
Box::pin(self.send_sync_msg()).await.log_err(self).ok(); |
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn set_config_u32(&self, key: Config, value: u32) -> Result<()> { |
|
|
self.set_config(key, Some(&value.to_string())).await?; |
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn set_config_bool(&self, key: Config, value: bool) -> Result<()> { |
|
|
self.set_config(key, from_bool(value)).await?; |
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn set_ui_config(&self, key: &str, value: Option<&str>) -> Result<()> { |
|
|
ensure!(key.starts_with("ui."), "set_ui_config(): prefix missing."); |
|
|
self.sql.set_raw_config(key, value).await |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn get_ui_config(&self, key: &str) -> Result<Option<String>> { |
|
|
ensure!(key.starts_with("ui."), "get_ui_config(): prefix missing."); |
|
|
self.sql.get_raw_config(key).await |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub(crate) fn from_bool(val: bool) -> Option<&'static str> { |
|
|
Some(if val { "1" } else { "0" }) |
|
|
} |
|
|
|
|
|
|
|
|
impl Context { |
|
|
|
|
|
|
|
|
pub(crate) async fn is_self_addr(&self, addr: &str) -> Result<bool> { |
|
|
Ok(self |
|
|
.get_config(Config::ConfiguredAddr) |
|
|
.await? |
|
|
.iter() |
|
|
.any(|a| addr_cmp(addr, a)) |
|
|
|| self |
|
|
.get_secondary_self_addrs() |
|
|
.await? |
|
|
.iter() |
|
|
.any(|a| addr_cmp(addr, a))) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) async fn set_primary_self_addr(&self, primary_new: &str) -> Result<()> { |
|
|
|
|
|
let mut secondary_addrs = self.get_all_self_addrs().await?; |
|
|
|
|
|
secondary_addrs.retain(|a| !addr_cmp(a, primary_new)); |
|
|
self.set_config_internal( |
|
|
Config::SecondaryAddrs, |
|
|
Some(secondary_addrs.join(" ").as_str()), |
|
|
) |
|
|
.await?; |
|
|
|
|
|
self.set_config_internal(Config::ConfiguredAddr, Some(primary_new)) |
|
|
.await?; |
|
|
|
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
pub(crate) async fn get_all_self_addrs(&self) -> Result<Vec<String>> { |
|
|
let primary_addrs = self.get_config(Config::ConfiguredAddr).await?.into_iter(); |
|
|
let secondary_addrs = self.get_secondary_self_addrs().await?.into_iter(); |
|
|
|
|
|
Ok(primary_addrs.chain(secondary_addrs).collect()) |
|
|
} |
|
|
|
|
|
|
|
|
pub(crate) async fn get_secondary_self_addrs(&self) -> Result<Vec<String>> { |
|
|
let secondary_addrs = self |
|
|
.get_config(Config::SecondaryAddrs) |
|
|
.await? |
|
|
.unwrap_or_default(); |
|
|
Ok(secondary_addrs |
|
|
.split_ascii_whitespace() |
|
|
.map(|s| s.to_string()) |
|
|
.collect()) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_primary_self_addr(&self) -> Result<String> { |
|
|
self.get_config(Config::ConfiguredAddr) |
|
|
.await? |
|
|
.context("No self addr configured") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fn get_config_keys_string() -> String { |
|
|
let keys = Config::iter().fold(String::new(), |mut acc, key| { |
|
|
acc += key.as_ref(); |
|
|
acc += " "; |
|
|
acc |
|
|
}); |
|
|
|
|
|
format!(" {keys} ") |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use num_traits::FromPrimitive; |
|
|
|
|
|
use super::*; |
|
|
use crate::test_utils::{sync, TestContext, TestContextManager}; |
|
|
|
|
|
#[test] |
|
|
fn test_to_string() { |
|
|
assert_eq!(Config::MailServer.to_string(), "mail_server"); |
|
|
assert_eq!(Config::from_str("mail_server"), Ok(Config::MailServer)); |
|
|
|
|
|
assert_eq!(Config::SysConfigKeys.to_string(), "sys.config_keys"); |
|
|
assert_eq!( |
|
|
Config::from_str("sys.config_keys"), |
|
|
Ok(Config::SysConfigKeys) |
|
|
); |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_set_config_addr() { |
|
|
let t = TestContext::new().await; |
|
|
|
|
|
|
|
|
assert!(t |
|
|
.set_config(Config::Addr, Some("Foobar@eXample.oRg")) |
|
|
.await |
|
|
.is_ok()); |
|
|
assert_eq!( |
|
|
t.get_config(Config::Addr).await.unwrap().unwrap(), |
|
|
"foobar@example.org" |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_set_config_bot() { |
|
|
let t = TestContext::new().await; |
|
|
|
|
|
assert!(t.set_config(Config::Bot, None).await.is_ok()); |
|
|
assert!(t.set_config(Config::Bot, Some("0")).await.is_ok()); |
|
|
assert!(t.set_config(Config::Bot, Some("1")).await.is_ok()); |
|
|
assert!(t.set_config(Config::Bot, Some("2")).await.is_err()); |
|
|
assert!(t.set_config(Config::Bot, Some("Foobar")).await.is_err()); |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_media_quality_config_option() { |
|
|
let t = TestContext::new().await; |
|
|
let media_quality = t.get_config_int(Config::MediaQuality).await.unwrap(); |
|
|
assert_eq!(media_quality, 0); |
|
|
let media_quality = constants::MediaQuality::from_i32(media_quality).unwrap_or_default(); |
|
|
assert_eq!(media_quality, constants::MediaQuality::Balanced); |
|
|
|
|
|
t.set_config(Config::MediaQuality, Some("1")).await.unwrap(); |
|
|
|
|
|
let media_quality = t.get_config_int(Config::MediaQuality).await.unwrap(); |
|
|
assert_eq!(media_quality, 1); |
|
|
assert_eq!(constants::MediaQuality::Worse as i32, 1); |
|
|
let media_quality = constants::MediaQuality::from_i32(media_quality).unwrap_or_default(); |
|
|
assert_eq!(media_quality, constants::MediaQuality::Worse); |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_ui_config() -> Result<()> { |
|
|
let t = TestContext::new().await; |
|
|
|
|
|
assert_eq!(t.get_ui_config("ui.desktop.linux.systray").await?, None); |
|
|
|
|
|
t.set_ui_config("ui.android.screen_security", Some("safe")) |
|
|
.await?; |
|
|
assert_eq!( |
|
|
t.get_ui_config("ui.android.screen_security").await?, |
|
|
Some("safe".to_string()) |
|
|
); |
|
|
|
|
|
t.set_ui_config("ui.android.screen_security", None).await?; |
|
|
assert_eq!(t.get_ui_config("ui.android.screen_security").await?, None); |
|
|
|
|
|
assert!(t.set_ui_config("configured", Some("bar")).await.is_err()); |
|
|
|
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_set_config_bool() -> Result<()> { |
|
|
let t = TestContext::new().await; |
|
|
|
|
|
|
|
|
let c = Config::E2eeEnabled; |
|
|
assert_eq!(t.get_config_bool(c).await?, true); |
|
|
t.set_config_bool(c, false).await?; |
|
|
assert_eq!(t.get_config_bool(c).await?, false); |
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_self_addrs() -> Result<()> { |
|
|
let alice = TestContext::new_alice().await; |
|
|
|
|
|
assert!(alice.is_self_addr("alice@example.org").await?); |
|
|
assert_eq!(alice.get_all_self_addrs().await?, vec!["alice@example.org"]); |
|
|
assert!(!alice.is_self_addr("alice@alice.com").await?); |
|
|
|
|
|
|
|
|
alice.set_primary_self_addr("alice@example.org").await?; |
|
|
alice.set_primary_self_addr("Alice@Example.Org").await?; |
|
|
assert_eq!(alice.get_all_self_addrs().await?, vec!["Alice@Example.Org"]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
alice.set_primary_self_addr("Alice@alice.com").await?; |
|
|
assert!(alice.is_self_addr("aliCe@example.org").await?); |
|
|
assert!(alice.is_self_addr("alice@alice.com").await?); |
|
|
assert_eq!( |
|
|
alice.get_all_self_addrs().await?, |
|
|
vec!["Alice@alice.com", "Alice@Example.Org"] |
|
|
); |
|
|
|
|
|
|
|
|
alice.set_primary_self_addr("alice@alice.com").await?; |
|
|
alice.set_primary_self_addr("alice@alice.com").await?; |
|
|
assert_eq!( |
|
|
alice.get_all_self_addrs().await?, |
|
|
vec!["alice@alice.com", "Alice@Example.Org"] |
|
|
); |
|
|
|
|
|
|
|
|
alice.set_primary_self_addr("alice@example.org").await?; |
|
|
assert_eq!( |
|
|
alice.get_all_self_addrs().await?, |
|
|
vec!["alice@example.org", "alice@alice.com"] |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
alice.set_primary_self_addr("alice@alice.xyz").await?; |
|
|
assert_eq!( |
|
|
alice.get_all_self_addrs().await?, |
|
|
vec!["alice@alice.xyz", "alice@example.org", "alice@alice.com"] |
|
|
); |
|
|
assert!(alice.is_self_addr("alice@example.org").await?); |
|
|
assert!(alice.is_self_addr("alice@alice.com").await?); |
|
|
assert!(alice.is_self_addr("Alice@alice.xyz").await?); |
|
|
|
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_sync() -> Result<()> { |
|
|
let alice0 = TestContext::new_alice().await; |
|
|
let alice1 = TestContext::new_alice().await; |
|
|
for a in [&alice0, &alice1] { |
|
|
a.set_config_bool(Config::SyncMsgs, true).await?; |
|
|
} |
|
|
|
|
|
let mdns_enabled = alice0.get_config_bool(Config::MdnsEnabled).await?; |
|
|
|
|
|
alice1 |
|
|
.set_config_bool(Config::MdnsEnabled, !mdns_enabled) |
|
|
.await?; |
|
|
|
|
|
alice0 |
|
|
.set_config_bool(Config::MdnsEnabled, mdns_enabled) |
|
|
.await?; |
|
|
sync(&alice0, &alice1).await; |
|
|
assert_eq!( |
|
|
alice1.get_config_bool(Config::MdnsEnabled).await?, |
|
|
mdns_enabled |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
alice0.set_config(Config::MdnsEnabled, None).await?; |
|
|
assert_eq!(alice0.get_config_bool(Config::MdnsEnabled).await?, true); |
|
|
alice0.set_config_bool(Config::MdnsEnabled, false).await?; |
|
|
sync(&alice0, &alice1).await; |
|
|
assert_eq!(alice1.get_config_bool(Config::MdnsEnabled).await?, false); |
|
|
|
|
|
let show_emails = alice0.get_config_bool(Config::ShowEmails).await?; |
|
|
alice0 |
|
|
.set_config_bool(Config::ShowEmails, !show_emails) |
|
|
.await?; |
|
|
sync(&alice0, &alice1).await; |
|
|
assert_eq!( |
|
|
alice1.get_config_bool(Config::ShowEmails).await?, |
|
|
!show_emails |
|
|
); |
|
|
|
|
|
|
|
|
alice0.set_config_bool(Config::SyncMsgs, false).await?; |
|
|
alice0.set_config_bool(Config::SyncMsgs, true).await?; |
|
|
alice0.set_config_bool(Config::MdnsEnabled, true).await?; |
|
|
sync(&alice0, &alice1).await; |
|
|
assert!(alice1.get_config_bool(Config::MdnsEnabled).await?); |
|
|
|
|
|
|
|
|
async fn test_config_str( |
|
|
alice0: &TestContext, |
|
|
alice1: &TestContext, |
|
|
key: Config, |
|
|
val: &str, |
|
|
) -> Result<()> { |
|
|
alice0.set_config(key, Some(val)).await?; |
|
|
sync(alice0, alice1).await; |
|
|
assert_eq!(alice1.get_config(key).await?, Some(val.to_string())); |
|
|
Ok(()) |
|
|
} |
|
|
test_config_str(&alice0, &alice1, Config::Displayname, "Alice Sync").await?; |
|
|
test_config_str(&alice0, &alice1, Config::Selfstatus, "My status").await?; |
|
|
|
|
|
assert!(alice0.get_config(Config::Selfavatar).await?.is_none()); |
|
|
let file = alice0.dir.path().join("avatar.png"); |
|
|
let bytes = include_bytes!("../test-data/image/avatar64x64.png"); |
|
|
tokio::fs::write(&file, bytes).await?; |
|
|
alice0 |
|
|
.set_config(Config::Selfavatar, Some(file.to_str().unwrap())) |
|
|
.await?; |
|
|
sync(&alice0, &alice1).await; |
|
|
|
|
|
|
|
|
|
|
|
let self_chat = alice0.get_self_chat().await; |
|
|
let self_chat_avatar_path = self_chat.get_profile_image(&alice0).await?.unwrap(); |
|
|
assert_eq!( |
|
|
self_chat_avatar_path, |
|
|
alice0.get_blobdir().join("icon-saved-messages.png") |
|
|
); |
|
|
assert!(alice1 |
|
|
.get_config(Config::Selfavatar) |
|
|
.await? |
|
|
.filter(|path| path.ends_with(".png")) |
|
|
.is_some()); |
|
|
alice0.set_config(Config::Selfavatar, None).await?; |
|
|
sync(&alice0, &alice1).await; |
|
|
assert!(alice1.get_config(Config::Selfavatar).await?.is_none()); |
|
|
|
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_no_sync_on_self_sent_msg() -> Result<()> { |
|
|
let mut tcm = TestContextManager::new(); |
|
|
let alice0 = &tcm.alice().await; |
|
|
let alice1 = &tcm.alice().await; |
|
|
for a in [alice0, alice1] { |
|
|
a.set_config_bool(Config::SyncMsgs, true).await?; |
|
|
} |
|
|
|
|
|
let status = "Synced via usual message"; |
|
|
alice0.set_config(Config::Selfstatus, Some(status)).await?; |
|
|
alice0.pop_sent_msg().await; |
|
|
let status1 = "Synced via sync message"; |
|
|
alice1.set_config(Config::Selfstatus, Some(status1)).await?; |
|
|
tcm.send_recv(alice0, alice1, "hi Alice!").await; |
|
|
assert_eq!( |
|
|
alice1.get_config(Config::Selfstatus).await?, |
|
|
Some(status.to_string()) |
|
|
); |
|
|
sync(alice1, alice0).await; |
|
|
assert_eq!( |
|
|
alice0.get_config(Config::Selfstatus).await?, |
|
|
Some(status1.to_string()) |
|
|
); |
|
|
|
|
|
|
|
|
let bob = &tcm.bob().await; |
|
|
let a0b_chat_id = tcm.send_recv_accept(bob, alice0, "hi").await.chat_id; |
|
|
let file = alice0.dir.path().join("avatar.png"); |
|
|
let bytes = include_bytes!("../test-data/image/avatar64x64.png"); |
|
|
tokio::fs::write(&file, bytes).await?; |
|
|
alice0 |
|
|
.set_config(Config::Selfavatar, Some(file.to_str().unwrap())) |
|
|
.await?; |
|
|
alice0.pop_sent_msg().await; |
|
|
let file = alice1.dir.path().join("avatar.jpg"); |
|
|
let bytes = include_bytes!("../test-data/image/avatar1000x1000.jpg"); |
|
|
tokio::fs::write(&file, bytes).await?; |
|
|
alice1 |
|
|
.set_config(Config::Selfavatar, Some(file.to_str().unwrap())) |
|
|
.await?; |
|
|
let sent_msg = alice0.send_text(a0b_chat_id, "hi").await; |
|
|
alice1.recv_msg(&sent_msg).await; |
|
|
assert!(alice1 |
|
|
.get_config(Config::Selfavatar) |
|
|
.await? |
|
|
.filter(|path| path.ends_with(".png")) |
|
|
.is_some()); |
|
|
sync(alice1, alice0).await; |
|
|
assert!(alice0 |
|
|
.get_config(Config::Selfavatar) |
|
|
.await? |
|
|
.filter(|path| path.ends_with(".jpg")) |
|
|
.is_some()); |
|
|
|
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_event_config_synced() -> Result<()> { |
|
|
let alice0 = TestContext::new_alice().await; |
|
|
let alice1 = TestContext::new_alice().await; |
|
|
for a in [&alice0, &alice1] { |
|
|
a.set_config_bool(Config::SyncMsgs, true).await?; |
|
|
} |
|
|
|
|
|
alice0 |
|
|
.set_config(Config::Displayname, Some("Alice Sync")) |
|
|
.await?; |
|
|
alice0 |
|
|
.evtracker |
|
|
.get_matching(|e| { |
|
|
matches!( |
|
|
e, |
|
|
EventType::ConfigSynced { |
|
|
key: Config::Displayname |
|
|
} |
|
|
) |
|
|
}) |
|
|
.await; |
|
|
sync(&alice0, &alice1).await; |
|
|
assert_eq!( |
|
|
alice1.get_config(Config::Displayname).await?, |
|
|
Some("Alice Sync".to_string()) |
|
|
); |
|
|
alice1 |
|
|
.evtracker |
|
|
.get_matching(|e| { |
|
|
matches!( |
|
|
e, |
|
|
EventType::ConfigSynced { |
|
|
key: Config::Displayname |
|
|
} |
|
|
) |
|
|
}) |
|
|
.await; |
|
|
|
|
|
alice0.set_config(Config::Displayname, None).await?; |
|
|
alice0 |
|
|
.evtracker |
|
|
.get_matching(|e| { |
|
|
matches!( |
|
|
e, |
|
|
EventType::ConfigSynced { |
|
|
key: Config::Displayname |
|
|
} |
|
|
) |
|
|
}) |
|
|
.await; |
|
|
|
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|
|