]> nmode's Git Repositories - signal-cli/blob - client/src/main.rs
Add missing command to json-rpc client
[signal-cli] / client / src / main.rs
1 use std::{path::PathBuf, time::Duration};
2
3 use clap::Parser;
4 use jsonrpsee::core::client::{Subscription, SubscriptionClientT};
5 use jsonrpsee::core::Error as RpcError;
6 use serde_json::Value;
7 use tokio::{select, time::sleep};
8
9 use cli::Cli;
10
11 use crate::cli::{CliCommands, GroupPermission, LinkState};
12 use crate::jsonrpc::RpcClient;
13
14 mod cli;
15 #[allow(non_snake_case, clippy::too_many_arguments)]
16 mod jsonrpc;
17 mod transports;
18
19 const DEFAULT_TCP: &str = "127.0.0.1:7583";
20 const DEFAULT_SOCKET_SUFFIX: &str = "signal-cli/socket";
21 const DEFAULT_HTTP: &str = "http://localhost:8080/api/v1/rpc";
22
23 #[tokio::main]
24 async fn main() -> Result<(), anyhow::Error> {
25 let cli = cli::Cli::parse();
26
27 let result = connect(cli).await;
28
29 match result {
30 Ok(Value::Null) => {}
31 Ok(v) => println!("{v}"),
32 Err(e) => return Err(anyhow::anyhow!("JSON-RPC command failed: {e:?}")),
33 }
34 Ok(())
35 }
36
37 async fn handle_command(
38 cli: Cli,
39 client: impl SubscriptionClientT + Sync,
40 ) -> Result<Value, RpcError> {
41 match cli.command {
42 CliCommands::Receive { timeout } => {
43 let mut stream = client.subscribe_receive(cli.account).await?;
44
45 {
46 while let Some(v) = stream_next(timeout, &mut stream).await {
47 let v = v?;
48 println!("{v}");
49 }
50 }
51 stream.unsubscribe().await?;
52 Ok(Value::Null)
53 }
54 CliCommands::AddDevice { uri } => client.add_device(cli.account, uri).await,
55 CliCommands::Block {
56 recipient,
57 group_id,
58 } => client.block(cli.account, recipient, group_id).await,
59 CliCommands::DeleteLocalAccountData { ignore_registered } => {
60 client
61 .delete_local_account_data(cli.account, ignore_registered)
62 .await
63 }
64 CliCommands::GetUserStatus { recipient } => {
65 client.get_user_status(cli.account, recipient).await
66 }
67 CliCommands::JoinGroup { uri } => client.join_group(cli.account, uri).await,
68 CliCommands::Link { name } => {
69 let url = client
70 .start_link(cli.account)
71 .await
72 .map_err(|e| RpcError::Custom(format!("JSON-RPC command startLink failed: {e:?}")))?
73 .device_link_uri;
74 println!("{}", url);
75 client.finish_link(url, name).await
76 }
77 CliCommands::ListAccounts => client.list_accounts().await,
78 CliCommands::ListContacts {
79 recipient,
80 all_recipients,
81 blocked,
82 name,
83 } => {
84 client
85 .list_contacts(cli.account, recipient, all_recipients, blocked, name)
86 .await
87 }
88 CliCommands::ListDevices => client.list_devices(cli.account).await,
89 CliCommands::ListGroups {
90 detailed: _,
91 group_id,
92 } => client.list_groups(cli.account, group_id).await,
93 CliCommands::ListIdentities { number } => client.list_identities(cli.account, number).await,
94 CliCommands::ListStickerPacks => client.list_sticker_packs(cli.account).await,
95 CliCommands::QuitGroup {
96 group_id,
97 delete,
98 admin,
99 } => {
100 client
101 .quit_group(cli.account, group_id, delete, admin)
102 .await
103 }
104 CliCommands::Register { voice, captcha } => {
105 client.register(cli.account, voice, captcha).await
106 }
107 CliCommands::RemoveContact {
108 recipient,
109 forget,
110 hide,
111 } => {
112 client
113 .remove_contact(cli.account, recipient, forget, hide)
114 .await
115 }
116 CliCommands::RemoveDevice { device_id } => {
117 client.remove_device(cli.account, device_id).await
118 }
119 CliCommands::RemovePin => client.remove_pin(cli.account).await,
120 CliCommands::RemoteDelete {
121 target_timestamp,
122 recipient,
123 group_id,
124 note_to_self,
125 } => {
126 client
127 .remote_delete(
128 cli.account,
129 target_timestamp,
130 recipient,
131 group_id,
132 note_to_self,
133 )
134 .await
135 }
136 CliCommands::Send {
137 recipient,
138 group_id,
139 note_to_self,
140 end_session,
141 message,
142 attachment,
143 mention,
144 text_style,
145 quote_timestamp,
146 quote_author,
147 quote_message,
148 quote_mention,
149 quote_text_style,
150 quote_attachment,
151 preview_url,
152 preview_title,
153 preview_description,
154 preview_image,
155 sticker,
156 story_timestamp,
157 story_author,
158 edit_timestamp,
159 } => {
160 client
161 .send(
162 cli.account,
163 recipient,
164 group_id,
165 note_to_self,
166 end_session,
167 message.unwrap_or_default(),
168 attachment,
169 mention,
170 text_style,
171 quote_timestamp,
172 quote_author,
173 quote_message,
174 quote_mention,
175 quote_text_style,
176 quote_attachment,
177 preview_url,
178 preview_title,
179 preview_description,
180 preview_image,
181 sticker,
182 story_timestamp,
183 story_author,
184 edit_timestamp,
185 )
186 .await
187 }
188 CliCommands::SendContacts => client.send_contacts(cli.account).await,
189 CliCommands::SendPaymentNotification {
190 recipient,
191 receipt,
192 note,
193 } => {
194 client
195 .send_payment_notification(cli.account, recipient, receipt, note)
196 .await
197 }
198 CliCommands::SendReaction {
199 recipient,
200 group_id,
201 note_to_self,
202 emoji,
203 target_author,
204 target_timestamp,
205 remove,
206 story,
207 } => {
208 client
209 .send_reaction(
210 cli.account,
211 recipient,
212 group_id,
213 note_to_self,
214 emoji,
215 target_author,
216 target_timestamp,
217 remove,
218 story,
219 )
220 .await
221 }
222 CliCommands::SendReceipt {
223 recipient,
224 target_timestamp,
225 r#type,
226 } => {
227 client
228 .send_receipt(
229 cli.account,
230 recipient,
231 target_timestamp,
232 match r#type {
233 cli::ReceiptType::Read => "read".to_owned(),
234 cli::ReceiptType::Viewed => "viewed".to_owned(),
235 },
236 )
237 .await
238 }
239 CliCommands::SendSyncRequest => client.send_sync_request(cli.account).await,
240 CliCommands::SendTyping {
241 recipient,
242 group_id,
243 stop,
244 } => {
245 client
246 .send_typing(cli.account, recipient, group_id, stop)
247 .await
248 }
249 CliCommands::SetPin { pin } => client.set_pin(cli.account, pin).await,
250 CliCommands::SubmitRateLimitChallenge { challenge, captcha } => {
251 client
252 .submit_rate_limit_challenge(cli.account, challenge, captcha)
253 .await
254 }
255 CliCommands::Trust {
256 recipient,
257 trust_all_known_keys,
258 verified_safety_number,
259 } => {
260 client
261 .trust(
262 cli.account,
263 recipient,
264 trust_all_known_keys,
265 verified_safety_number,
266 )
267 .await
268 }
269 CliCommands::Unblock {
270 recipient,
271 group_id,
272 } => client.unblock(cli.account, recipient, group_id).await,
273 CliCommands::Unregister { delete_account } => {
274 client.unregister(cli.account, delete_account).await
275 }
276 CliCommands::UpdateAccount {
277 device_name,
278 unrestricted_unidentified_sender,
279 } => {
280 client
281 .update_account(cli.account, device_name, unrestricted_unidentified_sender)
282 .await
283 }
284 CliCommands::UpdateConfiguration {
285 read_receipts,
286 unidentified_delivery_indicators,
287 typing_indicators,
288 link_previews,
289 } => {
290 client
291 .update_configuration(
292 cli.account,
293 read_receipts,
294 unidentified_delivery_indicators,
295 typing_indicators,
296 link_previews,
297 )
298 .await
299 }
300 CliCommands::UpdateContact {
301 recipient,
302 expiration,
303 name,
304 } => {
305 client
306 .update_contact(cli.account, recipient, name, expiration)
307 .await
308 }
309 CliCommands::UpdateGroup {
310 group_id,
311 name,
312 description,
313 avatar,
314 member,
315 remove_member,
316 admin,
317 remove_admin,
318 ban,
319 unban,
320 reset_link,
321 link,
322 set_permission_add_member,
323 set_permission_edit_details,
324 set_permission_send_messages,
325 expiration,
326 } => {
327 client
328 .update_group(
329 cli.account,
330 group_id,
331 name,
332 description,
333 avatar,
334 member,
335 remove_member,
336 admin,
337 remove_admin,
338 ban,
339 unban,
340 reset_link,
341 link.map(|link| match link {
342 LinkState::Enabled => "enabled".to_owned(),
343 LinkState::EnabledWithApproval => "enabledWithApproval".to_owned(),
344 LinkState::Disabled => "disabled".to_owned(),
345 }),
346 set_permission_add_member.map(|p| match p {
347 GroupPermission::EveryMember => "everyMember".to_owned(),
348 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
349 }),
350 set_permission_edit_details.map(|p| match p {
351 GroupPermission::EveryMember => "everyMember".to_owned(),
352 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
353 }),
354 set_permission_send_messages.map(|p| match p {
355 GroupPermission::EveryMember => "everyMember".to_owned(),
356 GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(),
357 }),
358 expiration,
359 )
360 .await
361 }
362 CliCommands::UpdateProfile {
363 given_name,
364 family_name,
365 about,
366 about_emoji,
367 mobile_coin_address,
368 avatar,
369 remove_avatar,
370 } => {
371 client
372 .update_profile(
373 cli.account,
374 given_name,
375 family_name,
376 about,
377 about_emoji,
378 mobile_coin_address,
379 avatar,
380 remove_avatar,
381 )
382 .await
383 }
384 CliCommands::UploadStickerPack { path } => {
385 client.upload_sticker_pack(cli.account, path).await
386 }
387 CliCommands::Verify {
388 verification_code,
389 pin,
390 } => client.verify(cli.account, verification_code, pin).await,
391 CliCommands::Version => client.version().await,
392 CliCommands::AddStickerPack { uri } => client.add_sticker_pack(cli.account, uri).await,
393 CliCommands::FinishChangeNumber {
394 number,
395 verification_code,
396 pin,
397 } => {
398 client
399 .finish_change_number(cli.account, number, verification_code, pin)
400 .await
401 }
402 CliCommands::GetAttachment {
403 id,
404 recipient,
405 group_id,
406 } => {
407 client
408 .get_attachment(cli.account, id, recipient, group_id)
409 .await
410 }
411 CliCommands::StartChangeNumber {
412 number,
413 voice,
414 captcha,
415 } => {
416 client
417 .start_change_number(cli.account, number, voice, captcha)
418 .await
419 }
420 }
421 }
422
423 async fn connect(cli: Cli) -> Result<Value, RpcError> {
424 if let Some(http) = &cli.json_rpc_http {
425 let uri = if let Some(uri) = http {
426 uri
427 } else {
428 DEFAULT_HTTP
429 };
430 let client = jsonrpc::connect_http(uri)
431 .await
432 .map_err(|e| RpcError::Custom(format!("Failed to connect to socket: {e}")))?;
433
434 handle_command(cli, client).await
435 } else if let Some(tcp) = cli.json_rpc_tcp {
436 let socket_addr = tcp.unwrap_or_else(|| DEFAULT_TCP.parse().unwrap());
437 let client = jsonrpc::connect_tcp(socket_addr)
438 .await
439 .map_err(|e| RpcError::Custom(format!("Failed to connect to socket: {e}")))?;
440
441 handle_command(cli, client).await
442 } else {
443 let socket_path = cli
444 .json_rpc_socket
445 .clone()
446 .unwrap_or(None)
447 .or_else(|| {
448 std::env::var_os("XDG_RUNTIME_DIR").map(|runtime_dir| {
449 PathBuf::from(runtime_dir)
450 .join(DEFAULT_SOCKET_SUFFIX)
451 .into()
452 })
453 })
454 .unwrap_or_else(|| ("/run".to_owned() + DEFAULT_SOCKET_SUFFIX).into());
455 let client = jsonrpc::connect_unix(socket_path)
456 .await
457 .map_err(|e| RpcError::Custom(format!("Failed to connect to socket: {e}")))?;
458
459 handle_command(cli, client).await
460 }
461 }
462
463 async fn stream_next(
464 timeout: f64,
465 stream: &mut Subscription<Value>,
466 ) -> Option<Result<Value, RpcError>> {
467 if timeout < 0.0 {
468 stream.next().await
469 } else {
470 select! {
471 v = stream.next() => v,
472 _= sleep(Duration::from_millis((timeout * 1000.0) as u64)) => None,
473 }
474 }
475 }