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
;
8 import org
.asamk
.signal
.GroupLinkState
;
9 import org
.asamk
.signal
.PlainTextWriterImpl
;
10 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
11 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
12 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
13 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
14 import org
.asamk
.signal
.manager
.Manager
;
15 import org
.asamk
.signal
.manager
.groups
.GroupId
;
16 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
17 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
18 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
19 import org
.asamk
.signal
.util
.ErrorUtils
;
20 import org
.asamk
.signal
.util
.Util
;
21 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
22 import org
.slf4j
.Logger
;
23 import org
.slf4j
.LoggerFactory
;
24 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
27 import java
.io
.IOException
;
28 import java
.util
.ArrayList
;
29 import java
.util
.Base64
;
30 import java
.util
.List
;
32 public class UpdateGroupCommand
implements DbusCommand
, LocalCommand
{
34 private final static Logger logger
= LoggerFactory
.getLogger(UpdateGroupCommand
.class);
37 public void attachToSubparser(final Subparser subparser
) {
38 subparser
.addArgument("-g", "--group").help("Specify the recipient group ID.");
39 subparser
.addArgument("-n", "--name").help("Specify the new group name.");
40 subparser
.addArgument("-d", "--description").help("Specify the new group description.");
41 subparser
.addArgument("-a", "--avatar").help("Specify a new group avatar image file");
42 subparser
.addArgument("-m", "--member").nargs("*").help("Specify one or more members to add to the group");
43 subparser
.addArgument("-r", "--remove-member")
45 .help("Specify one or more members to remove from the group");
46 subparser
.addArgument("--admin").nargs("*").help("Specify one or more members to make a group admin");
47 subparser
.addArgument("--remove-admin")
49 .help("Specify one or more members to remove group admin privileges");
51 subparser
.addArgument("--reset-link")
52 .action(Arguments
.storeTrue())
53 .help("Reset group link and create new link password");
54 subparser
.addArgument("--link")
55 .help("Set group link state, with or without admin approval")
56 .type(Arguments
.enumStringType(GroupLinkState
.class));
60 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
61 final var writer
= new PlainTextWriterImpl(System
.out
);
62 GroupId groupId
= null;
63 final var groupIdString
= ns
.getString("group");
64 if (groupIdString
!= null) {
66 groupId
= Util
.decodeGroupId(groupIdString
);
67 } catch (GroupIdFormatException e
) {
68 throw new UserErrorException("Invalid group id:" + e
.getMessage());
72 var groupName
= ns
.getString("name");
74 var groupDescription
= ns
.getString("description");
76 var groupMembers
= ns
.<String
>getList("member");
78 var groupRemoveMembers
= ns
.<String
>getList("remove-member");
80 var groupAdmins
= ns
.<String
>getList("admin");
82 var groupRemoveAdmins
= ns
.<String
>getList("remove-admin");
84 var groupAvatar
= ns
.getString("avatar");
86 var groupResetLink
= ns
.getBoolean("reset-link");
88 var groupLinkState
= ns
.<GroupLinkState
>get("link");
91 if (groupId
== null) {
92 var results
= m
.createGroup(groupName
,
94 groupAvatar
== null ?
null : new File(groupAvatar
));
95 ErrorUtils
.handleTimestampAndSendMessageResults(writer
, 0, results
.second());
96 final var newGroupId
= results
.first();
97 writer
.println("Created new group: \"{}\"", newGroupId
.toBase64());
99 var results
= m
.updateGroup(groupId
,
107 groupLinkState
!= null ? groupLinkState
.toLinkState() : null,
108 groupAvatar
== null ?
null : new File(groupAvatar
));
109 ErrorUtils
.handleTimestampAndSendMessageResults(writer
, results
.first(), results
.second());
111 } catch (AttachmentInvalidException e
) {
112 throw new UserErrorException("Failed to add avatar attachment for group\": " + e
.getMessage());
113 } catch (GroupNotFoundException e
) {
114 logger
.warn("Unknown group id: {}", groupIdString
);
115 } catch (NotAGroupMemberException e
) {
116 logger
.warn("You're not a group member");
117 } catch (InvalidNumberException e
) {
118 throw new UserErrorException("Failed to parse member number: " + e
.getMessage());
119 } catch (IOException e
) {
120 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
125 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
126 final var writer
= new PlainTextWriterImpl(System
.out
);
127 byte[] groupId
= null;
128 if (ns
.getString("group") != null) {
130 groupId
= Util
.decodeGroupId(ns
.getString("group")).serialize();
131 } catch (GroupIdFormatException e
) {
132 throw new UserErrorException("Invalid group id:" + e
.getMessage());
135 if (groupId
== null) {
136 groupId
= new byte[0];
139 var groupName
= ns
.getString("name");
140 if (groupName
== null) {
144 List
<String
> groupMembers
= ns
.getList("member");
145 if (groupMembers
== null) {
146 groupMembers
= new ArrayList
<>();
149 var groupAvatar
= ns
.getString("avatar");
150 if (groupAvatar
== null) {
155 var newGroupId
= signal
.updateGroup(groupId
, groupName
, groupMembers
, groupAvatar
);
156 if (groupId
.length
!= newGroupId
.length
) {
157 writer
.println("Created new group: \"{}\"", Base64
.getEncoder().encodeToString(newGroupId
));
159 } catch (Signal
.Error
.AttachmentInvalid e
) {
160 throw new UserErrorException("Failed to add avatar attachment for group\": " + e
.getMessage());
161 } catch (DBusExecutionException e
) {
162 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());