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
.TextSecureAttachment
;
27 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureAttachmentStream
;
28 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureContent
;
29 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureDataMessage
;
30 import org
.whispersystems
.textsecure
.api
.messages
.multidevice
.TextSecureSyncMessage
;
31 import org
.whispersystems
.textsecure
.api
.push
.TextSecureAddress
;
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
;
45 public static void main(String
[] args
) {
46 // Workaround for BKS truststore
47 Security
.insertProviderAt(new org
.spongycastle
.jce
.provider
.BouncyCastleProvider(), 1);
49 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("textsecure-cli")
51 .description("Commandline interface for TextSecure.");
52 Subparsers subparsers
= parser
.addSubparsers()
55 .description("valid subcommands")
56 .help("additional help");
57 Subparser parserRegister
= subparsers
.addParser("register");
58 parserRegister
.addArgument("-v", "--voice")
59 .help("The verification should be done over voice, not sms.")
60 .action(Arguments
.storeTrue());
61 Subparser parserVerify
= subparsers
.addParser("verify");
62 parserVerify
.addArgument("verificationCode")
63 .help("The verification code you received via sms or voice call.");
64 Subparser parserSend
= subparsers
.addParser("send");
65 parserSend
.addArgument("recipient")
66 .help("Specify the recipients' phone number.")
68 parserSend
.addArgument("-m", "--message")
69 .help("Specify the message, if missing standard input is used.");
70 parserSend
.addArgument("-a", "--attachment")
72 .help("Add file as attachment");
73 Subparser parserReceive
= subparsers
.addParser("receive");
74 parser
.addArgument("-u", "--username")
76 .help("Specify your phone number, that will be used for verification.");
79 ns
= parser
.parseArgs(args
);
80 } catch (ArgumentParserException e
) {
81 parser
.handleError(e
);
85 String username
= ns
.getString("username");
86 Manager m
= new Manager(username
);
90 } catch (Exception e
) {
91 System
.out
.println("Loading file error: " + e
.getMessage());
95 switch (ns
.getString("command")) {
97 if (!m
.userHasKeys()) {
98 m
.createNewIdentity();
101 m
.register(ns
.getBoolean("voice"));
102 } catch (IOException e
) {
103 System
.out
.println("Request verify error: " + e
.getMessage());
108 if (!m
.userHasKeys()) {
109 System
.out
.println("User has no keys, first call register.");
112 if (m
.isRegistered()) {
113 System
.out
.println("User registration is already verified");
117 m
.verifyAccount(ns
.getString("verificationCode"));
118 } catch (IOException e
) {
119 System
.out
.println("Verify error: " + e
.getMessage());
124 if (!m
.isRegistered()) {
125 System
.out
.println("User is not registered.");
128 TextSecureMessageSender messageSender
= m
.getMessageSender();
129 String messageText
= ns
.getString("message");
130 if (messageText
== null) {
132 messageText
= IOUtils
.toString(System
.in);
133 } catch (IOException e
) {
134 System
.out
.println("Failed to read message from stdin: " + e
.getMessage());
138 final TextSecureDataMessage
.Builder messageBuilder
= TextSecureDataMessage
.newBuilder().withBody(messageText
);
139 final List
<String
> attachments
= ns
.<String
>getList("attachment");
140 if (attachments
!= null) {
141 List
<TextSecureAttachment
> textSecureAttachments
= new ArrayList
<TextSecureAttachment
>(attachments
.size());
142 for (String attachment
: attachments
) {
144 File attachmentFile
= new File(attachment
);
145 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
146 final long attachmentSize
= attachmentFile
.length();
147 String mime
= Files
.probeContentType(Paths
.get(attachment
));
148 textSecureAttachments
.add(new TextSecureAttachmentStream(attachmentStream
, mime
, attachmentSize
, null));
149 } catch (IOException e
) {
150 System
.out
.println("Failed to add attachment \"" + attachment
+ "\": " + e
.getMessage());
154 messageBuilder
.withAttachments(textSecureAttachments
);
156 TextSecureDataMessage message
= messageBuilder
.build();
157 for (String recipient
: ns
.<String
>getList("recipient")) {
159 messageSender
.sendMessage(new TextSecureAddress(recipient
), message
);
160 } catch (UntrustedIdentityException
| IOException e
) {
161 System
.out
.println("Send message: " + e
.getMessage());
166 if (!m
.isRegistered()) {
167 System
.out
.println("User is not registered.");
171 TextSecureContent content
= m
.receiveMessage();
172 if (content
.getDataMessage().isPresent()) {
173 message
= content
.getDataMessage().get();
174 if (message
== null) {
177 System
.out
.println("Received message: " + message
.getBody().get());
180 if (content
.getSyncMessage().isPresent()) {
181 TextSecureSyncMessage syncMessage
= content
.getSyncMessage().get();
183 if (syncMessage
== null) {
186 System
.out
.println("Received sync message");
189 } catch (IOException
| InvalidVersionException e
) {
190 System
.out
.println("Receive message: " + e
.getMessage());