]> nmode's Git Repositories - signal-cli/commitdiff
Implement sending message quotes
authorAsamK <asamk@gmx.de>
Sun, 21 Nov 2021 18:18:17 +0000 (19:18 +0100)
committerAsamK <asamk@gmx.de>
Sun, 21 Nov 2021 18:18:17 +0000 (19:18 +0100)
Fixes #213

lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
lib/src/main/java/org/asamk/signal/manager/api/Message.java
man/signal-cli.1.adoc
src/main/java/org/asamk/signal/commands/SendCommand.java
src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java
src/main/java/org/asamk/signal/util/CommandUtil.java

index 93296a1c4f98775e5797c622926ad4a2bdde1246..84a866d00df9458fc7bd9dbe65a92d3a5f67ce87 100644 (file)
@@ -708,15 +708,27 @@ public class ManagerImpl implements Manager {
             messageBuilder.withAttachments(attachmentHelper.uploadAttachments(attachments));
         }
         if (message.mentions().size() > 0) {
             messageBuilder.withAttachments(attachmentHelper.uploadAttachments(attachments));
         }
         if (message.mentions().size() > 0) {
-            final var mentions = new ArrayList<SignalServiceDataMessage.Mention>();
-            for (final var m : message.mentions()) {
-                final var recipientId = resolveRecipient(m.recipient());
-                mentions.add(new SignalServiceDataMessage.Mention(resolveSignalServiceAddress(recipientId).getAci(),
-                        m.start(),
-                        m.length()));
-            }
-            messageBuilder.withMentions(mentions);
+            messageBuilder.withMentions(resolveMentions(message.mentions()));
+        }
+        if (message.quote().isPresent()) {
+            final var quote = message.quote().get();
+            messageBuilder.withQuote(new SignalServiceDataMessage.Quote(quote.timestamp(),
+                    resolveSignalServiceAddress(resolveRecipient(quote.author())),
+                    quote.message(),
+                    List.of(),
+                    resolveMentions(quote.mentions())));
+        }
+    }
+
+    private ArrayList<SignalServiceDataMessage.Mention> resolveMentions(final List<Message.Mention> mentionList) throws IOException {
+        final var mentions = new ArrayList<SignalServiceDataMessage.Mention>();
+        for (final var m : mentionList) {
+            final var recipientId = resolveRecipient(m.recipient());
+            mentions.add(new SignalServiceDataMessage.Mention(resolveSignalServiceAddress(recipientId).getAci(),
+                    m.start(),
+                    m.length()));
         }
         }
+        return mentions;
     }
 
     @Override
     }
 
     @Override
index 5d0c78f80c8b27b38048a9b611569ba206582814..13cb00cf72b1b1c6737a0f10146142772904bfc4 100644 (file)
@@ -1,8 +1,11 @@
 package org.asamk.signal.manager.api;
 
 import java.util.List;
 package org.asamk.signal.manager.api;
 
 import java.util.List;
+import java.util.Optional;
 
 
-public record Message(String messageText, List<String> attachments, List<Mention> mentions) {
+public record Message(String messageText, List<String> attachments, List<Mention> mentions, Optional<Quote> quote) {
 
     public record Mention(RecipientIdentifier.Single recipient, int start, int length) {}
 
     public record Mention(RecipientIdentifier.Single recipient, int start, int length) {}
+
+    public record Quote(long timestamp, RecipientIdentifier.Single author, String message, List<Mention> mentions) {}
 }
 }
index c5b190eb09a4273f1892dce19faa53239d06ce1f..ff49843cc8f47f17a2abaadf558424e895fabe1e 100644 (file)
@@ -210,6 +210,19 @@ Mention another group member (syntax: start:length:recipientNumber)
 In the apps the mention replaces part of the message text, which is specified by the start and length values.
 e.g.: `-m "Hi X!" --mention "3:1:+123456789"`
 
 In the apps the mention replaces part of the message text, which is specified by the start and length values.
 e.g.: `-m "Hi X!" --mention "3:1:+123456789"`
 
+*--quote-timestamp*::
+Specify the timestamp of a previous message with the recipient or group to add a
+quote to the new message.
+
+*--quote-author*::
+Specify the number of the author of the original message.
+
+*--quote-message*::
+Specify the message of the original message.
+
+*--quote-mention*::
+Specify the mentions of the original message (same format as `--mention`).
+
 === sendReaction
 
 Send reaction to a previously received or sent message.
 === sendReaction
 
 Send reaction to a previously received or sent message.
index 9af95e6b80fe5de2f0da935f5f296a5083b22abc..f73d3e70d3a1d13db17412fd518a9fe857daba4b 100644 (file)
@@ -28,6 +28,7 @@ import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
@@ -57,6 +58,14 @@ public class SendCommand implements JsonRpcLocalCommand {
         subparser.addArgument("--mention")
                 .nargs("*")
                 .help("Mention another group member (syntax: start:length:recipientNumber)");
         subparser.addArgument("--mention")
                 .nargs("*")
                 .help("Mention another group member (syntax: start:length:recipientNumber)");
+        subparser.addArgument("--quote-timestamp")
+                .type(long.class)
+                .help("Specify the timestamp of a previous message with the recipient or group to add a quote to the new message.");
+        subparser.addArgument("--quote-author").help("Specify the number of the author of the original message.");
+        subparser.addArgument("--quote-message").help("Specify the message of the original message.");
+        subparser.addArgument("--quote-mention")
+                .nargs("*")
+                .help("Quote with mention of another group member (syntax: start:length:recipientNumber)");
     }
 
     @Override
     }
 
     @Override
