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
;
40 private final static Logger logger
= LoggerFactory
.getLogger(App
.class);
42 private final Namespace ns
;
44 static ArgumentParser
buildArgumentParser() {
45 var parser
= ArgumentParsers
.newFor("signal-cli")
48 .description("Commandline interface for Signal.")
49 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
51 parser
.addArgument("-v", "--version").help("Show package version.").action(Arguments
.version());
52 parser
.addArgument("--verbose")
53 .help("Raise log level and include lib signal logs.")
54 .action(Arguments
.storeTrue());
55 parser
.addArgument("--config")
56 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
58 parser
.addArgument("-u", "--username").help("Specify your phone number, that will be your identifier.");
60 var mut
= parser
.addMutuallyExclusiveGroup();
61 mut
.addArgument("--dbus").help("Make request via user dbus.").action(Arguments
.storeTrue());
62 mut
.addArgument("--dbus-system").help("Make request via system dbus.").action(Arguments
.storeTrue());
64 parser
.addArgument("-o", "--output")
65 .help("Choose to output in plain text or JSON")
66 .type(Arguments
.enumStringType(OutputType
.class))
67 .setDefault(OutputType
.PLAIN_TEXT
);
69 parser
.addArgument("--service-environment")
70 .help("Choose the server environment to use, SANDBOX or LIVE.")
71 .type(Arguments
.enumStringType(ServiceEnvironmentCli
.class))
72 .setDefault(ServiceEnvironmentCli
.LIVE
);
74 var subparsers
= parser
.addSubparsers().title("subcommands").dest("command");
76 final var commands
= Commands
.getCommands();
77 for (var entry
: commands
.entrySet()) {
78 var subparser
= subparsers
.addParser(entry
.getKey());
79 entry
.getValue().attachToSubparser(subparser
);
85 public App(final Namespace ns
) {
89 public void init() throws CommandException
{
90 var commandKey
= ns
.getString("command");
91 var command
= Commands
.getCommand(commandKey
);
92 if (command
== null) {
93 throw new UserErrorException("Command not implemented!");
96 var outputType
= ns
.<OutputType
>get("output");
97 if (!command
.getSupportedOutputTypes().contains(outputType
)) {
98 throw new UserErrorException("Command doesn't support output type " + outputType
.toString());
101 var username
= ns
.getString("username");
103 final var useDbus
= ns
.getBoolean("dbus");
104 final var useDbusSystem
= ns
.getBoolean("dbus_system");
105 if (useDbus
|| useDbusSystem
) {
106 // If username is null, it will connect to the default object path
107 initDbusClient(command
, username
, useDbusSystem
);
112 var config
= ns
.getString("config");
113 if (config
!= null) {
114 dataPath
= new File(config
);
116 dataPath
= getDefaultDataPath();
119 final var serviceEnvironmentCli
= ns
.<ServiceEnvironmentCli
>get("service_environment");
120 final var serviceEnvironment
= serviceEnvironmentCli
== ServiceEnvironmentCli
.LIVE
121 ? ServiceEnvironment
.LIVE
122 : ServiceEnvironment
.SANDBOX
;
124 if (!ServiceConfig
.getCapabilities().isGv2()) {
125 logger
.warn("WARNING: Support for new group V2 is disabled,"
126 + " because the required native library dependency is missing: libzkgroup");
129 if (!ServiceConfig
.isSignalClientAvailable()) {
130 throw new UserErrorException("Missing required native library dependency: libsignal-client");
133 if (command
instanceof ProvisioningCommand
) {
134 if (username
!= null) {
135 throw new UserErrorException("You cannot specify a username (phone number) when linking");
138 handleProvisioningCommand((ProvisioningCommand
) command
, dataPath
, serviceEnvironment
);
142 if (username
== null) {
143 var usernames
= Manager
.getAllLocalUsernames(dataPath
);
145 if (command
instanceof MultiLocalCommand
) {
146 handleMultiLocalCommand((MultiLocalCommand
) command
, dataPath
, serviceEnvironment
, usernames
);
150 if (usernames
.size() == 0) {
151 throw new UserErrorException("No local users found, you first need to register or link an account");
152 } else if (usernames
.size() > 1) {
153 throw new UserErrorException(
154 "Multiple users found, you need to specify a username (phone number) with -u");
157 username
= usernames
.get(0);
158 } else if (!PhoneNumberFormatter
.isValidNumber(username
, null)) {
159 throw new UserErrorException("Invalid username (phone number), make sure you include the country code.");
162 if (command
instanceof RegistrationCommand
) {
163 handleRegistrationCommand((RegistrationCommand
) command
, username
, dataPath
, serviceEnvironment
);
167 if (!(command
instanceof LocalCommand
)) {
168 throw new UserErrorException("Command only works via dbus");
171 handleLocalCommand((LocalCommand
) command
, username
, dataPath
, serviceEnvironment
);
174 private void handleProvisioningCommand(
175 final ProvisioningCommand command
, final File dataPath
, final ServiceEnvironment serviceEnvironment
176 ) throws CommandException
{
177 var pm
= ProvisioningManager
.init(dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
178 command
.handleCommand(ns
, pm
);
181 private void handleRegistrationCommand(
182 final RegistrationCommand command
,
183 final String username
,
185 final ServiceEnvironment serviceEnvironment
186 ) throws CommandException
{
187 final RegistrationManager manager
;
189 manager
= RegistrationManager
.init(username
, dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
190 } catch (Throwable e
) {
191 throw new UnexpectedErrorException("Error loading or creating state file: "
194 + e
.getClass().getSimpleName()
197 try (var m
= manager
) {
198 command
.handleCommand(ns
, m
);
199 } catch (IOException e
) {
200 logger
.warn("Cleanup failed", e
);
204 private void handleLocalCommand(
205 final LocalCommand command
,
206 final String username
,
208 final ServiceEnvironment serviceEnvironment
209 ) throws CommandException
{
210 try (var m
= loadManager(username
, dataPath
, serviceEnvironment
)) {
211 command
.handleCommand(ns
, m
);
212 } catch (IOException e
) {
213 logger
.warn("Cleanup failed", e
);
217 private void handleMultiLocalCommand(
218 final MultiLocalCommand command
,
220 final ServiceEnvironment serviceEnvironment
,
221 final List
<String
> usernames
222 ) throws CommandException
{
223 final var managers
= new ArrayList
<Manager
>();
224 for (String u
: usernames
) {
226 managers
.add(loadManager(u
, dataPath
, serviceEnvironment
));
227 } catch (CommandException e
) {
228 logger
.warn("Ignoring {}: {}", u
, e
.getMessage());
232 command
.handleCommand(ns
, managers
);
234 for (var m
: managers
) {
237 } catch (IOException e
) {
238 logger
.warn("Cleanup failed", e
);
243 private Manager
loadManager(
244 final String username
, final File dataPath
, final ServiceEnvironment serviceEnvironment
245 ) throws CommandException
{
248 manager
= Manager
.init(username
, dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
249 } catch (NotRegisteredException e
) {
250 throw new UserErrorException("User " + username
+ " is not registered.");
251 } catch (Throwable e
) {
252 logger
.debug("Loading state file failed", e
);
253 throw new UnexpectedErrorException("Error loading state file for user "
258 + e
.getClass().getSimpleName()
263 manager
.checkAccountState();
264 } catch (IOException e
) {
265 throw new UnexpectedErrorException("Error while checking account " + username
+ ": " + e
.getMessage());
271 private void initDbusClient(
272 final Command command
, final String username
, final boolean systemBus
273 ) throws CommandException
{
275 DBusConnection
.DBusBusType busType
;
277 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
279 busType
= DBusConnection
.DBusBusType
.SESSION
;
281 try (var dBusConn
= DBusConnection
.getConnection(busType
)) {
282 var ts
= dBusConn
.getRemoteObject(DbusConfig
.getBusname(),
283 DbusConfig
.getObjectPath(username
),
286 handleCommand(command
, ts
, dBusConn
);
288 } catch (DBusException
| IOException e
) {
289 logger
.error("Dbus client failed", e
);
290 throw new UnexpectedErrorException("Dbus client failed");
294 private void handleCommand(Command command
, Signal ts
, DBusConnection dBusConn
) throws CommandException
{
295 if (command
instanceof ExtendedDbusCommand
) {
296 ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
297 } else if (command
instanceof DbusCommand
) {
298 ((DbusCommand
) command
).handleCommand(ns
, ts
);
300 throw new UserErrorException("Command is not yet implemented via dbus");
305 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
306 * - $HOME/.config/signal
307 * - $HOME/.config/textsecure
309 * @return the data directory to be used by signal-cli.
311 private static File
getDefaultDataPath() {
312 var dataPath
= new File(IOUtils
.getDataHomeDir(), "signal-cli");
313 if (dataPath
.exists()) {
317 var configPath
= new File(System
.getProperty("user.home"), ".config");
319 var legacySettingsPath
= new File(configPath
, "signal");
320 if (legacySettingsPath
.exists()) {
321 logger
.warn("Using legacy data path \"{}\", please move it to \"{}\".", legacySettingsPath
, dataPath
);
322 return legacySettingsPath
;
325 legacySettingsPath
= new File(configPath
, "textsecure");
326 if (legacySettingsPath
.exists()) {
327 logger
.warn("Using legacy data path \"{}\", please move it to \"{}\".", legacySettingsPath
, dataPath
);
328 return legacySettingsPath
;