]>
nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/RemoteDeleteCommand.java
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
.PlainTextWriterImpl
;
8 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
9 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
10 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
11 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
12 import org
.asamk
.signal
.util
.Util
;
13 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
14 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
16 import java
.util
.List
;
18 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
20 public class RemoteDeleteCommand
implements DbusCommand
{
23 public void attachToSubparser(final Subparser subparser
) {
24 subparser
.help("Remotely delete a previously sent message.");
25 subparser
.addArgument("-t", "--target-timestamp")
28 .help("Specify the timestamp of the message to delete.");
29 subparser
.addArgument("-g", "--group")
30 .help("Specify the recipient group ID.");
31 subparser
.addArgument("recipient")
32 .help("Specify the recipients' phone number.").nargs("*");
36 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
37 final List
<String
> recipients
= ns
.getList("recipient");
38 final var groupIdString
= ns
.getString("group");
40 final var noRecipients
= recipients
== null || recipients
.isEmpty();
41 if (noRecipients
&& groupIdString
== null) {
42 throw new UserErrorException("No recipients given");
44 if (!noRecipients
&& groupIdString
!= null) {
45 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
48 final long targetTimestamp
= ns
.getLong("target_timestamp");
50 final var writer
= new PlainTextWriterImpl(System
.out
);
52 byte[] groupId
= null;
53 if (groupIdString
!= null) {
55 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
56 } catch (GroupIdFormatException e
) {
57 throw new UserErrorException("Invalid group id: " + e
.getMessage());
63 if (groupId
!= null) {
64 timestamp
= signal
.sendGroupRemoteDeleteMessage(targetTimestamp
, groupId
);
66 timestamp
= signal
.sendRemoteDeleteMessage(targetTimestamp
, recipients
);
68 writer
.println("{}", timestamp
);
69 } catch (AssertionError e
) {
70 handleAssertionError(e
);
72 } catch (UnknownObject e
) {
73 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
74 } catch (Signal
.Error
.InvalidNumber e
) {
75 throw new UserErrorException("Invalid number: " + e
.getMessage());
76 } catch (Signal
.Error
.GroupNotFound e
) {
77 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
78 } catch (DBusExecutionException e
) {
79 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());