Private
Public Access
1
0
Files
Kordophone/kpcli/src/daemon/mod.rs

223 lines
6.9 KiB
Rust
Raw Normal View History

2025-02-12 00:10:33 -08:00
use anyhow::Result;
use async_trait::async_trait;
2025-02-12 00:10:33 -08:00
use clap::Subcommand;
// Platform-specific modules
#[cfg(target_os = "linux")]
mod dbus;
2025-08-01 12:26:17 -07:00
#[cfg(target_os = "macos")]
mod xpc;
#[async_trait]
pub trait DaemonInterface {
async fn print_version(&mut self) -> Result<()>;
async fn print_conversations(&mut self) -> Result<()>;
async fn sync_conversations(&mut self, conversation_id: Option<String>) -> Result<()>;
async fn sync_conversations_list(&mut self) -> Result<()>;
async fn print_messages(
&mut self,
conversation_id: String,
last_message_id: Option<String>,
) -> Result<()>;
async fn enqueue_outgoing_message(
&mut self,
conversation_id: String,
text: String,
) -> Result<()>;
async fn wait_for_signals(&mut self) -> Result<()>;
async fn config(&mut self, cmd: ConfigCommands) -> Result<()>;
async fn delete_all_conversations(&mut self) -> Result<()>;
async fn download_attachment(&mut self, attachment_id: String) -> Result<()>;
async fn upload_attachment(&mut self, path: String) -> Result<()>;
async fn mark_conversation_as_read(&mut self, conversation_id: String) -> Result<()>;
}
2025-02-12 00:10:33 -08:00
struct StubDaemonInterface;
impl StubDaemonInterface {
fn new() -> Result<Self> {
Ok(Self)
}
2025-02-12 00:10:33 -08:00
}
#[async_trait]
impl DaemonInterface for StubDaemonInterface {
async fn print_version(&mut self) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn print_conversations(&mut self) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn sync_conversations(&mut self, _conversation_id: Option<String>) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn sync_conversations_list(&mut self) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
2025-08-01 12:26:17 -07:00
async fn print_messages(
&mut self,
_conversation_id: String,
_last_message_id: Option<String>,
) -> Result<()> {
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
2025-08-01 12:26:17 -07:00
async fn enqueue_outgoing_message(
&mut self,
_conversation_id: String,
_text: String,
) -> Result<()> {
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn wait_for_signals(&mut self) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn config(&mut self, _cmd: ConfigCommands) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn delete_all_conversations(&mut self) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn download_attachment(&mut self, _attachment_id: String) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn upload_attachment(&mut self, _path: String) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
async fn mark_conversation_as_read(&mut self, _conversation_id: String) -> Result<()> {
2025-08-01 12:26:17 -07:00
Err(anyhow::anyhow!(
"Daemon interface not implemented on this platform"
))
}
}
pub fn new_daemon_interface() -> Result<Box<dyn DaemonInterface>> {
#[cfg(target_os = "linux")]
{
Ok(Box::new(dbus::DBusDaemonInterface::new()?))
}
2025-08-01 12:26:17 -07:00
#[cfg(target_os = "macos")]
{
Ok(Box::new(xpc::XpcDaemonInterface::new()?))
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
Ok(Box::new(StubDaemonInterface::new()?))
}
}
2025-02-12 00:10:33 -08:00
#[derive(Subcommand)]
pub enum Commands {
/// Gets all known conversations.
Conversations,
/// Runs a full sync operation for a conversation and its messages.
2025-06-06 16:39:31 -07:00
Sync { conversation_id: Option<String> },
/// Runs a sync operation for the conversation list.
SyncList,
2025-02-12 00:10:33 -08:00
/// Prints the server Kordophone version.
Version,
2025-06-06 16:39:31 -07:00
/// Configuration options
Config {
#[command(subcommand)]
command: ConfigCommands,
},
2025-04-27 22:44:05 -07:00
/// Waits for signals from the daemon.
Signals,
/// 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-06-12 17:58:03 -07:00
/// Downloads an attachment from the server to the attachment store. Returns the path to the attachment.
2025-06-16 19:26:13 -07:00
DownloadAttachment { attachment_id: String },
2025-06-12 17:58:03 -07:00
/// Uploads an attachment to the server, returns upload guid.
2025-06-16 19:26:13 -07:00
UploadAttachment { path: String },
2025-06-18 15:02:04 -07:00
/// Marks a conversation as read.
MarkConversationAsRead { conversation_id: String },
}
#[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 },
/// 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 = new_daemon_interface()?;
2025-02-12 00:10:33 -08:00
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::SyncList => client.sync_conversations_list().await,
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,
} => {
2025-08-01 12:26:17 -07:00
client
.print_messages(conversation_id, last_message_id)
.await
2025-06-06 16:39:31 -07:00
}
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-06-12 17:58:03 -07:00
Commands::UploadAttachment { path } => client.upload_attachment(path).await,
2025-06-16 19:26:13 -07:00
Commands::DownloadAttachment { attachment_id } => {
client.download_attachment(attachment_id).await
}
2025-06-18 15:02:04 -07:00
Commands::MarkConversationAsRead { conversation_id } => {
client.mark_conversation_as_read(conversation_id).await
}
2025-02-12 00:10:33 -08:00
}
}
}