]> nmode's Git Repositories - signal-cli/blob - src/main/java/cli/Main.java
Enable sending to groups
[signal-cli] / src / main / java / cli / Main.java
1 /**
2 * Copyright (C) 2015 AsamK
3 *
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 *
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 *
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.libaxolotl.InvalidMessageException;
24 import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
25 import org.whispersystems.textsecure.api.messages.*;
26 import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
27 import org.whispersystems.textsecure.api.push.TextSecureAddress;
28 import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
29 import org.whispersystems.textsecure.api.push.exceptions.NetworkFailureException;
30 import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException;
31 import org.whispersystems.textsecure.api.util.InvalidNumberException;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.nio.file.Files;
38 import java.nio.file.Paths;
39 import java.security.Security;
40 import java.util.ArrayList;
41 import java.util.List;
42
43 public class Main {
44
45 public static void main(String[] args) {
46 // Workaround for BKS truststore
47 Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
48
49 Namespace ns = parseArgs(args);
50 if (ns == null) {
51 System.exit(1);
52 }
53
54 final String username = ns.getString("username");
55 final Manager m = new Manager(username);
56 if (m.userExists()) {
57 try {
58 m.load();
59 } catch (Exception e) {
60 System.err.println("Error loading state file \"" + m.getFileName() + "\": " + e.getMessage());
61 System.exit(2);
62 }
63 }
64
65 switch (ns.getString("command")) {
66 case "register":
67 if (!m.userHasKeys()) {
68 m.createNewIdentity();
69 }
70 try {
71 m.register(ns.getBoolean("voice"));
72 } catch (IOException e) {
73 System.err.println("Request verify error: " + e.getMessage());
74 System.exit(3);
75 }
76 break;
77 case "verify":
78 if (!m.userHasKeys()) {
79 System.err.println("User has no keys, first call register.");
80 System.exit(1);
81 }
82 if (m.isRegistered()) {
83 System.err.println("User registration is already verified");
84 System.exit(1);
85 }
86 try {
87 m.verifyAccount(ns.getString("verificationCode"));
88 } catch (IOException e) {
89 System.err.println("Verify error: " + e.getMessage());
90 System.exit(3);
91 }
92 break;
93 case "send":
94 if (!m.isRegistered()) {
95 System.err.println("User is not registered.");
96 System.exit(1);
97 }
98 String messageText = ns.getString("message");
99 if (messageText == null) {
100 try {
101 messageText = IOUtils.toString(System.in);
102 } catch (IOException e) {
103 System.err.println("Failed to read message from stdin: " + e.getMessage());
104 System.err.println("Aborting sending.");
105 System.exit(1);
106 }
107 }
108
109 final List<String> attachments = ns.getList("attachment");
110 List<TextSecureAttachment> textSecureAttachments = null;
111 if (attachments != null) {
112 textSecureAttachments = new ArrayList<>(attachments.size());
113 for (String attachment : attachments) {
114 try {
115 File attachmentFile = new File(attachment);
116 InputStream attachmentStream = new FileInputStream(attachmentFile);
117 final long attachmentSize = attachmentFile.length();
118 String mime = Files.probeContentType(Paths.get(attachment));
119 textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null));
120 } catch (IOException e) {
121 System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
122 System.err.println("Aborting sending.");
123 System.exit(1);
124 }
125 }
126 }
127 TextSecureGroup group = null;
128 List<String> recipientStrings = null;
129 if (ns.getString("group") != null) {
130 try {
131 GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group")));
132 if (g == null) {
133 System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": Unknown group");
134 System.err.println("Aborting sending.");
135 System.exit(1);
136 }
137 group = new TextSecureGroup(g.groupId);
138 recipientStrings = g.members;
139 } catch (IOException e) {
140 System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage());
141 System.err.println("Aborting sending.");
142 System.exit(1);
143 }
144 } else {
145 recipientStrings = ns.<String>getList("recipient");
146 }
147
148 List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
149 for (String recipient : recipientStrings) {
150 try {
151 recipients.add(m.getPushAddress(recipient));
152 } catch (InvalidNumberException e) {
153 System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
154 System.err.println("Aborting sending.");
155 System.exit(1);
156 }
157 }
158
159 sendMessage(m, messageText, textSecureAttachments, recipients, group);
160 break;
161 case "receive":
162 if (!m.isRegistered()) {
163 System.err.println("User is not registered.");
164 System.exit(1);
165 }
166 int timeout = 5;
167 if (ns.getInt("timeout") != null) {
168 timeout = ns.getInt("timeout");
169 }
170 boolean returnOnTimeout = true;
171 if (timeout < 0) {
172 returnOnTimeout = false;
173 timeout = 3600;
174 }
175 try {
176 m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
177 } catch (IOException e) {
178 System.err.println("Error while receiving message: " + e.getMessage());
179 System.exit(3);
180 } catch (AssertionError e) {
181 System.err.println("Failed to receive message (Assertion): " + e.getMessage());
182 System.err.println(e.getStackTrace());
183 System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
184 System.exit(1);
185 }
186 break;
187 }
188 m.save();
189 System.exit(0);
190 }
191
192 private static Namespace parseArgs(String[] args) {
193 ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
194 .defaultHelp(true)
195 .description("Commandline interface for TextSecure.")
196 .version(Manager.PROJECT_NAME + " " + Manager.PROJECT_VERSION);
197
198 parser.addArgument("-u", "--username")
199 .help("Specify your phone number, that will be used for verification.");
200 parser.addArgument("-v", "--version")
201 .help("Show package version.")
202 .action(Arguments.version());
203
204 Subparsers subparsers = parser.addSubparsers()
205 .title("subcommands")
206 .dest("command")
207 .description("valid subcommands")
208 .help("additional help");
209
210 Subparser parserRegister = subparsers.addParser("register");
211 parserRegister.addArgument("-v", "--voice")
212 .help("The verification should be done over voice, not sms.")
213 .action(Arguments.storeTrue());
214
215 Subparser parserVerify = subparsers.addParser("verify");
216 parserVerify.addArgument("verificationCode")
217 .help("The verification code you received via sms or voice call.");
218
219 Subparser parserSend = subparsers.addParser("send");
220 parserSend.addArgument("-g", "--group")
221 .help("Specify the recipient group ID.");
222 parserSend.addArgument("recipient")
223 .help("Specify the recipients' phone number.")
224 .nargs("*");
225 parserSend.addArgument("-m", "--message")
226 .help("Specify the message, if missing standard input is used.");
227 parserSend.addArgument("-a", "--attachment")
228 .nargs("*")
229 .help("Add file as attachment");
230
231 Subparser parserReceive = subparsers.addParser("receive");
232 parserReceive.addArgument("-t", "--timeout")
233 .type(int.class)
234 .help("Number of seconds to wait for new messages (negative values disable timeout)");
235
236 try {
237 Namespace ns = parser.parseArgs(args);
238 if (ns.getString("username") == null) {
239 parser.printUsage();
240 System.err.println("You need to specify a username (phone number)");
241 System.exit(2);
242 }
243 if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
244 System.err.println("You cannot specify recipients by phone number and groups a the same time");
245 System.exit(2);
246 }
247 return ns;
248 } catch (ArgumentParserException e) {
249 parser.handleError(e);
250 return null;
251 }
252 }
253
254 private static void sendMessage(Manager m, String messageText, List<TextSecureAttachment> textSecureAttachments,
255 List<TextSecureAddress> recipients, TextSecureGroup group) {
256 final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
257 if (textSecureAttachments != null) {
258 messageBuilder.withAttachments(textSecureAttachments);
259 }
260 if (group != null) {
261 messageBuilder.asGroupMessage(group);
262 }
263 TextSecureDataMessage message = messageBuilder.build();
264
265 try {
266 m.sendMessage(recipients, message);
267 } catch (IOException e) {
268 System.err.println("Failed to send message: " + e.getMessage());
269 } catch (EncapsulatedExceptions e) {
270 System.err.println("Failed to send (some) messages:");
271 for (NetworkFailureException n : e.getNetworkExceptions()) {
272 System.err.println("Network failure for \"" + n.getE164number() + "\": " + n.getMessage());
273 }
274 for (UnregisteredUserException n : e.getUnregisteredUserExceptions()) {
275 System.err.println("Unregistered user \"" + n.getE164Number() + "\": " + n.getMessage());
276 }
277 for (UntrustedIdentityException n : e.getUntrustedIdentityExceptions()) {
278 System.err.println("Untrusted Identity for \"" + n.getE164Number() + "\": " + n.getMessage());
279 }
280 } catch (AssertionError e) {
281 System.err.println("Failed to send message (Assertion): " + e.getMessage());
282 System.err.println(e.getStackTrace());
283 System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
284 System.exit(1);
285 }
286 }
287
288 private static class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
289 final Manager m;
290
291 public ReceiveMessageHandler(Manager m) {
292 this.m = m;
293 }
294
295 @Override
296 public void handleMessage(TextSecureEnvelope envelope, TextSecureContent content, GroupInfo group) {
297 System.out.println("Envelope from: " + envelope.getSource());
298 System.out.println("Timestamp: " + envelope.getTimestamp());
299
300 if (envelope.isReceipt()) {
301 System.out.println("Got receipt.");
302 } else if (envelope.isWhisperMessage() | envelope.isPreKeyWhisperMessage()) {
303 if (content == null) {
304 System.out.println("Failed to decrypt message.");
305 } else {
306 if (content.getDataMessage().isPresent()) {
307 TextSecureDataMessage message = content.getDataMessage().get();
308
309 System.out.println("Message timestamp: " + message.getTimestamp());
310
311 if (message.getBody().isPresent()) {
312 System.out.println("Body: " + message.getBody().get());
313 }
314 if (message.getGroupInfo().isPresent()) {
315 TextSecureGroup groupInfo = message.getGroupInfo().get();
316 System.out.println("Group info:");
317 System.out.println(" Id: " + Base64.encodeBytes(groupInfo.getGroupId()));
318 if (groupInfo.getName().isPresent()) {
319 System.out.println(" Name: " + groupInfo.getName().get());
320 } else if (group != null) {
321 System.out.println(" Name: " + group.name);
322 } else {
323 System.out.println(" Name: <Unknown group>");
324 }
325 System.out.println(" Type: " + groupInfo.getType());
326 if (groupInfo.getMembers().isPresent()) {
327 for (String member : groupInfo.getMembers().get()) {
328 System.out.println(" Member: " + member);
329 }
330 }
331 if (groupInfo.getAvatar().isPresent()) {
332 System.out.println(" Avatar:");
333 printAttachment(groupInfo.getAvatar().get());
334 }
335 }
336 if (message.isEndSession()) {
337 System.out.println("Is end session");
338 m.handleEndSession(envelope.getSource());
339 }
340
341 if (message.getAttachments().isPresent()) {
342 System.out.println("Attachments: ");
343 for (TextSecureAttachment attachment : message.getAttachments().get()) {
344 printAttachment(attachment);
345 }
346 }
347 }
348 if (content.getSyncMessage().isPresent()) {
349 TextSecureSyncMessage syncMessage = content.getSyncMessage().get();
350 System.out.println("Received sync message");
351 }
352 }
353 } else {
354 System.out.println("Unknown message received.");
355 }
356 System.out.println();
357 }
358
359 private void printAttachment(TextSecureAttachment attachment) {
360 System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");
361 if (attachment.isPointer()) {
362 final TextSecureAttachmentPointer pointer = attachment.asPointer();
363 System.out.println(" Id: " + pointer.getId() + " Key length: " + pointer.getKey().length + (pointer.getRelay().isPresent() ? " Relay: " + pointer.getRelay().get() : ""));
364 System.out.println(" Size: " + (pointer.getSize().isPresent() ? pointer.getSize().get() + " bytes" : "<unavailable>") + (pointer.getPreview().isPresent() ? " (Preview is available: " + pointer.getPreview().get().length + " bytes)" : ""));
365 try {
366 File file = m.retrieveAttachment(pointer);
367 System.out.println(" Stored plaintext in: " + file);
368 } catch (IOException | InvalidMessageException e) {
369 System.out.println("Failed to retrieve attachment: " + e.getMessage());
370 }
371 }
372 }
373 }
374 }