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
.inf
.*;
21 import org
.apache
.commons
.io
.IOUtils
;
22 import org
.whispersystems
.libaxolotl
.InvalidVersionException
;
23 import org
.whispersystems
.textsecure
.api
.TextSecureMessageSender
;
24 import org
.whispersystems
.textsecure
.api
.crypto
.UntrustedIdentityException
;
25 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureContent
;
26 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureDataMessage
;
27 import org
.whispersystems
.textsecure
.api
.messages
.multidevice
.TextSecureSyncMessage
;
28 import org
.whispersystems
.textsecure
.api
.push
.TextSecureAddress
;
30 import java
.io
.IOException
;
31 import java
.security
.Security
;
35 public static void main(String
[] args
) {
36 // Workaround for BKS truststore
37 Security
.insertProviderAt(new org
.spongycastle
.jce
.provider
.BouncyCastleProvider(), 1);
39 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("textsecure-cli")
41 .description("Commandline interface for TextSecure.");
42 Subparsers subparsers
= parser
.addSubparsers()
45 .description("valid subcommands")
46 .help("additional help");
47 Subparser parserRegister
= subparsers
.addParser("register");
48 Subparser parserVerify
= subparsers
.addParser("verify");
49 parserVerify
.addArgument("verificationCode")
50 .help("The verification code you received via sms.");
51 Subparser parserSend
= subparsers
.addParser("send");
52 parserSend
.addArgument("recipient")
53 .help("Specify the recipients' phone number.")
55 parserSend
.addArgument("-m", "--message")
56 .help("Specify the message, if missing standard input is used.");
57 Subparser parserReceive
= subparsers
.addParser("receive");
58 parser
.addArgument("-u", "--username")
60 .help("Specify your phone number, that will be used for verification.");
63 ns
= parser
.parseArgs(args
);
64 } catch (ArgumentParserException e
) {
65 parser
.handleError(e
);
69 String username
= ns
.getString("username");
70 Manager m
= new Manager(username
);
74 } catch (Exception e
) {
75 System
.out
.println("Loading file error: " + e
.getMessage());
79 switch (ns
.getString("command")) {
81 if (!m
.userHasKeys()) {
82 m
.createNewIdentity();
86 } catch (IOException e
) {
87 System
.out
.println("Request verify error: " + e
.getMessage());
92 if (!m
.userHasKeys()) {
93 System
.out
.println("User has no keys, first call register.");
96 if (m
.isRegistered()) {
97 System
.out
.println("User registration is already verified");
101 m
.verifyAccount(ns
.getString("verificationCode"));
102 } catch (IOException e
) {
103 System
.out
.println("Verify error: " + e
.getMessage());
108 if (!m
.isRegistered()) {
109 System
.out
.println("User is not registered.");
112 TextSecureMessageSender messageSender
= m
.getMessageSender();
113 String messageText
= ns
.getString("message");
114 if (messageText
== null) {
116 messageText
= IOUtils
.toString(System
.in);
117 } catch (IOException e
) {
118 System
.out
.println("Failed to read message from stdin: " + e
.getMessage());
122 TextSecureDataMessage message
= TextSecureDataMessage
.newBuilder().withBody(messageText
).build();
123 for (String recipient
: ns
.<String
>getList("recipient")) {
125 messageSender
.sendMessage(new TextSecureAddress(recipient
), message
);
126 } catch (UntrustedIdentityException
| IOException e
) {
127 System
.out
.println("Send message: " + e
.getMessage());
132 if (!m
.isRegistered()) {
133 System
.out
.println("User is not registered.");
137 TextSecureContent content
= m
.receiveMessage();
138 if (content
.getDataMessage().isPresent()) {
139 message
= content
.getDataMessage().get();
140 if (message
== null) {
143 System
.out
.println("Received message: " + message
.getBody().get());
146 if (content
.getSyncMessage().isPresent()) {
147 TextSecureSyncMessage syncMessage
= content
.getSyncMessage().get();
149 if (syncMessage
== null) {
152 System
.out
.println("Received sync message");
155 } catch (IOException
| InvalidVersionException e
) {
156 System
.out
.println("Receive message: " + e
.getMessage());