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
;
7 import org
.asamk
.signal
.PlainTextWriterImpl
;
8 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
9 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
10 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
11 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
12 import org
.asamk
.signal
.manager
.Manager
;
13 import org
.asamk
.signal
.manager
.groups
.GroupId
;
14 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
15 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
16 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
17 import org
.asamk
.signal
.util
.ErrorUtils
;
18 import org
.asamk
.signal
.util
.Util
;
19 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
20 import org
.slf4j
.Logger
;
21 import org
.slf4j
.LoggerFactory
;
22 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
25 import java
.io
.IOException
;
26 import java
.util
.ArrayList
;
27 import java
.util
.Base64
;
28 import java
.util
.List
;
30 public class UpdateGroupCommand
implements DbusCommand
, LocalCommand
{
32 private final static Logger logger
= LoggerFactory
.getLogger(UpdateGroupCommand
.class);
35 public void attachToSubparser(final Subparser subparser
) {
36 subparser
.addArgument("-g", "--group").help("Specify the recipient group ID.");
37 subparser
.addArgument("-n", "--name").help("Specify the new group name.");
38 subparser
.addArgument("-d", "--description").help("Specify the new group description.");
39 subparser
.addArgument("-a", "--avatar").help("Specify a new group avatar image file");
40 subparser
.addArgument("-m", "--member").nargs("*").help("Specify one or more members to add to the group");
41 subparser
.addArgument("-r", "--remove-member")
43 .help("Specify one or more members to remove from the group");
47 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
48 final var writer
= new PlainTextWriterImpl(System
.out
);
49 GroupId groupId
= null;
50 final var groupIdString
= ns
.getString("group");
51 if (groupIdString
!= null) {
53 groupId
= Util
.decodeGroupId(groupIdString
);
54 } catch (GroupIdFormatException e
) {
55 throw new UserErrorException("Invalid group id:" + e
.getMessage());
59 var groupName
= ns
.getString("name");
61 var groupDescription
= ns
.getString("description");
63 List
<String
> groupMembers
= ns
.getList("member");
65 List
<String
> groupRemoveMembers
= ns
.getList("remove-member");
67 var groupAvatar
= ns
.getString("avatar");
70 if (groupId
== null) {
71 var results
= m
.createGroup(groupName
,
73 groupAvatar
== null ?
null : new File(groupAvatar
));
74 ErrorUtils
.handleTimestampAndSendMessageResults(writer
, 0, results
.second());
75 final var newGroupId
= results
.first();
76 writer
.println("Created new group: \"{}\"", newGroupId
.toBase64());
78 var results
= m
.updateGroup(groupId
,
83 groupAvatar
== null ?
null : new File(groupAvatar
));
84 ErrorUtils
.handleTimestampAndSendMessageResults(writer
, results
.first(), results
.second());
86 } catch (AttachmentInvalidException e
) {
87 throw new UserErrorException("Failed to add avatar attachment for group\": " + e
.getMessage());
88 } catch (GroupNotFoundException e
) {
89 logger
.warn("Unknown group id: {}", groupIdString
);
90 } catch (NotAGroupMemberException e
) {
91 logger
.warn("You're not a group member");
92 } catch (InvalidNumberException e
) {
93 throw new UserErrorException("Failed to parse member number: " + e
.getMessage());
94 } catch (IOException e
) {
95 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
100 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
101 final var writer
= new PlainTextWriterImpl(System
.out
);
102 byte[] groupId
= null;
103 if (ns
.getString("group") != null) {
105 groupId
= Util
.decodeGroupId(ns
.getString("group")).serialize();
106 } catch (GroupIdFormatException e
) {
107 throw new UserErrorException("Invalid group id:" + e
.getMessage());
110 if (groupId
== null) {
111 groupId
= new byte[0];
114 var groupName
= ns
.getString("name");
115 if (groupName
== null) {
119 List
<String
> groupMembers
= ns
.getList("member");
120 if (groupMembers
== null) {
121 groupMembers
= new ArrayList
<>();
124 var groupAvatar
= ns
.getString("avatar");
125 if (groupAvatar
== null) {
130 var newGroupId
= signal
.updateGroup(groupId
, groupName
, groupMembers
, groupAvatar
);
131 if (groupId
.length
!= newGroupId
.length
) {
132 writer
.println("Created new group: \"{}\"", Base64
.getEncoder().encodeToString(newGroupId
));
134 } catch (Signal
.Error
.AttachmentInvalid e
) {
135 throw new UserErrorException("Failed to add avatar attachment for group\": " + e
.getMessage());
136 } catch (DBusExecutionException e
) {
137 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());