1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.MutuallyExclusiveGroup
;
5 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
6 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
8 import org
.asamk
.signal
.manager
.Manager
;
9 import org
.asamk
.signal
.util
.ErrorUtils
;
10 import org
.asamk
.signal
.util
.Hex
;
11 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
13 import java
.util
.Locale
;
15 public class TrustCommand
implements LocalCommand
{
18 public void attachToSubparser(final Subparser subparser
) {
19 subparser
.addArgument("number")
20 .help("Specify the phone number, for which to set the trust.")
22 MutuallyExclusiveGroup mutTrust
= subparser
.addMutuallyExclusiveGroup();
23 mutTrust
.addArgument("-a", "--trust-all-known-keys")
24 .help("Trust all known keys of this user, only use this for testing.")
25 .action(Arguments
.storeTrue());
26 mutTrust
.addArgument("-v", "--verified-fingerprint")
27 .help("Specify the fingerprint of the key, only use this option if you have verified the fingerprint.");
31 public int handleCommand(final Namespace ns
, final Manager m
) {
32 if (!m
.isRegistered()) {
33 System
.err
.println("User is not registered.");
36 String number
= ns
.getString("number");
37 if (ns
.getBoolean("trust_all_known_keys")) {
38 boolean res
= m
.trustIdentityAllKeys(number
);
40 System
.err
.println("Failed to set the trust for this number, make sure the number is correct.");
44 String fingerprint
= ns
.getString("verified_fingerprint");
45 if (fingerprint
!= null) {
46 fingerprint
= fingerprint
.replaceAll(" ", "");
47 if (fingerprint
.length() == 66) {
48 byte[] fingerprintBytes
;
50 fingerprintBytes
= Hex
.toByteArray(fingerprint
.toLowerCase(Locale
.ROOT
));
51 } catch (Exception e
) {
52 System
.err
.println("Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
57 res
= m
.trustIdentityVerified(number
, fingerprintBytes
);
58 } catch (InvalidNumberException e
) {
59 ErrorUtils
.handleInvalidNumberException(e
);
63 System
.err
.println("Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
66 } else if (fingerprint
.length() == 60) {
69 res
= m
.trustIdentityVerifiedSafetyNumber(number
, fingerprint
);
70 } catch (InvalidNumberException e
) {
71 ErrorUtils
.handleInvalidNumberException(e
);
75 System
.err
.println("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 System
.err
.println("Fingerprint has invalid format, either specify the old hex fingerprint or the new safety number");
83 System
.err
.println("You need to specify the fingerprint you have verified with -v FINGERPRINT");