Start running delta connections. Lots of problems still

This commit is contained in:
2021-10-31 21:17:42 +00:00
parent eec8669b88
commit 29ea694b1e
6 changed files with 104 additions and 158 deletions

View File

@@ -2,8 +2,8 @@
// use super::protocol; // use super::protocol;
use crate::logging; use crate::logging;
use crate::messages::{ use crate::messages::{
AccountInfo, FdSender, GetChatInfoMessage, GetHistoryMessage, DeltaSystemHandle, JoinChatMessage, AccountInfo, DeltaSystemHandle, FdSender, GetChatInfoMessage, GetHistoryMessage,
PurpleMessage, SendMsgMessage, SystemMessage, JoinChatMessage, PurpleMessage, SendMsgMessage, SystemMessage,
}; };
// use crate::{Handle, ChatInfo}; // use crate::{Handle, ChatInfo};
use async_std::channel::{self, Receiver}; use async_std::channel::{self, Receiver};
@@ -97,113 +97,56 @@ impl DeltaSystem {
let password = { account_info.protocol_data.imap_pass.clone() }; let password = { account_info.protocol_data.imap_pass.clone() };
let handle = &account_info.handle; 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(|purple_account| {
let token =
account.get_string(protocol::RegistrationData::TOKEN_SETTING_KEY, "");
if token.is_empty() {
None
} else {
Some(protocol::RegistrationData {
token,
session_id: account
.get_string(protocol::RegistrationData::SESSION_ID_SETTING_KEY, ""),
session_key: account.get_string(
protocol::RegistrationData::SESSION_KEY_SETTING_KEY,
"",
),
host_time: account
.get_int(protocol::RegistrationData::HOST_TIME_SETTING_KEY, 0)
as u32,
})
}
})
.await
.ok_or_else(|| "Failed to read settings".to_string())?
};
if registered_account_info.is_none() {
let info = protocol::register(&phone_number, || {
log::debug!("read_code");
self.read_code(&account_info.handle)
})
.await
.map_err(|e| format!("Failed to register account: {:?}", e))?;
self.tx
.account_proxy(&handle)
.set_settings(info.clone())
.await
.map_err(|e| format!("Failed to write settings: {:?}", e))?;
registered_account_info = Some(info);
}
log::debug!("Registered account info: {:?}", registered_account_info);
if registered_account_info.is_none() {
self.tx
.connection_proxy(&handle)
.error_reason(
purple::PurpleConnectionError::PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
"Failed to register account".into(),
)
.await;
return Err("Failed to register account".into());
}
if let Some(registered_account_info) = registered_account_info {
self.tx self.tx
.connection_proxy(&handle) .connection_proxy(&handle)
.set_state(purple::PurpleConnectionState::PURPLE_CONNECTING) .set_state(purple::PurpleConnectionState::PURPLE_CONNECTING)
.await; .await;
let session_info = protocol::start_session(&registered_account_info).await; // TODO: make this properly async
log::debug!("Session info: {:?}", session_info); let ctx = match self
match session_info { .accounts
Ok(session) => { .get_all()
self.tx .await
.connection_proxy(&handle) .into_iter()
.set_state(purple::PurpleConnectionState::PURPLE_CONNECTED) .map(|id| async_std::task::block_on(self.accounts.get_account(id)).unwrap())
.await; .find(|ctx| {
(*account_info.protocol_data.session.write().await) = Some(session); async_std::task::block_on(ctx.is_self_addr(&email_address)).unwrap_or(false)
async_std::task::spawn_local(poller::fetch_events_loop( }) {
self.tx.clone(), None => {
account_info.clone(), let id = self.accounts.add_account().await.unwrap();
)); self.accounts.get_account(id).await.unwrap()
} }
Err(error) => { Some(ctx) => ctx,
let error_message = format!("Failed to start session: {:?}", error); };
self.tx
.connection_proxy(&handle) // Now transpose config into ctx. TODO: rest of the fields
.error_reason(purple::PurpleConnectionError::PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, ctx.set_config(deltachat::config::Config::Addr, Some(&email_address))
error_message.clone()).await; .await
return Err(error_message); .unwrap();
ctx.set_config(deltachat::config::Config::MailPw, Some(&password))
.await
.unwrap();
// FIXME: handle configuration failure nicely here. Right now we just panic.
ctx.configure().await.unwrap();
ctx.start_io().await;
async_std::task::spawn_local(DeltaSystem::deltachat_events(ctx));
// Hint from deleted code:
// self.tx.account_proxy(&handle).exec(|purple_account| ...);
Ok(())
} }
async fn deltachat_events(ctx: deltachat::context::Context) {
// TODO: loop until we're out of events
let emitter = ctx.get_event_emitter();
while let Some(event) = emitter.recv().await {
println!("Received event {:?}", event);
} }
}
*/ // FIXME: this is back to front. We need to stop_io to interrupt the loop
Err("TODO".into()) ctx.stop_io().await;
} }
async fn get_chat_info(&mut self, message: GetChatInfoMessage) -> Result<(), String> { async fn get_chat_info(&mut self, message: GetChatInfoMessage) -> Result<(), String> {
@@ -260,7 +203,7 @@ impl DeltaSystem {
Ok(()) Ok(())
} }
async fn get_history(&mut self, get_history_message: GetHistoryMessage) -> Result<(), String> { async fn get_history(&mut self, _get_history_message: GetHistoryMessage) -> Result<(), String> {
/* /*
let session = { let session = {
get_history_message get_history_message
@@ -307,4 +250,3 @@ impl DeltaSystem {
Ok(()) Ok(())
} }
} }

View File

@@ -2,10 +2,10 @@ extern crate async_std;
extern crate deltachat; extern crate deltachat;
extern crate lazy_static; extern crate lazy_static;
extern crate log; extern crate log;
extern crate purple_rs as purple;
extern crate openssl; extern crate openssl;
extern crate purple_rs as purple;
use async_std::sync::Arc; // RwLock use async_std::sync::Arc;
// use chat_info::ChatInfo; //PartialChatInfo, ChatInfoVersion // use chat_info::ChatInfo; //PartialChatInfo, ChatInfoVersion
use lazy_static::lazy_static; use lazy_static::lazy_static;
use messages::{AccountInfo, DeltaSystemHandle, PurpleMessage, SystemMessage}; use messages::{AccountInfo, DeltaSystemHandle, PurpleMessage, SystemMessage};
@@ -90,7 +90,6 @@ pub struct AccountData {
// Not exposed: server_flags, selfstatus, e2ee_enabled // Not exposed: server_flags, selfstatus, e2ee_enabled
session_closed: AtomicBool, session_closed: AtomicBool,
// session: RwLock<Option<delta::protocol::SessionInfo>>,
} }
impl Drop for AccountData { impl Drop for AccountData {
@@ -197,7 +196,6 @@ impl purple::LoginHandler for PurpleDelta {
bcc_self, bcc_self,
session_closed: AtomicBool::new(false), session_closed: AtomicBool::new(false),
// session: RwLock::new(None),
}); });
// SAFETY: // SAFETY:
@@ -225,6 +223,7 @@ impl purple::CloseHandler for PurpleDelta {
.data .data
.session_closed .session_closed
.store(true, Ordering::Relaxed); .store(true, Ordering::Relaxed);
self.connections.remove(*connection); self.connections.remove(*connection);
} }
None => { None => {
@@ -529,7 +528,10 @@ impl PurpleDelta {
fn process_message(&mut self, message: SystemMessage) { fn process_message(&mut self, message: SystemMessage) {
log::info!("received system message"); log::info!("received system message");
match message { match message {
SystemMessage::ExecAccount { handle, function } => { SystemMessage::ExecAccount {
handle: _,
function: _,
} => {
/* /*
self.connections self.connections
.get(handle) .get(handle)
@@ -540,7 +542,10 @@ impl PurpleDelta {
}); });
*/ */
} }
SystemMessage::ExecConnection { handle, function } => { SystemMessage::ExecConnection {
handle: _,
function: _,
} => {
/* /*
self.connections self.connections
.get(handle) .get(handle)
@@ -551,7 +556,10 @@ impl PurpleDelta {
}); });
*/ */
} }
SystemMessage::ExecHandle { handle, function } => { SystemMessage::ExecHandle {
handle: _,
function: _,
} => {
/* /*
self.connections self.connections
.get(handle) .get(handle)

View File

@@ -99,4 +99,3 @@ impl<'a> AccountProxy<'a> {
}) })
} }
} }

View File

@@ -52,4 +52,3 @@ impl<'a> ConnectionProxy<'a> {
.await .await
} }
} }

View File

@@ -41,4 +41,3 @@ impl<'a> HandleProxy<'a> {
.await; .await;
} }
} }

View File

@@ -191,4 +191,3 @@ pub struct DeltaSystemHandle {
pub rx: Receiver<SystemMessage>, pub rx: Receiver<SystemMessage>,
pub tx: Sender<PurpleMessage>, pub tx: Sender<PurpleMessage>,
} }