- try {
- var results = m.sendMessage(new Message(messageText, attachments), recipientIdentifiers);
- outputResult(outputWriter, results.getTimestamp());
- ErrorUtils.handleSendMessageResults(results.getResults());
- } catch (AttachmentInvalidException | IOException e) {
- throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
- } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
- throw new UserErrorException(e.getMessage());
- }
- }
-
- @Override
- public void handleCommand(
- final Namespace ns, final Signal signal, final OutputWriter outputWriter
- ) throws CommandException {
- final var recipients = ns.<String>getList("recipient");
- final var isEndSession = ns.getBoolean("end-session");
- final var groupIdStrings = ns.<String>getList("group-id");
- final var isNoteToSelf = ns.getBoolean("note-to-self");
-
- final var noRecipients = recipients == null || recipients.isEmpty();
- final var noGroups = groupIdStrings == null || groupIdStrings.isEmpty();
- if ((noRecipients && isEndSession) || (noRecipients && noGroups && !isNoteToSelf)) {
- throw new UserErrorException("No recipients given");
- }
- if (!noRecipients && !noGroups) {
- throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
- }
- if (!noRecipients && isNoteToSelf) {
- throw new UserErrorException(
- "You cannot specify recipients by phone number and note to self at the same time");
- }
-
- if (isEndSession) {
- try {
- signal.sendEndSessionMessage(recipients);
- return;
- } catch (Signal.Error.UntrustedIdentity e) {
- throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
- } catch (DBusExecutionException e) {
- throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
- }
- }
-
- var messageText = ns.getString("message");
- if (messageText == null) {
- try {
- messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
- } catch (IOException e) {
- throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
- }
- }
-
- List<String> attachments = ns.getList("attachment");
- if (attachments == null) {
- attachments = List.of();
- }
-
- if (!noGroups) {
- final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
-
- try {
- long timestamp = 0;
- for (final var groupId : groupIds) {
- timestamp = signal.sendGroupMessage(messageText, attachments, groupId.serialize());
+ 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'");