]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/Main.java
Delay auto responses to messages until caught up with old messages
[signal-cli] / src / main / java / org / asamk / signal / Main.java
1 /*
2 Copyright (C) 2015-2020 AsamK and contributors
3
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
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
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 org.asamk.signal;
18
19 import net.sourceforge.argparse4j.ArgumentParsers;
20 import net.sourceforge.argparse4j.impl.Arguments;
21 import net.sourceforge.argparse4j.inf.ArgumentParser;
22 import net.sourceforge.argparse4j.inf.ArgumentParserException;
23 import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
24 import net.sourceforge.argparse4j.inf.Namespace;
25 import net.sourceforge.argparse4j.inf.Subparser;
26 import net.sourceforge.argparse4j.inf.Subparsers;
27
28 import org.asamk.Signal;
29 import org.asamk.signal.commands.Command;
30 import org.asamk.signal.commands.Commands;
31 import org.asamk.signal.commands.DbusCommand;
32 import org.asamk.signal.commands.ExtendedDbusCommand;
33 import org.asamk.signal.commands.LocalCommand;
34 import org.asamk.signal.commands.ProvisioningCommand;
35 import org.asamk.signal.dbus.DbusSignalImpl;
36 import org.asamk.signal.manager.Manager;
37 import org.asamk.signal.manager.ProvisioningManager;
38 import org.asamk.signal.manager.ServiceConfig;
39 import org.asamk.signal.util.IOUtils;
40 import org.asamk.signal.util.SecurityProvider;
41 import org.bouncycastle.jce.provider.BouncyCastleProvider;
42 import org.freedesktop.dbus.connections.impl.DBusConnection;
43 import org.freedesktop.dbus.exceptions.DBusException;
44 import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
45 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
46 import org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
47
48 import java.io.File;
49 import java.io.IOException;
50 import java.security.Security;
51 import java.util.Map;
52
53 import static org.whispersystems.signalservice.internal.util.Util.isEmpty;
54
55 public class Main {
56
57 public static void main(String[] args) {
58 installSecurityProviderWorkaround();
59
60 Namespace ns = parseArgs(args);
61 if (ns == null) {
62 System.exit(1);
63 }
64
65 int res = handleCommands(ns);
66 System.exit(res);
67 }
68
69 public static void installSecurityProviderWorkaround() {
70 // Register our own security provider
71 Security.insertProviderAt(new SecurityProvider(), 1);
72 Security.addProvider(new BouncyCastleProvider());
73 }
74
75 private static int handleCommands(Namespace ns) {
76 final String username = ns.getString("username");
77
78 if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) {
79 try {
80 DBusConnection.DBusBusType busType;
81 if (ns.getBoolean("dbus_system")) {
82 busType = DBusConnection.DBusBusType.SYSTEM;
83 } else {
84 busType = DBusConnection.DBusBusType.SESSION;
85 }
86 try (DBusConnection dBusConn = DBusConnection.getConnection(busType)) {
87 Signal ts = dBusConn.getRemoteObject(
88 DbusConfig.SIGNAL_BUSNAME, DbusConfig.SIGNAL_OBJECTPATH,
89 Signal.class);
90
91 return handleCommands(ns, ts, dBusConn);
92 }
93 } catch (UnsatisfiedLinkError e) {
94 System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
95 return 1;
96 } catch (DBusException | IOException e) {
97 e.printStackTrace();
98 return 3;
99 }
100 } else {
101 String dataPath = ns.getString("config");
102 if (isEmpty(dataPath)) {
103 dataPath = getDefaultDataPath();
104 }
105
106 final SignalServiceConfiguration serviceConfiguration = ServiceConfig.createDefaultServiceConfiguration(BaseConfig.USER_AGENT);
107
108 if (username == null) {
109 ProvisioningManager pm = new ProvisioningManager(dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
110 return handleCommands(ns, pm);
111 }
112
113 Manager manager;
114 try {
115 manager = Manager.init(username, dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
116 } catch (Throwable e) {
117 System.err.println("Error loading state file: " + e.getMessage());
118 return 2;
119 }
120
121 try (Manager m = manager) {
122 try {
123 m.checkAccountState();
124 } catch (AuthorizationFailedException e) {
125 if (!"register".equals(ns.getString("command"))) {
126 // Register command should still be possible, if current authorization fails
127 System.err.println("Authorization failed, was the number registered elsewhere?");
128 return 2;
129 }
130 } catch (IOException e) {
131 System.err.println("Error while checking account: " + e.getMessage());
132 return 2;
133 }
134
135 return handleCommands(ns, m);
136 } catch (IOException e) {
137 e.printStackTrace();
138 return 3;
139 }
140 }
141 }
142
143 private static int handleCommands(Namespace ns, Signal ts, DBusConnection dBusConn) {
144 String commandKey = ns.getString("command");
145 final Map<String, Command> commands = Commands.getCommands();
146 if (commands.containsKey(commandKey)) {
147 Command command = commands.get(commandKey);
148
149 if (command instanceof ExtendedDbusCommand) {
150 return ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn);
151 } else if (command instanceof DbusCommand) {
152 return ((DbusCommand) command).handleCommand(ns, ts);
153 } else {
154 System.err.println(commandKey + " is not yet implemented via dbus");
155 return 1;
156 }
157 }
158 return 0;
159 }
160
161 private static int handleCommands(Namespace ns, ProvisioningManager pm) {
162 String commandKey = ns.getString("command");
163 final Map<String, Command> commands = Commands.getCommands();
164 if (commands.containsKey(commandKey)) {
165 Command command = commands.get(commandKey);
166
167 if (command instanceof ProvisioningCommand) {
168 return ((ProvisioningCommand) command).handleCommand(ns, pm);
169 } else {
170 System.err.println(commandKey + " only works with a username");
171 return 1;
172 }
173 }
174 return 0;
175 }
176
177 private static int handleCommands(Namespace ns, Manager m) {
178 String commandKey = ns.getString("command");
179 final Map<String, Command> commands = Commands.getCommands();
180 if (commands.containsKey(commandKey)) {
181 Command command = commands.get(commandKey);
182
183 if (command instanceof LocalCommand) {
184 return ((LocalCommand) command).handleCommand(ns, m);
185 } else if (command instanceof DbusCommand) {
186 return ((DbusCommand) command).handleCommand(ns, new DbusSignalImpl(m));
187 } else if (command instanceof ExtendedDbusCommand) {
188 System.err.println(commandKey + " only works via dbus");
189 }
190 return 1;
191 }
192 return 0;
193 }
194
195 /**
196 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
197 * - $HOME/.config/signal
198 * - $HOME/.config/textsecure
199 *
200 * @return the data directory to be used by signal-cli.
201 */
202 private static String getDefaultDataPath() {
203 String dataPath = IOUtils.getDataHomeDir() + "/signal-cli";
204 if (new File(dataPath).exists()) {
205 return dataPath;
206 }
207
208 String legacySettingsPath = System.getProperty("user.home") + "/.config/signal";
209 if (new File(legacySettingsPath).exists()) {
210 return legacySettingsPath;
211 }
212
213 legacySettingsPath = System.getProperty("user.home") + "/.config/textsecure";
214 if (new File(legacySettingsPath).exists()) {
215 return legacySettingsPath;
216 }
217
218 return dataPath;
219 }
220
221 private static Namespace parseArgs(String[] args) {
222 ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
223 .build()
224 .defaultHelp(true)
225 .description("Commandline interface for Signal.")
226 .version(BaseConfig.PROJECT_NAME + " " + BaseConfig.PROJECT_VERSION);
227
228 parser.addArgument("-v", "--version")
229 .help("Show package version.")
230 .action(Arguments.version());
231 parser.addArgument("--config")
232 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
233
234 MutuallyExclusiveGroup mut = parser.addMutuallyExclusiveGroup();
235 mut.addArgument("-u", "--username")
236 .help("Specify your phone number, that will be used for verification.");
237 mut.addArgument("--dbus")
238 .help("Make request via user dbus.")
239 .action(Arguments.storeTrue());
240 mut.addArgument("--dbus-system")
241 .help("Make request via system dbus.")
242 .action(Arguments.storeTrue());
243
244 Subparsers subparsers = parser.addSubparsers()
245 .title("subcommands")
246 .dest("command")
247 .description("valid subcommands")
248 .help("additional help");
249
250 final Map<String, Command> commands = Commands.getCommands();
251 for (Map.Entry<String, Command> entry : commands.entrySet()) {
252 Subparser subparser = subparsers.addParser(entry.getKey());
253 entry.getValue().attachToSubparser(subparser);
254 }
255
256 Namespace ns;
257 try {
258 ns = parser.parseArgs(args);
259 } catch (ArgumentParserException e) {
260 parser.handleError(e);
261 return null;
262 }
263
264 if ("link".equals(ns.getString("command"))) {
265 if (ns.getString("username") != null) {
266 parser.printUsage();
267 System.err.println("You cannot specify a username (phone number) when linking");
268 System.exit(2);
269 }
270 } else if (!ns.getBoolean("dbus") && !ns.getBoolean("dbus_system")) {
271 if (ns.getString("username") == null) {
272 parser.printUsage();
273 System.err.println("You need to specify a username (phone number)");
274 System.exit(2);
275 }
276 if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"), null)) {
277 System.err.println("Invalid username (phone number), make sure you include the country code.");
278 System.exit(2);
279 }
280 }
281 if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
282 System.err.println("You cannot specify recipients by phone number and groups at the same time");
283 System.exit(2);
284 }
285 return ns;
286 }
287 }