]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/Main.java
Prevent NullPointerException when destination is null for some reason
[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(DbusConfig.SIGNAL_BUSNAME,
88 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(
107 BaseConfig.USER_AGENT);
108
109 if (username == null) {
110 ProvisioningManager pm = new ProvisioningManager(dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
111 return handleCommands(ns, pm);
112 }
113
114 Manager manager;
115 try {
116 manager = Manager.init(username, dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
117 } catch (Throwable e) {
118 System.err.println("Error loading state file: " + e.getMessage());
119 return 2;
120 }
121
122 try (Manager m = manager) {
123 try {
124 m.checkAccountState();
125 } catch (AuthorizationFailedException e) {
126 if (!"register".equals(ns.getString("command"))) {
127 // Register command should still be possible, if current authorization fails
128 System.err.println("Authorization failed, was the number registered elsewhere?");
129 return 2;
130 }
131 } catch (IOException e) {
132 System.err.println("Error while checking account: " + e.getMessage());
133 return 2;
134 }
135
136 return handleCommands(ns, m);
137 } catch (IOException e) {
138 e.printStackTrace();
139 return 3;
140 }
141 }
142 }
143
144 private static int handleCommands(Namespace ns, Signal ts, DBusConnection dBusConn) {
145 String commandKey = ns.getString("command");
146 final Map<String, Command> commands = Commands.getCommands();
147 if (commands.containsKey(commandKey)) {
148 Command command = commands.get(commandKey);
149
150 if (command instanceof ExtendedDbusCommand) {
151 return ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn);
152 } else if (command instanceof DbusCommand) {
153 return ((DbusCommand) command).handleCommand(ns, ts);
154 } else {
155 System.err.println(commandKey + " is not yet implemented via dbus");
156 return 1;
157 }
158 }
159 return 0;
160 }
161
162 private static int handleCommands(Namespace ns, ProvisioningManager pm) {
163 String commandKey = ns.getString("command");
164 final Map<String, Command> commands = Commands.getCommands();
165 if (commands.containsKey(commandKey)) {
166 Command command = commands.get(commandKey);
167
168 if (command instanceof ProvisioningCommand) {
169 return ((ProvisioningCommand) command).handleCommand(ns, pm);
170 } else {
171 System.err.println(commandKey + " only works with a username");
172 return 1;
173 }
174 }
175 return 0;
176 }
177
178 private static int handleCommands(Namespace ns, Manager m) {
179 String commandKey = ns.getString("command");
180 final Map<String, Command> commands = Commands.getCommands();
181 if (commands.containsKey(commandKey)) {
182 Command command = commands.get(commandKey);
183
184 if (command instanceof LocalCommand) {
185 return ((LocalCommand) command).handleCommand(ns, m);
186 } else if (command instanceof DbusCommand) {
187 return ((DbusCommand) command).handleCommand(ns, new DbusSignalImpl(m));
188 } else if (command instanceof ExtendedDbusCommand) {
189 System.err.println(commandKey + " only works via dbus");
190 }
191 return 1;
192 }
193 return 0;
194 }
195
196 /**
197 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
198 * - $HOME/.config/signal
199 * - $HOME/.config/textsecure
200 *
201 * @return the data directory to be used by signal-cli.
202 */
203 private static String getDefaultDataPath() {
204 String dataPath = IOUtils.getDataHomeDir() + "/signal-cli";
205 if (new File(dataPath).exists()) {
206 return dataPath;
207 }
208
209 String legacySettingsPath = System.getProperty("user.home") + "/.config/signal";
210 if (new File(legacySettingsPath).exists()) {
211 return legacySettingsPath;
212 }
213
214 legacySettingsPath = System.getProperty("user.home") + "/.config/textsecure";
215 if (new File(legacySettingsPath).exists()) {
216 return legacySettingsPath;
217 }
218
219 return dataPath;
220 }
221
222 private static Namespace parseArgs(String[] args) {
223 ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
224 .build()
225 .defaultHelp(true)
226 .description("Commandline interface for Signal.")
227 .version(BaseConfig.PROJECT_NAME + " " + BaseConfig.PROJECT_VERSION);
228
229 parser.addArgument("-v", "--version").help("Show package version.").action(Arguments.version());
230 parser.addArgument("--config")
231 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
232
233 MutuallyExclusiveGroup mut = parser.addMutuallyExclusiveGroup();
234 mut.addArgument("-u", "--username").help("Specify your phone number, that will be used for verification.");
235 mut.addArgument("--dbus").help("Make request via user dbus.").action(Arguments.storeTrue());
236 mut.addArgument("--dbus-system").help("Make request via system dbus.").action(Arguments.storeTrue());
237
238 Subparsers subparsers = parser.addSubparsers()
239 .title("subcommands")
240 .dest("command")
241 .description("valid subcommands")
242 .help("additional help");
243
244 final Map<String, Command> commands = Commands.getCommands();
245 for (Map.Entry<String, Command> entry : commands.entrySet()) {
246 Subparser subparser = subparsers.addParser(entry.getKey());
247 entry.getValue().attachToSubparser(subparser);
248 }
249
250 Namespace ns;
251 try {
252 ns = parser.parseArgs(args);
253 } catch (ArgumentParserException e) {
254 parser.handleError(e);
255 return null;
256 }
257
258 if ("link".equals(ns.getString("command"))) {
259 if (ns.getString("username") != null) {
260 parser.printUsage();
261 System.err.println("You cannot specify a username (phone number) when linking");
262 System.exit(2);
263 }
264 } else if (!ns.getBoolean("dbus") && !ns.getBoolean("dbus_system")) {
265 if (ns.getString("username") == null) {
266 parser.printUsage();
267 System.err.println("You need to specify a username (phone number)");
268 System.exit(2);
269 }
270 if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"), null)) {
271 System.err.println("Invalid username (phone number), make sure you include the country code.");
272 System.exit(2);
273 }
274 }
275 if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
276 System.err.println("You cannot specify recipients by phone number and groups at the same time");
277 System.exit(2);
278 }
279 return ns;
280 }
281 }