- try {
- if (ns.getString("group") != null) {
- byte[] groupId;
- try {
- groupId = Util.decodeGroupId(ns.getString("group"));
- } catch (GroupIdFormatException e) {
- handleGroupIdFormatException(e);
- return 1;
- }
-
- long timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
- System.out.println(timestamp);
- return 0;
+ final var writer = new PlainTextWriterImpl(System.out);
+
+ if (groupIdString != null) {
+ byte[] groupId;
+ try {
+ groupId = Util.decodeGroupId(groupIdString).serialize();
+ } catch (GroupIdFormatException e) {
+ throw new UserErrorException("Invalid group id: " + e.getMessage());
+ }
+
+ try {
+ var timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
+ writer.println("{}", timestamp);
+ return;
+ } catch (DBusExecutionException e) {
+ throw new UnexpectedErrorException("Failed to send group message: " + e.getMessage());
+ }
+ }
+
+ if (isNoteToSelf) {
+ try {
+ var timestamp = signal.sendNoteToSelfMessage(messageText, attachments);
+ writer.println("{}", timestamp);
+ return;
+ } catch (Signal.Error.UntrustedIdentity e) {
+ throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
+ } catch (DBusExecutionException e) {
+ throw new UnexpectedErrorException("Failed to send note to self message: " + e.getMessage());