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
.TextSecureMessage
;
26 import org
.whispersystems
.textsecure
.api
.push
.TextSecureAddress
;
28 import java
.io
.IOException
;
29 import java
.security
.Security
;
33 public static void main(String
[] args
) {
34 // Workaround for BKS truststore
35 Security
.insertProviderAt(new org
.spongycastle
.jce
.provider
.BouncyCastleProvider(), 1);
37 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("textsecure-cli")
39 .description("Commandline interface for TextSecure.");
40 Subparsers subparsers
= parser
.addSubparsers()
43 .description("valid subcommands")
44 .help("additional help");
45 Subparser parserRegister
= subparsers
.addParser("register");
46 Subparser parserVerify
= subparsers
.addParser("verify");
47 parserVerify
.addArgument("verificationCode")
48 .help("The verification code you received via sms.");
49 Subparser parserSend
= subparsers
.addParser("send");
50 parserSend
.addArgument("recipient")
51 .help("Specify the recipients' phone number.")
53 parserSend
.addArgument("-m", "--message")
54 .help("Specify the message, if missing standard input is used.");
55 Subparser parserReceive
= subparsers
.addParser("receive");
56 parser
.addArgument("-u", "--username")
58 .help("Specify your phone number, that will be used for verification.");
61 ns
= parser
.parseArgs(args
);
62 } catch (ArgumentParserException e
) {
63 parser
.handleError(e
);
67 String username
= ns
.getString("username");
68 Manager m
= new Manager(username
);
72 } catch (Exception e
) {
73 System
.out
.println("Loading file error: " + e
.getMessage());
77 switch (ns
.getString("command")) {
79 if (!m
.userHasKeys()) {
80 m
.createNewIdentity();
84 } catch (IOException e
) {
85 System
.out
.println("Request verify error: " + e
.getMessage());
90 if (!m
.userHasKeys()) {
91 System
.out
.println("User has no keys, first call register.");
94 if (m
.isRegistered()) {
95 System
.out
.println("User registration is already verified");
99 m
.verifyAccount(ns
.getString("verificationCode"));
100 } catch (IOException e
) {
101 System
.out
.println("Verify error: " + e
.getMessage());
106 if (!m
.isRegistered()) {
107 System
.out
.println("User is not registered.");
110 TextSecureMessageSender messageSender
= m
.getMessageSender();
111 String messageText
= ns
.getString("message");
112 if (messageText
== null) {
114 messageText
= IOUtils
.toString(System
.in);
115 } catch (IOException e
) {
116 System
.out
.println("Failed to read message from stdin: " + e
.getMessage());
120 TextSecureMessage message
= TextSecureMessage
.newBuilder().withBody(messageText
).build();
121 for (String recipient
: ns
.<String
>getList("recipient")) {
123 messageSender
.sendMessage(new TextSecureAddress(recipient
), message
);
124 } catch (UntrustedIdentityException
| IOException e
) {
125 System
.out
.println("Send message: " + e
.getMessage());
130 if (!m
.isRegistered()) {
131 System
.out
.println("User is not registered.");
135 message
= m
.receiveMessage();
136 if (message
== null) {
139 System
.out
.println("Received message: " + message
.getBody().get());
141 } catch (IOException
| InvalidVersionException e
) {
142 System
.out
.println("Receive message: " + e
.getMessage());