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
.GroupSendingNotAllowedException
;
17 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
18 import org
.asamk
.signal
.util
.CommandUtil
;
19 import org
.asamk
.signal
.util
.ErrorUtils
;
20 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
21 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
23 import java
.io
.IOException
;
26 public class RemoteDeleteCommand
implements DbusCommand
, JsonRpcLocalCommand
{
29 public String
getName() {
30 return "remoteDelete";
34 public void attachToSubparser(final Subparser subparser
) {
35 subparser
.help("Remotely delete a previously sent message.");
36 subparser
.addArgument("-t", "--target-timestamp")
39 .help("Specify the timestamp of the message to delete.");
40 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
41 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
42 subparser
.addArgument("--note-to-self").action(Arguments
.storeTrue());
46 public void handleCommand(
47 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
48 ) throws CommandException
{
49 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
50 final var recipientStrings
= ns
.<String
>getList("recipient");
51 final var groupIdStrings
= ns
.<String
>getList("group-id");
53 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
58 final long targetTimestamp
= ns
.getLong("target-timestamp");
61 final var results
= m
.sendRemoteDeleteMessage(targetTimestamp
, recipientIdentifiers
);
62 outputResult(outputWriter
, results
.getTimestamp());
63 ErrorUtils
.handleSendMessageResults(results
.getResults());
64 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
65 throw new UserErrorException(e
.getMessage());
66 } catch (IOException e
) {
67 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
68 .getSimpleName() + ")", e
);
73 public void handleCommand(
74 final Namespace ns
, final Signal signal
, final OutputWriter outputWriter
75 ) throws CommandException
{
76 final var recipients
= ns
.<String
>getList("recipient");
77 final var groupIdStrings
= ns
.<String
>getList("group-id");
79 final var noRecipients
= recipients
== null || recipients
.isEmpty();
80 final var noGroups
= groupIdStrings
== null || groupIdStrings
.isEmpty();
81 if (noRecipients
&& noGroups
) {
82 throw new UserErrorException("No recipients given");
84 if (!noRecipients
&& !noGroups
) {
85 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
88 final long targetTimestamp
= ns
.getLong("target-timestamp");
93 final var groupIds
= CommandUtil
.getGroupIds(groupIdStrings
);
94 for (final var groupId
: groupIds
) {
95 timestamp
= signal
.sendGroupRemoteDeleteMessage(targetTimestamp
, groupId
.serialize());
98 timestamp
= signal
.sendRemoteDeleteMessage(targetTimestamp
, recipients
);
100 outputResult(outputWriter
, timestamp
);
101 } catch (UnknownObject e
) {
102 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
103 } catch (Signal
.Error
.InvalidNumber e
) {
104 throw new UserErrorException("Invalid number: " + e
.getMessage());
105 } catch (Signal
.Error
.GroupNotFound e
) {
106 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
107 } catch (DBusExecutionException e
) {
108 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
109 .getSimpleName() + ")", e
);
113 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
114 if (outputWriter
instanceof PlainTextWriter
) {
115 final var writer
= (PlainTextWriter
) outputWriter
;
116 writer
.println("{}", timestamp
);
118 final var writer
= (JsonWriter
) outputWriter
;
119 writer
.write(Map
.of("timestamp", timestamp
));