2025-02-12 00:10:33 -08:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use clap::Subcommand;
|
|
|
|
|
use dbus::blocking::{Connection, Proxy};
|
2025-04-27 18:07:58 -07:00
|
|
|
use crate::printers::{ConversationPrinter, MessagePrinter};
|
2025-02-12 00:10:33 -08:00
|
|
|
|
|
|
|
|
const DBUS_NAME: &str = "net.buzzert.kordophonecd";
|
2025-04-25 20:02:18 -07:00
|
|
|
const DBUS_PATH: &str = "/net/buzzert/kordophonecd/daemon";
|
2025-02-12 00:10:33 -08:00
|
|
|
|
|
|
|
|
mod dbus_interface {
|
|
|
|
|
#![allow(unused)]
|
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/kordophone-client.rs"));
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
use dbus_interface::NetBuzzertKordophoneRepository as KordophoneRepository;
|
2025-04-27 18:07:58 -07:00
|
|
|
use dbus_interface::NetBuzzertKordophoneSettings as KordophoneSettings;
|
2025-02-12 00:10:33 -08:00
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
|
pub enum Commands {
|
2025-04-25 18:02:54 -07:00
|
|
|
/// Gets all known conversations.
|
|
|
|
|
Conversations,
|
|
|
|
|
|
|
|
|
|
/// Runs a sync operation.
|
|
|
|
|
Sync,
|
|
|
|
|
|
2025-02-12 00:10:33 -08:00
|
|
|
/// Prints the server Kordophone version.
|
|
|
|
|
Version,
|
2025-04-27 18:07:58 -07:00
|
|
|
|
|
|
|
|
/// Configuration options
|
|
|
|
|
Config {
|
|
|
|
|
#[command(subcommand)]
|
|
|
|
|
command: ConfigCommands,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
|
},
|
2025-02-12 00:10:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Commands {
|
|
|
|
|
pub async fn run(cmd: Commands) -> Result<()> {
|
|
|
|
|
let mut client = DaemonCli::new()?;
|
|
|
|
|
match cmd {
|
|
|
|
|
Commands::Version => client.print_version().await,
|
2025-04-25 18:02:54 -07:00
|
|
|
Commands::Conversations => client.print_conversations().await,
|
|
|
|
|
Commands::Sync => client.sync_conversations().await,
|
2025-04-27 18:07:58 -07:00
|
|
|
Commands::Config { command } => client.config(command).await,
|
2025-02-12 00:10:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct DaemonCli {
|
|
|
|
|
conn: Connection,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DaemonCli {
|
|
|
|
|
pub fn new() -> Result<Self> {
|
|
|
|
|
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<()> {
|
2025-04-25 18:02:54 -07:00
|
|
|
let version = KordophoneRepository::get_version(&self.proxy())?;
|
2025-02-12 00:10:33 -08:00
|
|
|
println!("Server version: {}", version);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-04-25 18:02:54 -07:00
|
|
|
|
|
|
|
|
pub async fn print_conversations(&mut self) -> Result<()> {
|
|
|
|
|
let conversations = KordophoneRepository::get_conversations(&self.proxy())?;
|
2025-04-27 18:07:58 -07:00
|
|
|
println!("Number of conversations: {}", conversations.len());
|
|
|
|
|
|
|
|
|
|
for conversation in conversations {
|
|
|
|
|
println!("{}", ConversationPrinter::new(&conversation.into()));
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn sync_conversations(&mut self) -> Result<()> {
|
2025-04-27 18:07:58 -07:00
|
|
|
KordophoneRepository::sync_all_conversations(&self.proxy())
|
|
|
|
|
.map_err(|e| anyhow::anyhow!("Failed to sync conversations: {}", e))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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())?;
|
|
|
|
|
let username = KordophoneSettings::username(&self.proxy())?;
|
|
|
|
|
let credential_item = KordophoneSettings::credential_item(&self.proxy())?;
|
|
|
|
|
|
|
|
|
|
println!("Server URL: {}", server_url);
|
|
|
|
|
println!("Username: {}", username);
|
|
|
|
|
println!("Credential Item: {}", credential_item);
|
2025-04-25 18:02:54 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-04-27 18:07:58 -07:00
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 00:10:33 -08:00
|
|
|
}
|