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