]> nmode's Git Repositories - signal-cli/blob - src/main/java/cli/Main.java
92e1b46c42095f6ef8b3541a009b8986ebd0ed77
[signal-cli] / src / main / java / cli / Main.java
1 /**
2 * Copyright (C) 2015 AsamK
3 * <p>
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.
8 * <p>
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.
13 * <p>
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/>.
16 */
17 package cli;
18
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.TextSecureContent;
27 import org.whispersystems.textsecure.api.messages.TextSecureDataMessage;
28 import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
29 import org.whispersystems.textsecure.api.push.TextSecureAddress;
30
31 import java.io.IOException;
32 import java.security.Security;
33
34 public class Main {
35
36 public static void main(String[] args) {
37 // Workaround for BKS truststore
38 Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
39
40 ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
41 .defaultHelp(true)
42 .description("Commandline interface for TextSecure.");
43 Subparsers subparsers = parser.addSubparsers()
44 .title("subcommands")
45 .dest("command")
46 .description("valid subcommands")
47 .help("additional help");
48 Subparser parserRegister = subparsers.addParser("register");
49 parserRegister.addArgument("-v", "--voice")
50 .help("The verification should be done over voice, not sms.")
51 .action(Arguments.storeTrue());
52 Subparser parserVerify = subparsers.addParser("verify");
53 parserVerify.addArgument("verificationCode")
54 .help("The verification code you received via sms or voice call.");
55 Subparser parserSend = subparsers.addParser("send");
56 parserSend.addArgument("recipient")
57 .help("Specify the recipients' phone number.")
58 .nargs("*");
59 parserSend.addArgument("-m", "--message")
60 .help("Specify the message, if missing standard input is used.");
61 Subparser parserReceive = subparsers.addParser("receive");
62 parser.addArgument("-u", "--username")
63 .required(true)
64 .help("Specify your phone number, that will be used for verification.");
65 Namespace ns = null;
66 try {
67 ns = parser.parseArgs(args);
68 } catch (ArgumentParserException e) {
69 parser.handleError(e);
70 System.exit(1);
71 }
72
73 String username = ns.getString("username");
74 Manager m = new Manager(username);
75 if (m.userExists()) {
76 try {
77 m.load();
78 } catch (Exception e) {
79 System.out.println("Loading file error: " + e.getMessage());
80 System.exit(2);
81 }
82 }
83 switch (ns.getString("command")) {
84 case "register":
85 if (!m.userHasKeys()) {
86 m.createNewIdentity();
87 }
88 try {
89 m.register(ns.getBoolean("voice"));
90 } catch (IOException e) {
91 System.out.println("Request verify error: " + e.getMessage());
92 System.exit(3);
93 }
94 break;
95 case "verify":
96 if (!m.userHasKeys()) {
97 System.out.println("User has no keys, first call register.");
98 System.exit(1);
99 }
100 if (m.isRegistered()) {
101 System.out.println("User registration is already verified");
102 System.exit(1);
103 }
104 try {
105 m.verifyAccount(ns.getString("verificationCode"));
106 } catch (IOException e) {
107 System.out.println("Verify error: " + e.getMessage());
108 System.exit(3);
109 }
110 break;
111 case "send":
112 if (!m.isRegistered()) {
113 System.out.println("User is not registered.");
114 System.exit(1);
115 }
116 TextSecureMessageSender messageSender = m.getMessageSender();
117 String messageText = ns.getString("message");
118 if (messageText == null) {
119 try {
120 messageText = IOUtils.toString(System.in);
121 } catch (IOException e) {
122 System.out.println("Failed to read message from stdin: " + e.getMessage());
123 System.exit(1);
124 }
125 }
126 TextSecureDataMessage message = TextSecureDataMessage.newBuilder().withBody(messageText).build();
127 for (String recipient : ns.<String>getList("recipient")) {
128 try {
129 messageSender.sendMessage(new TextSecureAddress(recipient), message);
130 } catch (UntrustedIdentityException | IOException e) {
131 System.out.println("Send message: " + e.getMessage());
132 }
133 }
134 break;
135 case "receive":
136 if (!m.isRegistered()) {
137 System.out.println("User is not registered.");
138 System.exit(1);
139 }
140 try {
141 TextSecureContent content = m.receiveMessage();
142 if (content.getDataMessage().isPresent()) {
143 message = content.getDataMessage().get();
144 if (message == null) {
145 System.exit(0);
146 } else {
147 System.out.println("Received message: " + message.getBody().get());
148 }
149 }
150 if (content.getSyncMessage().isPresent()) {
151 TextSecureSyncMessage syncMessage = content.getSyncMessage().get();
152
153 if (syncMessage == null) {
154 System.exit(0);
155 } else {
156 System.out.println("Received sync message");
157 }
158 }
159 } catch (IOException | InvalidVersionException e) {
160 System.out.println("Receive message: " + e.getMessage());
161 }
162 break;
163 }
164 m.save();
165 }
166 }