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;
System.err.println("User is not registered.");
System.exit(1);
}
- String messageText = ns.getString("message");
- if (messageText == null) {
+
+ byte[] groupId = null;
+ List<String> recipients = null;
+ if (ns.getString("group") != null) {
try {
- messageText = IOUtils.toString(System.in);
+ 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);
+ }
+ groupId = g.groupId;
+ recipients = g.members;
} catch (IOException e) {
- System.err.println("Failed to read message from stdin: " + e.getMessage());
+ System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage());
+ System.err.println("Aborting sending.");
System.exit(1);
}
+ } else {
+ recipients = ns.<String>getList("recipient");
}
- final List<String> attachments = ns.getList("attachment");
- List<TextSecureAttachment> textSecureAttachments = null;
- if (attachments != null) {
- textSecureAttachments = new ArrayList<>(attachments.size());
- for (String attachment : attachments) {
+ if (ns.getBoolean("endsession")) {
+ sendEndSessionMessage(m, recipients);
+ } else {
+ 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);
+ }
+ }
+ }
+
+ String messageText = ns.getString("message");
+ if (messageText == null) {
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));
+ messageText = IOUtils.toString(System.in);
} catch (IOException e) {
- System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
+ System.err.println("Failed to read message from stdin: " + e.getMessage());
System.err.println("Aborting sending.");
System.exit(1);
}
}
- }
- List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
- for (String recipient : ns.<String>getList("recipient")) {
- try {
- recipients.add(m.getPushAddress(recipient));
- } catch (InvalidNumberException e) {
- System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
- System.err.println("Aborting sending.");
- System.exit(1);
- }
+ sendMessage(m, messageText, textSecureAttachments, recipients, groupId);
}
- sendMessage(m, messageText, textSecureAttachments, recipients);
+
break;
case "receive":
if (!m.isRegistered()) {
System.err.println("User is not registered.");
System.exit(1);
}
+ int timeout = 5;
+ if (ns.getInt("timeout") != null) {
+ timeout = ns.getInt("timeout");
+ }
+ boolean returnOnTimeout = true;
+ if (timeout < 0) {
+ returnOnTimeout = false;
+ timeout = 3600;
+ }
try {
- m.receiveMessages(5, true, new ReceiveMessageHandler(m));
+ m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
} catch (IOException e) {
System.err.println("Error while receiving message: " + e.getMessage());
System.exit(3);
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();
private static Namespace parseArgs(String[] args) {
ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
.defaultHelp(true)
- .description("Commandline interface for TextSecure.");
+ .description("Commandline interface for TextSecure.")
+ .version(Manager.PROJECT_NAME + " " + Manager.PROJECT_VERSION);
+
+ parser.addArgument("-u", "--username")
+ .help("Specify your phone number, that will be used for verification.");
+ parser.addArgument("-v", "--version")
+ .help("Show package version.")
+ .action(Arguments.version());
+
Subparsers subparsers = parser.addSubparsers()
.title("subcommands")
.dest("command")
.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("*");
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");
- parser.addArgument("-u", "--username")
+ Subparser parserLeaveGroup = subparsers.addParser("quitGroup");
+ parserLeaveGroup.addArgument("-g", "--group")
.required(true)
- .help("Specify your phone number, that will be used for verification.");
+ .help("Specify the recipient group ID.");
+
+ Subparser parserReceive = subparsers.addParser("receive");
+ parserReceive.addArgument("-t", "--timeout")
+ .type(int.class)
+ .help("Number of seconds to wait for new messages (negative values disable timeout)");
try {
- return parser.parseArgs(args);
+ Namespace ns = parser.parseArgs(args);
+ if (ns.getString("username") == null) {
+ parser.printUsage();
+ 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);
return null;
}
private static void sendMessage(Manager m, String messageText, List<TextSecureAttachment> textSecureAttachments,
- List<TextSecureAddress> recipients) {
+ List<String> recipients, byte[] groupId) {
final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
if (textSecureAttachments != null) {
messageBuilder.withAttachments(textSecureAttachments);
}
+ 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 sendMessage(Manager m, TextSecureDataMessage message, List<String> recipients) {
try {
m.sendMessage(recipients, message);
} catch (IOException e) {
}
@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 {
if (content.getDataMessage().isPresent()) {
TextSecureDataMessage message = content.getDataMessage().get();
- System.out.println("Body: " + message.getBody().get());
+ System.out.println("Message timestamp: " + message.getTimestamp());
+
+ if (message.getBody().isPresent()) {
+ System.out.println("Body: " + message.getBody().get());
+ }
+ if (message.getGroupInfo().isPresent()) {
+ TextSecureGroup groupInfo = message.getGroupInfo().get();
+ System.out.println("Group info:");
+ 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: <Unknown group>");
+ }
+ System.out.println(" Type: " + groupInfo.getType());
+ if (groupInfo.getMembers().isPresent()) {
+ for (String member : groupInfo.getMembers().get()) {
+ System.out.println(" Member: " + member);
+ }
+ }
+ if (groupInfo.getAvatar().isPresent()) {
+ System.out.println(" Avatar:");
+ printAttachment(groupInfo.getAvatar().get());
+ }
+ }
if (message.isEndSession()) {
- m.handleEndSession(envelope.getSource());
- } else if (message.getAttachments().isPresent()) {
+ System.out.println("Is end session");
+ }
+
+ if (message.getAttachments().isPresent()) {
System.out.println("Attachments: ");
for (TextSecureAttachment attachment : message.getAttachments().get()) {
- System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");
- if (attachment.isPointer()) {
- 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((pointer.getSize().isPresent() ? " Size: " + pointer.getSize().get() : " bytes") + (pointer.getPreview().isPresent() ? " (Preview is available: " + pointer.getPreview().get().length + " bytes)" : ""));
- try {
- File file = m.retrieveAttachment(pointer);
- System.out.println(" Stored plaintext in: " + file);
- } catch (IOException | InvalidMessageException e) {
- System.out.println("Failed to retrieve attachment: " + e.getMessage());
- }
- }
+ printAttachment(attachment);
}
}
}
}
System.out.println();
}
+
+ private void printAttachment(TextSecureAttachment attachment) {
+ System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");
+ if (attachment.isPointer()) {
+ 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" : "<unavailable>") + (pointer.getPreview().isPresent() ? " (Preview is available: " + pointer.getPreview().get().length + " bytes)" : ""));
+ File file = m.getAttachmentFile(pointer.getId());
+ if (file.exists()) {
+ System.out.println(" Stored plaintext in: " + file);
+ }
+ }
+ }
}
}