- List<String> attachments = ns.getList("attachment");
- if (attachments == null) {
- attachments = new ArrayList<>();
- }
- if (ns.getString("group") != null) {
- byte[] groupId = Util.decodeGroupId(ns.getString("group"));
- signal.sendGroupMessage(messageText, attachments, groupId);
- } else {
- signal.sendMessage(messageText, attachments, ns.<String>getList("recipient"));
- }
- return 0;
- } catch (IOException e) {
- handleIOException(e);
- return 3;
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- return 3;
- } catch (AssertionError e) {
- handleAssertionError(e);
- return 1;
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- return 1;
- } catch (NotAGroupMemberException e) {
- handleNotAGroupMemberException(e);
- return 1;
- } catch (AttachmentInvalidException e) {
- System.err.println("Failed to add attachment: " + e.getMessage());
- System.err.println("Aborting sending.");
- return 1;
- } catch (DBusExecutionException e) {
- handleDBusExecutionException(e);
- return 1;
- } catch (GroupIdFormatException e) {
- handleGroupIdFormatException(e);
- return 1;
+ var results = m.sendMessage(new Message(messageText, attachments), recipientIdentifiers);
+ outputResult(outputWriter, results.timestamp());
+ ErrorUtils.handleSendMessageResults(results.results());
+ } catch (AttachmentInvalidException | IOException e) {
+ throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
+ .getSimpleName() + ")", e);
+ } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
+ throw new UserErrorException(e.getMessage());
+ }
+ }
+
+ private void outputResult(final OutputWriter outputWriter, final long timestamp) {
+ if (outputWriter instanceof PlainTextWriter writer) {
+ writer.println("{}", timestamp);
+ } else {
+ final var writer = (JsonWriter) outputWriter;
+ writer.write(Map.of("timestamp", timestamp));