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");
44 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
45 final var writer
= new PlainTextWriterImpl(System
.out
);
46 GroupId groupId
= null;
47 final var groupIdString
= ns
.getString("group");
48 if (groupIdString
!= null) {
50 groupId
= Util
.decodeGroupId(groupIdString
);
51 } catch (GroupIdFormatException e
) {
52 throw new UserErrorException("Invalid group id:" + e
.getMessage());
56 var groupName
= ns
.getString("name");
58 var groupDescription
= ns
.getString("description");
60 List
<String
> groupMembers
= ns
.getList("member");
62 var groupAvatar
= ns
.getString("avatar");
65 if (groupId
== null) {
66 var results
= m
.createGroup(groupName
,
68 groupAvatar
== null ?
null : new File(groupAvatar
));
69 ErrorUtils
.handleTimestampAndSendMessageResults(writer
, 0, results
.second());
70 final var newGroupId
= results
.first();
71 writer
.println("Created new group: \"{}\"", newGroupId
.toBase64());
73 var results
= m
.updateGroup(groupId
,
77 groupAvatar
== null ?
null : new File(groupAvatar
));
78 ErrorUtils
.handleTimestampAndSendMessageResults(writer
, results
.first(), results
.second());
80 } catch (AttachmentInvalidException e
) {
81 throw new UserErrorException("Failed to add avatar attachment for group\": " + e
.getMessage());
82 } catch (GroupNotFoundException e
) {
83 logger
.warn("Unknown group id: {}", groupIdString
);
84 } catch (NotAGroupMemberException e
) {
85 logger
.warn("You're not a group member");
86 } catch (InvalidNumberException e
) {
87 throw new UserErrorException("Failed to parse member number: " + e
.getMessage());
88 } catch (IOException e
) {
89 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
94 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
95 final var writer
= new PlainTextWriterImpl(System
.out
);
96 byte[] groupId
= null;
97 if (ns
.getString("group") != null) {
99 groupId
= Util
.decodeGroupId(ns
.getString("group")).serialize();
100 } catch (GroupIdFormatException e
) {
101 throw new UserErrorException("Invalid group id:" + e
.getMessage());
104 if (groupId
== null) {
105 groupId
= new byte[0];
108 var groupName
= ns
.getString("name");
109 if (groupName
== null) {
113 List
<String
> groupMembers
= ns
.getList("member");
114 if (groupMembers
== null) {
115 groupMembers
= new ArrayList
<>();
118 var groupAvatar
= ns
.getString("avatar");
119 if (groupAvatar
== null) {
124 var newGroupId
= signal
.updateGroup(groupId
, groupName
, groupMembers
, groupAvatar
);
125 if (groupId
.length
!= newGroupId
.length
) {
126 writer
.println("Created new group: \"{}\"", Base64
.getEncoder().encodeToString(newGroupId
));
128 } catch (Signal
.Error
.AttachmentInvalid e
) {
129 throw new UserErrorException("Failed to add avatar attachment for group\": " + e
.getMessage());
130 } catch (DBusExecutionException e
) {
131 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());