- try {
- long timestamp = 0;
- for (final var groupId : groupIds) {
- timestamp = signal.sendGroupMessage(messageText, attachments, groupId.serialize());
- }
- outputResult(outputWriter, timestamp);
- return;
- } catch (DBusExecutionException e) {
- throw new UnexpectedErrorException("Failed to send group message: " + e.getMessage());
+ final var editTimestamp = ns.getLong("edit-timestamp");
+
+ try {
+ final var message = new Message(messageText,
+ attachments,
+ viewOnce,
+ mentions,
+ Optional.ofNullable(quote),
+ Optional.ofNullable(sticker),
+ previews,
+ Optional.ofNullable((storyReply)),
+ textStyles);
+ var results = editTimestamp != null
+ ? m.sendEditMessage(message, recipientIdentifiers, editTimestamp)
+ : m.sendMessage(message, recipientIdentifiers, notifySelf);
+ outputResult(outputWriter, results);
+ } catch (AttachmentInvalidException | IOException e) {
+ if (e instanceof IOException io && io.getMessage().contains("No prekeys available")) {
+ throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
+ .getSimpleName() + "), maybe one of the devices of the recipient wasn't online for a while.",
+ e);
+ } else {
+ throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
+ .getSimpleName() + ")", e);
+ }
+ } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
+ throw new UserErrorException(e.getMessage());
+ } catch (UnregisteredRecipientException e) {
+ throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
+ } catch (InvalidStickerException e) {
+ throw new UserErrorException("Failed to send sticker: " + e.getMessage(), e);
+ }
+ }
+
+ private List<Message.Mention> parseMentions(
+ final String selfNumber,
+ final List<String> mentionStrings
+ ) throws UserErrorException {
+ final var mentionPattern = Pattern.compile("(\\d+):(\\d+):(.+)");
+ final var mentions = new ArrayList<Message.Mention>();
+ for (final var mention : mentionStrings) {
+ final var matcher = mentionPattern.matcher(mention);
+ if (!matcher.matches()) {
+ throw new UserErrorException("Invalid mention syntax ("
+ + mention
+ + ") expected 'start:length:recipientNumber'");