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