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