]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/TrustCommand.java
Add UnregisteredRecipientException
[signal-cli] / src / main / java / org / asamk / signal / commands / TrustCommand.java
1 package org.asamk.signal.commands;
2
3 import net.sourceforge.argparse4j.impl.Arguments;
4 import net.sourceforge.argparse4j.inf.Namespace;
5 import net.sourceforge.argparse4j.inf.Subparser;
6
7 import org.asamk.signal.commands.exceptions.CommandException;
8 import org.asamk.signal.commands.exceptions.UserErrorException;
9 import org.asamk.signal.manager.Manager;
10 import org.asamk.signal.manager.api.UnregisteredRecipientException;
11 import org.asamk.signal.output.OutputWriter;
12 import org.asamk.signal.util.CommandUtil;
13 import org.asamk.signal.util.Hex;
14
15 import java.util.Base64;
16 import java.util.Locale;
17
18 public class TrustCommand implements JsonRpcLocalCommand {
19
20 @Override
21 public String getName() {
22 return "trust";
23 }
24
25 @Override
26 public void attachToSubparser(final Subparser subparser) {
27 subparser.help("Set the trust level of a given number.");
28 subparser.addArgument("recipient").help("Specify the phone number, for which to set the trust.").required(true);
29 var mutTrust = subparser.addMutuallyExclusiveGroup();
30 mutTrust.addArgument("-a", "--trust-all-known-keys")
31 .help("Trust all known keys of this user, only use this for testing.")
32 .action(Arguments.storeTrue());
33 mutTrust.addArgument("-v", "--verified-safety-number", "--verified-fingerprint")
34 .help("Specify the safety number of the key, only use this option if you have verified the safety number.");
35 }
36
37 @Override
38 public void handleCommand(
39 final Namespace ns, final Manager m, final OutputWriter outputWriter
40 ) throws CommandException {
41 var recipentString = ns.getString("recipient");
42 var recipient = CommandUtil.getSingleRecipientIdentifier(recipentString, m.getSelfNumber());
43 if (Boolean.TRUE.equals(ns.getBoolean("trust-all-known-keys"))) {
44 boolean res;
45 try {
46 res = m.trustIdentityAllKeys(recipient);
47 } catch (UnregisteredRecipientException e) {
48 throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
49 }
50 if (!res) {
51 throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
52 }
53 } else {
54 var safetyNumber = ns.getString("verified-safety-number");
55 if (safetyNumber == null) {
56 throw new UserErrorException(
57 "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
58 }
59
60 safetyNumber = safetyNumber.replaceAll(" ", "");
61 if (safetyNumber.length() == 66) {
62 byte[] fingerprintBytes;
63 try {
64 fingerprintBytes = Hex.toByteArray(safetyNumber.toLowerCase(Locale.ROOT));
65 } catch (Exception e) {
66 throw new UserErrorException(
67 "Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
68 }
69 boolean res;
70 try {
71 res = m.trustIdentityVerified(recipient, fingerprintBytes);
72 } catch (UnregisteredRecipientException e) {
73 throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
74 }
75 if (!res) {
76 throw new UserErrorException(
77 "Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
78 }
79 } else if (safetyNumber.length() == 60) {
80 boolean res;
81 try {
82 res = m.trustIdentityVerifiedSafetyNumber(recipient, safetyNumber);
83 } catch (UnregisteredRecipientException e) {
84 throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
85 }
86 if (!res) {
87 throw new UserErrorException(
88 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
89 }
90 } else {
91 final byte[] scannableSafetyNumber;
92 try {
93 scannableSafetyNumber = Base64.getDecoder().decode(safetyNumber);
94 } catch (IllegalArgumentException e) {
95 throw new UserErrorException(
96 "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
97 }
98 boolean res;
99 try {
100 res = m.trustIdentityVerifiedSafetyNumber(recipient, scannableSafetyNumber);
101 } catch (UnregisteredRecipientException e) {
102 throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
103 }
104 if (!res) {
105 throw new UserErrorException(
106 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
107 }
108 }
109 }
110 }
111 }