1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.Signal
;
8 import org
.asamk
.signal
.JsonWriter
;
9 import org
.asamk
.signal
.OutputWriter
;
10 import org
.asamk
.signal
.PlainTextWriter
;
11 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
12 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
13 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
14 import org
.asamk
.signal
.manager
.Manager
;
15 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
16 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
17 import org
.asamk
.signal
.util
.CommandUtil
;
18 import org
.asamk
.signal
.util
.ErrorUtils
;
19 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
20 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
22 import java
.io
.IOException
;
25 public class RemoteDeleteCommand
implements DbusCommand
, JsonRpcLocalCommand
{
28 public String
getName() {
29 return "remoteDelete";
33 public void attachToSubparser(final Subparser subparser
) {
34 subparser
.help("Remotely delete a previously sent message.");
35 subparser
.addArgument("-t", "--target-timestamp")
38 .help("Specify the timestamp of the message to delete.");
39 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
40 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
41 subparser
.addArgument("--note-to-self").action(Arguments
.storeTrue());
45 public void handleCommand(
46 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
47 ) throws CommandException
{
48 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
49 final var recipientStrings
= ns
.<String
>getList("recipient");
50 final var groupIdStrings
= ns
.<String
>getList("group-id");
52 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
57 final long targetTimestamp
= ns
.getLong("target-timestamp");
60 final var results
= m
.sendRemoteDeleteMessage(targetTimestamp
, recipientIdentifiers
);
61 outputResult(outputWriter
, results
.getTimestamp());
62 ErrorUtils
.handleSendMessageResults(results
.getResults());
63 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
64 throw new UserErrorException(e
.getMessage());
65 } catch (IOException e
) {
66 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
71 public void handleCommand(
72 final Namespace ns
, final Signal signal
, final OutputWriter outputWriter
73 ) throws CommandException
{
74 final var recipients
= ns
.<String
>getList("recipient");
75 final var groupIdStrings
= ns
.<String
>getList("group-id");
77 final var noRecipients
= recipients
== null || recipients
.isEmpty();
78 final var noGroups
= groupIdStrings
== null || groupIdStrings
.isEmpty();
79 if (noRecipients
&& noGroups
) {
80 throw new UserErrorException("No recipients given");
82 if (!noRecipients
&& !noGroups
) {
83 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
86 final long targetTimestamp
= ns
.getLong("target-timestamp");
91 final var groupIds
= CommandUtil
.getGroupIds(groupIdStrings
);
92 for (final var groupId
: groupIds
) {
93 timestamp
= signal
.sendGroupRemoteDeleteMessage(targetTimestamp
, groupId
.serialize());
96 timestamp
= signal
.sendRemoteDeleteMessage(targetTimestamp
, recipients
);
98 outputResult(outputWriter
, timestamp
);
99 } catch (UnknownObject e
) {
100 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
101 } catch (Signal
.Error
.InvalidNumber e
) {
102 throw new UserErrorException("Invalid number: " + e
.getMessage());
103 } catch (Signal
.Error
.GroupNotFound e
) {
104 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
105 } catch (DBusExecutionException e
) {
106 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
110 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
111 if (outputWriter
instanceof PlainTextWriter
) {
112 final var writer
= (PlainTextWriter
) outputWriter
;
113 writer
.println("{}", timestamp
);
115 final var writer
= (JsonWriter
) outputWriter
;
116 writer
.write(Map
.of("timestamp", timestamp
));