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