@@ -108,26 +117,28 @@ public class SendCommand implements JsonRpcLocalCommand {
         }
 
         List<String> mentionStrings = ns.getList("mention");
         }
 
         List<String> mentionStrings = ns.getList("mention");
-        List<Message.Mention> mentions;
-        if (mentionStrings == null) {
-            mentions = List.of();
+        final var mentions = mentionStrings == null ? List.<Message.Mention>of() : parseMentions(m, mentionStrings);
+
+        final Message.Quote quote;
+        final var quoteTimestamp = ns.getLong("quote-timestamp");
+        if (quoteTimestamp != null) {
+            final var quoteAuthor = ns.getString("quote-author");
+            final var quoteMessage = ns.getString("quote-message");
+            List<String> quoteMentionStrings = ns.getList("quote-mention");
+            final var quoteMentions = quoteMentionStrings == null
+                    ? List.<Message.Mention>of()
+                    : parseMentions(m, quoteMentionStrings);
+            quote = new Message.Quote(quoteTimestamp,
+                    CommandUtil.getSingleRecipientIdentifier(quoteAuthor, m.getSelfNumber()),
+                    quoteMessage,
+                    quoteMentions);
         } else {
         } 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))));
-            }
+            quote = null;
         }
 
         try {
         }
 
         try {
-            var results = m.sendMessage(new Message(messageText, attachments, mentions), recipientIdentifiers);
+            var results = m.sendMessage(new Message(messageText, attachments, mentions, Optional.ofNullable(quote)),
+                    recipientIdentifiers);
             outputResult(outputWriter, results.timestamp());
             ErrorUtils.handleSendMessageResults(results.results());
         } catch (AttachmentInvalidException | IOException e) {
             outputResult(outputWriter, results.timestamp());
             ErrorUtils.handleSendMessageResults(results.results());
         } catch (AttachmentInvalidException | IOException e) {
@@ -138,6 +149,25 @@ public class SendCommand implements JsonRpcLocalCommand {
         }
     }
 
         }
     }
 
+    private List<Message.Mention> parseMentions(
+            final Manager m, final List<String> mentionStrings
+    ) throws UserErrorException {
+        List<Message.Mention> mentions;
+        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))));
+        }
+        return mentions;
+    }
+
     private void outputResult(final OutputWriter outputWriter, final long timestamp) {
         if (outputWriter instanceof PlainTextWriter writer) {
             writer.println("{}", timestamp);
     private void outputResult(final OutputWriter outputWriter, final long timestamp) {
         if (outputWriter instanceof PlainTextWriter writer) {
             writer.println("{}", timestamp);
index 7eb65af32b70b5ad901c23aa8a8d54cfd12c52fe..6395eb1466f2e03d0e3dc7178003a829a988328d 100644 (file)
@@ -199,7 +199,7 @@ public class DbusSignalImpl implements Signal {
     @Override
     public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) {
         try {
     @Override
     public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) {
         try {
-            final var results = m.sendMessage(new Message(message, attachments, List.of()),
+            final var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
                     getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
                             .map(RecipientIdentifier.class::cast)
                             .collect(Collectors.toSet()));
                     getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
                             .map(RecipientIdentifier.class::cast)
                             .collect(Collectors.toSet()));
@@ -364,7 +364,7 @@ public class DbusSignalImpl implements Signal {
             final String message, final List<String> attachments
     ) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
         try {
             final String message, final List<String> attachments
     ) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
         try {
-            final var results = m.sendMessage(new Message(message, attachments, List.of()),
+            final var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
                     Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
             checkSendMessageResults(results.timestamp(), results.results());
             return results.timestamp();
                     Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
             checkSendMessageResults(results.timestamp(), results.results());
             return results.timestamp();
@@ -390,7 +390,7 @@ public class DbusSignalImpl implements Signal {
     @Override
     public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
         try {
     @Override
     public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
         try {
-            var results = m.sendMessage(new Message(message, attachments, List.of()),
+            var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
                     Set.of(new RecipientIdentifier.Group(getGroupId(groupId))));
             checkSendMessageResults(results.timestamp(), results.results());
             return results.timestamp();
                     Set.of(new RecipientIdentifier.Group(getGroupId(groupId))));
             checkSendMessageResults(results.timestamp(), results.results());
             return results.timestamp();
index b9574d0e628f1dcceeaada0a8b348e1ceea1eb9e..b86a99894328acc744fac71190f7f29d13803163 100644 (file)
@@ -93,7 +93,7 @@ public class CommandUtil {
         try {
             return RecipientIdentifier.Single.fromString(recipientString, localNumber);
         } catch (InvalidNumberException e) {
         try {
             return RecipientIdentifier.Single.fromString(recipientString, localNumber);
         } catch (InvalidNumberException e) {
-            throw new UserErrorException("Invalid phone number '" + recipientString + "': " + e.getMessage());
+            throw new UserErrorException("Invalid phone number '" + recipientString + "': " + e.getMessage(), e);
         }
     }
 }
         }
     }
 }