2025-02-12 00:10:33 -08:00
|
|
|
use anyhow::Result;
|
2025-07-31 19:30:54 -07:00
|
|
|
use async_trait::async_trait;
|
2025-02-12 00:10:33 -08:00
|
|
|
use clap::Subcommand;
|
|
|
|
|
|
2025-07-31 19:30:54 -07:00
|
|
|
// Platform-specific modules
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
mod dbus;
|
|
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2025-07-31 19:30:54 -07:00
|
|
|
struct StubDaemonInterface;
|
|
|
|
|
impl StubDaemonInterface {
|
|
|
|
|
fn new() -> Result<Self> {
|
|
|
|
|
Ok(Self)
|
|
|
|
|
}
|
2025-02-12 00:10:33 -08:00
|
|
|
}
|
|
|
|
|
|
2025-07-31 19:30:54 -07:00
|
|
|
#[async_trait]
|
|
|
|
|
impl DaemonInterface for StubDaemonInterface {
|
|
|
|
|
async fn print_version(&mut self) -> Result<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
async fn print_conversations(&mut self) -> Result<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
async fn sync_conversations(&mut self, _conversation_id: Option<String>) -> Result<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
async fn sync_conversations_list(&mut self) -> Result<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
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"))
|
|
|
|
|
}
|
|
|
|
|
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<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
async fn config(&mut self, _cmd: ConfigCommands) -> Result<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
async fn delete_all_conversations(&mut self) -> Result<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
async fn download_attachment(&mut self, _attachment_id: String) -> Result<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
async fn upload_attachment(&mut self, _path: String) -> Result<()> {
|
|
|
|
|
Err(anyhow::anyhow!("Daemon interface not implemented on this platform"))
|
|
|
|
|
}
|
|
|
|
|
async fn mark_conversation_as_read(&mut self, _conversation_id: String) -> Result<()> {
|
|
|
|
|
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()?))
|
|
|
|
|
}
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
|
|
|
{
|
|
|
|
|
Ok(Box::new(StubDaemonInterface::new()?))
|
|
|
|
|
}
|
|
|
|
|
}
|
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-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 },
|
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<()> {
|
2025-07-31 19:30:54 -07:00
|
|
|
let mut client = new_daemon_interface()?;
|
2025-02-12 00:10:33 -08:00
|
|
|
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,
|
|
|
|
|
} => {
|
2025-07-31 19:30:54 -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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|