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
.messages
.*;
25 import org
.whispersystems
.textsecure
.api
.messages
.multidevice
.TextSecureSyncMessage
;
26 import org
.whispersystems
.textsecure
.api
.push
.TextSecureAddress
;
27 import org
.whispersystems
.textsecure
.api
.push
.exceptions
.EncapsulatedExceptions
;
28 import org
.whispersystems
.textsecure
.api
.util
.InvalidNumberException
;
31 import java
.io
.FileInputStream
;
32 import java
.io
.IOException
;
33 import java
.io
.InputStream
;
34 import java
.nio
.file
.Files
;
35 import java
.nio
.file
.Paths
;
36 import java
.security
.Security
;
37 import java
.util
.ArrayList
;
38 import java
.util
.List
;
42 public static void main(String
[] args
) {
43 // Workaround for BKS truststore
44 Security
.insertProviderAt(new org
.spongycastle
.jce
.provider
.BouncyCastleProvider(), 1);
46 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("textsecure-cli")
48 .description("Commandline interface for TextSecure.");
49 Subparsers subparsers
= parser
.addSubparsers()
52 .description("valid subcommands")
53 .help("additional help");
54 Subparser parserRegister
= subparsers
.addParser("register");
55 parserRegister
.addArgument("-v", "--voice")
56 .help("The verification should be done over voice, not sms.")
57 .action(Arguments
.storeTrue());
58 Subparser parserVerify
= subparsers
.addParser("verify");
59 parserVerify
.addArgument("verificationCode")
60 .help("The verification code you received via sms or voice call.");
61 Subparser parserSend
= subparsers
.addParser("send");
62 parserSend
.addArgument("recipient")
63 .help("Specify the recipients' phone number.")
65 parserSend
.addArgument("-m", "--message")
66 .help("Specify the message, if missing standard input is used.");
67 parserSend
.addArgument("-a", "--attachment")
69 .help("Add file as attachment");
70 Subparser parserReceive
= subparsers
.addParser("receive");
71 parser
.addArgument("-u", "--username")
73 .help("Specify your phone number, that will be used for verification.");
76 ns
= parser
.parseArgs(args
);
77 } catch (ArgumentParserException e
) {
78 parser
.handleError(e
);
82 final String username
= ns
.getString("username");
83 final Manager m
= new Manager(username
);
87 } catch (Exception e
) {
88 System
.out
.println("Loading file error: " + e
.getMessage());
92 switch (ns
.getString("command")) {
94 if (!m
.userHasKeys()) {
95 m
.createNewIdentity();
98 m
.register(ns
.getBoolean("voice"));
99 } catch (IOException e
) {
100 System
.out
.println("Request verify error: " + e
.getMessage());
105 if (!m
.userHasKeys()) {
106 System
.out
.println("User has no keys, first call register.");
109 if (m
.isRegistered()) {
110 System
.out
.println("User registration is already verified");
114 m
.verifyAccount(ns
.getString("verificationCode"));
115 } catch (IOException e
) {
116 System
.out
.println("Verify error: " + e
.getMessage());
121 if (!m
.isRegistered()) {
122 System
.out
.println("User is not registered.");
125 TextSecureMessageSender messageSender
= m
.getMessageSender();
126 String messageText
= ns
.getString("message");
127 if (messageText
== null) {
129 messageText
= IOUtils
.toString(System
.in);
130 } catch (IOException e
) {
131 System
.out
.println("Failed to read message from stdin: " + e
.getMessage());
135 final TextSecureDataMessage
.Builder messageBuilder
= TextSecureDataMessage
.newBuilder().withBody(messageText
);
136 final List
<String
> attachments
= ns
.<String
>getList("attachment");
137 if (attachments
!= null) {
138 List
<TextSecureAttachment
> textSecureAttachments
= new ArrayList
<TextSecureAttachment
>(attachments
.size());
139 for (String attachment
: attachments
) {
141 File attachmentFile
= new File(attachment
);
142 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
143 final long attachmentSize
= attachmentFile
.length();
144 String mime
= Files
.probeContentType(Paths
.get(attachment
));
145 textSecureAttachments
.add(new TextSecureAttachmentStream(attachmentStream
, mime
, attachmentSize
, null));
146 } catch (IOException e
) {
147 System
.out
.println("Failed to add attachment \"" + attachment
+ "\": " + e
.getMessage());
151 messageBuilder
.withAttachments(textSecureAttachments
);
153 TextSecureDataMessage message
= messageBuilder
.build();
155 List
<TextSecureAddress
> recipients
= new ArrayList
<>(ns
.<String
>getList("recipient").size());
156 for (String recipient
: ns
.<String
>getList("recipient")) {
158 recipients
.add(m
.getPushAddress(recipient
));
159 } catch (InvalidNumberException e
) {
160 System
.out
.println("Failed to send message to \"" + recipient
+ "\": " + e
.getMessage());
164 messageSender
.sendMessage(recipients
, message
);
165 } catch (IOException
| EncapsulatedExceptions e
) {
166 System
.out
.println("Failed to send message: " + e
.getMessage());
170 if (!m
.isRegistered()) {
171 System
.out
.println("User is not registered.");
175 m
.receiveMessages(new Manager
.ReceiveMessageHandler() {
177 public void handleMessage(TextSecureEnvelope envelope
) {
178 System
.out
.println("Envelope from: " + envelope
.getSource());
179 System
.out
.println("Timestamp: " + envelope
.getTimestamp());
181 if (envelope
.isReceipt()) {
182 System
.out
.println("Got receipt.");
183 } else if (envelope
.isWhisperMessage() | envelope
.isPreKeyWhisperMessage()) {
184 TextSecureContent content
= m
.decryptMessage(envelope
);
186 if (content
== null) {
187 System
.out
.println("Failed to decrypt message.");
189 if (content
.getDataMessage().isPresent()) {
190 TextSecureDataMessage message
= content
.getDataMessage().get();
192 System
.out
.println("Body: " + message
.getBody().get());
193 if (message
.getAttachments().isPresent()) {
194 System
.out
.println("Attachments: ");
195 for (TextSecureAttachment attachment
: message
.getAttachments().get()) {
196 System
.out
.println("- " + attachment
.getContentType() + " (" + (attachment
.isPointer() ?
"Pointer" : "") + (attachment
.isStream() ?
"Stream" : "") + ")");
197 if (attachment
.isPointer()) {
198 System
.out
.println(" Id: " + attachment
.asPointer().getId() + " Key length: " + attachment
.asPointer().getKey().length
+ (attachment
.asPointer().getRelay().isPresent() ?
" Relay: " + attachment
.asPointer().getRelay().get() : ""));
203 if (content
.getSyncMessage().isPresent()) {
204 TextSecureSyncMessage syncMessage
= content
.getSyncMessage().get();
205 System
.out
.println("Received sync message");
209 System
.out
.println("Unknown message received.");
211 System
.out
.println();
214 } catch (IOException e
) {
215 System
.out
.println("Error while receiving message: " + e
.getMessage());