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
.ServiceConfig
;
25 import org
.asamk
.signal
.util
.IOUtils
;
26 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
27 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
28 import org
.slf4j
.Logger
;
29 import org
.slf4j
.LoggerFactory
;
30 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
31 import org
.whispersystems
.signalservice
.internal
.configuration
.SignalServiceConfiguration
;
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 SignalServiceConfiguration serviceConfiguration
= ServiceConfig
.createDefaultServiceConfiguration(
118 BaseConfig
.USER_AGENT
);
120 if (!ServiceConfig
.getCapabilities().isGv2()) {
121 logger
.warn("WARNING: Support for new group V2 is disabled,"
122 + " because the required native library dependency is missing: libzkgroup");
125 if (command
instanceof ProvisioningCommand
) {
126 if (username
!= null) {
127 System
.err
.println("You cannot specify a username (phone number) when linking");
131 return handleProvisioningCommand((ProvisioningCommand
) command
, dataPath
, serviceConfiguration
);
134 if (username
== null) {
135 List
<String
> usernames
= Manager
.getAllLocalUsernames(dataPath
);
136 if (usernames
.size() == 0) {
137 System
.err
.println("No local users found, you first need to register or link an account");
141 if (command
instanceof MultiLocalCommand
) {
142 return handleMultiLocalCommand((MultiLocalCommand
) command
, dataPath
, serviceConfiguration
, usernames
);
145 if (usernames
.size() > 1) {
146 System
.err
.println("Multiple users found, you need to specify a username (phone number) with -u");
150 username
= usernames
.get(0);
151 } else if (!PhoneNumberFormatter
.isValidNumber(username
, null)) {
152 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
156 if (command
instanceof RegistrationCommand
) {
157 return handleRegistrationCommand((RegistrationCommand
) command
, username
, dataPath
, serviceConfiguration
);
160 if (!(command
instanceof LocalCommand
)) {
161 System
.err
.println("Command only works via dbus");
165 return handleLocalCommand((LocalCommand
) command
, username
, dataPath
, serviceConfiguration
);
168 private int handleProvisioningCommand(
169 final ProvisioningCommand command
,
171 final SignalServiceConfiguration serviceConfiguration
173 ProvisioningManager pm
= new ProvisioningManager(dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
174 return command
.handleCommand(ns
, pm
);
177 private int handleRegistrationCommand(
178 final RegistrationCommand command
,
179 final String username
,
181 final SignalServiceConfiguration serviceConfiguration
183 final RegistrationManager manager
;
185 manager
= RegistrationManager
.init(username
, dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
186 } catch (Throwable e
) {
187 logger
.error("Error loading or creating state file: {}", e
.getMessage());
190 try (RegistrationManager m
= manager
) {
191 return command
.handleCommand(ns
, m
);
192 } catch (IOException e
) {
193 logger
.error("Cleanup failed", e
);
198 private int handleLocalCommand(
199 final LocalCommand command
,
200 final String username
,
202 final SignalServiceConfiguration serviceConfiguration
204 try (Manager m
= loadManager(username
, dataPath
, serviceConfiguration
)) {
209 return command
.handleCommand(ns
, m
);
210 } catch (IOException e
) {
211 logger
.error("Cleanup failed", e
);
216 private int handleMultiLocalCommand(
217 final MultiLocalCommand command
,
219 final SignalServiceConfiguration serviceConfiguration
,
220 final List
<String
> usernames
222 final List
<Manager
> managers
= usernames
.stream()
223 .map(u
-> loadManager(u
, dataPath
, serviceConfiguration
))
224 .filter(Objects
::nonNull
)
225 .collect(Collectors
.toList());
227 int result
= command
.handleCommand(ns
, managers
);
229 for (Manager m
: managers
) {
232 } catch (IOException e
) {
233 logger
.warn("Cleanup failed", e
);
239 private Manager
loadManager(
240 final String username
, final File dataPath
, final SignalServiceConfiguration serviceConfiguration
244 manager
= Manager
.init(username
, dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
245 } catch (NotRegisteredException e
) {
246 logger
.error("User " + username
+ " is not registered.");
248 } catch (Throwable e
) {
249 logger
.error("Error loading state file for user " + username
+ ": {}", e
.getMessage());
254 manager
.checkAccountState();
255 } catch (IOException e
) {
256 logger
.error("Error while checking account " + username
+ ": {}", e
.getMessage());
263 private int initDbusClient(final Command command
, final String username
, final boolean systemBus
) {
265 DBusConnection
.DBusBusType busType
;
267 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
269 busType
= DBusConnection
.DBusBusType
.SESSION
;
271 try (DBusConnection dBusConn
= DBusConnection
.getConnection(busType
)) {
272 Signal ts
= dBusConn
.getRemoteObject(DbusConfig
.getBusname(),
273 DbusConfig
.getObjectPath(username
),
276 return handleCommand(command
, ts
, dBusConn
);
278 } catch (DBusException
| IOException e
) {
279 logger
.error("Dbus client failed", e
);
284 private int handleCommand(Command command
, Signal ts
, DBusConnection dBusConn
) {
285 if (command
instanceof ExtendedDbusCommand
) {
286 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
287 } else if (command
instanceof DbusCommand
) {
288 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
290 System
.err
.println("Command is not yet implemented via dbus");
296 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
297 * - $HOME/.config/signal
298 * - $HOME/.config/textsecure
300 * @return the data directory to be used by signal-cli.
302 private static File
getDefaultDataPath() {
303 File dataPath
= new File(IOUtils
.getDataHomeDir(), "signal-cli");
304 if (dataPath
.exists()) {
308 File configPath
= new File(System
.getProperty("user.home"), ".config");
310 File legacySettingsPath
= new File(configPath
, "signal");
311 if (legacySettingsPath
.exists()) {
312 return legacySettingsPath
;
315 legacySettingsPath
= new File(configPath
, "textsecure");
316 if (legacySettingsPath
.exists()) {
317 return legacySettingsPath
;