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
.MutuallyExclusiveGroup
;
7 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
8 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
9 import net
.sourceforge
.argparse4j
.inf
.Subparsers
;
11 import org
.asamk
.Signal
;
12 import org
.asamk
.signal
.commands
.Command
;
13 import org
.asamk
.signal
.commands
.Commands
;
14 import org
.asamk
.signal
.commands
.DbusCommand
;
15 import org
.asamk
.signal
.commands
.ExtendedDbusCommand
;
16 import org
.asamk
.signal
.commands
.LocalCommand
;
17 import org
.asamk
.signal
.commands
.MultiLocalCommand
;
18 import org
.asamk
.signal
.commands
.ProvisioningCommand
;
19 import org
.asamk
.signal
.commands
.RegistrationCommand
;
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
.List
;
37 import java
.util
.Objects
;
38 import java
.util
.stream
.Collectors
;
42 private final static Logger logger
= LoggerFactory
.getLogger(App
.class);
44 private final Namespace ns
;
46 static ArgumentParser
buildArgumentParser() {
47 ArgumentParser parser
= ArgumentParsers
.newFor("signal-cli")
50 .description("Commandline interface for Signal.")
51 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
53 parser
.addArgument("-v", "--version").help("Show package version.").action(Arguments
.version());
54 parser
.addArgument("--verbose")
55 .help("Raise log level and include lib signal logs.")
56 .action(Arguments
.storeTrue());
57 parser
.addArgument("--config")
58 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
60 parser
.addArgument("-u", "--username").help("Specify your phone number, that will be used for verification.");
62 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
63 mut
.addArgument("--dbus").help("Make request via user dbus.").action(Arguments
.storeTrue());
64 mut
.addArgument("--dbus-system").help("Make request via system dbus.").action(Arguments
.storeTrue());
66 parser
.addArgument("-o", "--output")
67 .help("Choose to output in plain text or JSON")
68 .type(Arguments
.enumStringType(OutputType
.class))
69 .setDefault(OutputType
.PLAIN_TEXT
);
71 Subparsers subparsers
= parser
.addSubparsers().title("subcommands").dest("command");
73 final Map
<String
, Command
> commands
= Commands
.getCommands();
74 for (Map
.Entry
<String
, Command
> entry
: commands
.entrySet()) {
75 Subparser subparser
= subparsers
.addParser(entry
.getKey());
76 entry
.getValue().attachToSubparser(subparser
);
82 public App(final Namespace ns
) {
87 String commandKey
= ns
.getString("command");
88 Command command
= Commands
.getCommand(commandKey
);
89 if (command
== null) {
90 logger
.error("Command not implemented!");
94 OutputType outputType
= ns
.get("output");
95 if (!command
.getSupportedOutputTypes().contains(outputType
)) {
96 logger
.error("Command doesn't support output type {}", outputType
.toString());
100 String username
= ns
.getString("username");
102 final boolean useDbus
= ns
.getBoolean("dbus");
103 final boolean useDbusSystem
= ns
.getBoolean("dbus_system");
104 if (useDbus
|| useDbusSystem
) {
105 // If username is null, it will connect to the default object path
106 return initDbusClient(command
, username
, useDbusSystem
);
110 String config
= ns
.getString("config");
111 if (config
!= null) {
112 dataPath
= new File(config
);
114 dataPath
= getDefaultDataPath();
117 final ServiceEnvironment serviceEnvironment
= ServiceEnvironment
.LIVE
;
119 if (!ServiceConfig
.getCapabilities().isGv2()) {
120 logger
.warn("WARNING: Support for new group V2 is disabled,"
121 + " because the required native library dependency is missing: libzkgroup");
124 if (command
instanceof ProvisioningCommand
) {
125 if (username
!= null) {
126 System
.err
.println("You cannot specify a username (phone number) when linking");
130 return handleProvisioningCommand((ProvisioningCommand
) command
, dataPath
, serviceEnvironment
);
133 if (username
== null) {
134 List
<String
> usernames
= Manager
.getAllLocalUsernames(dataPath
);
135 if (usernames
.size() == 0) {
136 System
.err
.println("No local users found, you first need to register or link an account");
140 if (command
instanceof MultiLocalCommand
) {
141 return handleMultiLocalCommand((MultiLocalCommand
) command
, dataPath
, serviceEnvironment
, usernames
);
144 if (usernames
.size() > 1) {
145 System
.err
.println("Multiple users found, you need to specify a username (phone number) with -u");
149 username
= usernames
.get(0);
150 } else if (!PhoneNumberFormatter
.isValidNumber(username
, null)) {
151 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
155 if (command
instanceof RegistrationCommand
) {
156 return handleRegistrationCommand((RegistrationCommand
) command
, username
, dataPath
, serviceEnvironment
);
159 if (!(command
instanceof LocalCommand
)) {
160 System
.err
.println("Command only works via dbus");
164 return handleLocalCommand((LocalCommand
) command
, username
, dataPath
, serviceEnvironment
);
167 private int handleProvisioningCommand(
168 final ProvisioningCommand command
, final File dataPath
, final ServiceEnvironment serviceEnvironment
170 ProvisioningManager pm
= ProvisioningManager
.init(dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
171 return command
.handleCommand(ns
, pm
);
174 private int handleRegistrationCommand(
175 final RegistrationCommand command
,
176 final String username
,
178 final ServiceEnvironment serviceEnvironment
180 final RegistrationManager manager
;
182 manager
= RegistrationManager
.init(username
, dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
183 } catch (Throwable e
) {
184 logger
.error("Error loading or creating state file: {}", e
.getMessage());
187 try (RegistrationManager m
= manager
) {
188 return command
.handleCommand(ns
, m
);
189 } catch (IOException e
) {
190 logger
.error("Cleanup failed", e
);
195 private int handleLocalCommand(
196 final LocalCommand command
,
197 final String username
,
199 final ServiceEnvironment serviceEnvironment
201 try (Manager m
= loadManager(username
, dataPath
, serviceEnvironment
)) {
206 return command
.handleCommand(ns
, m
);
207 } catch (IOException e
) {
208 logger
.error("Cleanup failed", e
);
213 private int handleMultiLocalCommand(
214 final MultiLocalCommand command
,
216 final ServiceEnvironment serviceEnvironment
,
217 final List
<String
> usernames
219 final List
<Manager
> managers
= usernames
.stream()
220 .map(u
-> loadManager(u
, dataPath
, serviceEnvironment
))
221 .filter(Objects
::nonNull
)
222 .collect(Collectors
.toList());
224 int result
= command
.handleCommand(ns
, managers
);
226 for (Manager m
: managers
) {
229 } catch (IOException e
) {
230 logger
.warn("Cleanup failed", e
);
236 private Manager
loadManager(
237 final String username
, final File dataPath
, final ServiceEnvironment serviceEnvironment
241 manager
= Manager
.init(username
, dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
242 } catch (NotRegisteredException e
) {
243 logger
.error("User " + username
+ " is not registered.");
245 } catch (Throwable e
) {
246 logger
.error("Error loading state file for user " + username
+ ": {}", e
.getMessage());
251 manager
.checkAccountState();
252 } catch (IOException e
) {
253 logger
.error("Error while checking account " + username
+ ": {}", e
.getMessage());
260 private int initDbusClient(final Command command
, final String username
, final boolean systemBus
) {
262 DBusConnection
.DBusBusType busType
;
264 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
266 busType
= DBusConnection
.DBusBusType
.SESSION
;
268 try (DBusConnection dBusConn
= DBusConnection
.getConnection(busType
)) {
269 Signal ts
= dBusConn
.getRemoteObject(DbusConfig
.getBusname(),
270 DbusConfig
.getObjectPath(username
),
273 return handleCommand(command
, ts
, dBusConn
);
275 } catch (DBusException
| IOException e
) {
276 logger
.error("Dbus client failed", e
);
281 private int handleCommand(Command command
, Signal ts
, DBusConnection dBusConn
) {
282 if (command
instanceof ExtendedDbusCommand
) {
283 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
284 } else if (command
instanceof DbusCommand
) {
285 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
287 System
.err
.println("Command is not yet implemented via dbus");
293 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
294 * - $HOME/.config/signal
295 * - $HOME/.config/textsecure
297 * @return the data directory to be used by signal-cli.
299 private static File
getDefaultDataPath() {
300 File dataPath
= new File(IOUtils
.getDataHomeDir(), "signal-cli");
301 if (dataPath
.exists()) {
305 File configPath
= new File(System
.getProperty("user.home"), ".config");
307 File legacySettingsPath
= new File(configPath
, "signal");
308 if (legacySettingsPath
.exists()) {
309 return legacySettingsPath
;
312 legacySettingsPath
= new File(configPath
, "textsecure");
313 if (legacySettingsPath
.exists()) {
314 return legacySettingsPath
;