2 Copyright (C) 2015-2020 AsamK and contributors
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/>.
17 package org
.asamk
.signal
;
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
;
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
;
49 import java
.io
.IOException
;
50 import java
.security
.Security
;
53 import static org
.whispersystems
.signalservice
.internal
.util
.Util
.isEmpty
;
57 public static void main(String
[] args
) {
58 installSecurityProviderWorkaround();
60 Namespace ns
= parseArgs(args
);
65 int res
= handleCommands(ns
);
69 public static void installSecurityProviderWorkaround() {
70 // Register our own security provider
71 Security
.insertProviderAt(new SecurityProvider(), 1);
72 Security
.addProvider(new BouncyCastleProvider());
75 private static int handleCommands(Namespace ns
) {
76 final String username
= ns
.getString("username");
78 if (ns
.getBoolean("dbus") || ns
.getBoolean("dbus_system")) {
80 DBusConnection
.DBusBusType busType
;
81 if (ns
.getBoolean("dbus_system")) {
82 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
84 busType
= DBusConnection
.DBusBusType
.SESSION
;
86 try (DBusConnection dBusConn
= DBusConnection
.getConnection(busType
)) {
87 Signal ts
= dBusConn
.getRemoteObject(DbusConfig
.SIGNAL_BUSNAME
,
88 DbusConfig
.SIGNAL_OBJECTPATH
,
91 return handleCommands(ns
, ts
, dBusConn
);
93 } catch (UnsatisfiedLinkError e
) {
94 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
96 } catch (DBusException
| IOException e
) {
101 String dataPath
= ns
.getString("config");
102 if (isEmpty(dataPath
)) {
103 dataPath
= getDefaultDataPath();
106 final SignalServiceConfiguration serviceConfiguration
= ServiceConfig
.createDefaultServiceConfiguration(
107 BaseConfig
.USER_AGENT
);
109 if (!ServiceConfig
.getCapabilities().isGv2()) {
110 System
.err
.println("WARNING: Support for new group V2 is disabled,"
111 + " because the required native library dependency is missing: libzkgroup");
114 if (username
== null) {
115 ProvisioningManager pm
= new ProvisioningManager(dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
116 return handleCommands(ns
, pm
);
121 manager
= Manager
.init(username
, dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
122 } catch (Throwable e
) {
123 System
.err
.println("Error loading state file: " + e
.getMessage());
127 try (Manager m
= manager
) {
129 m
.checkAccountState();
130 } catch (AuthorizationFailedException e
) {
131 if (!"register".equals(ns
.getString("command"))) {
132 // Register command should still be possible, if current authorization fails
133 System
.err
.println("Authorization failed, was the number registered elsewhere?");
136 } catch (IOException e
) {
137 System
.err
.println("Error while checking account: " + e
.getMessage());
141 return handleCommands(ns
, m
);
142 } catch (IOException e
) {
149 private static int handleCommands(Namespace ns
, Signal ts
, DBusConnection dBusConn
) {
150 String commandKey
= ns
.getString("command");
151 final Map
<String
, Command
> commands
= Commands
.getCommands();
152 if (commands
.containsKey(commandKey
)) {
153 Command command
= commands
.get(commandKey
);
155 if (command
instanceof ExtendedDbusCommand
) {
156 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
157 } else if (command
instanceof DbusCommand
) {
158 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
160 System
.err
.println(commandKey
+ " is not yet implemented via dbus");
167 private static int handleCommands(Namespace ns
, ProvisioningManager pm
) {
168 String commandKey
= ns
.getString("command");
169 final Map
<String
, Command
> commands
= Commands
.getCommands();
170 if (commands
.containsKey(commandKey
)) {
171 Command command
= commands
.get(commandKey
);
173 if (command
instanceof ProvisioningCommand
) {
174 return ((ProvisioningCommand
) command
).handleCommand(ns
, pm
);
176 System
.err
.println(commandKey
+ " only works with a username");
183 private static int handleCommands(Namespace ns
, Manager m
) {
184 String commandKey
= ns
.getString("command");
185 final Map
<String
, Command
> commands
= Commands
.getCommands();
186 if (commands
.containsKey(commandKey
)) {
187 Command command
= commands
.get(commandKey
);
189 if (command
instanceof LocalCommand
) {
190 return ((LocalCommand
) command
).handleCommand(ns
, m
);
191 } else if (command
instanceof DbusCommand
) {
192 return ((DbusCommand
) command
).handleCommand(ns
, new DbusSignalImpl(m
));
193 } else if (command
instanceof ExtendedDbusCommand
) {
194 System
.err
.println(commandKey
+ " only works via dbus");
202 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
203 * - $HOME/.config/signal
204 * - $HOME/.config/textsecure
206 * @return the data directory to be used by signal-cli.
208 private static String
getDefaultDataPath() {
209 String dataPath
= IOUtils
.getDataHomeDir() + "/signal-cli";
210 if (new File(dataPath
).exists()) {
214 String legacySettingsPath
= System
.getProperty("user.home") + "/.config/signal";
215 if (new File(legacySettingsPath
).exists()) {
216 return legacySettingsPath
;
219 legacySettingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
220 if (new File(legacySettingsPath
).exists()) {
221 return legacySettingsPath
;
227 private static Namespace
parseArgs(String
[] args
) {
228 ArgumentParser parser
= ArgumentParsers
.newFor("signal-cli")
231 .description("Commandline interface for Signal.")
232 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
234 parser
.addArgument("-v", "--version").help("Show package version.").action(Arguments
.version());
235 parser
.addArgument("--config")
236 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
238 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
239 mut
.addArgument("-u", "--username").help("Specify your phone number, that will be used for verification.");
240 mut
.addArgument("--dbus").help("Make request via user dbus.").action(Arguments
.storeTrue());
241 mut
.addArgument("--dbus-system").help("Make request via system dbus.").action(Arguments
.storeTrue());
243 Subparsers subparsers
= parser
.addSubparsers()
244 .title("subcommands")
246 .description("valid subcommands")
247 .help("additional help");
249 final Map
<String
, Command
> commands
= Commands
.getCommands();
250 for (Map
.Entry
<String
, Command
> entry
: commands
.entrySet()) {
251 Subparser subparser
= subparsers
.addParser(entry
.getKey());
252 entry
.getValue().attachToSubparser(subparser
);
257 ns
= parser
.parseArgs(args
);
258 } catch (ArgumentParserException e
) {
259 parser
.handleError(e
);
263 if ("link".equals(ns
.getString("command"))) {
264 if (ns
.getString("username") != null) {
266 System
.err
.println("You cannot specify a username (phone number) when linking");
269 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
270 if (ns
.getString("username") == null) {
272 System
.err
.println("You need to specify a username (phone number)");
275 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"), null)) {
276 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
280 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
281 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");