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
.OutputWriter
;
8 import org
.asamk
.signal
.PlainTextWriterImpl
;
9 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
10 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
11 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
12 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
13 import org
.asamk
.signal
.util
.Util
;
14 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
15 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
17 import java
.util
.List
;
19 public class RemoteDeleteCommand
implements DbusCommand
{
21 private final OutputWriter outputWriter
;
23 public RemoteDeleteCommand(final OutputWriter outputWriter
) {
24 this.outputWriter
= outputWriter
;
27 public static void attachToSubparser(final Subparser subparser
) {
28 subparser
.help("Remotely delete a previously sent message.");
29 subparser
.addArgument("-t", "--target-timestamp")
32 .help("Specify the timestamp of the message to delete.");
33 subparser
.addArgument("-g", "--group").help("Specify the recipient group ID.");
34 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
38 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
39 final List
<String
> recipients
= ns
.getList("recipient");
40 final var groupIdString
= ns
.getString("group");
42 final var noRecipients
= recipients
== null || recipients
.isEmpty();
43 if (noRecipients
&& groupIdString
== null) {
44 throw new UserErrorException("No recipients given");
46 if (!noRecipients
&& groupIdString
!= null) {
47 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
50 final long targetTimestamp
= ns
.getLong("target-timestamp");
52 final var writer
= (PlainTextWriterImpl
) outputWriter
;
54 byte[] groupId
= null;
55 if (groupIdString
!= null) {
57 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
58 } catch (GroupIdFormatException e
) {
59 throw new UserErrorException("Invalid group id: " + e
.getMessage());
65 if (groupId
!= null) {
66 timestamp
= signal
.sendGroupRemoteDeleteMessage(targetTimestamp
, groupId
);
68 timestamp
= signal
.sendRemoteDeleteMessage(targetTimestamp
, recipients
);
70 writer
.println("{}", timestamp
);
71 } catch (UnknownObject e
) {
72 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
73 } catch (Signal
.Error
.InvalidNumber e
) {
74 throw new UserErrorException("Invalid number: " + e
.getMessage());
75 } catch (Signal
.Error
.GroupNotFound e
) {
76 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
77 } catch (DBusExecutionException e
) {
78 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());