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
{
19 public String
getName() {
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.");
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")) {
43 res
= m
.trustIdentityAllKeys(number
);
44 } catch (InvalidNumberException e
) {
45 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
48 throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
51 var safetyNumber
= ns
.getString("verified-safety-number");
52 if (safetyNumber
!= null) {
53 safetyNumber
= safetyNumber
.replaceAll(" ", "");
54 if (safetyNumber
.length() == 66) {
55 byte[] fingerprintBytes
;
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.");
64 res
= m
.trustIdentityVerified(number
, fingerprintBytes
);
65 } catch (InvalidNumberException e
) {
66 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
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.");
72 } else if (safetyNumber
.length() == 60) {
75 res
= m
.trustIdentityVerifiedSafetyNumber(number
, safetyNumber
);
76 } catch (InvalidNumberException e
) {
77 throw new UserErrorException("Failed to parse recipient: " + e
.getMessage());
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.");
84 throw new UserErrorException(
85 "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
88 throw new UserErrorException(
89 "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");