]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/TrustCommand.java
Fix null pointer regression
[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,
37 final Manager m,
38 final OutputWriter outputWriter
39 ) throws CommandException {
40 var recipientString = ns.getString("recipient");
41 var recipient = CommandUtil.getSingleRecipientIdentifier(recipientString, m.getSelfNumber());
42 if (Boolean.TRUE.equals(ns.getBoolean("trust-all-known-keys"))) {
43 try {
44 final var res = m.trustIdentityAllKeys(recipient);
45 if (!res) {
46 throw new UserErrorException(
47 "Failed to set the trust for this number, make sure the number is correct.");
48 }
49 } catch (UnregisteredRecipientException e) {
50 throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
51 }
52 } else {
53 var safetyNumber = ns.getString("verified-safety-number");
54 if (safetyNumber == null) {
55 throw new UserErrorException(
56 "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
57 }
58
59 final IdentityVerificationCode verificationCode;
60 try {
61 verificationCode = IdentityVerificationCode.parse(safetyNumber);
62 } catch (Exception e) {
63 throw new UserErrorException(
64 "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
65 }
66
67 try {
68 final var res = m.trustIdentityVerified(recipient, verificationCode);
69 if (!res) {
70 throw new UserErrorException(
71 "Failed to set the trust for this number, make sure the number and the fingerprint/safety number are correct.");
72 }
73 } catch (UnregisteredRecipientException e) {
74 throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
75 }
76 }
77 }
78 }