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));
58 subparser
.addArgument("-e", "--expiration").type(int.class).help("Set expiration time of messages (seconds)");
62 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
63 final var writer
= new PlainTextWriterImpl(System
.out
);
64 GroupId groupId
= null;
65 final var groupIdString
= ns
.getString("group");
66 if (groupIdString
!= null) {
68 groupId
= Util
.decodeGroupId(groupIdString
);
69 } catch (GroupIdFormatException e
) {
70 throw new UserErrorException("Invalid group id:" + e
.getMessage());
74 var groupName
= ns
.getString("name");
76 var groupDescription
= ns
.getString("description");
78 var groupMembers
= ns
.<String
>getList("member");
80 var groupRemoveMembers
= ns
.<String
>getList("remove-member");
82 var groupAdmins
= ns
.<String
>getList("admin");
84 var groupRemoveAdmins
= ns
.<String
>getList("remove-admin");
86 var groupAvatar
= ns
.getString("avatar");
88 var groupResetLink
= ns
.getBoolean("reset-link");
90 var groupLinkState
= ns
.<GroupLinkState
>get("link");
92 var groupExpiration
= ns
.getInt("expiration");
95 if (groupId
== null) {
96 var results
= m
.createGroup(groupName
,
98 groupAvatar
== null ?
null : new File(groupAvatar
));
99 ErrorUtils
.handleTimestampAndSendMessageResults(writer
, 0, results
.second());
100 final var newGroupId
= results
.first();
101 writer
.println("Created new group: \"{}\"", newGroupId
.toBase64());
103 var results
= m
.updateGroup(groupId
,
111 groupLinkState
!= null ? groupLinkState
.toLinkState() : null,
112 groupAvatar
== null ?
null : new File(groupAvatar
),
114 ErrorUtils
.handleTimestampAndSendMessageResults(writer
, results
.first(), results
.second());
116 } catch (AttachmentInvalidException e
) {
117 throw new UserErrorException("Failed to add avatar attachment for group\": " + e
.getMessage());
118 } catch (GroupNotFoundException e
) {
119 logger
.warn("Unknown group id: {}", groupIdString
);
120 } catch (NotAGroupMemberException e
) {
121 logger
.warn("You're not a group member");
122 } catch (InvalidNumberException e
) {
123 throw new UserErrorException("Failed to parse member number: " + e
.getMessage());
124 } catch (IOException e
) {
125 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
130 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
131 final var writer
= new PlainTextWriterImpl(System
.out
);
132 byte[] groupId
= null;
133 if (ns
.getString("group") != null) {
135 groupId
= Util
.decodeGroupId(ns
.getString("group")).serialize();
136 } catch (GroupIdFormatException e
) {
137 throw new UserErrorException("Invalid group id:" + e
.getMessage());
140 if (groupId
== null) {
141 groupId
= new byte[0];
144 var groupName
= ns
.getString("name");
145 if (groupName
== null) {
149 List
<String
> groupMembers
= ns
.getList("member");
150 if (groupMembers
== null) {
151 groupMembers
= new ArrayList
<>();
154 var groupAvatar
= ns
.getString("avatar");
155 if (groupAvatar
== null) {
160 var newGroupId
= signal
.updateGroup(groupId
, groupName
, groupMembers
, groupAvatar
);
161 if (groupId
.length
!= newGroupId
.length
) {
162 writer
.println("Created new group: \"{}\"", Base64
.getEncoder().encodeToString(newGroupId
));
164 } catch (Signal
.Error
.AttachmentInvalid e
) {
165 throw new UserErrorException("Failed to add avatar attachment for group\": " + e
.getMessage());
166 } catch (DBusExecutionException e
) {
167 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());