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
.commands
.exceptions
.CommandException
;
8 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
9 import org
.asamk
.signal
.manager
.Manager
;
10 import org
.asamk
.signal
.manager
.api
.UnregisteredRecipientException
;
11 import org
.asamk
.signal
.output
.OutputWriter
;
12 import org
.asamk
.signal
.util
.CommandUtil
;
13 import org
.asamk
.signal
.util
.Hex
;
15 import java
.util
.Base64
;
16 import java
.util
.Locale
;
18 public class TrustCommand
implements JsonRpcLocalCommand
{
21 public String
getName() {
26 public void attachToSubparser(final Subparser subparser
) {
27 subparser
.help("Set the trust level of a given number.");
28 subparser
.addArgument("recipient").help("Specify the phone number, for which to set the trust.").required(true);
29 var mutTrust
= subparser
.addMutuallyExclusiveGroup();
30 mutTrust
.addArgument("-a", "--trust-all-known-keys")
31 .help("Trust all known keys of this user, only use this for testing.")
32 .action(Arguments
.storeTrue());
33 mutTrust
.addArgument("-v", "--verified-safety-number", "--verified-fingerprint")
34 .help("Specify the safety number of the key, only use this option if you have verified the safety number.");
38 public void handleCommand(
39 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
40 ) throws CommandException
{
41 var recipentString
= ns
.getString("recipient");
42 var recipient
= CommandUtil
.getSingleRecipientIdentifier(recipentString
, m
.getSelfNumber());
43 if (Boolean
.TRUE
.equals(ns
.getBoolean("trust-all-known-keys"))) {
46 res
= m
.trustIdentityAllKeys(recipient
);
47 } catch (UnregisteredRecipientException e
) {
48 throw new UserErrorException("The user " + e
.getSender().getIdentifier() + " is not registered.");
51 throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
54 var safetyNumber
= ns
.getString("verified-safety-number");
55 if (safetyNumber
== null) {
56 throw new UserErrorException(
57 "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
60 safetyNumber
= safetyNumber
.replaceAll(" ", "");
61 if (safetyNumber
.length() == 66) {
62 byte[] fingerprintBytes
;
64 fingerprintBytes
= Hex
.toByteArray(safetyNumber
.toLowerCase(Locale
.ROOT
));
65 } catch (Exception e
) {
66 throw new UserErrorException(
67 "Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
71 res
= m
.trustIdentityVerified(recipient
, fingerprintBytes
);
72 } catch (UnregisteredRecipientException e
) {
73 throw new UserErrorException("The user " + e
.getSender().getIdentifier() + " is not registered.");
76 throw new UserErrorException(
77 "Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
79 } else if (safetyNumber
.length() == 60) {
82 res
= m
.trustIdentityVerifiedSafetyNumber(recipient
, safetyNumber
);
83 } catch (UnregisteredRecipientException e
) {
84 throw new UserErrorException("The user " + e
.getSender().getIdentifier() + " is not registered.");
87 throw new UserErrorException(
88 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
91 final byte[] scannableSafetyNumber
;
93 scannableSafetyNumber
= Base64
.getDecoder().decode(safetyNumber
);
94 } catch (IllegalArgumentException e
) {
95 throw new UserErrorException(
96 "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
100 res
= m
.trustIdentityVerifiedSafetyNumber(recipient
, scannableSafetyNumber
);
101 } catch (UnregisteredRecipientException e
) {
102 throw new UserErrorException("The user " + e
.getSender().getIdentifier() + " is not registered.");
105 throw new UserErrorException(
106 "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");