2020-05-16 14:23:20 +01:00
|
|
|
mod avatars;
|
|
|
|
pub use self::avatars::*;
|
|
|
|
|
2020-05-16 14:51:41 +01:00
|
|
|
#[allow(clippy::module_inception)]
|
2020-05-16 14:23:20 +01:00
|
|
|
mod connection;
|
|
|
|
pub use self::connection::*;
|
|
|
|
|
|
|
|
mod contacts;
|
|
|
|
pub use self::contacts::*;
|
|
|
|
|
|
|
|
mod contact_list;
|
|
|
|
pub use self::contact_list::*;
|
|
|
|
|
|
|
|
mod requests;
|
|
|
|
pub use self::requests::*;
|
|
|
|
|
|
|
|
mod simple_presence;
|
|
|
|
pub use self::simple_presence::*;
|
|
|
|
|
2020-05-10 23:09:58 +01:00
|
|
|
use crate::telepathy;
|
2020-05-16 14:23:20 +01:00
|
|
|
|
2020-05-10 23:09:58 +01:00
|
|
|
use dbus::blocking::{stdintf::org_freedesktop_dbus::RequestNameReply, LocalConnection};
|
2020-05-12 23:52:52 +01:00
|
|
|
use dbus::channel::Sender;
|
2020-05-16 14:23:20 +01:00
|
|
|
use dbus::tree::MethodErr;
|
2020-05-12 01:25:48 +01:00
|
|
|
|
|
|
|
use dc::config::Config;
|
|
|
|
use dc::context::Context;
|
|
|
|
use dc::Event;
|
|
|
|
use deltachat as dc;
|
|
|
|
|
2020-05-12 23:52:52 +01:00
|
|
|
use std::collections::{HashMap, VecDeque};
|
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2020-05-10 23:09:58 +01:00
|
|
|
use std::time::Duration;
|
2020-05-10 19:04:14 +01:00
|
|
|
|
2020-05-16 14:23:20 +01:00
|
|
|
pub const CONN_BUS_NAME: &str = "org.freedesktop.Telepathy.Connection.padfoot.delta";
|
|
|
|
pub const CONN_OBJECT_PATH: &str = "/org/freedesktop/Telepathy/Connection/padfoot/delta";
|
2020-05-12 23:52:52 +01:00
|
|
|
|
2020-05-11 00:48:18 +01:00
|
|
|
#[derive(Debug)]
|
2020-05-15 00:42:15 +01:00
|
|
|
// A Deltachast connection uses email addresses as handles, and delta's Db IDs
|
2020-05-10 19:04:14 +01:00
|
|
|
pub struct Connection {
|
2020-05-10 23:09:58 +01:00
|
|
|
id: String,
|
2020-05-12 01:25:48 +01:00
|
|
|
ctx: Arc<RwLock<Context>>,
|
2020-05-12 22:28:36 +01:00
|
|
|
|
2020-05-12 23:52:52 +01:00
|
|
|
state: Arc<RwLock<ConnState>>,
|
|
|
|
|
|
|
|
// Used for sending out messages
|
|
|
|
msgq: Arc<Mutex<VecDeque<dbus::Message>>>,
|
2020-05-10 19:04:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Connection {
|
2020-05-16 14:23:20 +01:00
|
|
|
pub fn new(params: HashMap<&str, super::Variant>) -> Result<Self, MethodErr> {
|
|
|
|
let err = Err(MethodErr::no_arg());
|
2020-05-12 01:25:48 +01:00
|
|
|
|
|
|
|
let acct = match params.get("account") {
|
|
|
|
Some(variant) => match variant.0.as_str() {
|
|
|
|
Some(str) => str.to_string(),
|
|
|
|
None => return err,
|
|
|
|
},
|
|
|
|
None => return err,
|
|
|
|
};
|
|
|
|
|
|
|
|
let id = escape(acct.to_owned());
|
|
|
|
|
|
|
|
let password = match params.get("password") {
|
|
|
|
Some(variant) => match variant.0.as_str() {
|
|
|
|
Some(str) => str.to_string(),
|
|
|
|
None => return err,
|
|
|
|
},
|
|
|
|
None => return err,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut dbfile = directories::ProjectDirs::from("gs", "ur", "telepathy-padfoot")
|
2020-05-16 14:51:41 +01:00
|
|
|
.ok_or_else(MethodErr::no_arg)
|
2020-05-12 01:25:48 +01:00
|
|
|
.and_then(|p| Ok(p.data_local_dir().to_path_buf()))?;
|
|
|
|
|
|
|
|
dbfile.push(&id);
|
|
|
|
dbfile.push("db.sqlite3");
|
|
|
|
|
|
|
|
// FIXME: how to give it access to the connection (initialized later)?
|
2020-05-12 23:52:52 +01:00
|
|
|
let msgq = Arc::new(Mutex::new(VecDeque::<dbus::Message>::new()));
|
|
|
|
|
2020-05-12 01:25:48 +01:00
|
|
|
let id2 = id.clone();
|
2020-05-13 01:46:35 +01:00
|
|
|
// Use this if we need to send messages in response to DC events:
|
|
|
|
// let msgq2 = msgq.clone();
|
2020-05-12 01:25:48 +01:00
|
|
|
let f = move |_c: &Context, e: Event| {
|
|
|
|
match e {
|
|
|
|
Event::Info(msg) => println!("Connection<{}>: INFO: {}", id2, msg),
|
|
|
|
Event::Warning(msg) => println!("Connection<{}>: WARN : {}", id2, msg),
|
|
|
|
Event::Error(msg) | Event::ErrorNetwork(msg) | Event::ErrorSelfNotInGroup(msg) => {
|
|
|
|
println!("Connection<{}>: ERR : {}", id2, msg)
|
|
|
|
}
|
2020-05-12 23:52:52 +01:00
|
|
|
Event::ConfigureProgress(progress) => {
|
|
|
|
println!("Connection<{}>: Configuration progress: {}", id2, progress)
|
|
|
|
}
|
|
|
|
Event::ImapConnected(msg) | Event::SmtpConnected(msg) => {
|
|
|
|
println!("Connection<{}>: Network: {}", id2, msg);
|
|
|
|
}
|
|
|
|
/* Unhandled messages:
|
|
|
|
SmtpMessageSent(String),
|
|
|
|
ImapMessageDeleted(String),
|
|
|
|
ImapMessageMoved(String),
|
|
|
|
ImapFolderEmptied(String),
|
|
|
|
NewBlobFile(String),
|
|
|
|
DeletedBlobFile(String),
|
|
|
|
MsgsChanged
|
|
|
|
IncomingMsg
|
|
|
|
MsgDelivered
|
|
|
|
MsgFailed
|
|
|
|
MsgRead
|
|
|
|
ChatModified(ChatId),
|
|
|
|
ContactsChanged(Option<u32>),
|
|
|
|
LocationChanged(Option<u32>),
|
|
|
|
ImexProgress(usize),
|
|
|
|
ImexFileWritten(PathBuf),
|
|
|
|
SecurejoinInviterProgress
|
|
|
|
SecurejoinJoinerProgress
|
|
|
|
*/
|
2020-05-12 01:25:48 +01:00
|
|
|
_ => println!("Connection<{}>: unhandled event received: {:?}", id2, e),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
let ctx =
|
|
|
|
Context::new(Box::new(f), "telepathy-padfoot".to_string(), dbfile).map_err(|e| {
|
|
|
|
println!("Connection<{}>: couldn't get delta context: {}", id, e);
|
2020-05-16 14:23:20 +01:00
|
|
|
MethodErr::no_arg() // FIXME: better error handling
|
2020-05-12 01:25:48 +01:00
|
|
|
})?;
|
|
|
|
|
|
|
|
ctx.set_config(Config::Addr, Some(&acct))
|
2020-05-16 14:23:20 +01:00
|
|
|
.map_err(|_e| MethodErr::no_arg())?;
|
2020-05-12 01:25:48 +01:00
|
|
|
ctx.set_config(Config::MailPw, Some(&password))
|
2020-05-16 14:23:20 +01:00
|
|
|
.map_err(|_e| MethodErr::no_arg())?;
|
2020-05-12 01:25:48 +01:00
|
|
|
ctx.set_config(Config::SentboxWatch, Some(&"Sent"))
|
2020-05-16 14:23:20 +01:00
|
|
|
.map_err(|_e| MethodErr::no_arg())?;
|
2020-05-12 01:25:48 +01:00
|
|
|
|
|
|
|
if !ctx.is_configured() {
|
|
|
|
ctx.configure();
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Connection {
|
2020-05-16 14:23:20 +01:00
|
|
|
id,
|
2020-05-16 14:51:41 +01:00
|
|
|
msgq,
|
2020-05-12 01:25:48 +01:00
|
|
|
ctx: Arc::new(RwLock::new(ctx)),
|
2020-05-12 23:52:52 +01:00
|
|
|
state: Arc::new(RwLock::new(ConnState::Initial)),
|
2020-05-12 01:25:48 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-10 23:09:58 +01:00
|
|
|
// This is run inside its own thread
|
|
|
|
//
|
|
|
|
// FIXME: running several +process+ loops sure is convenient, but it also
|
|
|
|
// seems inefficient...
|
|
|
|
pub fn run(self) {
|
|
|
|
let bus = self.bus();
|
|
|
|
let path = self.path();
|
2020-05-12 23:52:52 +01:00
|
|
|
let state = self.state.clone();
|
|
|
|
let msgq = self.msgq.clone();
|
|
|
|
let id = self.id.clone();
|
2020-05-11 00:48:18 +01:00
|
|
|
let c_rc = std::rc::Rc::new(self);
|
|
|
|
|
2020-05-16 14:23:20 +01:00
|
|
|
let f = dbus::tree::Factory::new_fn::<()>();
|
2020-05-10 23:09:58 +01:00
|
|
|
let mut tree = f.tree(());
|
2020-05-10 19:04:14 +01:00
|
|
|
|
2020-05-13 01:46:35 +01:00
|
|
|
let c_rc1 = c_rc.clone();
|
|
|
|
let conn_iface = telepathy::connection_server(&f, (), move |_| c_rc1.clone());
|
|
|
|
|
|
|
|
let c_rc2 = c_rc.clone();
|
2020-05-15 00:42:15 +01:00
|
|
|
let avatars_iface =
|
|
|
|
telepathy::connection_interface_avatars_server(&f, (), move |_| c_rc2.clone());
|
2020-05-13 01:46:35 +01:00
|
|
|
|
|
|
|
let c_rc3 = c_rc.clone();
|
2020-05-15 00:42:15 +01:00
|
|
|
let contacts_iface =
|
|
|
|
telepathy::connection_interface_contacts_server(&f, (), move |_| c_rc3.clone());
|
|
|
|
|
|
|
|
let c_rc4 = c_rc.clone();
|
|
|
|
let contact_list_iface =
|
|
|
|
telepathy::connection_interface_contact_list_server(&f, (), move |_| c_rc4.clone());
|
|
|
|
|
|
|
|
let c_rc5 = c_rc.clone();
|
2020-05-13 01:46:35 +01:00
|
|
|
let requests_iface =
|
2020-05-15 00:42:15 +01:00
|
|
|
telepathy::connection_interface_requests_server(&f, (), move |_| c_rc5.clone());
|
|
|
|
|
|
|
|
let simple_presence_iface =
|
2020-05-16 14:51:41 +01:00
|
|
|
telepathy::connection_interface_simple_presence_server(&f, (), move |_| c_rc.clone());
|
2020-05-13 01:46:35 +01:00
|
|
|
|
|
|
|
tree = tree.add(
|
2020-05-16 14:51:41 +01:00
|
|
|
f.object_path(path, ())
|
2020-05-13 01:46:35 +01:00
|
|
|
.introspectable()
|
|
|
|
.add(conn_iface)
|
2020-05-15 00:42:15 +01:00
|
|
|
.add(avatars_iface)
|
2020-05-13 01:46:35 +01:00
|
|
|
.add(contacts_iface)
|
2020-05-15 00:42:15 +01:00
|
|
|
.add(contact_list_iface)
|
|
|
|
.add(requests_iface)
|
|
|
|
.add(simple_presence_iface),
|
2020-05-13 01:46:35 +01:00
|
|
|
);
|
2020-05-10 23:09:58 +01:00
|
|
|
tree = tree.add(f.object_path("/", ()).introspectable());
|
|
|
|
|
|
|
|
// Setup DBus connection
|
|
|
|
let mut c = match LocalConnection::new_session() {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => {
|
|
|
|
println!("Failed to establish DBUS session for {}: {}", bus, e);
|
|
|
|
return; // Leave early
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
tree.start_receive(&c);
|
|
|
|
|
|
|
|
match c.request_name(bus.clone(), false, false, true) {
|
|
|
|
Ok(RequestNameReply::Exists) => {
|
|
|
|
println!("Another process is already registered on {}", bus);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Failed to register {}: {}", bus, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => println!("{} listening on {}", c.unique_name(), bus), // All other responses we can get are a success
|
|
|
|
};
|
|
|
|
|
2020-05-12 01:25:48 +01:00
|
|
|
// Set up delta jobs last in case registering to DBUS fails
|
|
|
|
// "Borrowed" from https://github.com/deltachat/deltachat-core-rust/blob/master/examples/simple.rs
|
2020-05-12 23:52:52 +01:00
|
|
|
while *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-16 14:23:20 +01:00
|
|
|
if let Err(e) = c.process(Duration::from_millis(100)) {
|
|
|
|
println!("Error processing: {}", e);
|
|
|
|
|
|
|
|
return;
|
2020-05-16 14:51:41 +01:00
|
|
|
};
|
2020-05-12 23:52:52 +01:00
|
|
|
|
|
|
|
// Spend a bit of time sending any outgoing messages - signals, mostly
|
2020-05-16 14:51:41 +01:00
|
|
|
while let Some(msg) = msgq.lock().unwrap().pop_front() {
|
2020-05-12 23:52:52 +01:00
|
|
|
print!("Connection<{}>: Sending message...", id);
|
2020-05-16 14:51:41 +01:00
|
|
|
|
2020-05-12 23:52:52 +01:00
|
|
|
match c.send(msg) {
|
2020-05-16 14:51:41 +01:00
|
|
|
Err(e) => println!("error! {:?}", e), // FIXME: handle error better?
|
2020-05-12 23:52:52 +01:00
|
|
|
_ => println!("OK!"),
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 22:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: clean up, emit disconnected signal. Join on threads started in
|
|
|
|
// connect() ?
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bus(&self) -> String {
|
|
|
|
CONN_BUS_NAME.to_owned() + "." + &self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path(&self) -> String {
|
|
|
|
CONN_OBJECT_PATH.to_owned() + "/" + &self.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 22:12:08 +01:00
|
|
|
fn escape_one(b: u8) -> String {
|
|
|
|
format!("_{:0<2x}", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some non-empty sequence of ASCII letters, digits and underscores
|
|
|
|
fn escape(s: String) -> String {
|
|
|
|
// Special-case the empty string
|
2020-05-16 14:23:20 +01:00
|
|
|
if s.is_empty() {
|
2020-05-12 22:12:08 +01:00
|
|
|
return "_".to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
let bytes = s.into_bytes();
|
|
|
|
let mut iter = bytes.iter();
|
|
|
|
let mut out = String::new();
|
|
|
|
|
|
|
|
// Only alphanumeric in the first byte
|
|
|
|
let x = *iter.next().expect("Already checked len > 0");
|
|
|
|
let first = match x {
|
|
|
|
b'a'..=b'z' | b'A'..=b'Z' => unsafe { String::from_utf8_unchecked(vec![x]) },
|
|
|
|
_ => escape_one(x),
|
|
|
|
};
|
|
|
|
|
|
|
|
out.push_str(&first);
|
|
|
|
|
|
|
|
for x in iter {
|
|
|
|
let next = match x {
|
|
|
|
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => unsafe {
|
|
|
|
String::from_utf8_unchecked(vec![*x])
|
|
|
|
},
|
|
|
|
_ => escape_one(*x),
|
|
|
|
};
|
|
|
|
|
|
|
|
out.push_str(&next);
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_escape() {
|
|
|
|
assert_eq!(escape("".to_string()), "_");
|
|
|
|
assert_eq!(escape("foo".to_string()), "foo");
|
|
|
|
assert_eq!(escape("foo@bar".to_string()), "foo_40bar");
|
|
|
|
assert_eq!(escape("foo_bar".to_string()), "foo_5fbar");
|
|
|
|
assert_eq!(escape("foo__@__bar".to_string()), "foo_5f_5f_40_5f_5fbar");
|
|
|
|
assert_eq!(escape("1foo".to_string()), "_31foo");
|
|
|
|
}
|
|
|
|
}
|