2 use jsonrpc_client_transports::{RpcError, TypedSubscriptionStream};
3 use jsonrpc_core::{futures_util::StreamExt, Value};
4 use std::{path::PathBuf, time::Duration};
5 use tokio::{select, time::sleep};
7 use crate::cli::{GroupPermission, LinkState};
10 #[allow(clippy::too_many_arguments)]
14 const DEFAULT_TCP: &str = "127.0.0.1:7583";
15 const DEFAULT_SOCKET_SUFFIX: &str = "signal-cli/socket";
18 async fn main() -> Result<(), anyhow::Error> {
19 let cli = cli::Cli::parse();
21 let client = connect(&cli)
23 .map_err(|e| anyhow::anyhow!("Failed to connect to socket: {e}"))?;
25 let result = match cli.command {
26 cli::CliCommands::Receive { timeout } => {
27 let mut stream = client
28 .subscribe_receive(cli.account)
29 .map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {:?}", e))?;
32 while let Some(v) = stream_next(timeout, &mut stream).await {
33 let v = v.map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {:?}", e))?;
39 cli::CliCommands::AddDevice { uri } => client.add_device(cli.account, uri).await,
40 cli::CliCommands::Block {
43 } => client.block(cli.account, recipient, group_id).await,
44 cli::CliCommands::DeleteLocalAccountData { ignore_registered } => {
46 .delete_local_account_data(cli.account, ignore_registered)
49 cli::CliCommands::GetUserStatus { recipient } => {
50 client.get_user_status(cli.account, recipient).await
52 cli::CliCommands::JoinGroup { uri } => client.join_group(cli.account, uri).await,
53 cli::CliCommands::Link { name } => {
55 .start_link(cli.account)
57 .map_err(|e| anyhow::anyhow!("JSON-RPC command startLink failed: {e:?}",))?
60 client.finish_link(url, name).await
62 cli::CliCommands::ListAccounts => client.list_accounts().await,
63 cli::CliCommands::ListContacts {
70 .list_contacts(cli.account, recipient, all_recipients, blocked, name)
73 cli::CliCommands::ListDevices => client.list_devices(cli.account).await,
74 cli::CliCommands::ListGroups {
77 } => client.list_groups(cli.account, group_id).await,
78 cli::CliCommands::ListIdentities { number } => {
79 client.list_identities(cli.account, number).await
81 cli::CliCommands::ListStickerPacks => client.list_sticker_packs(cli.account).await,
82 cli::CliCommands::QuitGroup {
88 .quit_group(cli.account, group_id, delete, admin)
91 cli::CliCommands::Register { voice, captcha } => {
92 client.register(cli.account, voice, captcha).await
94 cli::CliCommands::RemoveContact { recipient, forget } => {
95 client.remove_contact(cli.account, recipient, forget).await
97 cli::CliCommands::RemoveDevice { device_id } => {
98 client.remove_device(cli.account, device_id).await
100 cli::CliCommands::RemovePin => client.remove_pin(cli.account).await,
101 cli::CliCommands::RemoteDelete {
117 cli::CliCommands::Send {
138 message.unwrap_or_default(),
149 cli::CliCommands::SendContacts => client.send_contacts(cli.account).await,
150 cli::CliCommands::SendPaymentNotification {
156 .send_payment_notification(cli.account, recipient, receipt, note)
159 cli::CliCommands::SendReaction {
183 cli::CliCommands::SendReceipt {
194 cli::ReceiptType::Read => "read".to_owned(),
195 cli::ReceiptType::Viewed => "viewed".to_owned(),
200 cli::CliCommands::SendSyncRequest => client.send_sync_request(cli.account).await,
201 cli::CliCommands::SendTyping {
207 .send_typing(cli.account, recipient, group_id, stop)
210 cli::CliCommands::SetPin { pin } => client.set_pin(cli.account, pin).await,
211 cli::CliCommands::SubmitRateLimitChallenge { challenge, captcha } => {
213 .submit_rate_limit_challenge(cli.account, challenge, captcha)
216 cli::CliCommands::Trust {
218 trust_all_known_keys,
219 verified_safety_number,
225 trust_all_known_keys,
226 verified_safety_number,
230 cli::CliCommands::Unblock {
233 } => client.unblock(cli.account, recipient, group_id).await,
234 cli::CliCommands::Unregister { delete_account } => {
235 client.unregister(cli.account, delete_account).await
237 cli::CliCommands::UpdateAccount { device_name } => {
238 client.update_account(cli.account, device_name).await
240 cli::CliCommands::UpdateConfiguration {
242 unidentified_delivery_indicators,
247 .update_configuration(
250 unidentified_delivery_indicators,
256 cli::CliCommands::UpdateContact {
262 .update_contact(cli.account, recipient, name, expiration)
265 cli::CliCommands::UpdateGroup {
278 set_permission_add_member,
279 set_permission_edit_details,
280 set_permission_send_messages,
297 link.map(|link| match link {
298 LinkState::Enabled => "enabled".to_owned(),
299 LinkState::EnabledWithApproval => "enabledWithApproval".to_owned(),
300 LinkState::Disabled => "disabled".to_owned(),
302 set_permission_add_member.map(|p| match p {
303 GroupPermission::EveryMember => "everyMember".to_owned(),
304 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
306 set_permission_edit_details.map(|p| match p {
307 GroupPermission::EveryMember => "everyMember".to_owned(),
308 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
310 set_permission_send_messages.map(|p| match p {
311 GroupPermission::EveryMember => "everyMember".to_owned(),
312 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
318 cli::CliCommands::UpdateProfile {
340 cli::CliCommands::UploadStickerPack { path } => {
341 client.upload_sticker_pack(cli.account, path).await
343 cli::CliCommands::Verify {
346 } => client.verify(cli.account, verification_code, pin).await,
347 cli::CliCommands::Version => client.version().await,
351 .map(|v| println!("{v}"))
352 .map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {e:?}",))?;
356 async fn connect(cli: &cli::Cli) -> Result<jsonrpc::SignalCliClient, RpcError> {
357 if let Some(tcp) = cli.json_rpc_tcp {
358 let socket_addr = tcp.unwrap_or_else(|| DEFAULT_TCP.parse().unwrap());
359 jsonrpc::connect_tcp(socket_addr).await
361 let socket_path = cli
366 std::env::var_os("XDG_RUNTIME_DIR").map(|runtime_dir| {
367 PathBuf::from(runtime_dir)
368 .join(DEFAULT_SOCKET_SUFFIX)
372 .unwrap_or_else(|| ("/run".to_owned() + DEFAULT_SOCKET_SUFFIX).into());
373 jsonrpc::connect_unix(socket_path).await
377 async fn stream_next(
379 stream: &mut TypedSubscriptionStream<Value>,
380 ) -> Option<Result<Value, RpcError>> {
385 v = stream.next() => v,
386 _= sleep(Duration::from_millis((timeout * 1000.0) as u64)) => None,