1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
4 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
6 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
7 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
8 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
9 import org
.asamk
.signal
.manager
.Manager
;
10 import org
.asamk
.signal
.manager
.api
.NotMasterDeviceException
;
11 import org
.asamk
.signal
.manager
.api
.UnregisteredRecipientException
;
12 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
13 import org
.asamk
.signal
.output
.OutputWriter
;
14 import org
.asamk
.signal
.util
.CommandUtil
;
15 import org
.slf4j
.Logger
;
16 import org
.slf4j
.LoggerFactory
;
18 import java
.io
.IOException
;
20 public class BlockCommand
implements JsonRpcLocalCommand
{
22 private final static Logger logger
= LoggerFactory
.getLogger(BlockCommand
.class);
25 public String
getName() {
30 public void attachToSubparser(final Subparser subparser
) {
31 subparser
.help("Block the given contacts or groups (no messages will be received)");
32 subparser
.addArgument("recipient").help("Contact number").nargs("*");
33 subparser
.addArgument("-g", "--group-id", "--group").help("Group ID").nargs("*");
37 public void handleCommand(
38 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
39 ) throws CommandException
{
40 final var contacts
= ns
.<String
>getList("recipient");
41 for (var contact
: CommandUtil
.getSingleRecipientIdentifiers(contacts
, m
.getSelfNumber())) {
43 m
.setContactBlocked(contact
, true);
44 } catch (NotMasterDeviceException e
) {
45 throw new UserErrorException("This command doesn't work on linked devices.");
46 } catch (IOException e
) {
47 throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e
.getMessage(), e
);
48 } catch (UnregisteredRecipientException e
) {
49 throw new UserErrorException("The user " + e
.getSender().getIdentifier() + " is not registered.");
53 final var groupIdStrings
= ns
.<String
>getList("group-id");
54 if (groupIdStrings
!= null) {
55 for (var groupId
: CommandUtil
.getGroupIds(groupIdStrings
)) {
57 m
.setGroupBlocked(groupId
, true);
58 } catch (NotMasterDeviceException e
) {
59 throw new UserErrorException("This command doesn't work on linked devices.");
60 } catch (GroupNotFoundException e
) {
61 logger
.warn("Group not found {}: {}", groupId
.toBase64(), e
.getMessage());
62 } catch (IOException e
) {
63 throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e
.getMessage(), e
);