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
.textsecure
.api
.TextSecureMessageSender
;
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
;
30 import java
.io
.FileInputStream
;
31 import java
.io
.IOException
;
32 import java
.io
.InputStream
;
33 import java
.nio
.file
.Files
;
34 import java
.nio
.file
.Paths
;
35 import java
.security
.Security
;
36 import java
.util
.ArrayList
;
37 import java
.util
.List
;
41 public static void main(String
[] args
) {
42 // Workaround for BKS truststore
43 Security
.insertProviderAt(new org
.spongycastle
.jce
.provider
.BouncyCastleProvider(), 1);
45 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("textsecure-cli")
47 .description("Commandline interface for TextSecure.");
48 Subparsers subparsers
= parser
.addSubparsers()
51 .description("valid subcommands")
52 .help("additional help");
53 Subparser parserRegister
= subparsers
.addParser("register");
54 parserRegister
.addArgument("-v", "--voice")
55 .help("The verification should be done over voice, not sms.")
56 .action(Arguments
.storeTrue());
57 Subparser parserVerify
= subparsers
.addParser("verify");
58 parserVerify
.addArgument("verificationCode")
59 .help("The verification code you received via sms or voice call.");
60 Subparser parserSend
= subparsers
.addParser("send");
61 parserSend
.addArgument("recipient")
62 .help("Specify the recipients' phone number.")
64 parserSend
.addArgument("-m", "--message")
65 .help("Specify the message, if missing standard input is used.");
66 parserSend
.addArgument("-a", "--attachment")
68 .help("Add file as attachment");
69 Subparser parserReceive
= subparsers
.addParser("receive");
70 parser
.addArgument("-u", "--username")
72 .help("Specify your phone number, that will be used for verification.");
75 ns
= parser
.parseArgs(args
);
76 } catch (ArgumentParserException e
) {
77 parser
.handleError(e
);
81 final String username
= ns
.getString("username");
82 final Manager m
= new Manager(username
);
86 } catch (Exception e
) {
87 System
.out
.println("Loading file error: " + e
.getMessage());
91 switch (ns
.getString("command")) {
93 if (!m
.userHasKeys()) {
94 m
.createNewIdentity();
97 m
.register(ns
.getBoolean("voice"));
98 } catch (IOException e
) {
99 System
.out
.println("Request verify error: " + e
.getMessage());
104 if (!m
.userHasKeys()) {
105 System
.out
.println("User has no keys, first call register.");
108 if (m
.isRegistered()) {
109 System
.out
.println("User registration is already verified");
113 m
.verifyAccount(ns
.getString("verificationCode"));
114 } catch (IOException e
) {
115 System
.out
.println("Verify error: " + e
.getMessage());
120 if (!m
.isRegistered()) {
121 System
.out
.println("User is not registered.");
124 TextSecureMessageSender messageSender
= m
.getMessageSender();
125 String messageText
= ns
.getString("message");
126 if (messageText
== null) {
128 messageText
= IOUtils
.toString(System
.in);
129 } catch (IOException e
) {
130 System
.out
.println("Failed to read message from stdin: " + e
.getMessage());
134 final TextSecureDataMessage
.Builder messageBuilder
= TextSecureDataMessage
.newBuilder().withBody(messageText
);
135 final List
<String
> attachments
= ns
.<String
>getList("attachment");
136 if (attachments
!= null) {
137 List
<TextSecureAttachment
> textSecureAttachments
= new ArrayList
<TextSecureAttachment
>(attachments
.size());
138 for (String attachment
: attachments
) {
140 File attachmentFile
= new File(attachment
);
141 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
142 final long attachmentSize
= attachmentFile
.length();
143 String mime
= Files
.probeContentType(Paths
.get(attachment
));
144 textSecureAttachments
.add(new TextSecureAttachmentStream(attachmentStream
, mime
, attachmentSize
, null));
145 } catch (IOException e
) {
146 System
.out
.println("Failed to add attachment \"" + attachment
+ "\": " + e
.getMessage());
150 messageBuilder
.withAttachments(textSecureAttachments
);
152 TextSecureDataMessage message
= messageBuilder
.build();
153 for (String recipient
: ns
.<String
>getList("recipient")) {
155 messageSender
.sendMessage(new TextSecureAddress(recipient
), message
);
156 } catch (UntrustedIdentityException
| IOException e
) {
157 System
.out
.println("Send message: " + e
.getMessage());
162 if (!m
.isRegistered()) {
163 System
.out
.println("User is not registered.");
167 m
.receiveMessages(new Manager
.ReceiveMessageHandler() {
169 public void handleMessage(TextSecureEnvelope envelope
) {
170 System
.out
.println("Envelope from: " + envelope
.getSource());
171 System
.out
.println("Timestamp: " + envelope
.getTimestamp());
173 if (envelope
.isReceipt()) {
174 System
.out
.println("Got receipt.");
175 } else if (envelope
.isWhisperMessage() | envelope
.isPreKeyWhisperMessage()) {
176 TextSecureContent content
= m
.decryptMessage(envelope
);
178 if (content
== null) {
179 System
.out
.println("Failed to decrypt message.");
181 if (content
.getDataMessage().isPresent()) {
182 TextSecureDataMessage message
= content
.getDataMessage().get();
184 System
.out
.println("Body: " + message
.getBody().get());
185 if (message
.getAttachments().isPresent()) {
186 System
.out
.println("Attachments: ");
187 for (TextSecureAttachment attachment
: message
.getAttachments().get()) {
188 System
.out
.println("- " + attachment
.getContentType() + " (" + (attachment
.isPointer() ?
"Pointer" : "") + (attachment
.isStream() ?
"Stream" : "") + ")");
189 if (attachment
.isPointer()) {
190 System
.out
.println(" Id: " + attachment
.asPointer().getId() + " Key length: " + attachment
.asPointer().getKey().length
+ (attachment
.asPointer().getRelay().isPresent() ?
" Relay: " + attachment
.asPointer().getRelay().get() : ""));
195 if (content
.getSyncMessage().isPresent()) {
196 TextSecureSyncMessage syncMessage
= content
.getSyncMessage().get();
197 System
.out
.println("Received sync message");
201 System
.out
.println("Unknown message received.");
203 System
.out
.println();
206 } catch (IOException e
) {
207 System
.out
.println("Error while receiving message: " + e
.getMessage());