mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Implement JSON-RPC client PoC
This commit is contained in:
parent
7261129609
commit
abde122a35
8 changed files with 1982 additions and 13 deletions
340
client/src/cli.rs
Normal file
340
client/src/cli.rs
Normal file
|
@ -0,0 +1,340 @@
|
|||
use clap::{crate_version, ArgEnum, Parser, Subcommand};
|
||||
use std::{ffi::OsString, net::SocketAddr};
|
||||
|
||||
/// JSON-RPC client for signal-cli
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(rename_all = "kebab-case", version=crate_version!())]
|
||||
pub struct Cli {
|
||||
/// Account to use (for daemon in multi-account mode)
|
||||
#[clap(short = 'a', long)]
|
||||
pub account: Option<String>,
|
||||
|
||||
/// TCP host and port of signal-cli daemon
|
||||
#[clap(long)]
|
||||
pub json_rpc_tcp: Option<Option<SocketAddr>>,
|
||||
|
||||
/// UNIX socket address and port of signal-cli daemon
|
||||
#[clap(long)]
|
||||
pub json_rpc_socket: Option<Option<OsString>>,
|
||||
|
||||
#[clap(arg_enum, long, default_value_t = OutputTypes::Json)]
|
||||
pub output: OutputTypes,
|
||||
|
||||
#[clap(long)]
|
||||
pub verbose: bool,
|
||||
|
||||
#[clap(subcommand)]
|
||||
pub command: CliCommands,
|
||||
}
|
||||
|
||||
#[derive(ArgEnum, Clone, Debug)]
|
||||
#[clap(rename_all = "kebab-case")]
|
||||
pub enum OutputTypes {
|
||||
PlainText,
|
||||
Json,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
#[derive(Subcommand, Debug)]
|
||||
#[clap(rename_all = "camelCase", version=crate_version!())]
|
||||
pub enum CliCommands {
|
||||
AddDevice {
|
||||
#[clap(long)]
|
||||
uri: String,
|
||||
},
|
||||
#[clap(rename_all = "kebab-case")]
|
||||
Block {
|
||||
recipient: Vec<String>,
|
||||
|
||||
#[clap(short = 'g', long)]
|
||||
group_id: Vec<String>,
|
||||
},
|
||||
GetUserStatus {
|
||||
recipient: Vec<String>,
|
||||
},
|
||||
JoinGroup {
|
||||
#[clap(long)]
|
||||
uri: String,
|
||||
},
|
||||
Link {
|
||||
#[clap(short = 'n', long)]
|
||||
name: String,
|
||||
},
|
||||
ListAccounts,
|
||||
ListContacts,
|
||||
ListDevices,
|
||||
ListGroups {
|
||||
#[clap(short = 'd', long)]
|
||||
detailed: bool,
|
||||
},
|
||||
ListIdentities {
|
||||
#[clap(short = 'n', long)]
|
||||
number: Option<String>,
|
||||
},
|
||||
ListStickerPacks,
|
||||
QuitGroup {
|
||||
#[clap(short = 'g', long = "group-id")]
|
||||
group_id: String,
|
||||
#[clap(long)]
|
||||
delete: bool,
|
||||
#[clap(long)]
|
||||
admin: Vec<String>,
|
||||
},
|
||||
Receive {
|
||||
#[clap(short = 't', long, default_value_t = 3.0)]
|
||||
timeout: f64,
|
||||
},
|
||||
Register {
|
||||
#[clap(short = 'v', long)]
|
||||
voice: bool,
|
||||
#[clap(long)]
|
||||
captcha: Option<String>,
|
||||
},
|
||||
RemoveContact {
|
||||
recipient: String,
|
||||
#[clap(long)]
|
||||
forget: bool,
|
||||
},
|
||||
RemoveDevice {
|
||||
#[clap(short = 'd', long = "device-id")]
|
||||
device_id: u32,
|
||||
},
|
||||
RemovePin,
|
||||
RemoteDelete {
|
||||
#[clap(short = 't', long = "target-timestamp")]
|
||||
target_timestamp: u64,
|
||||
|
||||
recipient: Vec<String>,
|
||||
|
||||
#[clap(short = 'g', long = "group-id")]
|
||||
group_id: Vec<String>,
|
||||
|
||||
#[clap(long = "note-to-self")]
|
||||
note_to_self: bool,
|
||||
},
|
||||
#[clap(rename_all = "kebab-case")]
|
||||
Send {
|
||||
recipient: Vec<String>,
|
||||
|
||||
#[clap(short = 'g', long)]
|
||||
group_id: Vec<String>,
|
||||
|
||||
#[clap(long)]
|
||||
note_to_self: bool,
|
||||
|
||||
#[clap(short = 'e', long)]
|
||||
end_session: bool,
|
||||
|
||||
#[clap(short = 'm', long)]
|
||||
message: Option<String>,
|
||||
|
||||
#[clap(short = 'a', long)]
|
||||
attachment: Vec<String>,
|
||||
|
||||
#[clap(long)]
|
||||
mention: Vec<String>,
|
||||
|
||||
#[clap(long)]
|
||||
quote_timestamp: Option<u64>,
|
||||
|
||||
#[clap(long)]
|
||||
quote_author: Option<String>,
|
||||
|
||||
#[clap(long)]
|
||||
quote_message: Option<String>,
|
||||
|
||||
#[clap(long)]
|
||||
quote_mention: Vec<String>,
|
||||
|
||||
#[clap(long)]
|
||||
sticker: Option<String>,
|
||||
},
|
||||
SendContacts,
|
||||
SendReaction {
|
||||
recipient: Vec<String>,
|
||||
|
||||
#[clap(short = 'g', long = "group-id")]
|
||||
group_id: Vec<String>,
|
||||
|
||||
#[clap(long = "note-to-self")]
|
||||
note_to_self: bool,
|
||||
|
||||
#[clap(short = 'e', long)]
|
||||
emoji: String,
|
||||
|
||||
#[clap(short = 'a', long = "target-author")]
|
||||
target_author: String,
|
||||
|
||||
#[clap(short = 't', long = "target-timestamp")]
|
||||
target_timestamp: u64,
|
||||
|
||||
#[clap(short = 'r', long)]
|
||||
remove: bool,
|
||||
},
|
||||
SendReceipt {
|
||||
recipient: String,
|
||||
|
||||
#[clap(short = 't', long = "target-timestamp")]
|
||||
target_timestamp: Vec<u64>,
|
||||
|
||||
#[clap(arg_enum, long)]
|
||||
r#type: ReceiptType,
|
||||
},
|
||||
SendSyncRequest,
|
||||
SendTyping {
|
||||
recipient: Vec<String>,
|
||||
|
||||
#[clap(short = 'g', long = "group-id")]
|
||||
group_id: Vec<String>,
|
||||
|
||||
#[clap(short = 's', long)]
|
||||
stop: bool,
|
||||
},
|
||||
SetPin {
|
||||
pin: String,
|
||||
},
|
||||
SubmitRateLimitChallenge {
|
||||
challenge: String,
|
||||
captcha: String,
|
||||
},
|
||||
Trust {
|
||||
recipient: String,
|
||||
|
||||
#[clap(short = 'a', long = "trust-all-known-keys")]
|
||||
trust_all_known_keys: bool,
|
||||
|
||||
#[clap(short = 'v', long = "verified-safety-number")]
|
||||
verified_safety_number: Option<String>,
|
||||
},
|
||||
#[clap(rename_all = "kebab-case")]
|
||||
Unblock {
|
||||
recipient: Vec<String>,
|
||||
|
||||
#[clap(short = 'g', long)]
|
||||
group_id: Vec<String>,
|
||||
},
|
||||
Unregister {
|
||||
#[clap(long = "delete-account")]
|
||||
delete_account: bool,
|
||||
},
|
||||
UpdateAccount {
|
||||
#[clap(short = 'n', long = "device-name")]
|
||||
device_name: Option<String>,
|
||||
},
|
||||
UpdateConfiguration {
|
||||
#[clap(long = "read-receipts", parse(try_from_str))]
|
||||
read_receipts: Option<bool>,
|
||||
|
||||
#[clap(long = "unidentified-delivery-indicators")]
|
||||
unidentified_delivery_indicators: Option<bool>,
|
||||
|
||||
#[clap(long = "typing-indicators")]
|
||||
typing_indicators: Option<bool>,
|
||||
|
||||
#[clap(long = "link-previews")]
|
||||
link_previews: Option<bool>,
|
||||
},
|
||||
UpdateContact {
|
||||
recipient: String,
|
||||
|
||||
#[clap(short = 'e', long)]
|
||||
expiration: Option<u32>,
|
||||
|
||||
#[clap(short = 'n', long)]
|
||||
name: Option<String>,
|
||||
},
|
||||
UpdateGroup {
|
||||
#[clap(short = 'g', long = "group-id")]
|
||||
group_id: Option<String>,
|
||||
|
||||
#[clap(short = 'n', long)]
|
||||
name: Option<String>,
|
||||
|
||||
#[clap(short = 'd', long)]
|
||||
description: Option<String>,
|
||||
|
||||
#[clap(short = 'a', long)]
|
||||
avatar: Option<String>,
|
||||
|
||||
#[clap(short = 'm', long)]
|
||||
member: Vec<String>,
|
||||
|
||||
#[clap(short = 'r', long = "remove-member")]
|
||||
remove_member: Vec<String>,
|
||||
|
||||
#[clap(long)]
|
||||
admin: Vec<String>,
|
||||
|
||||
#[clap(long = "remove-admin")]
|
||||
remove_admin: Vec<String>,
|
||||
|
||||
#[clap(long = "reset-link")]
|
||||
reset_link: bool,
|
||||
|
||||
#[clap(arg_enum, long)]
|
||||
link: Option<LinkState>,
|
||||
|
||||
#[clap(arg_enum, long = "set-permission-add-member")]
|
||||
set_permission_add_member: Option<GroupPermission>,
|
||||
|
||||
#[clap(arg_enum, long = "set-permission-edit-details")]
|
||||
set_permission_edit_details: Option<GroupPermission>,
|
||||
|
||||
#[clap(arg_enum, long = "set-permission-send-messages")]
|
||||
set_permission_send_messages: Option<GroupPermission>,
|
||||
|
||||
#[clap(short = 'e', long)]
|
||||
expiration: Option<u32>,
|
||||
},
|
||||
UpdateProfile {
|
||||
#[clap(long = "given-name")]
|
||||
given_name: Option<String>,
|
||||
|
||||
#[clap(long = "family-name")]
|
||||
family_name: Option<String>,
|
||||
|
||||
#[clap(long)]
|
||||
about: Option<String>,
|
||||
|
||||
#[clap(long = "about-emoji")]
|
||||
about_emoji: Option<String>,
|
||||
|
||||
#[clap(long)]
|
||||
avatar: Option<String>,
|
||||
|
||||
#[clap(long = "remove-avatar")]
|
||||
remove_avatar: bool,
|
||||
},
|
||||
UploadStickerPack {
|
||||
path: String,
|
||||
},
|
||||
Verify {
|
||||
verification_code: String,
|
||||
|
||||
#[clap(short = 'p', long)]
|
||||
pin: Option<String>,
|
||||
},
|
||||
Version,
|
||||
}
|
||||
|
||||
#[derive(ArgEnum, Clone, Debug)]
|
||||
#[clap(rename_all = "kebab-case")]
|
||||
pub enum ReceiptType {
|
||||
Read,
|
||||
Viewed,
|
||||
}
|
||||
|
||||
#[derive(ArgEnum, Clone, Debug)]
|
||||
#[clap(rename_all = "kebab-case")]
|
||||
pub enum LinkState {
|
||||
Enabled,
|
||||
EnabledWithApproval,
|
||||
Disabled,
|
||||
}
|
||||
|
||||
#[derive(ArgEnum, Clone, Debug)]
|
||||
#[clap(rename_all = "kebab-case")]
|
||||
pub enum GroupPermission {
|
||||
EveryMember,
|
||||
OnlyAdmins,
|
||||
}
|
288
client/src/jsonrpc.rs
Normal file
288
client/src/jsonrpc.rs
Normal file
|
@ -0,0 +1,288 @@
|
|||
use std::path::Path;
|
||||
|
||||
use jsonrpc_client_transports::{transports::ipc, RpcError};
|
||||
use jsonrpc_core::serde::Deserialize;
|
||||
use jsonrpc_derive::rpc;
|
||||
use tokio::net::ToSocketAddrs;
|
||||
|
||||
pub type SignalCliClient = gen_client::Client;
|
||||
|
||||
#[rpc(client, params = "named")]
|
||||
pub trait Rpc {
|
||||
#[rpc(name = "addDevice", params = "named")]
|
||||
fn add_device(&self, account: Option<String>, uri: String) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "block", params = "named")]
|
||||
fn block(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipients: Vec<String>,
|
||||
#[allow(non_snake_case)] groupIds: Vec<String>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "getUserStatus", params = "named")]
|
||||
fn get_user_status(&self, account: Option<String>, recipients: Vec<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "joinGroup", params = "named")]
|
||||
fn join_group(&self, account: Option<String>, uri: String) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "finishLink", params = "named")]
|
||||
fn finish_link(
|
||||
&self,
|
||||
#[allow(non_snake_case)] deviceLinkUri: String,
|
||||
#[allow(non_snake_case)] deviceName: String,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "listAccounts", params = "named")]
|
||||
fn list_accounts(&self) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "listContacts", params = "named")]
|
||||
fn list_contacts(&self, account: Option<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "listDevices", params = "named")]
|
||||
fn list_devices(&self, account: Option<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "listGroups", params = "named")]
|
||||
fn list_groups(&self, account: Option<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "listIdentities", params = "named")]
|
||||
fn list_identities(&self, account: Option<String>, number: Option<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "listStickerPacks", params = "named")]
|
||||
fn list_sticker_packs(&self, account: Option<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "quitGroup", params = "named")]
|
||||
fn quit_group(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] groupId: String,
|
||||
delete: bool,
|
||||
admins: Vec<String>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "register", params = "named")]
|
||||
fn register(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
voice: bool,
|
||||
captcha: Option<String>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "removeContact", params = "named")]
|
||||
fn remove_contact(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipient: String,
|
||||
forget: bool,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "removeDevice", params = "named")]
|
||||
fn remove_device(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] deviceId: u32,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "removePin", params = "named")]
|
||||
fn remove_pin(&self, account: Option<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "remoteDelete", params = "named")]
|
||||
fn remote_delete(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] targetTimestamp: u64,
|
||||
recipients: Vec<String>,
|
||||
#[allow(non_snake_case)] groupIds: Vec<String>,
|
||||
#[allow(non_snake_case)] noteToSelf: bool,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "send", params = "named")]
|
||||
fn send(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipients: Vec<String>,
|
||||
#[allow(non_snake_case)] groupIds: Vec<String>,
|
||||
#[allow(non_snake_case)] noteToSelf: bool,
|
||||
#[allow(non_snake_case)] endSession: bool,
|
||||
message: String,
|
||||
attachments: Vec<String>,
|
||||
mentions: Vec<String>,
|
||||
#[allow(non_snake_case)] quoteTimestamp: Option<u64>,
|
||||
#[allow(non_snake_case)] quoteAuthor: Option<String>,
|
||||
#[allow(non_snake_case)] quoteMessage: Option<String>,
|
||||
#[allow(non_snake_case)] quoteMention: Vec<String>,
|
||||
sticker: Option<String>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "sendContacts", params = "named")]
|
||||
fn send_contacts(&self, account: Option<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "sendReaction", params = "named")]
|
||||
fn send_reaction(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipients: Vec<String>,
|
||||
#[allow(non_snake_case)] groupIds: Vec<String>,
|
||||
#[allow(non_snake_case)] noteToSelf: bool,
|
||||
emoji: String,
|
||||
#[allow(non_snake_case)] targetAuthor: String,
|
||||
#[allow(non_snake_case)] targetTimestamp: u64,
|
||||
remove: bool,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "sendReceipt", params = "named")]
|
||||
fn send_receipt(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipient: String,
|
||||
#[allow(non_snake_case)] targetTimestamps: Vec<u64>,
|
||||
r#type: String,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "sendSyncRequest", params = "named")]
|
||||
fn send_sync_request(&self, account: Option<String>) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "sendTyping", params = "named")]
|
||||
fn send_typing(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipients: Vec<String>,
|
||||
#[allow(non_snake_case)] groupIds: Vec<String>,
|
||||
stop: bool,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "setPin", params = "named")]
|
||||
fn set_pin(&self, account: Option<String>, pin: String) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "submitRateLimitChallenge", params = "named")]
|
||||
fn submit_rate_limit_challenge(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
challenge: String,
|
||||
captcha: String,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "startLink", params = "named")]
|
||||
fn start_link(&self, account: Option<String>) -> Result<JsonLink>;
|
||||
|
||||
#[rpc(name = "trust", params = "named")]
|
||||
fn trust(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipient: String,
|
||||
#[allow(non_snake_case)] trustAllKnownKeys: bool,
|
||||
#[allow(non_snake_case)] verifiedSafetyNumber: Option<String>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "unblock", params = "named")]
|
||||
fn unblock(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipients: Vec<String>,
|
||||
#[allow(non_snake_case)] groupIds: Vec<String>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "unregister", params = "named")]
|
||||
fn unregister(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] deleteAccount: bool,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "updateAccount", params = "named")]
|
||||
fn update_account(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] deviceName: Option<String>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "updateConfiguration", params = "named")]
|
||||
fn update_configuration(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] readReceiptes: Option<bool>,
|
||||
#[allow(non_snake_case)] unidentifiedDeliveryIndicators: Option<bool>,
|
||||
#[allow(non_snake_case)] typingIndicators: Option<bool>,
|
||||
#[allow(non_snake_case)] linkPreviews: Option<bool>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "updateContact", params = "named")]
|
||||
fn update_contact(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
recipient: String,
|
||||
name: Option<String>,
|
||||
expiration: Option<u32>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "updateGroup", params = "named")]
|
||||
fn update_group(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] groupId: Option<String>,
|
||||
name: Option<String>,
|
||||
description: Option<String>,
|
||||
avatar: Option<String>,
|
||||
member: Vec<String>,
|
||||
#[allow(non_snake_case)] removeMember: Vec<String>,
|
||||
admin: Vec<String>,
|
||||
#[allow(non_snake_case)] removeAdmin: Vec<String>,
|
||||
#[allow(non_snake_case)] resetLink: bool,
|
||||
#[allow(non_snake_case)] link: Option<String>,
|
||||
#[allow(non_snake_case)] setPermissionAddMember: Option<String>,
|
||||
#[allow(non_snake_case)] setPermissionEditDetails: Option<String>,
|
||||
#[allow(non_snake_case)] setPermissionSendMessages: Option<String>,
|
||||
expiration: Option<u32>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "updateProfile", params = "named")]
|
||||
fn update_profile(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] givenName: Option<String>,
|
||||
#[allow(non_snake_case)] familyName: Option<String>,
|
||||
about: Option<String>,
|
||||
#[allow(non_snake_case)] aboutEmoji: Option<String>,
|
||||
avatar: Option<String>,
|
||||
#[allow(non_snake_case)] removeAvatar: bool,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "uploadStickerPack", params = "named")]
|
||||
fn upload_sticker_pack(&self, account: Option<String>, path: String) -> Result<Value>;
|
||||
|
||||
#[rpc(name = "verify", params = "named")]
|
||||
fn verify(
|
||||
&self,
|
||||
account: Option<String>,
|
||||
#[allow(non_snake_case)] verificationCode: String,
|
||||
pin: Option<String>,
|
||||
) -> Result<Value>;
|
||||
|
||||
#[pubsub(
|
||||
subscription = "receive",
|
||||
subscribe,
|
||||
name = "subscribeReceive",
|
||||
params = "named"
|
||||
)]
|
||||
fn subscribe_receive(&self, _: Self::Metadata, _: Subscriber<Value>, account: Option<String>);
|
||||
|
||||
#[pubsub(subscription = "receive", unsubscribe, name = "unsubscribeReceive")]
|
||||
fn unsubscribe_receive(&self, _: Option<Self::Metadata>, _: SubscriptionId) -> Result<bool>;
|
||||
|
||||
#[rpc(name = "version")]
|
||||
fn version(&self) -> Result<Value>;
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct JsonLink {
|
||||
pub device_link_uri: String,
|
||||
}
|
||||
|
||||
pub async fn connect_tcp(tcp: impl ToSocketAddrs) -> Result<SignalCliClient, RpcError> {
|
||||
super::tcp::connect::<_, SignalCliClient>(tcp).await
|
||||
}
|
||||
|
||||
pub async fn connect_unix(socket_path: impl AsRef<Path>) -> Result<SignalCliClient, RpcError> {
|
||||
ipc::connect::<_, SignalCliClient>(socket_path).await
|
||||
}
|
355
client/src/main.rs
Normal file
355
client/src/main.rs
Normal file
|
@ -0,0 +1,355 @@
|
|||
use clap::StructOpt;
|
||||
use jsonrpc_client_transports::{RpcError, TypedSubscriptionStream};
|
||||
use jsonrpc_core::{futures_util::StreamExt, Value};
|
||||
use std::{path::PathBuf, time::Duration};
|
||||
use tokio::{select, time::sleep};
|
||||
|
||||
use crate::cli::{GroupPermission, LinkState};
|
||||
|
||||
mod cli;
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
mod jsonrpc;
|
||||
mod tcp;
|
||||
|
||||
const DEFAULT_TCP: &str = "127.0.0.1:7583";
|
||||
const DEFAULT_SOCKET_SUFFIX: &str = "signal-cli/socket";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), anyhow::Error> {
|
||||
let cli = cli::Cli::parse();
|
||||
|
||||
let client = connect(&cli)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to connect to socket: {e}"))?;
|
||||
|
||||
let result = match cli.command {
|
||||
cli::CliCommands::Receive { timeout } => {
|
||||
let mut stream = client
|
||||
.subscribe_receive(cli.account)
|
||||
.map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {:?}", e))?;
|
||||
|
||||
{
|
||||
while let Some(v) = stream_next(timeout, &mut stream).await {
|
||||
let v = v.map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {:?}", e))?;
|
||||
println!("{v}");
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
cli::CliCommands::AddDevice { uri } => client.add_device(cli.account, uri).await,
|
||||
cli::CliCommands::Block {
|
||||
recipient,
|
||||
group_id,
|
||||
} => client.block(cli.account, recipient, group_id).await,
|
||||
cli::CliCommands::GetUserStatus { recipient } => {
|
||||
client.get_user_status(cli.account, recipient).await
|
||||
}
|
||||
cli::CliCommands::JoinGroup { uri } => client.join_group(cli.account, uri).await,
|
||||
cli::CliCommands::Link { name } => {
|
||||
let url = client
|
||||
.start_link(cli.account)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("JSON-RPC command startLink failed: {e:?}",))?
|
||||
.device_link_uri;
|
||||
println!("{}", url);
|
||||
client.finish_link(url, name).await
|
||||
}
|
||||
cli::CliCommands::ListAccounts => client.list_accounts().await,
|
||||
cli::CliCommands::ListContacts => client.list_contacts(cli.account).await,
|
||||
cli::CliCommands::ListDevices => client.list_devices(cli.account).await,
|
||||
cli::CliCommands::ListGroups { detailed: _ } => client.list_groups(cli.account).await,
|
||||
cli::CliCommands::ListIdentities { number } => {
|
||||
client.list_identities(cli.account, number).await
|
||||
}
|
||||
cli::CliCommands::ListStickerPacks => client.list_sticker_packs(cli.account).await,
|
||||
cli::CliCommands::QuitGroup {
|
||||
group_id,
|
||||
delete,
|
||||
admin,
|
||||
} => {
|
||||
client
|
||||
.quit_group(cli.account, group_id, delete, admin)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::Register { voice, captcha } => {
|
||||
client.register(cli.account, voice, captcha).await
|
||||
}
|
||||
cli::CliCommands::RemoveContact { recipient, forget } => {
|
||||
client.remove_contact(cli.account, recipient, forget).await
|
||||
}
|
||||
cli::CliCommands::RemoveDevice { device_id } => {
|
||||
client.remove_device(cli.account, device_id).await
|
||||
}
|
||||
cli::CliCommands::RemovePin => client.remove_pin(cli.account).await,
|
||||
cli::CliCommands::RemoteDelete {
|
||||
target_timestamp,
|
||||
recipient,
|
||||
group_id,
|
||||
note_to_self,
|
||||
} => {
|
||||
client
|
||||
.remote_delete(
|
||||
cli.account,
|
||||
target_timestamp,
|
||||
recipient,
|
||||
group_id,
|
||||
note_to_self,
|
||||
)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::Send {
|
||||
recipient,
|
||||
group_id,
|
||||
note_to_self,
|
||||
end_session,
|
||||
message,
|
||||
attachment,
|
||||
mention,
|
||||
quote_timestamp,
|
||||
quote_author,
|
||||
quote_message,
|
||||
quote_mention,
|
||||
sticker,
|
||||
} => {
|
||||
client
|
||||
.send(
|
||||
cli.account,
|
||||
recipient,
|
||||
group_id,
|
||||
note_to_self,
|
||||
end_session,
|
||||
message.unwrap_or_default(),
|
||||
attachment,
|
||||
mention,
|
||||
quote_timestamp,
|
||||
quote_author,
|
||||
quote_message,
|
||||
quote_mention,
|
||||
sticker,
|
||||
)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::SendContacts => client.send_contacts(cli.account).await,
|
||||
cli::CliCommands::SendReaction {
|
||||
recipient,
|
||||
group_id,
|
||||
note_to_self,
|
||||
emoji,
|
||||
target_author,
|
||||
target_timestamp,
|
||||
remove,
|
||||
} => {
|
||||
client
|
||||
.send_reaction(
|
||||
cli.account,
|
||||
recipient,
|
||||
group_id,
|
||||
note_to_self,
|
||||
emoji,
|
||||
target_author,
|
||||
target_timestamp,
|
||||
remove,
|
||||
)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::SendReceipt {
|
||||
recipient,
|
||||
target_timestamp,
|
||||
r#type,
|
||||
} => {
|
||||
client
|
||||
.send_receipt(
|
||||
cli.account,
|
||||
recipient,
|
||||
target_timestamp,
|
||||
match r#type {
|
||||
cli::ReceiptType::Read => "read".to_owned(),
|
||||
cli::ReceiptType::Viewed => "viewed".to_owned(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::SendSyncRequest => client.send_sync_request(cli.account).await,
|
||||
cli::CliCommands::SendTyping {
|
||||
recipient,
|
||||
group_id,
|
||||
stop,
|
||||
} => {
|
||||
client
|
||||
.send_typing(cli.account, recipient, group_id, stop)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::SetPin { pin } => client.set_pin(cli.account, pin).await,
|
||||
cli::CliCommands::SubmitRateLimitChallenge { challenge, captcha } => {
|
||||
client
|
||||
.submit_rate_limit_challenge(cli.account, challenge, captcha)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::Trust {
|
||||
recipient,
|
||||
trust_all_known_keys,
|
||||
verified_safety_number,
|
||||
} => {
|
||||
client
|
||||
.trust(
|
||||
cli.account,
|
||||
recipient,
|
||||
trust_all_known_keys,
|
||||
verified_safety_number,
|
||||
)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::Unblock {
|
||||
recipient,
|
||||
group_id,
|
||||
} => client.unblock(cli.account, recipient, group_id).await,
|
||||
cli::CliCommands::Unregister { delete_account } => {
|
||||
client.unregister(cli.account, delete_account).await
|
||||
}
|
||||
cli::CliCommands::UpdateAccount { device_name } => {
|
||||
client.update_account(cli.account, device_name).await
|
||||
}
|
||||
cli::CliCommands::UpdateConfiguration {
|
||||
read_receipts,
|
||||
unidentified_delivery_indicators,
|
||||
typing_indicators,
|
||||
link_previews,
|
||||
} => {
|
||||
client
|
||||
.update_configuration(
|
||||
cli.account,
|
||||
read_receipts,
|
||||
unidentified_delivery_indicators,
|
||||
typing_indicators,
|
||||
link_previews,
|
||||
)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::UpdateContact {
|
||||
recipient,
|
||||
expiration,
|
||||
name,
|
||||
} => {
|
||||
client
|
||||
.update_contact(cli.account, recipient, name, expiration)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::UpdateGroup {
|
||||
group_id,
|
||||
name,
|
||||
description,
|
||||
avatar,
|
||||
member,
|
||||
remove_member,
|
||||
admin,
|
||||
remove_admin,
|
||||
reset_link,
|
||||
link,
|
||||
set_permission_add_member,
|
||||
set_permission_edit_details,
|
||||
set_permission_send_messages,
|
||||
expiration,
|
||||
} => {
|
||||
client
|
||||
.update_group(
|
||||
cli.account,
|
||||
group_id,
|
||||
name,
|
||||
description,
|
||||
avatar,
|
||||
member,
|
||||
remove_member,
|
||||
admin,
|
||||
remove_admin,
|
||||
reset_link,
|
||||
link.map(|link| match link {
|
||||
LinkState::Enabled => "enabled".to_owned(),
|
||||
LinkState::EnabledWithApproval => "enabledWithApproval".to_owned(),
|
||||
LinkState::Disabled => "disabled".to_owned(),
|
||||
}),
|
||||
set_permission_add_member.map(|p| match p {
|
||||
GroupPermission::EveryMember => "everyMember".to_owned(),
|
||||
GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
|
||||
}),
|
||||
set_permission_edit_details.map(|p| match p {
|
||||
GroupPermission::EveryMember => "everyMember".to_owned(),
|
||||
GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
|
||||
}),
|
||||
set_permission_send_messages.map(|p| match p {
|
||||
GroupPermission::EveryMember => "everyMember".to_owned(),
|
||||
GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
|
||||
}),
|
||||
expiration,
|
||||
)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::UpdateProfile {
|
||||
given_name,
|
||||
family_name,
|
||||
about,
|
||||
about_emoji,
|
||||
avatar,
|
||||
remove_avatar,
|
||||
} => {
|
||||
client
|
||||
.update_profile(
|
||||
cli.account,
|
||||
given_name,
|
||||
family_name,
|
||||
about,
|
||||
about_emoji,
|
||||
avatar,
|
||||
remove_avatar,
|
||||
)
|
||||
.await
|
||||
}
|
||||
cli::CliCommands::UploadStickerPack { path } => {
|
||||
client.upload_sticker_pack(cli.account, path).await
|
||||
}
|
||||
cli::CliCommands::Verify {
|
||||
verification_code,
|
||||
pin,
|
||||
} => client.verify(cli.account, verification_code, pin).await,
|
||||
cli::CliCommands::Version => client.version().await,
|
||||
};
|
||||
|
||||
result
|
||||
.map(|v| println!("{v}"))
|
||||
.map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {e:?}",))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn connect(cli: &cli::Cli) -> Result<jsonrpc::SignalCliClient, RpcError> {
|
||||
if let Some(tcp) = cli.json_rpc_tcp {
|
||||
let socket_addr = tcp.unwrap_or_else(|| DEFAULT_TCP.parse().unwrap());
|
||||
jsonrpc::connect_tcp(socket_addr).await
|
||||
} else {
|
||||
let socket_path = cli
|
||||
.json_rpc_socket
|
||||
.clone()
|
||||
.unwrap_or(None)
|
||||
.or_else(|| {
|
||||
std::env::var_os("XDG_RUNTIME_DIR").map(|runtime_dir| {
|
||||
PathBuf::from(runtime_dir)
|
||||
.join(DEFAULT_SOCKET_SUFFIX)
|
||||
.into()
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| ("/run".to_owned() + DEFAULT_SOCKET_SUFFIX).into());
|
||||
jsonrpc::connect_unix(socket_path).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn stream_next(
|
||||
timeout: f64,
|
||||
stream: &mut TypedSubscriptionStream<Value>,
|
||||
) -> Option<Result<Value, RpcError>> {
|
||||
if timeout < 0.0 {
|
||||
stream.next().await
|
||||
} else {
|
||||
select! {
|
||||
v = stream.next() => v,
|
||||
_= sleep(Duration::from_millis((timeout * 1000.0) as u64)) => None,
|
||||
}
|
||||
}
|
||||
}
|
29
client/src/tcp.rs
Normal file
29
client/src/tcp.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use jsonrpc_client_transports::{transports::duplex, RpcChannel, RpcError};
|
||||
use jsonrpc_core::futures_util::{SinkExt, StreamExt, TryStreamExt};
|
||||
use jsonrpc_server_utils::{codecs::StreamCodec, tokio_util::codec::Decoder};
|
||||
use tokio::net::{TcpStream, ToSocketAddrs};
|
||||
|
||||
/// Connect to a JSON-RPC TCP server.
|
||||
pub async fn connect<S: ToSocketAddrs, Client: From<RpcChannel>>(
|
||||
socket: S,
|
||||
) -> Result<Client, RpcError> {
|
||||
let connection = TcpStream::connect(socket)
|
||||
.await
|
||||
.map_err(|e| RpcError::Other(Box::new(e)))?;
|
||||
let (sink, stream) = StreamCodec::stream_incoming().framed(connection).split();
|
||||
let sink = sink.sink_map_err(|e| RpcError::Other(Box::new(e)));
|
||||
let stream = stream.map_err(|e| log::error!("TCP stream error: {}", e));
|
||||
|
||||
let (client, sender) = duplex(
|
||||
Box::pin(sink),
|
||||
Box::pin(
|
||||
stream
|
||||
.take_while(|x| std::future::ready(x.is_ok()))
|
||||
.map(|x| x.expect("Stream is closed upon first error.")),
|
||||
),
|
||||
);
|
||||
|
||||
tokio::spawn(client);
|
||||
|
||||
Ok(sender.into())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue