+ throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
+ }
+ }
+
+ List<String> attachments = ns.getList("attachment");
+ if (attachments == null) {
+ attachments = List.of();
+ }
+
+ 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 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());