+ public void handleCommand(
+ final Namespace ns, final Manager m, final OutputWriter outputWriter
+ ) throws CommandException {
+ final var isNoteToSelf = ns.getBoolean("note-to-self");
+ final var recipientStrings = ns.<String>getList("recipient");
+ final var groupIdStrings = ns.<String>getList("group-id");
+
+ final var recipientIdentifiers = CommandUtil.getRecipientIdentifiers(m,
+ isNoteToSelf,
+ recipientStrings,
+ groupIdStrings);
+
+ final var isEndSession = ns.getBoolean("end-session");
+ if (isEndSession) {
+ final var singleRecipients = recipientIdentifiers.stream()
+ .filter(r -> r instanceof RecipientIdentifier.Single)
+ .map(RecipientIdentifier.Single.class::cast)
+ .collect(Collectors.toSet());
+ if (singleRecipients.isEmpty()) {
+ throw new UserErrorException("No recipients given");
+ }
+
+ try {
+ m.sendEndSessionMessage(singleRecipients);
+ return;
+ } catch (IOException e) {
+ throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
+ .getSimpleName() + ")");
+ }
+ }
+
+ 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();
+ }
+
+ 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() + " (" + e.getClass()
+ .getSimpleName() + ")");
+ } 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");