use anyhow::Result; use clap::Subcommand; use dbus::blocking::{Connection, Proxy}; use prettytable::table; use crate::printers::{ConversationPrinter, MessagePrinter}; const DBUS_NAME: &str = "net.buzzert.kordophonecd"; const DBUS_PATH: &str = "/net/buzzert/kordophonecd/daemon"; mod dbus_interface { #![allow(unused)] include!(concat!(env!("OUT_DIR"), "/kordophone-client.rs")); } use dbus_interface::NetBuzzertKordophoneRepository as KordophoneRepository; use dbus_interface::NetBuzzertKordophoneSettings as KordophoneSettings; #[derive(Subcommand)] pub enum Commands { /// Gets all known conversations. Conversations, /// Runs a sync operation. Sync { conversation_id: Option, }, /// Prints the server Kordophone version. Version, /// Configuration options Config { #[command(subcommand)] command: ConfigCommands, }, /// Waits for signals from the daemon. Signals, /// Prints the messages for a conversation. Messages { conversation_id: String, last_message_id: Option, }, /// Deletes all conversations. DeleteAllConversations, } #[derive(Subcommand)] pub enum ConfigCommands { /// Prints the current settings. Print, /// Sets the server URL. SetServerUrl { url: String, }, /// Sets the username. SetUsername { username: String, }, /// Sets the credential item. SetCredentialItem { item: String, }, } impl Commands { pub async fn run(cmd: Commands) -> Result<()> { let mut client = DaemonCli::new()?; match cmd { Commands::Version => client.print_version().await, Commands::Conversations => client.print_conversations().await, Commands::Sync { conversation_id } => client.sync_conversations(conversation_id).await, Commands::Config { command } => client.config(command).await, Commands::Signals => client.wait_for_signals().await, Commands::Messages { conversation_id, last_message_id } => client.print_messages(conversation_id, last_message_id).await, Commands::DeleteAllConversations => client.delete_all_conversations().await, } } } struct DaemonCli { conn: Connection, } impl DaemonCli { pub fn new() -> Result { Ok(Self { conn: Connection::new_session()? }) } fn proxy(&self) -> Proxy<&Connection> { self.conn.with_proxy(DBUS_NAME, DBUS_PATH, std::time::Duration::from_millis(5000)) } pub async fn print_version(&mut self) -> Result<()> { let version = KordophoneRepository::get_version(&self.proxy())?; println!("Server version: {}", version); Ok(()) } pub async fn print_conversations(&mut self) -> Result<()> { let conversations = KordophoneRepository::get_conversations(&self.proxy())?; println!("Number of conversations: {}", conversations.len()); for conversation in conversations { println!("{}", ConversationPrinter::new(&conversation.into())); } Ok(()) } pub async fn sync_conversations(&mut self, conversation_id: Option) -> Result<()> { if let Some(conversation_id) = conversation_id { KordophoneRepository::sync_conversation(&self.proxy(), &conversation_id) .map_err(|e| anyhow::anyhow!("Failed to sync conversation: {}", e)) } else { KordophoneRepository::sync_all_conversations(&self.proxy()) .map_err(|e| anyhow::anyhow!("Failed to sync conversations: {}", e)) } } pub async fn print_messages(&mut self, conversation_id: String, last_message_id: Option) -> Result<()> { let messages = KordophoneRepository::get_messages(&self.proxy(), &conversation_id, &last_message_id.unwrap_or_default())?; println!("Number of messages: {}", messages.len()); for message in messages { println!("{}", MessagePrinter::new(&message.into())); } Ok(()) } pub async fn wait_for_signals(&mut self) -> Result<()> { use dbus::Message; mod dbus_signals { pub use super::dbus_interface::NetBuzzertKordophoneRepositoryConversationsUpdated as ConversationsUpdated; } let _id = self.proxy().match_signal(|h: dbus_signals::ConversationsUpdated, _: &Connection, _: &Message| { println!("Signal: Conversations updated"); true }); println!("Waiting for signals..."); loop { self.conn.process(std::time::Duration::from_millis(1000))?; } } pub async fn config(&mut self, cmd: ConfigCommands) -> Result<()> { match cmd { ConfigCommands::Print => self.print_settings().await, ConfigCommands::SetServerUrl { url } => self.set_server_url(url).await, ConfigCommands::SetUsername { username } => self.set_username(username).await, ConfigCommands::SetCredentialItem { item } => self.set_credential_item(item).await, } } pub async fn print_settings(&mut self) -> Result<()> { let server_url = KordophoneSettings::server_url(&self.proxy()).unwrap_or_default(); let username = KordophoneSettings::username(&self.proxy()).unwrap_or_default(); let credential_item = KordophoneSettings::credential_item(&self.proxy()).unwrap_or_default(); let table = table!( [ b->"Server URL", &server_url ], [ b->"Username", &username ], [ b->"Credential Item", &credential_item ] ); table.printstd(); Ok(()) } pub async fn set_server_url(&mut self, url: String) -> Result<()> { KordophoneSettings::set_server_url(&self.proxy(), url) .map_err(|e| anyhow::anyhow!("Failed to set server URL: {}", e)) } pub async fn set_username(&mut self, username: String) -> Result<()> { KordophoneSettings::set_username(&self.proxy(), username) .map_err(|e| anyhow::anyhow!("Failed to set username: {}", e)) } pub async fn set_credential_item(&mut self, item: String) -> Result<()> { KordophoneSettings::set_credential_item(&self.proxy(), item.into()) .map_err(|e| anyhow::anyhow!("Failed to set credential item: {}", e)) } pub async fn delete_all_conversations(&mut self) -> Result<()> { KordophoneRepository::delete_all_conversations(&self.proxy()) .map_err(|e| anyhow::anyhow!("Failed to delete all conversations: {}", e)) } }