]> nmode's Git Repositories - signal-cli/commitdiff
Move recipients/group argument check to send commands
authorAsamK <asamk@gmx.de>
Sat, 16 Jan 2021 09:21:38 +0000 (10:21 +0100)
committerAsamK <asamk@gmx.de>
Sat, 16 Jan 2021 09:21:38 +0000 (10:21 +0100)
src/main/java/org/asamk/signal/Main.java
src/main/java/org/asamk/signal/commands/SendCommand.java
src/main/java/org/asamk/signal/commands/SendReactionCommand.java

index ddcf47e10ab6056adc9a03fc2faed97b45ab96e8..45945c0211f6e6f1252e2bf81c0d1eaca6fb0781 100644 (file)
@@ -93,11 +93,6 @@ public class Main {
             return null;
         }
 
-        if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
-            System.err.println("You cannot specify recipients by phone number and groups at the same time");
-            System.exit(2);
-        }
-
         return ns;
     }
 
index 235fc53cb4638125586362badf0034342ae20680..d3717aa2ff7c261449957ef88aa258ced3001ca2 100644 (file)
@@ -34,17 +34,24 @@ public class SendCommand implements DbusCommand {
 
     @Override
     public int handleCommand(final Namespace ns, final Signal signal) {
-        if ((ns.getList("recipient") == null || ns.getList("recipient").size() == 0) && (
-                ns.getBoolean("endsession") || ns.getString("group") == null
-        )) {
+        final List<String> recipients = ns.getList("recipient");
+        final Boolean isEndSession = ns.getBoolean("endsession");
+        final String groupIdString = ns.getString("group");
+
+        final boolean noRecipients = recipients == null || recipients.isEmpty();
+        if ((noRecipients && isEndSession) || (noRecipients && groupIdString == null)) {
             System.err.println("No recipients given");
             System.err.println("Aborting sending.");
             return 1;
         }
+        if (!noRecipients && groupIdString != null) {
+            System.err.println("You cannot specify recipients by phone number and groups at the same time");
+            return 1;
+        }
 
-        if (ns.getBoolean("endsession")) {
+        if (isEndSession) {
             try {
-                signal.sendEndSessionMessage(ns.getList("recipient"));
+                signal.sendEndSessionMessage(recipients);
                 return 0;
             } catch (AssertionError e) {
                 handleAssertionError(e);
@@ -72,10 +79,10 @@ public class SendCommand implements DbusCommand {
         }
 
         try {
-            if (ns.getString("group") != null) {
+            if (groupIdString != null) {
                 byte[] groupId;
                 try {
-                    groupId = Util.decodeGroupId(ns.getString("group")).serialize();
+                    groupId = Util.decodeGroupId(groupIdString).serialize();
                 } catch (GroupIdFormatException e) {
                     handleGroupIdFormatException(e);
                     return 1;
@@ -94,7 +101,7 @@ public class SendCommand implements DbusCommand {
         }
 
         try {
-            long timestamp = signal.sendMessage(messageText, attachments, ns.getList("recipient"));
+            long timestamp = signal.sendMessage(messageText, attachments, recipients);
             System.out.println(timestamp);
             return 0;
         } catch (AssertionError e) {
index 2a9afa74d8debf54a0debe7112d6967d26c27572..e9ddc1f0dbd59e46e6b7e1d1659d86b0abfa51c3 100644 (file)
@@ -47,28 +47,32 @@ public class SendReactionCommand implements LocalCommand {
 
     @Override
     public int handleCommand(final Namespace ns, final Manager m) {
-        if ((ns.getList("recipient") == null || ns.getList("recipient").size() == 0) && ns.getString("group") == null) {
+        final List<String> recipients = ns.getList("recipient");
+        final String groupIdString = ns.getString("group");
+
+        final boolean noRecipients = recipients == null || recipients.isEmpty();
+        if (noRecipients && groupIdString == null) {
             System.err.println("No recipients given");
             System.err.println("Aborting sending.");
             return 1;
         }
+        if (!noRecipients && groupIdString != null) {
+            System.err.println("You cannot specify recipients by phone number and groups at the same time");
+            return 1;
+        }
 
-        String emoji = ns.getString("emoji");
-        boolean isRemove = ns.getBoolean("remove");
-        String targetAuthor = ns.getString("target_author");
-        long targetTimestamp = ns.getLong("target_timestamp");
+        final String emoji = ns.getString("emoji");
+        final boolean isRemove = ns.getBoolean("remove");
+        final String targetAuthor = ns.getString("target_author");
+        final long targetTimestamp = ns.getLong("target_timestamp");
 
         try {
             final Pair<Long, List<SendMessageResult>> results;
-            if (ns.getString("group") != null) {
-                GroupId groupId = Util.decodeGroupId(ns.getString("group"));
+            if (groupIdString != null) {
+                GroupId groupId = Util.decodeGroupId(groupIdString);
                 results = m.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
             } else {
-                results = m.sendMessageReaction(emoji,
-                        isRemove,
-                        targetAuthor,
-                        targetTimestamp,
-                        ns.getList("recipient"));
+                results = m.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
             }
             return handleTimestampAndSendMessageResults(results.first(), results.second());
         } catch (IOException e) {