1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
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
;
14 import java
.util
.Locale
;
16 public class TrustCommand
implements JsonRpcLocalCommand
{
18 public TrustCommand(final OutputWriter outputWriter
) {
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.");
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")) {
38 res
= m
.trustIdentityAllKeys(number
);
39 } catch (InvalidNumberException e
) {
40 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
43 throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
46 var safetyNumber
= ns
.getString("verified-safety-number");
47 if (safetyNumber
!= null) {
48 safetyNumber
= safetyNumber
.replaceAll(" ", "");
49 if (safetyNumber
.length() == 66) {
50 byte[] fingerprintBytes
;
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.");
59 res
= m
.trustIdentityVerified(number
, fingerprintBytes
);
60 } catch (InvalidNumberException e
) {
61 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
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.");
67 } else if (safetyNumber
.length() == 60) {
70 res
= m
.trustIdentityVerifiedSafetyNumber(number
, safetyNumber
);
71 } catch (InvalidNumberException e
) {
72 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
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.");
79 throw new UserErrorException(
80 "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
83 throw new UserErrorException(
84 "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");