2020-05-10 23:09:58 +01:00
|
|
|
use crate::telepathy;
|
2020-05-15 00:42:15 +01:00
|
|
|
use crate::telepathy::ConnectionInterfaceContacts;
|
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;
|
|
|
|
use dbus::message::SignalArgs;
|
2020-05-13 01:46:35 +01:00
|
|
|
use dbus::{arg, tree};
|
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};
|
2020-05-15 00:42:15 +01:00
|
|
|
use std::convert::TryInto;
|
2020-05-12 23:52:52 +01:00
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2020-05-12 01:25:48 +01:00
|
|
|
use std::thread;
|
2020-05-10 23:09:58 +01:00
|
|
|
use std::time::Duration;
|
2020-05-10 19:04:14 +01:00
|
|
|
|
|
|
|
pub const CONN_BUS_NAME: &'static str = "org.freedesktop.Telepathy.Connection.padfoot.delta";
|
2020-05-10 23:09:58 +01:00
|
|
|
pub const CONN_OBJECT_PATH: &'static str = "/org/freedesktop/Telepathy/Connection/padfoot/delta";
|
2020-05-10 19:04:14 +01:00
|
|
|
|
2020-05-12 23:52:52 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
enum ConnState {
|
|
|
|
Initial,
|
|
|
|
Connected,
|
|
|
|
Disconnected,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
pub fn connection_interfaces() -> Vec<String> {
|
|
|
|
vec![
|
|
|
|
"org.freedesktop.Telepathy.Connection".to_string(),
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.Avatars".to_string(),
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.Contacts".to_string(),
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.ContactList".to_string(),
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.Requests".to_string(),
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.SimplePresence".to_string(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<dyn telepathy::Connection + 'static> for std::rc::Rc<Connection> {
|
|
|
|
fn as_ref(&self) -> &(dyn telepathy::Connection + 'static) {
|
|
|
|
&**self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:04:14 +01:00
|
|
|
impl Connection {
|
2020-05-12 01:25:48 +01:00
|
|
|
pub fn new(params: HashMap<&str, super::Variant>) -> Result<Self, dbus::tree::MethodErr> {
|
|
|
|
let err = Err(dbus::tree::MethodErr::no_arg());
|
|
|
|
|
|
|
|
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")
|
|
|
|
.ok_or_else(|| tree::MethodErr::no_arg())
|
|
|
|
.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);
|
|
|
|
tree::MethodErr::no_arg() // FIXME: better error handling
|
|
|
|
})?;
|
|
|
|
|
|
|
|
ctx.set_config(Config::Addr, Some(&acct))
|
|
|
|
.map_err(|_e| tree::MethodErr::no_arg())?;
|
|
|
|
ctx.set_config(Config::MailPw, Some(&password))
|
|
|
|
.map_err(|_e| tree::MethodErr::no_arg())?;
|
|
|
|
ctx.set_config(Config::SentboxWatch, Some(&"Sent"))
|
|
|
|
.map_err(|_e| tree::MethodErr::no_arg())?;
|
|
|
|
|
|
|
|
if !ctx.is_configured() {
|
|
|
|
ctx.configure();
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Connection {
|
|
|
|
id: id,
|
|
|
|
ctx: Arc::new(RwLock::new(ctx)),
|
2020-05-12 23:52:52 +01:00
|
|
|
state: Arc::new(RwLock::new(ConnState::Initial)),
|
|
|
|
msgq: msgq.clone(),
|
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);
|
|
|
|
|
|
|
|
let f = 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 c_rc6 = c_rc.clone();
|
|
|
|
let simple_presence_iface =
|
|
|
|
telepathy::connection_interface_simple_presence_server(&f, (), move |_| c_rc6.clone());
|
2020-05-13 01:46:35 +01:00
|
|
|
|
|
|
|
tree = tree.add(
|
|
|
|
f.object_path(path.clone(), ())
|
|
|
|
.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-12 22:28:36 +01:00
|
|
|
match c.process(Duration::from_millis(100)) {
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error processing: {}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-05-12 23:52:52 +01:00
|
|
|
|
|
|
|
// Spend a bit of time sending any outgoing messages - signals, mostly
|
|
|
|
loop {
|
|
|
|
let msg = match msgq.lock().unwrap().pop_front() {
|
|
|
|
Some(msg) => msg,
|
|
|
|
None => break,
|
|
|
|
};
|
|
|
|
|
|
|
|
print!("Connection<{}>: Sending message...", id);
|
|
|
|
match c.send(msg) {
|
|
|
|
Err(e) => println!("error! {:?}", e),
|
|
|
|
_ => 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl telepathy::Connection for Connection {
|
|
|
|
// In connect(), we start the threads that drive the deltachat context
|
|
|
|
fn connect(&self) -> Result<(), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::connect()", self.id);
|
2020-05-12 01:25:48 +01:00
|
|
|
|
2020-05-12 22:28:36 +01:00
|
|
|
let inbox_ctx = self.ctx.clone();
|
2020-05-12 23:52:52 +01:00
|
|
|
let state = self.state.clone();
|
2020-05-15 00:42:15 +01:00
|
|
|
let id = self.id.clone();
|
2020-05-12 22:28:36 +01:00
|
|
|
let _inbox_thread = thread::spawn(move || {
|
2020-05-12 23:52:52 +01:00
|
|
|
while *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_inbox_jobs(&inbox_ctx.read().unwrap());
|
2020-05-12 23:52:52 +01:00
|
|
|
if *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_inbox_fetch(&inbox_ctx.read().unwrap());
|
|
|
|
|
2020-05-12 23:52:52 +01:00
|
|
|
if *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_inbox_idle(&inbox_ctx.read().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 00:42:15 +01:00
|
|
|
|
|
|
|
println!("Connection<{}>::connect(): inbox thread exited", id);
|
2020-05-12 01:25:48 +01:00
|
|
|
});
|
|
|
|
|
2020-05-12 22:28:36 +01:00
|
|
|
let smtp_ctx = self.ctx.clone();
|
2020-05-12 23:52:52 +01:00
|
|
|
let state = self.state.clone();
|
2020-05-15 00:42:15 +01:00
|
|
|
let id = self.id.clone();
|
2020-05-12 22:28:36 +01:00
|
|
|
let _smtp_thread = thread::spawn(move || {
|
2020-05-12 23:52:52 +01:00
|
|
|
while *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_smtp_jobs(&smtp_ctx.read().unwrap());
|
2020-05-12 23:52:52 +01:00
|
|
|
if *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_smtp_idle(&smtp_ctx.read().unwrap());
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 00:42:15 +01:00
|
|
|
|
|
|
|
println!("Connection<{}>::connect(): smtp thread exited", id);
|
2020-05-12 01:25:48 +01:00
|
|
|
});
|
|
|
|
|
2020-05-12 22:28:36 +01:00
|
|
|
let mvbox_ctx = self.ctx.clone();
|
2020-05-12 23:52:52 +01:00
|
|
|
let state = self.state.clone();
|
2020-05-15 00:42:15 +01:00
|
|
|
let id = self.id.clone();
|
2020-05-12 22:28:36 +01:00
|
|
|
let _mvbox_thread = thread::spawn(move || {
|
2020-05-12 23:52:52 +01:00
|
|
|
while *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_mvbox_fetch(&mvbox_ctx.read().unwrap());
|
2020-05-12 23:52:52 +01:00
|
|
|
if *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_mvbox_idle(&mvbox_ctx.read().unwrap());
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 00:42:15 +01:00
|
|
|
|
|
|
|
println!("Connection<{}>::connect(): mvbox thread exited", id);
|
2020-05-12 01:25:48 +01:00
|
|
|
});
|
|
|
|
|
2020-05-12 22:28:36 +01:00
|
|
|
let sentbox_ctx = self.ctx.clone();
|
2020-05-12 23:52:52 +01:00
|
|
|
let state = self.state.clone();
|
2020-05-15 00:42:15 +01:00
|
|
|
let id = self.id.clone();
|
2020-05-12 22:28:36 +01:00
|
|
|
let _sentbox_thread = thread::spawn(move || {
|
2020-05-12 23:52:52 +01:00
|
|
|
while *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_sentbox_fetch(&sentbox_ctx.read().unwrap());
|
2020-05-12 23:52:52 +01:00
|
|
|
if *state.read().unwrap() != ConnState::Disconnected {
|
2020-05-12 01:25:48 +01:00
|
|
|
dc::job::perform_sentbox_idle(&sentbox_ctx.read().unwrap());
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 00:42:15 +01:00
|
|
|
|
|
|
|
println!("Connection<{}>::connect(): sentbox thread exited", id);
|
2020-05-12 01:25:48 +01:00
|
|
|
});
|
|
|
|
|
2020-05-12 23:52:52 +01:00
|
|
|
// Just pretend to be connected all the time for now. Tracking IMAP+SMTP
|
|
|
|
// state is a pain
|
|
|
|
let state = self.state.clone();
|
|
|
|
let mut w = state.write().unwrap();
|
|
|
|
*w = ConnState::Connected;
|
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
// Emit a StatusChanged signal for the benefit of others, but the caller
|
2020-05-12 23:52:52 +01:00
|
|
|
// learns from our RPC response
|
|
|
|
let sig = telepathy::ConnectionStatusChanged {
|
|
|
|
status: 0, // Connected
|
|
|
|
reason: 1, // Requested
|
|
|
|
};
|
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
let dbus_conn_path = dbus::strings::Path::new(self.path().to_string())
|
2020-05-12 23:52:52 +01:00
|
|
|
.expect("Object path should meet DBUS requirements");
|
|
|
|
|
|
|
|
self.msgq
|
|
|
|
.clone()
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push_back(sig.to_emit_message(&dbus_conn_path));
|
|
|
|
|
2020-05-12 22:28:36 +01:00
|
|
|
Ok(())
|
2020-05-10 23:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn disconnect(&self) -> Result<(), tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!("Connection<{}>::disconnect()", self.id);
|
2020-05-12 23:52:52 +01:00
|
|
|
|
|
|
|
let state = self.state.clone();
|
|
|
|
let mut w = state.write().unwrap();
|
|
|
|
*w = ConnState::Disconnected;
|
|
|
|
|
|
|
|
// FIXME: we need to signal to the CM that they should remove the
|
|
|
|
// connection from the active list
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn interfaces(&self) -> Result<Vec<String>, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::interfaces()", self.id);
|
|
|
|
|
|
|
|
self.get_interfaces()
|
2020-05-10 23:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_interfaces(&self) -> Result<Vec<String>, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!("Connection<{}>::get_interfaces()", self.id);
|
2020-05-12 23:52:52 +01:00
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
Ok(connection_interfaces())
|
2020-05-10 23:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_protocol(&self) -> Result<String, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!("Connection<{}>::get_protocol()", self.id);
|
2020-05-12 23:52:52 +01:00
|
|
|
|
2020-05-10 23:09:58 +01:00
|
|
|
Ok(super::PROTO_NAME.to_string())
|
|
|
|
}
|
|
|
|
|
2020-05-12 23:52:52 +01:00
|
|
|
fn self_handle(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::self_handle()", self.id);
|
|
|
|
|
|
|
|
self.get_self_handle()
|
|
|
|
}
|
|
|
|
|
2020-05-10 23:09:58 +01:00
|
|
|
fn get_self_handle(&self) -> Result<u32, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!("Connection<{}>::get_self_handle()", self.id);
|
2020-05-12 23:52:52 +01:00
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
Ok(dc::constants::DC_CONTACT_ID_SELF)
|
2020-05-12 23:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn status(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::status()", self.id);
|
|
|
|
|
|
|
|
self.get_status()
|
2020-05-10 23:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_status(&self) -> Result<u32, tree::MethodErr> {
|
2020-05-12 23:52:52 +01:00
|
|
|
match *self.state.clone().read().unwrap() {
|
|
|
|
ConnState::Initial | ConnState::Disconnected => Ok(2),
|
|
|
|
ConnState::Connected => Ok(0),
|
|
|
|
}
|
2020-05-10 23:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hold_handles(&self, handle_type: u32, handles: Vec<u32>) -> Result<(), tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!(
|
|
|
|
"Connection<{}>::hold_handles({}, {:?})",
|
|
|
|
self.id, handle_type, handles
|
|
|
|
);
|
2020-05-15 00:42:15 +01:00
|
|
|
|
|
|
|
// Since HasImmortalHandles is true, this doesn't need to do anything
|
|
|
|
Ok(())
|
2020-05-10 23:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inspect_handles(
|
|
|
|
&self,
|
|
|
|
handle_type: u32,
|
|
|
|
handles: Vec<u32>,
|
|
|
|
) -> Result<Vec<String>, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!(
|
|
|
|
"Connection<{}>::inspect_handles({}, {:?})",
|
|
|
|
self.id, handle_type, handles
|
|
|
|
);
|
2020-05-10 23:09:58 +01:00
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list_channels(
|
|
|
|
&self,
|
|
|
|
) -> Result<Vec<(dbus::Path<'static>, String, u32, u32)>, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!("Connection<{}>::list_channels()", self.id);
|
2020-05-10 23:09:58 +01:00
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn release_handles(&self, handle_type: u32, handles: Vec<u32>) -> Result<(), tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!(
|
|
|
|
"Connection<{}>::release_handles({}, {:?})",
|
|
|
|
self.id, handle_type, handles
|
|
|
|
);
|
2020-05-15 00:42:15 +01:00
|
|
|
|
|
|
|
// Since HasImmortalHandles is true, we don't need to do anything
|
|
|
|
Ok(())
|
2020-05-10 23:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn request_channel(
|
|
|
|
&self,
|
|
|
|
type_: &str,
|
|
|
|
handle_type: u32,
|
|
|
|
handle: u32,
|
|
|
|
suppress_handler: bool,
|
|
|
|
) -> Result<dbus::Path<'static>, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!(
|
|
|
|
"Connection<{}>::request_channel({}, {}, {}, {})",
|
|
|
|
self.id, type_, handle_type, handle, suppress_handler
|
|
|
|
);
|
2020-05-10 23:09:58 +01:00
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_handles(
|
|
|
|
&self,
|
|
|
|
handle_type: u32,
|
|
|
|
identifiers: Vec<&str>,
|
|
|
|
) -> Result<Vec<u32>, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!(
|
|
|
|
"Connection<{}>::request_handles({}, {:?})",
|
|
|
|
self.id, handle_type, identifiers
|
|
|
|
);
|
2020-05-10 23:09:58 +01:00
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_client_interest(&self, tokens: Vec<&str>) -> Result<(), tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!("Connection<{}>::add_client_interest({:?})", self.id, tokens);
|
2020-05-10 23:09:58 +01:00
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_client_interest(&self, tokens: Vec<&str>) -> Result<(), tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!(
|
|
|
|
"Connection<{}>::remove_client_interest({:?})",
|
|
|
|
self.id, tokens
|
|
|
|
);
|
2020-05-10 23:09:58 +01:00
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn self_id(&self) -> Result<String, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!("Connection<{}>::self_id()", self.id);
|
2020-05-10 23:09:58 +01:00
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
let contact = match dc::contact::Contact::get_by_id(
|
|
|
|
&self.ctx.read().unwrap(),
|
|
|
|
dc::constants::DC_CONTACT_ID_SELF,
|
|
|
|
) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => {
|
|
|
|
println!(" err: {}", e);
|
|
|
|
return Err(tree::MethodErr::no_arg());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(contact.get_addr().to_string())
|
2020-05-10 23:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn has_immortal_handles(&self) -> Result<bool, tree::MethodErr> {
|
2020-05-10 23:25:41 +01:00
|
|
|
println!("Connection<{}>::has_immortal_handles()", self.id);
|
2020-05-12 23:52:52 +01:00
|
|
|
|
2020-05-10 23:09:58 +01:00
|
|
|
Ok(true)
|
2020-05-10 19:04:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 22:12:08 +01:00
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
impl AsRef<dyn telepathy::ConnectionInterfaceAvatars + 'static> for std::rc::Rc<Connection> {
|
|
|
|
fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceAvatars + 'static) {
|
|
|
|
&**self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: come back and do this properly, I'm just putting it in for consistency
|
|
|
|
impl telepathy::ConnectionInterfaceAvatars for Connection {
|
|
|
|
fn get_avatar_requirements(
|
|
|
|
&self,
|
|
|
|
) -> Result<(Vec<String>, u16, u16, u16, u16, u32), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::get_avatar_requirements()", self.id);
|
|
|
|
Ok((vec![], 0, 0, 0, 0, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_avatar_tokens(&self, contacts: Vec<u32>) -> Result<Vec<String>, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::get_avatar_tokens({:?})", self.id, contacts);
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_known_avatar_tokens(
|
|
|
|
&self,
|
|
|
|
contacts: Vec<u32>,
|
|
|
|
) -> Result<::std::collections::HashMap<u32, String>, tree::MethodErr> {
|
|
|
|
println!(
|
|
|
|
"Connection<{}>::get_known_avatar_tokens({:?})",
|
|
|
|
self.id, contacts
|
|
|
|
);
|
|
|
|
Ok(HashMap::<u32, String>::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_avatar(&self, contact: u32) -> Result<(Vec<u8>, String), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::request_avatar({})", self.id, contact);
|
|
|
|
Ok((vec![], "".to_string()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_avatars(&self, contacts: Vec<u32>) -> Result<(), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::request_avatar({:?})", self.id, contacts);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_avatar(&self, _avatar: Vec<u8>, mimetype: &str) -> Result<String, tree::MethodErr> {
|
|
|
|
println!(
|
|
|
|
"Connection<{}>::set_avatar((data), {:?})",
|
|
|
|
self.id, mimetype
|
|
|
|
);
|
|
|
|
Ok("".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_avatar(&self) -> Result<(), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::clear_avatar()", self.id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn supported_avatar_mimetypes(&self) -> Result<Vec<String>, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::supported_avatar_mimetypes()", self.id);
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn minimum_avatar_height(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::minimum_avatar_height()", self.id);
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn minimum_avatar_width(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::minimum_avatar_width()", self.id);
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn recommended_avatar_height(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::recommended_avatar_height()", self.id);
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn recommended_avatar_width(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::recommended_avatar_width()", self.id);
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maximum_avatar_height(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::maximum_avatar_height()", self.id);
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maximum_avatar_width(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::maximum_avatar_width()", self.id);
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maximum_avatar_bytes(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::maximum_avatar_bytes()", self.id);
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<dyn telepathy::ConnectionInterfaceContacts + 'static> for std::rc::Rc<Connection> {
|
|
|
|
fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceContacts + 'static) {
|
|
|
|
&**self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 01:46:35 +01:00
|
|
|
impl telepathy::ConnectionInterfaceContacts for Connection {
|
|
|
|
fn get_contact_attributes(
|
|
|
|
&self,
|
|
|
|
handles: Vec<u32>,
|
|
|
|
interfaces: Vec<&str>,
|
|
|
|
hold: bool,
|
|
|
|
) -> Result<HashMap<u32, HashMap<String, super::Variant>>, tree::MethodErr> {
|
|
|
|
println!(
|
|
|
|
"Connection<{}>::get_contact_attributes({:?}, {:?}, {})",
|
|
|
|
self.id, handles, interfaces, hold
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut out = HashMap::<u32, HashMap<String, super::Variant>>::new();
|
|
|
|
for id in handles.iter() {
|
|
|
|
// FIXME: work out how to use get_all
|
|
|
|
let contact = match dc::contact::Contact::get_by_id(&self.ctx.read().unwrap(), *id) {
|
|
|
|
Ok(c) => c,
|
2020-05-15 00:42:15 +01:00
|
|
|
Err(_e) => continue, // Invalid IDs are silently ignored
|
2020-05-13 01:46:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut props = HashMap::<String, super::Variant>::new();
|
|
|
|
// This is mandatory
|
|
|
|
props.insert(
|
|
|
|
"org.freedesktop.Telepathy.Connection/contact-id".to_string(),
|
2020-05-15 00:42:15 +01:00
|
|
|
arg::Variant(Box::new(contact.get_addr().to_string())),
|
2020-05-13 01:46:35 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// The empty string means "no avatar"
|
|
|
|
props.insert(
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.Avatars/token".to_string(),
|
|
|
|
arg::Variant(Box::new("".to_string())),
|
|
|
|
);
|
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
// TODO: we need to publish DBUS services on these endpoints
|
|
|
|
props.insert(
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.ContactList/publish".to_string(),
|
|
|
|
arg::Variant(Box::new(4)),
|
|
|
|
);
|
|
|
|
|
|
|
|
props.insert(
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.ContactList/subscribe".to_string(),
|
|
|
|
arg::Variant(Box::new(4)),
|
|
|
|
);
|
|
|
|
|
2020-05-13 01:46:35 +01:00
|
|
|
out.insert(*id, props);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_contact_by_id(
|
|
|
|
&self,
|
|
|
|
identifier: &str,
|
|
|
|
interfaces: Vec<&str>,
|
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
u32,
|
|
|
|
::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg + 'static>>>,
|
|
|
|
),
|
|
|
|
tree::MethodErr,
|
|
|
|
> {
|
|
|
|
println!(
|
|
|
|
"Connection<{}>::get_contact_by_id({}, {:?})",
|
|
|
|
self.id, identifier, interfaces
|
|
|
|
);
|
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contact_attribute_interfaces(&self) -> Result<Vec<String>, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::contact_attribute_interfaces()", self.id);
|
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
Ok(vec![
|
|
|
|
"org.freedesktop.Telepathy.Connection".to_string(),
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.Avatars".to_string(),
|
|
|
|
"org.freedesktop.Telepathy.Connection.Interface.ContactList".to_string(),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<dyn telepathy::ConnectionInterfaceContactList + 'static> for std::rc::Rc<Connection> {
|
|
|
|
fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceContactList + 'static) {
|
|
|
|
&**self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: come back and do this properly later
|
|
|
|
impl telepathy::ConnectionInterfaceContactList for Connection {
|
|
|
|
fn get_contact_list_attributes(
|
|
|
|
&self,
|
|
|
|
interfaces: Vec<&str>,
|
|
|
|
hold: bool,
|
|
|
|
) -> Result<HashMap<u32, HashMap<String, super::Variant>>, tree::MethodErr> {
|
|
|
|
println!(
|
|
|
|
"Connection<{}>::get_contact_list_attributes({:?}, {})",
|
|
|
|
self.id, interfaces, hold
|
|
|
|
);
|
|
|
|
|
|
|
|
let ids = match dc::contact::Contact::get_all(
|
|
|
|
&self.ctx.read().unwrap(),
|
|
|
|
dc::constants::DC_GCL_ADD_SELF.try_into().unwrap(),
|
|
|
|
None::<String>,
|
|
|
|
) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => {
|
|
|
|
println!(" err: {}", e);
|
|
|
|
return Err(tree::MethodErr::no_arg());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get_contact_attributes(ids, vec![], true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_subscription(
|
|
|
|
&self,
|
|
|
|
contacts: Vec<u32>,
|
|
|
|
message: &str,
|
|
|
|
) -> Result<(), tree::MethodErr> {
|
|
|
|
println!(
|
|
|
|
"Connection<{}>::request_subscription({:?}, {})",
|
|
|
|
self.id, contacts, message
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn authorize_publication(&self, contacts: Vec<u32>) -> Result<(), tree::MethodErr> {
|
|
|
|
println!(
|
|
|
|
"Connection<{}>::authorize_publication({:?})",
|
|
|
|
self.id, contacts
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn remove_contacts(&self, contacts: Vec<u32>) -> Result<(), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::remove_contacts({:?})", self.id, contacts);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn unsubscribe(&self, contacts: Vec<u32>) -> Result<(), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::unsubscribe({:?})", self.id, contacts);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unpublish(&self, contacts: Vec<u32>) -> Result<(), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::unpublish({:?})", self.id, contacts);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn download(&self) -> Result<(), tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::download()", self.id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contact_list_state(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::contact_list_state()", self.id);
|
|
|
|
Ok(3) // Success
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contact_list_persists(&self) -> Result<bool, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::contact_list_persists()", self.id);
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_change_contact_list(&self) -> Result<bool, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::can_change_contact_list()", self.id);
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
fn request_uses_message(&self) -> Result<bool, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::request_uses_message()", self.id);
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn download_at_connection(&self) -> Result<bool, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::download_at_connection()", self.id);
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<dyn telepathy::ConnectionInterfaceRequests + 'static> for std::rc::Rc<Connection> {
|
|
|
|
fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceRequests + 'static) {
|
|
|
|
&**self
|
2020-05-13 01:46:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl telepathy::ConnectionInterfaceRequests for Connection {
|
|
|
|
fn create_channel(
|
|
|
|
&self,
|
|
|
|
request: ::std::collections::HashMap<&str, arg::Variant<Box<dyn arg::RefArg>>>,
|
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
dbus::Path<'static>,
|
|
|
|
::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg + 'static>>>,
|
|
|
|
),
|
|
|
|
tree::MethodErr,
|
|
|
|
> {
|
|
|
|
println!("Connection<{}>::create_channel({:?})", self.id, request);
|
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_channel(
|
|
|
|
&self,
|
|
|
|
request: ::std::collections::HashMap<&str, arg::Variant<Box<dyn arg::RefArg>>>,
|
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
bool,
|
|
|
|
dbus::Path<'static>,
|
|
|
|
::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg + 'static>>>,
|
|
|
|
),
|
|
|
|
tree::MethodErr,
|
|
|
|
> {
|
|
|
|
println!("Connection<{}>::ensure_channel({:?})", self.id, request);
|
|
|
|
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn channels(
|
|
|
|
&self,
|
|
|
|
) -> Result<
|
|
|
|
Vec<(
|
|
|
|
dbus::Path<'static>,
|
|
|
|
::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg + 'static>>>,
|
|
|
|
)>,
|
|
|
|
tree::MethodErr,
|
|
|
|
> {
|
|
|
|
println!("Connection<{}>::channels()", self.id);
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn requestable_channel_classes(
|
|
|
|
&self,
|
|
|
|
) -> Result<
|
|
|
|
Vec<(
|
|
|
|
::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg + 'static>>>,
|
|
|
|
Vec<String>,
|
|
|
|
)>,
|
|
|
|
tree::MethodErr,
|
|
|
|
> {
|
|
|
|
println!("Connection<{}>::requestable_channel_classes()", self.id);
|
|
|
|
Ok(super::requestables())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 00:42:15 +01:00
|
|
|
pub type SimplePresenceSpec = (
|
|
|
|
u32, // connection presence type
|
|
|
|
String, // status
|
|
|
|
String, // status message
|
|
|
|
);
|
|
|
|
|
|
|
|
pub type SimpleStatusSpec = (
|
|
|
|
u32, // connection presence type
|
|
|
|
bool, // may set on self?
|
|
|
|
bool, // can have message?
|
|
|
|
);
|
|
|
|
|
|
|
|
impl AsRef<dyn telepathy::ConnectionInterfaceSimplePresence + 'static> for std::rc::Rc<Connection> {
|
|
|
|
fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceSimplePresence + 'static) {
|
|
|
|
&**self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl telepathy::ConnectionInterfaceSimplePresence for Connection {
|
|
|
|
fn set_presence(&self, status: &str, status_message: &str) -> Result<(), tree::MethodErr> {
|
|
|
|
println!(
|
|
|
|
"Connection<{}>::set_presence({}, {:?})",
|
|
|
|
self.id, status, status_message
|
|
|
|
);
|
|
|
|
|
|
|
|
// FIXME: emit a presence changed signal
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_presences(
|
|
|
|
&self,
|
|
|
|
contacts: Vec<u32>,
|
|
|
|
) -> Result<HashMap<u32, SimplePresenceSpec>, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::get_presences({:?})", self.id, contacts);
|
|
|
|
|
|
|
|
let mut out = HashMap::<u32, SimplePresenceSpec>::new();
|
|
|
|
|
|
|
|
for id in contacts {
|
|
|
|
out.insert(id, (2, "available".to_string(), "".to_string())); // Available
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn statuses(&self) -> Result<HashMap<String, SimpleStatusSpec>, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::statuses()", self.id);
|
|
|
|
|
|
|
|
let mut out = HashMap::<String, SimpleStatusSpec>::new();
|
|
|
|
|
|
|
|
out.insert("available".to_string(), (2, true, false));
|
|
|
|
out.insert("offline".to_string(), (1, true, false));
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maximum_status_message_length(&self) -> Result<u32, tree::MethodErr> {
|
|
|
|
println!("Connection<{}>::maximum_status_message_length()", self.id);
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if s.len() == 0 {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|