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
{
26 public String
getName() {
27 return "remoteDelete";
31 public 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(
43 final Namespace ns
, final Signal signal
, final OutputWriter outputWriter
44 ) throws CommandException
{
45 final List
<String
> recipients
= ns
.getList("recipient");
46 final var groupIdString
= ns
.getString("group-id");
48 final var noRecipients
= recipients
== null || recipients
.isEmpty();
49 if (noRecipients
&& groupIdString
== null) {
50 throw new UserErrorException("No recipients given");
52 if (!noRecipients
&& groupIdString
!= null) {
53 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
56 final long targetTimestamp
= ns
.getLong("target-timestamp");
58 byte[] groupId
= null;
59 if (groupIdString
!= null) {
61 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
62 } catch (GroupIdFormatException e
) {
63 throw new UserErrorException("Invalid group id: " + e
.getMessage());
69 if (groupId
!= null) {
70 timestamp
= signal
.sendGroupRemoteDeleteMessage(targetTimestamp
, groupId
);
72 timestamp
= signal
.sendRemoteDeleteMessage(targetTimestamp
, recipients
);
74 outputResult(outputWriter
, timestamp
);
75 } catch (UnknownObject e
) {
76 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
77 } catch (Signal
.Error
.InvalidNumber e
) {
78 throw new UserErrorException("Invalid number: " + e
.getMessage());
79 } catch (Signal
.Error
.GroupNotFound e
) {
80 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
81 } catch (DBusExecutionException e
) {
82 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
87 public void handleCommand(
88 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
89 ) throws CommandException
{
90 handleCommand(ns
, new DbusSignalImpl(m
, null), outputWriter
);
93 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
94 if (outputWriter
instanceof PlainTextWriter
) {
95 final var writer
= (PlainTextWriter
) outputWriter
;
96 writer
.println("{}", timestamp
);
98 final var writer
= (JsonWriter
) outputWriter
;
99 writer
.write(Map
.of("timestamp", timestamp
));