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