diff --git a/src/delta/system.rs b/src/delta/system.rs index 67210e6..f2a1df7 100644 --- a/src/delta/system.rs +++ b/src/delta/system.rs @@ -7,7 +7,6 @@ use crate::messages::{ }; // use crate::{Handle, ChatInfo}; use async_std::channel::{self, Receiver}; - use deltachat::accounts::Accounts; const CHANNEL_CAPACITY: usize = 1024; @@ -32,19 +31,27 @@ pub fn spawn() -> DeltaSystemHandle { pub fn run(tx: FdSender, rx: Receiver) { logging::set_thread_logger(logging::RemoteLogger(tx.clone())); log::info!("Starting Delta system"); - let mut system = DeltaSystem::new(tx, rx); + log::debug!("Performing delta accounts setup"); + + let mut config_dir = DeltaSystem::user_dir(); + config_dir.push("purple-plugin-delta"); + + let accounts = + async_std::task::block_on(Accounts::new("purple-plugin-delta".into(), config_dir)).unwrap(); + + let mut system = DeltaSystem::new(tx, rx, accounts); async_std::task::block_on(system.run()); } pub struct DeltaSystem { tx: FdSender, rx: Receiver, - accounts: Option, + accounts: Accounts, } impl DeltaSystem { - fn new(tx: FdSender, rx: Receiver) -> Self { - Self { tx, rx, accounts: None } + fn new(tx: FdSender, rx: Receiver, accounts: Accounts) -> Self { + Self { tx, rx, accounts } } fn user_dir() -> async_std::path::PathBuf { @@ -59,12 +66,6 @@ impl DeltaSystem { } async fn run(&mut self) { - log::debug!("Performing delta accounts setup"); - let mut config_dir = DeltaSystem::user_dir(); - config_dir.push("purple-plugin-delta"); - - self.accounts = Some(Accounts::new("purple-plugin-delta".into(), config_dir).await.unwrap()); - log::info!("Looping on messages"); loop { let purple_message = match self.rx.recv().await { @@ -92,13 +93,34 @@ impl DeltaSystem { async fn login(&mut self, account_info: AccountInfo) -> std::result::Result<(), String> { log::debug!("login"); - /* - let phone_number = { account_info.protocol_data.phone_number.clone() }; + let email_address = { account_info.protocol_data.email_address.clone() }; + let password = { account_info.protocol_data.imap_pass.clone() }; let handle = &account_info.handle; + + // TODO: make this properly async + let ctx = match self.accounts.get_all().await.into_iter() + .map( |id| async_std::task::block_on(self.accounts.get_account(id)).unwrap() ) + .find( |ctx| async_std::task::block_on(ctx.is_self_addr(&email_address)).unwrap() ) { + None => { + let id = self.accounts.add_account().await.unwrap(); + self.accounts.get_account(id).await.unwrap() + }, + Some(ctx) => ctx + }; + + // Now transpose config into ctx. TODO: rest of the fields + ctx.set_config(deltachat::config::Config::Addr, Some(&email_address)).await.unwrap(); + ctx.set_config(deltachat::config::Config::MailPw, Some(&password)).await.unwrap(); + + ctx.configure().await.unwrap(); + ctx.start_io(); // TODO: what to do with this future? + // TODO: set off event emitter + + /* let mut registered_account_info = { self.tx .account_proxy(&handle) - .exec(|account| { + .exec(|purple_account| { let token = account.get_string(protocol::RegistrationData::TOKEN_SETTING_KEY, ""); if token.is_empty() { diff --git a/src/lib.rs b/src/lib.rs index 7dca262..506048a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,6 +76,7 @@ pub struct MsgInfo { #[derive(Debug, Default)] pub struct AccountData { + email_address: String, display_name: String, imap_host: String, imap_port: String, @@ -139,7 +140,8 @@ impl purple::PrplPlugin for PurpleDelta { .with_string_option("Display Name".into(), "displayname".into(), "".into()) .with_string_option("IMAP server host".into(), "mail_server".into(), "".into()) .with_string_option("IMAP server port".into(), "mail_port".into(), "".into()) - // Username and password are mail_user and mail_pw + .with_string_option("IMAP server username".into(), "mail_user".into(), "".into()) + // Password is account password .with_string_option("SMTP server host".into(), "send_server".into(), "".into()) .with_string_option("SMTP server port".into(), "send_port".into(), "".into()) .with_string_option("SMTP server username".into(), "send_user".into(), "".into()) @@ -163,11 +165,12 @@ impl purple::PrplPlugin for PurpleDelta { impl purple::LoginHandler for PurpleDelta { fn login(&mut self, account: &mut Account) { + let email_address = account.get_username().unwrap().into(); let display_name = account.get_string("displayname", ""); let imap_host = account.get_string("mail_server", ""); let imap_port = account.get_string("mail_port", ""); - let imap_user = account.get_username().unwrap().into(); + let imap_user = account.get_string("mail_user", ""); let imap_pass = account.get_password().unwrap().into(); let smtp_host = account.get_string("send_server", ""); @@ -178,6 +181,7 @@ impl purple::LoginHandler for PurpleDelta { let bcc_self = account.get_bool("bcc_self", false); let protocol_data: AccountDataBox = Arc::new(AccountData { + email_address, display_name, imap_host,