]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/SendCommand.java
Implement sending mentions
[signal-cli] / src / main / java / org / asamk / signal / commands / SendCommand.java
index ab8bce16cbfded8ba33c17d508c233a9b9b41e90..8d863d5468c1857ff3a52e52fc347249401d514c 100644 (file)
@@ -25,8 +25,10 @@ import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.nio.charset.Charset;
 
 import java.io.IOException;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 public class SendCommand implements JsonRpcLocalCommand {
 import java.util.stream.Collectors;
 
 public class SendCommand implements JsonRpcLocalCommand {
@@ -52,6 +54,9 @@ public class SendCommand implements JsonRpcLocalCommand {
         subparser.addArgument("-e", "--end-session", "--endsession")
                 .help("Clear session state and send end session message.")
                 .action(Arguments.storeTrue());
         subparser.addArgument("-e", "--end-session", "--endsession")
                 .help("Clear session state and send end session message.")
                 .action(Arguments.storeTrue());
+        subparser.addArgument("--mention")
+                .nargs("*")
+                .help("Mention another group member (syntax: start:length:recipientNumber)");
     }
 
     @Override
     }
 
     @Override
@@ -100,10 +105,29 @@ public class SendCommand implements JsonRpcLocalCommand {
             attachments = List.of();
         }
 
             attachments = List.of();
         }
 
+        List<String> mentionStrings = ns.getList("mention");
+        List<Message.Mention> mentions;
+        if (mentionStrings == null) {
+            mentions = List.of();
+        } else {
+            final Pattern mentionPattern = Pattern.compile("([0-9]+):([0-9]+):(.+)");
+            mentions = new ArrayList<>();
+            for (final var mention : mentionStrings) {
+                final var matcher = mentionPattern.matcher(mention);
+                if (!matcher.matches()) {
+                    throw new UserErrorException("Invalid mention syntax ("
+                            + mention
+                            + ") expected 'start:end:recipientNumber'");
+                }
+                mentions.add(new Message.Mention(CommandUtil.getSingleRecipientIdentifier(matcher.group(3),
+                        m.getSelfNumber()), Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2))));
+            }
+        }
+
         try {
         try {
-            var results = m.sendMessage(new Message(messageText, attachments), recipientIdentifiers);
-            outputResult(outputWriter, results.getTimestamp());
-            ErrorUtils.handleSendMessageResults(results.getResults());
+            var results = m.sendMessage(new Message(messageText, attachments, mentions), recipientIdentifiers);
+            outputResult(outputWriter, results.timestamp());
+            ErrorUtils.handleSendMessageResults(results.results());
         } catch (AttachmentInvalidException | IOException e) {
             throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
                     .getSimpleName() + ")", e);
         } catch (AttachmentInvalidException | IOException e) {
             throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
                     .getSimpleName() + ")", e);