1 package org
.asamk
.signal
;
3 import net
.sourceforge
.argparse4j
.ArgumentParsers
;
4 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
5 import net
.sourceforge
.argparse4j
.inf
.ArgumentParser
;
6 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
8 import org
.asamk
.Signal
;
9 import org
.asamk
.signal
.commands
.Command
;
10 import org
.asamk
.signal
.commands
.Commands
;
11 import org
.asamk
.signal
.commands
.DbusCommand
;
12 import org
.asamk
.signal
.commands
.ExtendedDbusCommand
;
13 import org
.asamk
.signal
.commands
.LocalCommand
;
14 import org
.asamk
.signal
.commands
.MultiLocalCommand
;
15 import org
.asamk
.signal
.commands
.ProvisioningCommand
;
16 import org
.asamk
.signal
.commands
.RegistrationCommand
;
17 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
18 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
19 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
20 import org
.asamk
.signal
.manager
.Manager
;
21 import org
.asamk
.signal
.manager
.NotRegisteredException
;
22 import org
.asamk
.signal
.manager
.ProvisioningManager
;
23 import org
.asamk
.signal
.manager
.RegistrationManager
;
24 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
25 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
26 import org
.asamk
.signal
.util
.IOUtils
;
27 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
28 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
29 import org
.slf4j
.Logger
;
30 import org
.slf4j
.LoggerFactory
;
31 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
34 import java
.io
.IOException
;
35 import java
.util
.ArrayList
;
36 import java
.util
.List
;
38 import static net
.sourceforge
.argparse4j
.DefaultSettings
.VERSION_0_9_0_DEFAULT_SETTINGS
;
42 private final static Logger logger
= LoggerFactory
.getLogger(App
.class);
44 private final Namespace ns
;
46 static ArgumentParser
buildArgumentParser() {
47 var parser
= ArgumentParsers
.newFor("signal-cli", VERSION_0_9_0_DEFAULT_SETTINGS
)
48 .includeArgumentNamesAsKeysInResult(true)
51 .description("Commandline interface for Signal.")
52 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
54 parser
.addArgument("-v", "--version").help("Show package version.").action(Arguments
.version());
55 parser
.addArgument("--verbose")
56 .help("Raise log level and include lib signal logs.")
57 .action(Arguments
.storeTrue());
58 parser
.addArgument("--config")
59 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
61 parser
.addArgument("-u", "--username").help("Specify your phone number, that will be your identifier.");
63 var mut
= parser
.addMutuallyExclusiveGroup();
64 mut
.addArgument("--dbus").help("Make request via user dbus.").action(Arguments
.storeTrue());
65 mut
.addArgument("--dbus-system").help("Make request via system dbus.").action(Arguments
.storeTrue());
67 parser
.addArgument("-o", "--output")
68 .help("Choose to output in plain text or JSON")
69 .type(Arguments
.enumStringType(OutputType
.class))
70 .setDefault(OutputType
.PLAIN_TEXT
);
72 parser
.addArgument("--service-environment")
73 .help("Choose the server environment to use.")
74 .type(Arguments
.enumStringType(ServiceEnvironmentCli
.class))
75 .setDefault(ServiceEnvironmentCli
.LIVE
);
77 var subparsers
= parser
.addSubparsers().title("subcommands").dest("command");
79 final var commands
= Commands
.getCommands();
80 for (var entry
: commands
.entrySet()) {
81 var subparser
= subparsers
.addParser(entry
.getKey());
82 entry
.getValue().attachToSubparser(subparser
);
88 public App(final Namespace ns
) {
92 public void init() throws CommandException
{
93 var commandKey
= ns
.getString("command");
94 var command
= Commands
.getCommand(commandKey
);
95 if (command
== null) {
96 throw new UserErrorException("Command not implemented!");
99 var outputType
= ns
.<OutputType
>get("output");
100 if (!command
.getSupportedOutputTypes().contains(outputType
)) {
101 throw new UserErrorException("Command doesn't support output type " + outputType
.toString());
104 var username
= ns
.getString("username");
106 final var useDbus
= ns
.getBoolean("dbus");
107 final var useDbusSystem
= ns
.getBoolean("dbus-system");
108 if (useDbus
|| useDbusSystem
) {
109 // If username is null, it will connect to the default object path
110 initDbusClient(command
, username
, useDbusSystem
);
115 var config
= ns
.getString("config");
116 if (config
!= null) {
117 dataPath
= new File(config
);
119 dataPath
= getDefaultDataPath();
122 final var serviceEnvironmentCli
= ns
.<ServiceEnvironmentCli
>get("service-environment");
123 final var serviceEnvironment
= serviceEnvironmentCli
== ServiceEnvironmentCli
.LIVE
124 ? ServiceEnvironment
.LIVE
125 : ServiceEnvironment
.SANDBOX
;
127 if (!ServiceConfig
.getCapabilities().isGv2()) {
128 logger
.warn("WARNING: Support for new group V2 is disabled,"
129 + " because the required native library dependency is missing: libzkgroup");
132 if (!ServiceConfig
.isSignalClientAvailable()) {
133 throw new UserErrorException("Missing required native library dependency: libsignal-client");
136 if (command
instanceof ProvisioningCommand
) {
137 if (username
!= null) {
138 throw new UserErrorException("You cannot specify a username (phone number) when linking");
141 handleProvisioningCommand((ProvisioningCommand
) command
, dataPath
, serviceEnvironment
);
145 if (username
== null) {
146 var usernames
= Manager
.getAllLocalUsernames(dataPath
);
148 if (command
instanceof MultiLocalCommand
) {
149 handleMultiLocalCommand((MultiLocalCommand
) command
, dataPath
, serviceEnvironment
, usernames
);
153 if (usernames
.size() == 0) {
154 throw new UserErrorException("No local users found, you first need to register or link an account");
155 } else if (usernames
.size() > 1) {
156 throw new UserErrorException(
157 "Multiple users found, you need to specify a username (phone number) with -u");
160 username
= usernames
.get(0);
161 } else if (!PhoneNumberFormatter
.isValidNumber(username
, null)) {
162 throw new UserErrorException("Invalid username (phone number), make sure you include the country code.");
165 if (command
instanceof RegistrationCommand
) {
166 handleRegistrationCommand((RegistrationCommand
) command
, username
, dataPath
, serviceEnvironment
);
170 if (!(command
instanceof LocalCommand
)) {
171 throw new UserErrorException("Command only works via dbus");
174 handleLocalCommand((LocalCommand
) command
, username
, dataPath
, serviceEnvironment
);
177 private void handleProvisioningCommand(
178 final ProvisioningCommand command
, final File dataPath
, final ServiceEnvironment serviceEnvironment
179 ) throws CommandException
{
180 var pm
= ProvisioningManager
.init(dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
181 command
.handleCommand(ns
, pm
);
184 private void handleRegistrationCommand(
185 final RegistrationCommand command
,
186 final String username
,
188 final ServiceEnvironment serviceEnvironment
189 ) throws CommandException
{
190 final RegistrationManager manager
;
192 manager
= RegistrationManager
.init(username
, dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
193 } catch (Throwable e
) {
194 throw new UnexpectedErrorException("Error loading or creating state file: "
197 + e
.getClass().getSimpleName()
200 try (var m
= manager
) {
201 command
.handleCommand(ns
, m
);
202 } catch (IOException e
) {
203 logger
.warn("Cleanup failed", e
);
207 private void handleLocalCommand(
208 final LocalCommand command
,
209 final String username
,
211 final ServiceEnvironment serviceEnvironment
212 ) throws CommandException
{
213 try (var m
= loadManager(username
, dataPath
, serviceEnvironment
)) {
214 command
.handleCommand(ns
, m
);
215 } catch (IOException e
) {
216 logger
.warn("Cleanup failed", e
);
220 private void handleMultiLocalCommand(
221 final MultiLocalCommand command
,
223 final ServiceEnvironment serviceEnvironment
,
224 final List
<String
> usernames
225 ) throws CommandException
{
226 final var managers
= new ArrayList
<Manager
>();
227 for (String u
: usernames
) {
229 managers
.add(loadManager(u
, dataPath
, serviceEnvironment
));
230 } catch (CommandException e
) {
231 logger
.warn("Ignoring {}: {}", u
, e
.getMessage());
235 command
.handleCommand(ns
, managers
);
237 for (var m
: managers
) {
240 } catch (IOException e
) {
241 logger
.warn("Cleanup failed", e
);
246 private Manager
loadManager(
247 final String username
, final File dataPath
, final ServiceEnvironment serviceEnvironment
248 ) throws CommandException
{
251 manager
= Manager
.init(username
, dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
252 } catch (NotRegisteredException e
) {
253 throw new UserErrorException("User " + username
+ " is not registered.");
254 } catch (Throwable e
) {
255 logger
.debug("Loading state file failed", e
);
256 throw new UnexpectedErrorException("Error loading state file for user "
261 + e
.getClass().getSimpleName()
266 manager
.checkAccountState();
267 } catch (IOException e
) {
268 throw new UnexpectedErrorException("Error while checking account " + username
+ ": " + e
.getMessage());
274 private void initDbusClient(
275 final Command command
, final String username
, final boolean systemBus
276 ) throws CommandException
{
278 DBusConnection
.DBusBusType busType
;
280 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
282 busType
= DBusConnection
.DBusBusType
.SESSION
;
284 try (var dBusConn
= DBusConnection
.getConnection(busType
)) {
285 var ts
= dBusConn
.getRemoteObject(DbusConfig
.getBusname(),
286 DbusConfig
.getObjectPath(username
),
289 handleCommand(command
, ts
, dBusConn
);
291 } catch (DBusException
| IOException e
) {
292 logger
.error("Dbus client failed", e
);
293 throw new UnexpectedErrorException("Dbus client failed");
297 private void handleCommand(Command command
, Signal ts
, DBusConnection dBusConn
) throws CommandException
{
298 if (command
instanceof ExtendedDbusCommand
) {
299 ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
300 } else if (command
instanceof DbusCommand
) {
301 ((DbusCommand
) command
).handleCommand(ns
, ts
);
303 throw new UserErrorException("Command is not yet implemented via dbus");
308 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
309 * - $HOME/.config/signal
310 * - $HOME/.config/textsecure
312 * @return the data directory to be used by signal-cli.
314 private static File
getDefaultDataPath() {
315 var dataPath
= new File(IOUtils
.getDataHomeDir(), "signal-cli");
316 if (dataPath
.exists()) {
320 var configPath
= new File(System
.getProperty("user.home"), ".config");
322 var legacySettingsPath
= new File(configPath
, "signal");
323 if (legacySettingsPath
.exists()) {
324 logger
.warn("Using legacy data path \"{}\", please move it to \"{}\".", legacySettingsPath
, dataPath
);
325 return legacySettingsPath
;
328 legacySettingsPath
= new File(configPath
, "textsecure");
329 if (legacySettingsPath
.exists()) {
330 logger
.warn("Using legacy data path \"{}\", please move it to \"{}\".", legacySettingsPath
, dataPath
);
331 return legacySettingsPath
;