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
.JsonWriter
;
8 import org
.asamk
.signal
.OutputWriter
;
9 import org
.asamk
.signal
.PlainTextWriter
;
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
.dbus
.DbusSignalImpl
;
14 import org
.asamk
.signal
.manager
.Manager
;
15 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
16 import org
.asamk
.signal
.util
.Util
;
17 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
18 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
20 import java
.util
.List
;
23 public class RemoteDeleteCommand
implements DbusCommand
, JsonRpcLocalCommand
{
25 private final OutputWriter outputWriter
;
27 public RemoteDeleteCommand(final OutputWriter outputWriter
) {
28 this.outputWriter
= outputWriter
;
31 public static void attachToSubparser(final Subparser subparser
) {
32 subparser
.help("Remotely delete a previously sent message.");
33 subparser
.addArgument("-t", "--target-timestamp")
36 .help("Specify the timestamp of the message to delete.");
37 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.");
38 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
42 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
43 final List
<String
> recipients
= ns
.getList("recipient");
44 final var groupIdString
= ns
.getString("group-id");
46 final var noRecipients
= recipients
== null || recipients
.isEmpty();
47 if (noRecipients
&& groupIdString
== null) {
48 throw new UserErrorException("No recipients given");
50 if (!noRecipients
&& groupIdString
!= null) {
51 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
54 final long targetTimestamp
= ns
.getLong("target-timestamp");
56 byte[] groupId
= null;
57 if (groupIdString
!= null) {
59 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
60 } catch (GroupIdFormatException e
) {
61 throw new UserErrorException("Invalid group id: " + e
.getMessage());
67 if (groupId
!= null) {
68 timestamp
= signal
.sendGroupRemoteDeleteMessage(targetTimestamp
, groupId
);
70 timestamp
= signal
.sendRemoteDeleteMessage(targetTimestamp
, recipients
);
72 outputResult(timestamp
);
73 } catch (UnknownObject e
) {
74 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
75 } catch (Signal
.Error
.InvalidNumber e
) {
76 throw new UserErrorException("Invalid number: " + e
.getMessage());
77 } catch (Signal
.Error
.GroupNotFound e
) {
78 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
79 } catch (DBusExecutionException e
) {
80 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
85 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
86 handleCommand(ns
, new DbusSignalImpl(m
, null));
89 private void outputResult(final long timestamp
) {
90 if (outputWriter
instanceof PlainTextWriter
) {
91 final var writer
= (PlainTextWriter
) outputWriter
;
92 writer
.println("{}", timestamp
);
94 final var writer
= (JsonWriter
) outputWriter
;
95 writer
.write(Map
.of("timestamp", timestamp
));