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