]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/TrustCommand.java
62f5190c7eeddd0f0e62c3adfebae75528df6c31
[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.OutputWriter;
8 import org.asamk.signal.commands.exceptions.CommandException;
9 import org.asamk.signal.commands.exceptions.UserErrorException;
10 import org.asamk.signal.manager.Manager;
11 import org.asamk.signal.util.Hex;
12 import org.whispersystems.signalservice.api.util.InvalidNumberException;
13
14 import java.util.Locale;
15
16 public class TrustCommand implements JsonRpcLocalCommand {
17
18 public TrustCommand(final OutputWriter outputWriter) {
19 }
20
21 public static void attachToSubparser(final Subparser subparser) {
22 subparser.help("Set the trust level of a given number.");
23 subparser.addArgument("number").help("Specify the phone number, for which to set the trust.").required(true);
24 var mutTrust = subparser.addMutuallyExclusiveGroup();
25 mutTrust.addArgument("-a", "--trust-all-known-keys")
26 .help("Trust all known keys of this user, only use this for testing.")
27 .action(Arguments.storeTrue());
28 mutTrust.addArgument("-v", "--verified-safety-number", "--verified-fingerprint")
29 .help("Specify the safety number of the key, only use this option if you have verified the safety number.");
30 }
31
32 @Override
33 public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
34 var number = ns.getString("number");
35 if (ns.getBoolean("trust-all-known-keys")) {
36 boolean res;
37 try {
38 res = m.trustIdentityAllKeys(number);
39 } catch (InvalidNumberException e) {
40 throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
41 }
42 if (!res) {
43 throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
44 }
45 } else {
46 var safetyNumber = ns.getString("verified-safety-number");
47 if (safetyNumber != null) {
48 safetyNumber = safetyNumber.replaceAll(" ", "");
49 if (safetyNumber.length() == 66) {
50 byte[] fingerprintBytes;
51 try {
52 fingerprintBytes = Hex.toByteArray(safetyNumber.toLowerCase(Locale.ROOT));
53 } catch (Exception e) {
54 throw new UserErrorException(
55 "Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
56 }
57 boolean res;
58 try {
59 res = m.trustIdentityVerified(number, fingerprintBytes);
60 } catch (InvalidNumberException e) {
61 throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
62 }
63 if (!res) {
64 throw new UserErrorException(
65 "Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
66 }
67 } else if (safetyNumber.length() == 60) {
68 boolean res;
69 try {
70 res = m.trustIdentityVerifiedSafetyNumber(number, safetyNumber);
71 } catch (InvalidNumberException e) {
72 throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
73 }
74 if (!res) {
75 throw new UserErrorException(
76 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
77 }
78 } else {
79 throw new UserErrorException(
80 "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
81 }
82 } else {
83 throw new UserErrorException(
84 "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
85 }
86 }
87 }
88 }