]> nmode's Git Repositories - signal-cli/blob - src/main/java/cli/Main.java
Correctly send to multiple recipients
[signal-cli] / src / main / java / cli / Main.java
1 /**
2 * Copyright (C) 2015 AsamK
3 * <p>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 * <p>
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * <p>
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 package cli;
18
19 import net.sourceforge.argparse4j.ArgumentParsers;
20 import net.sourceforge.argparse4j.impl.Arguments;
21 import net.sourceforge.argparse4j.inf.*;
22 import org.apache.commons.io.IOUtils;
23 import org.whispersystems.textsecure.api.TextSecureMessageSender;
24 import org.whispersystems.textsecure.api.messages.*;
25 import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
26 import org.whispersystems.textsecure.api.push.TextSecureAddress;
27 import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
28 import org.whispersystems.textsecure.api.util.InvalidNumberException;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.security.Security;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 public class Main {
41
42 public static void main(String[] args) {
43 // Workaround for BKS truststore
44 Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
45
46 ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
47 .defaultHelp(true)
48 .description("Commandline interface for TextSecure.");
49 Subparsers subparsers = parser.addSubparsers()
50 .title("subcommands")
51 .dest("command")
52 .description("valid subcommands")
53 .help("additional help");
54 Subparser parserRegister = subparsers.addParser("register");
55 parserRegister.addArgument("-v", "--voice")
56 .help("The verification should be done over voice, not sms.")
57 .action(Arguments.storeTrue());
58 Subparser parserVerify = subparsers.addParser("verify");
59 parserVerify.addArgument("verificationCode")
60 .help("The verification code you received via sms or voice call.");
61 Subparser parserSend = subparsers.addParser("send");
62 parserSend.addArgument("recipient")
63 .help("Specify the recipients' phone number.")
64 .nargs("*");
65 parserSend.addArgument("-m", "--message")
66 .help("Specify the message, if missing standard input is used.");
67 parserSend.addArgument("-a", "--attachment")
68 .nargs("*")
69 .help("Add file as attachment");
70 Subparser parserReceive = subparsers.addParser("receive");
71 parser.addArgument("-u", "--username")
72 .required(true)
73 .help("Specify your phone number, that will be used for verification.");
74 Namespace ns = null;
75 try {
76 ns = parser.parseArgs(args);
77 } catch (ArgumentParserException e) {
78 parser.handleError(e);
79 System.exit(1);
80 }
81
82 final String username = ns.getString("username");
83 final Manager m = new Manager(username);
84 if (m.userExists()) {
85 try {
86 m.load();
87 } catch (Exception e) {
88 System.out.println("Loading file error: " + e.getMessage());
89 System.exit(2);
90 }
91 }
92 switch (ns.getString("command")) {
93 case "register":
94 if (!m.userHasKeys()) {
95 m.createNewIdentity();
96 }
97 try {
98 m.register(ns.getBoolean("voice"));
99 } catch (IOException e) {
100 System.out.println("Request verify error: " + e.getMessage());
101 System.exit(3);
102 }
103 break;
104 case "verify":
105 if (!m.userHasKeys()) {
106 System.out.println("User has no keys, first call register.");
107 System.exit(1);
108 }
109 if (m.isRegistered()) {
110 System.out.println("User registration is already verified");
111 System.exit(1);
112 }
113 try {
114 m.verifyAccount(ns.getString("verificationCode"));
115 } catch (IOException e) {
116 System.out.println("Verify error: " + e.getMessage());
117 System.exit(3);
118 }
119 break;
120 case "send":
121 if (!m.isRegistered()) {
122 System.out.println("User is not registered.");
123 System.exit(1);
124 }
125 TextSecureMessageSender messageSender = m.getMessageSender();
126 String messageText = ns.getString("message");
127 if (messageText == null) {
128 try {
129 messageText = IOUtils.toString(System.in);
130 } catch (IOException e) {
131 System.out.println("Failed to read message from stdin: " + e.getMessage());
132 System.exit(1);
133 }
134 }
135 final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
136 final List<String> attachments = ns.<String>getList("attachment");
137 if (attachments != null) {
138 List<TextSecureAttachment> textSecureAttachments = new ArrayList<TextSecureAttachment>(attachments.size());
139 for (String attachment : attachments) {
140 try {
141 File attachmentFile = new File(attachment);
142 InputStream attachmentStream = new FileInputStream(attachmentFile);
143 final long attachmentSize = attachmentFile.length();
144 String mime = Files.probeContentType(Paths.get(attachment));
145 textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null));
146 } catch (IOException e) {
147 System.out.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
148 System.exit(1);
149 }
150 }
151 messageBuilder.withAttachments(textSecureAttachments);
152 }
153 TextSecureDataMessage message = messageBuilder.build();
154
155 List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
156 for (String recipient : ns.<String>getList("recipient")) {
157 try {
158 recipients.add(m.getPushAddress(recipient));
159 } catch (InvalidNumberException e) {
160 System.out.println("Failed to send message to \"" + recipient + "\": " + e.getMessage());
161 }
162 }
163 try {
164 messageSender.sendMessage(recipients, message);
165 } catch (IOException | EncapsulatedExceptions e) {
166 System.out.println("Failed to send message: " + e.getMessage());
167 }
168 break;
169 case "receive":
170 if (!m.isRegistered()) {
171 System.out.println("User is not registered.");
172 System.exit(1);
173 }
174 try {
175 m.receiveMessages(new Manager.ReceiveMessageHandler() {
176 @Override
177 public void handleMessage(TextSecureEnvelope envelope) {
178 System.out.println("Envelope from: " + envelope.getSource());
179 System.out.println("Timestamp: " + envelope.getTimestamp());
180
181 if (envelope.isReceipt()) {
182 System.out.println("Got receipt.");
183 } else if (envelope.isWhisperMessage() | envelope.isPreKeyWhisperMessage()) {
184 TextSecureContent content = m.decryptMessage(envelope);
185
186 if (content == null) {
187 System.out.println("Failed to decrypt message.");
188 } else {
189 if (content.getDataMessage().isPresent()) {
190 TextSecureDataMessage message = content.getDataMessage().get();
191
192 System.out.println("Body: " + message.getBody().get());
193 if (message.getAttachments().isPresent()) {
194 System.out.println("Attachments: ");
195 for (TextSecureAttachment attachment : message.getAttachments().get()) {
196 System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");
197 if (attachment.isPointer()) {
198 System.out.println(" Id: " + attachment.asPointer().getId() + " Key length: " + attachment.asPointer().getKey().length + (attachment.asPointer().getRelay().isPresent() ? " Relay: " + attachment.asPointer().getRelay().get() : ""));
199 }
200 }
201 }
202 }
203 if (content.getSyncMessage().isPresent()) {
204 TextSecureSyncMessage syncMessage = content.getSyncMessage().get();
205 System.out.println("Received sync message");
206 }
207 }
208 } else {
209 System.out.println("Unknown message received.");
210 }
211 System.out.println();
212 }
213 });
214 } catch (IOException e) {
215 System.out.println("Error while receiving message: " + e.getMessage());
216 }
217 break;
218 }
219 m.save();
220 }
221 }