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 org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
import java.io.File;
import java.io.FileInputStream;
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<String> attachments = ns.getList("attachment");
- List<TextSecureAttachment> 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<String> recipients = 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("Failed to send to group \"" + ns.getString("group") + "\": Unknown group");
System.err.println("Aborting sending.");
System.exit(1);
}
- group = new TextSecureGroup(g.groupId);
- recipients = g.members;
+ groupId = g.groupId;
+ recipients = new ArrayList<>(g.members);
} catch (IOException e) {
- System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage());
+ System.err.println("Failed to send to group \"" + ns.getString("group") + "\": " + e.getMessage());
System.err.println("Aborting sending.");
System.exit(1);
}
recipients = ns.<String>getList("recipient");
}
- sendMessage(m, messageText, textSecureAttachments, recipients, group);
+ if (ns.getBoolean("endsession")) {
+ sendEndSessionMessage(m, recipients);
+ } else {
+ List<TextSecureAttachment> textSecureAttachments = null;
+ try {
+ textSecureAttachments = getTextSecureAttachments(ns.<String>getList("attachment"));
+ } catch (IOException e) {
+ System.err.println("Failed to add 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()) {
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 group \"" + ns.getString("group") + "\": Unknown group");
+ System.err.println("Aborting sending.");
+ System.exit(1);
+ }
+
+ sendQuitGroupMessage(m, new ArrayList<>(g.members), g.groupId);
+ } catch (IOException e) {
+ System.err.println("Failed to send to group \"" + ns.getString("group") + "\": " + e.getMessage());
+ System.err.println("Aborting sending.");
+ System.exit(1);
+ }
+ break;
+ case "updateGroup":
+ if (!m.isRegistered()) {
+ System.err.println("User is not registered.");
+ System.exit(1);
+ }
+
+ try {
+ GroupInfo g;
+ if (ns.getString("group") != null) {
+ g = m.getGroupInfo(Base64.decode(ns.getString("group")));
+ if (g == null) {
+ System.err.println("Failed to send to group \"" + ns.getString("group") + "\": Unknown group");
+ System.err.println("Aborting sending.");
+ System.exit(1);
+ }
+ } else {
+ // Create new group
+ g = new GroupInfo(Util.getSecretBytes(16));
+ g.members.add(m.getUsername());
+ System.out.println("Creating new group \"" + Base64.encodeBytes(g.groupId) + "\" …");
+ }
+
+ String name = ns.getString("name");
+ if (name != null) {
+ g.name = name;
+ }
+
+ final List<String> members = ns.getList("member");
+
+ if (members != null) {
+ for (String member : members) {
+ try {
+ g.members.add(m.canonicalizeNumber(member));
+ } catch (InvalidNumberException e) {
+ System.err.println("Failed to add member \"" + member + "\" to group: " + e.getMessage());
+ System.err.println("Aborting…");
+ System.exit(1);
+ }
+ }
+ }
+
+ TextSecureGroup.Builder group = TextSecureGroup.newBuilder(TextSecureGroup.Type.UPDATE)
+ .withId(g.groupId)
+ .withName(g.name)
+ .withMembers(new ArrayList<>(g.members));
+
+ String avatar = ns.getString("avatar");
+ if (avatar != null) {
+ try {
+ group.withAvatar(createAttachment(avatar));
+ // TODO
+ g.avatarId = 0;
+ } catch (IOException e) {
+ System.err.println("Failed to add attachment \"" + avatar + "\": " + e.getMessage());
+ System.err.println("Aborting sending.");
+ System.exit(1);
+ }
+ }
+
+ m.setGroupInfo(g);
+
+ sendUpdateGroupMessage(m, group.build());
+ } catch (IOException e) {
+ System.err.println("Failed to send to group \"" + ns.getString("group") + "\": " + e.getMessage());
+ System.err.println("Aborting sending.");
+ System.exit(1);
+ }
+
break;
}
m.save();
System.exit(0);
}
+ private static List<TextSecureAttachment> getTextSecureAttachments(List<String> attachments) {
+ private static List<TextSecureAttachment> getTextSecureAttachments(List<String> attachments) throws IOException {
+ List<TextSecureAttachment> textSecureAttachments = null;
+ if (attachments != null) {
+ textSecureAttachments = new ArrayList<>(attachments.size());
+ for (String attachment : attachments) {
+ textSecureAttachments.add(createAttachment(attachment));
+ }
+ }
+ return textSecureAttachments;
+ }
+
+ private static TextSecureAttachmentStream createAttachment(String attachment) throws IOException {
+ File attachmentFile = new File(attachment);
+ InputStream attachmentStream = new FileInputStream(attachmentFile);
+ final long attachmentSize = attachmentFile.length();
+ String mime = Files.probeContentType(Paths.get(attachment));
+ return new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null);
+ }
+
private static Namespace parseArgs(String[] args) {
ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
.defaultHelp(true)
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 parserUpdateGroup = subparsers.addParser("updateGroup");
+ parserUpdateGroup.addArgument("-g", "--group")
+ .help("Specify the recipient group ID.");
+ parserUpdateGroup.addArgument("-n", "--name")
+ .help("Specify the new group name.");
+ parserUpdateGroup.addArgument("-a", "--avatar")
+ .help("Specify a new group avatar image file");
+ parserUpdateGroup.addArgument("-m", "--member")
+ .nargs("*")
+ .help("Specify one or more members to add to the group");
Subparser parserReceive = subparsers.addParser("receive");
parserReceive.addArgument("-t", "--timeout")
System.err.println("You need to specify a username (phone number)");
System.exit(2);
}
+ if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"))) {
+ System.err.println("Invalid username (phone number), make sure you include the country code.");
+ 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);
}
private static void sendMessage(Manager m, String messageText, List<TextSecureAttachment> textSecureAttachments,
- List<String> recipients, TextSecureGroup group) {
+ List<String> 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<String> recipients) {
+ final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().asEndSessionMessage();
+
+ TextSecureDataMessage message = messageBuilder.build();
+
+ sendMessage(m, message, recipients);
+ }
+
+ private static void sendQuitGroupMessage(Manager m, List<String> 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 sendUpdateGroupMessage(Manager m, TextSecureGroup g) {
+ final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder();
+
+ messageBuilder.asGroupMessage(g);
+
+ TextSecureDataMessage message = messageBuilder.build();
+
+ sendMessage(m, message, g.getMembers().get());
+ }
+
+ private static void sendMessage(Manager m, TextSecureDataMessage message, List<String> recipients) {
try {
m.sendMessage(recipients, message);
} catch (IOException e) {