2025-06-06 16:39:31 -07:00
|
|
|
use crate::printers::{ConversationPrinter, MessagePrinter};
|
2025-02-12 00:10:33 -08:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use clap::Subcommand;
|
|
|
|
|
use dbus::blocking::{Connection, Proxy};
|
2025-04-28 16:00:04 -07:00
|
|
|
use prettytable::table;
|
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,
|
|
|
|
|
|
2025-05-01 20:36:43 -07:00
|
|
|
/// Runs a full sync operation for a conversation and its messages.
|
2025-06-06 16:39:31 -07:00
|
|
|
Sync { conversation_id: Option<String> },
|
2025-04-25 18:02:54 -07:00
|
|
|
|
2025-05-01 20:36:43 -07:00
|
|
|
/// Runs a sync operation for the conversation list.
|
|
|
|
|
SyncList,
|
|
|
|
|
|
2025-02-12 00:10:33 -08:00
|
|
|
/// Prints the server Kordophone version.
|
|
|
|
|
Version,
|
2025-04-27 18:07:58 -07:00
|
|
|
|
2025-06-06 16:39:31 -07:00
|
|
|
/// Configuration options
|
2025-04-27 18:07:58 -07:00
|
|
|
Config {
|
|
|
|
|
#[command(subcommand)]
|
|
|
|
|
command: ConfigCommands,
|
|
|
|
|
},
|
2025-04-27 22:44:05 -07:00
|
|
|
|
|
|
|
|
/// Waits for signals from the daemon.
|
|
|
|
|
Signals,
|
2025-04-28 16:00:04 -07:00
|
|
|
|
|
|
|
|
/// Prints the messages for a conversation.
|
|
|
|
|
Messages {
|
|
|
|
|
conversation_id: String,
|
|
|
|
|
last_message_id: Option<String>,
|
|
|
|
|
},
|
2025-05-01 01:08:13 -07:00
|
|
|
|
|
|
|
|
/// Deletes all conversations.
|
|
|
|
|
DeleteAllConversations,
|
2025-05-02 14:22:43 -07:00
|
|
|
|
|
|
|
|
/// Enqueues an outgoing message to be sent to a conversation.
|
|
|
|
|
SendMessage {
|
|
|
|
|
conversation_id: String,
|
|
|
|
|
text: String,
|
|
|
|
|
},
|
2025-04-27 18:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
|
pub enum ConfigCommands {
|
|
|
|
|
/// Prints the current settings.
|
|
|
|
|
Print,
|
|
|
|
|
|
|
|
|
|
/// Sets the server URL.
|
2025-06-06 16:39:31 -07:00
|
|
|
SetServerUrl { url: String },
|
2025-04-27 18:07:58 -07:00
|
|
|
|
|
|
|
|
/// Sets the username.
|
2025-06-06 16:39:31 -07:00
|
|
|
SetUsername { username: 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,
|
2025-04-28 18:39:52 -07:00
|
|
|
Commands::Sync { conversation_id } => client.sync_conversations(conversation_id).await,
|
2025-05-01 20:36:43 -07:00
|
|
|
Commands::SyncList => client.sync_conversations_list().await,
|
2025-04-27 18:07:58 -07:00
|
|
|
Commands::Config { command } => client.config(command).await,
|
2025-04-27 22:44:05 -07:00
|
|
|
Commands::Signals => client.wait_for_signals().await,
|
2025-06-06 16:39:31 -07:00
|
|
|
Commands::Messages {
|
|
|
|
|
conversation_id,
|
|
|
|
|
last_message_id,
|
|
|
|
|
} => {
|
|
|
|
|
client
|
|
|
|
|
.print_messages(conversation_id, last_message_id)
|
|
|
|
|
.await
|
|
|
|
|
}
|
2025-05-01 01:08:13 -07:00
|
|
|
Commands::DeleteAllConversations => client.delete_all_conversations().await,
|
2025-06-06 16:39:31 -07:00
|
|
|
Commands::SendMessage {
|
|
|
|
|
conversation_id,
|
|
|
|
|
text,
|
|
|
|
|
} => client.enqueue_outgoing_message(conversation_id, text).await,
|
2025-02-12 00:10:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct DaemonCli {
|
|
|
|
|
conn: Connection,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DaemonCli {
|
|
|
|
|
pub fn new() -> Result<Self> {
|
|
|
|
|
Ok(Self {
|
2025-06-06 16:39:31 -07:00
|
|
|
conn: Connection::new_session()?,
|
2025-02-12 00:10:33 -08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn proxy(&self) -> Proxy<&Connection> {
|
2025-06-06 16:39:31 -07:00
|
|
|
self.conn
|
|
|
|
|
.with_proxy(DBUS_NAME, DBUS_PATH, std::time::Duration::from_millis(5000))
|
2025-02-12 00:10:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<()> {
|
2025-05-03 18:19:48 -07:00
|
|
|
let conversations = KordophoneRepository::get_conversations(&self.proxy(), 100, 0)?;
|
2025-04-27 18:07:58 -07:00
|
|
|
println!("Number of conversations: {}", conversations.len());
|
|
|
|
|
|
|
|
|
|
for conversation in conversations {
|
|
|
|
|
println!("{}", ConversationPrinter::new(&conversation.into()));
|
|
|
|
|
}
|
2025-06-06 16:39:31 -07:00
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-28 18:39:52 -07:00
|
|
|
pub async fn sync_conversations(&mut self, conversation_id: Option<String>) -> 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))
|
|
|
|
|
}
|
2025-04-27 18:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-01 20:36:43 -07:00
|
|
|
pub async fn sync_conversations_list(&mut self) -> Result<()> {
|
|
|
|
|
KordophoneRepository::sync_conversation_list(&self.proxy())
|
|
|
|
|
.map_err(|e| anyhow::anyhow!("Failed to sync conversations: {}", e))
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 16:39:31 -07:00
|
|
|
pub async fn print_messages(
|
|
|
|
|
&mut self,
|
|
|
|
|
conversation_id: String,
|
|
|
|
|
last_message_id: Option<String>,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
let messages = KordophoneRepository::get_messages(
|
|
|
|
|
&self.proxy(),
|
|
|
|
|
&conversation_id,
|
|
|
|
|
&last_message_id.unwrap_or_default(),
|
|
|
|
|
)?;
|
2025-04-28 16:00:04 -07:00
|
|
|
println!("Number of messages: {}", messages.len());
|
|
|
|
|
|
|
|
|
|
for message in messages {
|
|
|
|
|
println!("{}", MessagePrinter::new(&message.into()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 16:39:31 -07:00
|
|
|
pub async fn enqueue_outgoing_message(
|
|
|
|
|
&mut self,
|
|
|
|
|
conversation_id: String,
|
|
|
|
|
text: String,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
let outgoing_message_id =
|
|
|
|
|
KordophoneRepository::send_message(&self.proxy(), &conversation_id, &text)?;
|
2025-05-02 14:22:43 -07:00
|
|
|
println!("Outgoing message ID: {}", outgoing_message_id);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 22:44:05 -07:00
|
|
|
pub async fn wait_for_signals(&mut self) -> Result<()> {
|
|
|
|
|
use dbus::Message;
|
|
|
|
|
mod dbus_signals {
|
|
|
|
|
pub use super::dbus_interface::NetBuzzertKordophoneRepositoryConversationsUpdated as ConversationsUpdated;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 16:39:31 -07:00
|
|
|
let _id = self.proxy().match_signal(
|
|
|
|
|
|h: dbus_signals::ConversationsUpdated, _: &Connection, _: &Message| {
|
|
|
|
|
println!("Signal: Conversations updated");
|
|
|
|
|
true
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-04-27 22:44:05 -07:00
|
|
|
|
|
|
|
|
println!("Waiting for signals...");
|
|
|
|
|
loop {
|
|
|
|
|
self.conn.process(std::time::Duration::from_millis(1000))?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 18:07:58 -07:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn print_settings(&mut self) -> Result<()> {
|
2025-04-28 16:00:04 -07:00
|
|
|
let server_url = KordophoneSettings::server_url(&self.proxy()).unwrap_or_default();
|
|
|
|
|
let username = KordophoneSettings::username(&self.proxy()).unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
let table = table!(
|
|
|
|
|
[ b->"Server URL", &server_url ],
|
2025-05-03 01:06:50 -07:00
|
|
|
[ b->"Username", &username ]
|
2025-04-28 16:00:04 -07:00
|
|
|
);
|
|
|
|
|
table.printstd();
|
2025-04-27 18:07:58 -07:00
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 01:08:13 -07:00
|
|
|
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))
|
|
|
|
|
}
|
2025-06-06 16:39:31 -07:00
|
|
|
}
|