From: AsamK Date: Wed, 25 Nov 2015 13:38:08 +0000 (+0100) Subject: Enable sending to groups X-Git-Tag: v0.1.0~6 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/2517919c49ccf3ed3c4bfa90782a4afa54fbf547 Enable sending to groups --- diff --git a/src/main/java/cli/Main.java b/src/main/java/cli/Main.java index 85ca227d..cac746d2 100644 --- a/src/main/java/cli/Main.java +++ b/src/main/java/cli/Main.java @@ -101,6 +101,7 @@ public class Main { messageText = IOUtils.toString(System.in); } catch (IOException e) { System.err.println("Failed to read message from stdin: " + e.getMessage()); + System.err.println("Aborting sending."); System.exit(1); } } @@ -123,9 +124,29 @@ public class Main { } } } + TextSecureGroup group = null; + List recipientStrings = null; + if (ns.getString("group") != null) { + try { + GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group"))); + if (g == null) { + System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": Unknown group"); + System.err.println("Aborting sending."); + System.exit(1); + } + group = new TextSecureGroup(g.groupId); + recipientStrings = g.members; + } catch (IOException e) { + System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage()); + System.err.println("Aborting sending."); + System.exit(1); + } + } else { + recipientStrings = ns.getList("recipient"); + } List recipients = new ArrayList<>(ns.getList("recipient").size()); - for (String recipient : ns.getList("recipient")) { + for (String recipient : recipientStrings) { try { recipients.add(m.getPushAddress(recipient)); } catch (InvalidNumberException e) { @@ -134,7 +155,8 @@ public class Main { System.exit(1); } } - sendMessage(m, messageText, textSecureAttachments, recipients); + + sendMessage(m, messageText, textSecureAttachments, recipients, group); break; case "receive": if (!m.isRegistered()) { @@ -195,6 +217,8 @@ public class Main { .help("The verification code you received via sms or voice call."); Subparser parserSend = subparsers.addParser("send"); + parserSend.addArgument("-g", "--group") + .help("Specify the recipient group ID."); parserSend.addArgument("recipient") .help("Specify the recipients' phone number.") .nargs("*"); @@ -216,6 +240,10 @@ public class Main { System.err.println("You need to specify a username (phone number)"); System.exit(2); } + if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) { + System.err.println("You cannot specify recipients by phone number and groups a the same time"); + System.exit(2); + } return ns; } catch (ArgumentParserException e) { parser.handleError(e); @@ -224,11 +252,14 @@ public class Main { } private static void sendMessage(Manager m, String messageText, List textSecureAttachments, - List recipients) { + List recipients, TextSecureGroup group) { final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText); if (textSecureAttachments != null) { messageBuilder.withAttachments(textSecureAttachments); } + if (group != null) { + messageBuilder.asGroupMessage(group); + } TextSecureDataMessage message = messageBuilder.build(); try { diff --git a/src/main/java/cli/Manager.java b/src/main/java/cli/Manager.java index 2258c99d..758dabc3 100644 --- a/src/main/java/cli/Manager.java +++ b/src/main/java/cli/Manager.java @@ -378,4 +378,8 @@ class Manager { String e164number = canonicalizeNumber(number); return new TextSecureAddress(e164number); } + + GroupInfo getGroupInfo(byte[] groupId) { + return groupStore.getGroup(groupId); + } }