X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/6208eb72d823a5b4937c2a30f3219fec3a3b3a59..2c6796e3ce830b0285223baa11ec5e8553b4d52f:/src/main/java/cli/Main.java diff --git a/src/main/java/cli/Main.java b/src/main/java/cli/Main.java index 44a89505..199ea1eb 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,25 +124,38 @@ public class Main { } } } - - List recipients = new ArrayList<>(ns.getList("recipient").size()); - for (String recipient : ns.getList("recipient")) { + TextSecureGroup group = null; + List recipients = null; + if (ns.getString("group") != null) { try { - recipients.add(m.getPushAddress(recipient)); - } catch (InvalidNumberException e) { - System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage()); + 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); + recipients = 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 { + recipients = ns.getList("recipient"); } - sendMessage(m, messageText, textSecureAttachments, recipients); + + sendMessage(m, messageText, textSecureAttachments, recipients, group); break; case "receive": if (!m.isRegistered()) { System.err.println("User is not registered."); System.exit(1); } - int timeout = ns.getInt("timeout"); + int timeout = 5; + if (ns.getInt("timeout") != null) { + timeout = ns.getInt("timeout"); + } boolean returnOnTimeout = true; if (timeout < 0) { returnOnTimeout = false; @@ -192,6 +206,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("*"); @@ -213,6 +229,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); @@ -221,11 +241,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 { @@ -259,15 +282,13 @@ public class Main { } @Override - public void handleMessage(TextSecureEnvelope envelope) { + public void handleMessage(TextSecureEnvelope envelope, TextSecureContent content, GroupInfo group) { System.out.println("Envelope from: " + envelope.getSource()); System.out.println("Timestamp: " + envelope.getTimestamp()); if (envelope.isReceipt()) { System.out.println("Got receipt."); } else if (envelope.isWhisperMessage() | envelope.isPreKeyWhisperMessage()) { - TextSecureContent content = m.decryptMessage(envelope); - if (content == null) { System.out.println("Failed to decrypt message."); } else { @@ -285,6 +306,10 @@ public class Main { System.out.println(" Id: " + Base64.encodeBytes(groupInfo.getGroupId())); if (groupInfo.getName().isPresent()) { System.out.println(" Name: " + groupInfo.getName().get()); + } else if (group != null) { + System.out.println(" Name: " + group.name); + } else { + System.out.println(" Name: "); } System.out.println(" Type: " + groupInfo.getType()); if (groupInfo.getMembers().isPresent()) { @@ -299,7 +324,6 @@ public class Main { } if (message.isEndSession()) { System.out.println("Is end session"); - m.handleEndSession(envelope.getSource()); } if (message.getAttachments().isPresent()) { @@ -326,11 +350,9 @@ public class Main { final TextSecureAttachmentPointer pointer = attachment.asPointer(); System.out.println(" Id: " + pointer.getId() + " Key length: " + pointer.getKey().length + (pointer.getRelay().isPresent() ? " Relay: " + pointer.getRelay().get() : "")); System.out.println(" Size: " + (pointer.getSize().isPresent() ? pointer.getSize().get() + " bytes" : "") + (pointer.getPreview().isPresent() ? " (Preview is available: " + pointer.getPreview().get().length + " bytes)" : "")); - try { - File file = m.retrieveAttachment(pointer); + File file = m.getAttachmentFile(pointer.getId()); + if (file.exists()) { System.out.println(" Stored plaintext in: " + file); - } catch (IOException | InvalidMessageException e) { - System.out.println("Failed to retrieve attachment: " + e.getMessage()); } } }