From: AsamK Date: Mon, 6 Jul 2015 13:12:59 +0000 (+0200) Subject: Support sending attachments X-Git-Tag: v0.0.2~11 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/08f8d2b02676d78f35df498d58264cbf32e54e0d Support sending attachments Limitations: - the textsecure protocol allows sending arbitrary mime types but the android client only supports image, audio and video - sending multiple attachments is supported, but the android client only shows the first one --- diff --git a/src/main/java/cli/Main.java b/src/main/java/cli/Main.java index 9a915dee..4ba09da3 100644 --- a/src/main/java/cli/Main.java +++ b/src/main/java/cli/Main.java @@ -23,13 +23,22 @@ import org.apache.commons.io.IOUtils; import org.whispersystems.libaxolotl.InvalidVersionException; import org.whispersystems.textsecure.api.TextSecureMessageSender; import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException; +import org.whispersystems.textsecure.api.messages.TextSecureAttachment; +import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream; import org.whispersystems.textsecure.api.messages.TextSecureContent; import org.whispersystems.textsecure.api.messages.TextSecureDataMessage; import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage; import org.whispersystems.textsecure.api.push.TextSecureAddress; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.security.Security; +import java.util.ArrayList; +import java.util.List; public class Main { @@ -58,6 +67,9 @@ public class Main { .nargs("*"); parserSend.addArgument("-m", "--message") .help("Specify the message, if missing standard input is used."); + parserSend.addArgument("-a", "--attachment") + .nargs("*") + .help("Add file as attachment"); Subparser parserReceive = subparsers.addParser("receive"); parser.addArgument("-u", "--username") .required(true) @@ -123,7 +135,25 @@ public class Main { System.exit(1); } } - TextSecureDataMessage message = TextSecureDataMessage.newBuilder().withBody(messageText).build(); + final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText); + final List attachments = ns.getList("attachment"); + if (attachments != null) { + List 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.out.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage()); + System.exit(1); + } + } + messageBuilder.withAttachments(textSecureAttachments); + } + TextSecureDataMessage message = messageBuilder.build(); for (String recipient : ns.getList("recipient")) { try { messageSender.sendMessage(new TextSecureAddress(recipient), message);