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::GetUserStatus { recipient } => {
45 client.get_user_status(cli.account, recipient).await
47 cli::CliCommands::JoinGroup { uri } => client.join_group(cli.account, uri).await,
48 cli::CliCommands::Link { name } => {
50 .start_link(cli.account)
52 .map_err(|e| anyhow::anyhow!("JSON-RPC command startLink failed: {e:?}",))?
55 client.finish_link(url, name).await
57 cli::CliCommands::ListAccounts => client.list_accounts().await,
58 cli::CliCommands::ListContacts => client.list_contacts(cli.account).await,
59 cli::CliCommands::ListDevices => client.list_devices(cli.account).await,
60 cli::CliCommands::ListGroups { detailed: _ } => client.list_groups(cli.account).await,
61 cli::CliCommands::ListIdentities { number } => {
62 client.list_identities(cli.account, number).await
64 cli::CliCommands::ListStickerPacks => client.list_sticker_packs(cli.account).await,
65 cli::CliCommands::QuitGroup {
71 .quit_group(cli.account, group_id, delete, admin)
74 cli::CliCommands::Register { voice, captcha } => {
75 client.register(cli.account, voice, captcha).await
77 cli::CliCommands::RemoveContact { recipient, forget } => {
78 client.remove_contact(cli.account, recipient, forget).await
80 cli::CliCommands::RemoveDevice { device_id } => {
81 client.remove_device(cli.account, device_id).await
83 cli::CliCommands::RemovePin => client.remove_pin(cli.account).await,
84 cli::CliCommands::RemoteDelete {
100 cli::CliCommands::Send {
121 message.unwrap_or_default(),
132 cli::CliCommands::SendContacts => client.send_contacts(cli.account).await,
133 cli::CliCommands::SendReaction {
155 cli::CliCommands::SendReceipt {
166 cli::ReceiptType::Read => "read".to_owned(),
167 cli::ReceiptType::Viewed => "viewed".to_owned(),
172 cli::CliCommands::SendSyncRequest => client.send_sync_request(cli.account).await,
173 cli::CliCommands::SendTyping {
179 .send_typing(cli.account, recipient, group_id, stop)
182 cli::CliCommands::SetPin { pin } => client.set_pin(cli.account, pin).await,
183 cli::CliCommands::SubmitRateLimitChallenge { challenge, captcha } => {
185 .submit_rate_limit_challenge(cli.account, challenge, captcha)
188 cli::CliCommands::Trust {
190 trust_all_known_keys,
191 verified_safety_number,
197 trust_all_known_keys,
198 verified_safety_number,
202 cli::CliCommands::Unblock {
205 } => client.unblock(cli.account, recipient, group_id).await,
206 cli::CliCommands::Unregister { delete_account } => {
207 client.unregister(cli.account, delete_account).await
209 cli::CliCommands::UpdateAccount { device_name } => {
210 client.update_account(cli.account, device_name).await
212 cli::CliCommands::UpdateConfiguration {
214 unidentified_delivery_indicators,
219 .update_configuration(
222 unidentified_delivery_indicators,
228 cli::CliCommands::UpdateContact {
234 .update_contact(cli.account, recipient, name, expiration)
237 cli::CliCommands::UpdateGroup {
248 set_permission_add_member,
249 set_permission_edit_details,
250 set_permission_send_messages,
265 link.map(|link| match link {
266 LinkState::Enabled => "enabled".to_owned(),
267 LinkState::EnabledWithApproval => "enabledWithApproval".to_owned(),
268 LinkState::Disabled => "disabled".to_owned(),
270 set_permission_add_member.map(|p| match p {
271 GroupPermission::EveryMember => "everyMember".to_owned(),
272 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
274 set_permission_edit_details.map(|p| match p {
275 GroupPermission::EveryMember => "everyMember".to_owned(),
276 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
278 set_permission_send_messages.map(|p| match p {
279 GroupPermission::EveryMember => "everyMember".to_owned(),
280 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
286 cli::CliCommands::UpdateProfile {
306 cli::CliCommands::UploadStickerPack { path } => {
307 client.upload_sticker_pack(cli.account, path).await
309 cli::CliCommands::Verify {
312 } => client.verify(cli.account, verification_code, pin).await,
313 cli::CliCommands::Version => client.version().await,
317 .map(|v| println!("{v}"))
318 .map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {e:?}",))?;
322 async fn connect(cli: &cli::Cli) -> Result<jsonrpc::SignalCliClient, RpcError> {
323 if let Some(tcp) = cli.json_rpc_tcp {
324 let socket_addr = tcp.unwrap_or_else(|| DEFAULT_TCP.parse().unwrap());
325 jsonrpc::connect_tcp(socket_addr).await
327 let socket_path = cli
332 std::env::var_os("XDG_RUNTIME_DIR").map(|runtime_dir| {
333 PathBuf::from(runtime_dir)
334 .join(DEFAULT_SOCKET_SUFFIX)
338 .unwrap_or_else(|| ("/run".to_owned() + DEFAULT_SOCKET_SUFFIX).into());
339 jsonrpc::connect_unix(socket_path).await
343 async fn stream_next(
345 stream: &mut TypedSubscriptionStream<Value>,
346 ) -> Option<Result<Value, RpcError>> {
351 v = stream.next() => v,
352 _= sleep(Duration::from_millis((timeout * 1000.0) as u64)) => None,