2 * Copyright (C) 2015 AsamK
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.
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.
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/>.
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
.InvalidVersionException
;
24 import org
.whispersystems
.textsecure
.api
.TextSecureMessageSender
;
25 import org
.whispersystems
.textsecure
.api
.crypto
.UntrustedIdentityException
;
26 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureContent
;
27 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureDataMessage
;
28 import org
.whispersystems
.textsecure
.api
.messages
.multidevice
.TextSecureSyncMessage
;
29 import org
.whispersystems
.textsecure
.api
.push
.TextSecureAddress
;
31 import java
.io
.IOException
;
32 import java
.security
.Security
;
36 public static void main(String
[] args
) {
37 // Workaround for BKS truststore
38 Security
.insertProviderAt(new org
.spongycastle
.jce
.provider
.BouncyCastleProvider(), 1);
40 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("textsecure-cli")
42 .description("Commandline interface for TextSecure.");
43 Subparsers subparsers
= parser
.addSubparsers()
46 .description("valid subcommands")
47 .help("additional help");
48 Subparser parserRegister
= subparsers
.addParser("register");
49 parserRegister
.addArgument("-v", "--voice")
50 .help("The verification should be done over voice, not sms.")
51 .action(Arguments
.storeTrue());
52 Subparser parserVerify
= subparsers
.addParser("verify");
53 parserVerify
.addArgument("verificationCode")
54 .help("The verification code you received via sms or voice call.");
55 Subparser parserSend
= subparsers
.addParser("send");
56 parserSend
.addArgument("recipient")
57 .help("Specify the recipients' phone number.")
59 parserSend
.addArgument("-m", "--message")
60 .help("Specify the message, if missing standard input is used.");
61 Subparser parserReceive
= subparsers
.addParser("receive");
62 parser
.addArgument("-u", "--username")
64 .help("Specify your phone number, that will be used for verification.");
67 ns
= parser
.parseArgs(args
);
68 } catch (ArgumentParserException e
) {
69 parser
.handleError(e
);
73 String username
= ns
.getString("username");
74 Manager m
= new Manager(username
);
78 } catch (Exception e
) {
79 System
.out
.println("Loading file error: " + e
.getMessage());
83 switch (ns
.getString("command")) {
85 if (!m
.userHasKeys()) {
86 m
.createNewIdentity();
89 m
.register(ns
.getBoolean("voice"));
90 } catch (IOException e
) {
91 System
.out
.println("Request verify error: " + e
.getMessage());
96 if (!m
.userHasKeys()) {
97 System
.out
.println("User has no keys, first call register.");
100 if (m
.isRegistered()) {
101 System
.out
.println("User registration is already verified");
105 m
.verifyAccount(ns
.getString("verificationCode"));
106 } catch (IOException e
) {
107 System
.out
.println("Verify error: " + e
.getMessage());
112 if (!m
.isRegistered()) {
113 System
.out
.println("User is not registered.");
116 TextSecureMessageSender messageSender
= m
.getMessageSender();
117 String messageText
= ns
.getString("message");
118 if (messageText
== null) {
120 messageText
= IOUtils
.toString(System
.in);
121 } catch (IOException e
) {
122 System
.out
.println("Failed to read message from stdin: " + e
.getMessage());
126 TextSecureDataMessage message
= TextSecureDataMessage
.newBuilder().withBody(messageText
).build();
127 for (String recipient
: ns
.<String
>getList("recipient")) {
129 messageSender
.sendMessage(new TextSecureAddress(recipient
), message
);
130 } catch (UntrustedIdentityException
| IOException e
) {
131 System
.out
.println("Send message: " + e
.getMessage());
136 if (!m
.isRegistered()) {
137 System
.out
.println("User is not registered.");
141 TextSecureContent content
= m
.receiveMessage();
142 if (content
.getDataMessage().isPresent()) {
143 message
= content
.getDataMessage().get();
144 if (message
== null) {
147 System
.out
.println("Received message: " + message
.getBody().get());
150 if (content
.getSyncMessage().isPresent()) {
151 TextSecureSyncMessage syncMessage
= content
.getSyncMessage().get();
153 if (syncMessage
== null) {
156 System
.out
.println("Received sync message");
159 } catch (IOException
| InvalidVersionException e
) {
160 System
.out
.println("Receive message: " + e
.getMessage());