X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/2c6796e3ce830b0285223baa11ec5e8553b4d52f..1689dfcb3857f2dd5ea74f9184b80d9499f534c0:/src/main/java/cli/Main.java diff --git a/src/main/java/cli/Main.java b/src/main/java/cli/Main.java index 199ea1eb..db2a941b 100644 --- a/src/main/java/cli/Main.java +++ b/src/main/java/cli/Main.java @@ -20,15 +20,12 @@ import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.inf.*; import org.apache.commons.io.IOUtils; -import org.whispersystems.libaxolotl.InvalidMessageException; import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException; import org.whispersystems.textsecure.api.messages.*; import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage; -import org.whispersystems.textsecure.api.push.TextSecureAddress; import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions; import org.whispersystems.textsecure.api.push.exceptions.NetworkFailureException; import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException; -import org.whispersystems.textsecure.api.util.InvalidNumberException; import java.io.File; import java.io.FileInputStream; @@ -95,36 +92,8 @@ 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; + byte[] groupId = null; List recipients = null; if (ns.getString("group") != null) { try { @@ -134,7 +103,7 @@ public class Main { System.err.println("Aborting sending."); System.exit(1); } - group = new TextSecureGroup(g.groupId); + groupId = g.groupId; recipients = g.members; } catch (IOException e) { System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage()); @@ -145,7 +114,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, groupId); + } + break; case "receive": if (!m.isRegistered()) { @@ -172,6 +176,28 @@ public class Main { System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README"); System.exit(1); } + break; + case "quitGroup": + if (!m.isRegistered()) { + System.err.println("User is not registered."); + System.exit(1); + } + + 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); + } + + sendQuitGroupMessage(m, g.members, g.groupId); + } catch (IOException e) { + System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage()); + System.err.println("Aborting sending."); + System.exit(1); + } + break; } m.save(); @@ -216,6 +242,14 @@ 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 parserLeaveGroup = subparsers.addParser("quitGroup"); + parserLeaveGroup.addArgument("-g", "--group") + .required(true) + .help("Specify the recipient group ID."); Subparser parserReceive = subparsers.addParser("receive"); parserReceive.addArgument("-t", "--timeout") @@ -241,16 +275,38 @@ public class Main { } private static void sendMessage(Manager m, String messageText, List textSecureAttachments, - List recipients, TextSecureGroup group) { + List recipients, byte[] groupId) { final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText); if (textSecureAttachments != null) { messageBuilder.withAttachments(textSecureAttachments); } - if (group != null) { - messageBuilder.asGroupMessage(group); + if (groupId != null) { + messageBuilder.asGroupMessage(new TextSecureGroup(groupId)); } 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 sendQuitGroupMessage(Manager m, List recipients, byte[] groupId) { + final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder(); + TextSecureGroup group = TextSecureGroup.newBuilder(TextSecureGroup.Type.QUIT).withId(groupId).build(); + messageBuilder.asGroupMessage(group); + + 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) {