From: AsamK Date: Thu, 26 Nov 2015 13:38:47 +0000 (+0100) Subject: Implement sending EndSession messages X-Git-Tag: v0.1.0~4 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/09decf7916245fe1e031c8925b9f2128a449b150 Implement sending EndSession messages --- diff --git a/src/main/java/cli/Main.java b/src/main/java/cli/Main.java index 199ea1eb..c4a6a4c0 100644 --- a/src/main/java/cli/Main.java +++ b/src/main/java/cli/Main.java @@ -95,35 +95,7 @@ public class Main { System.err.println("User is not registered."); System.exit(1); } - String messageText = ns.getString("message"); - if (messageText == null) { - try { - 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); - } - } - final List attachments = ns.getList("attachment"); - List textSecureAttachments = null; - if (attachments != null) { - textSecureAttachments = new ArrayList<>(attachments.size()); - for (String attachment : attachments) { - try { - File attachmentFile = new File(attachment); - InputStream attachmentStream = new FileInputStream(attachmentFile); - final long attachmentSize = attachmentFile.length(); - String mime = Files.probeContentType(Paths.get(attachment)); - textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null)); - } catch (IOException e) { - System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage()); - System.err.println("Aborting sending."); - System.exit(1); - } - } - } TextSecureGroup group = null; List recipients = null; if (ns.getString("group") != null) { @@ -145,7 +117,42 @@ public class Main { recipients = ns.getList("recipient"); } - sendMessage(m, messageText, textSecureAttachments, recipients, group); + if (ns.getBoolean("endsession")) { + sendEndSessionMessage(m, recipients); + } else { + final List attachments = ns.getList("attachment"); + List textSecureAttachments = null; + if (attachments != null) { + textSecureAttachments = new ArrayList<>(attachments.size()); + for (String attachment : attachments) { + try { + File attachmentFile = new File(attachment); + InputStream attachmentStream = new FileInputStream(attachmentFile); + final long attachmentSize = attachmentFile.length(); + String mime = Files.probeContentType(Paths.get(attachment)); + textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null)); + } catch (IOException e) { + System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage()); + System.err.println("Aborting sending."); + System.exit(1); + } + } + } + + String messageText = ns.getString("message"); + if (messageText == null) { + try { + 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); + } + } + + sendMessage(m, messageText, textSecureAttachments, recipients, group); + } + break; case "receive": if (!m.isRegistered()) { @@ -216,6 +223,9 @@ public class Main { parserSend.addArgument("-a", "--attachment") .nargs("*") .help("Add file as attachment"); + parserSend.addArgument("-e", "--endsession") + .help("Clear session state and send end session message.") + .action(Arguments.storeTrue()); Subparser parserReceive = subparsers.addParser("receive"); parserReceive.addArgument("-t", "--timeout") @@ -251,6 +261,18 @@ public class Main { } TextSecureDataMessage message = messageBuilder.build(); + sendMessage(m, message, recipients); + } + + private static void sendEndSessionMessage(Manager m, List recipients) { + final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().asEndSessionMessage(); + + TextSecureDataMessage message = messageBuilder.build(); + + sendMessage(m, message, recipients); + } + + private static void sendMessage(Manager m, TextSecureDataMessage message, List recipients) { try { m.sendMessage(recipients, message); } catch (IOException e) {