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(Main
.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 .choices("plain-text", "json")
69 .setDefault("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 Cli(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 String username
= ns
.getString("username");
96 final boolean useDbus
= ns
.getBoolean("dbus");
97 final boolean useDbusSystem
= ns
.getBoolean("dbus_system");
98 if (useDbus
|| useDbusSystem
) {
99 // If username is null, it will connect to the default object path
100 return initDbusClient(command
, username
, useDbusSystem
);
104 String config
= ns
.getString("config");
105 if (config
!= null) {
106 dataPath
= new File(config
);
108 dataPath
= getDefaultDataPath();
111 final SignalServiceConfiguration serviceConfiguration
= ServiceConfig
.createDefaultServiceConfiguration(
112 BaseConfig
.USER_AGENT
);
114 if (!ServiceConfig
.getCapabilities().isGv2()) {
115 logger
.warn("WARNING: Support for new group V2 is disabled,"
116 + " because the required native library dependency is missing: libzkgroup");
119 if (command
instanceof ProvisioningCommand
) {
120 if (username
!= null) {
121 System
.err
.println("You cannot specify a username (phone number) when linking");
125 return handleProvisioningCommand((ProvisioningCommand
) command
, dataPath
, serviceConfiguration
);
128 if (username
== null) {
129 List
<String
> usernames
= Manager
.getAllLocalUsernames(dataPath
);
130 if (usernames
.size() == 0) {
131 System
.err
.println("No local users found, you first need to register or link an account");
135 if (command
instanceof MultiLocalCommand
) {
136 return handleMultiLocalCommand((MultiLocalCommand
) command
, dataPath
, serviceConfiguration
, usernames
);
139 if (usernames
.size() > 1) {
140 System
.err
.println("Multiple users found, you need to specify a username (phone number) with -u");
144 username
= usernames
.get(0);
145 } else if (!PhoneNumberFormatter
.isValidNumber(username
, null)) {
146 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
150 if (command
instanceof RegistrationCommand
) {
151 return handleRegistrationCommand((RegistrationCommand
) command
, username
, dataPath
, serviceConfiguration
);
154 if (!(command
instanceof LocalCommand
)) {
155 System
.err
.println("Command only works via dbus");
159 return handleLocalCommand((LocalCommand
) command
, username
, dataPath
, serviceConfiguration
);
162 private int handleProvisioningCommand(
163 final ProvisioningCommand command
,
165 final SignalServiceConfiguration serviceConfiguration
167 ProvisioningManager pm
= new ProvisioningManager(dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
168 return command
.handleCommand(ns
, pm
);
171 private int handleRegistrationCommand(
172 final RegistrationCommand command
,
173 final String username
,
175 final SignalServiceConfiguration serviceConfiguration
177 final RegistrationManager manager
;
179 manager
= RegistrationManager
.init(username
, dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
180 } catch (Throwable e
) {
181 logger
.error("Error loading or creating state file: {}", e
.getMessage());
184 try (RegistrationManager m
= manager
) {
185 return command
.handleCommand(ns
, m
);
186 } catch (IOException e
) {
187 logger
.error("Cleanup failed", e
);
192 private int handleLocalCommand(
193 final LocalCommand command
,
194 final String username
,
196 final SignalServiceConfiguration serviceConfiguration
198 try (Manager m
= loadManager(username
, dataPath
, serviceConfiguration
)) {
203 return command
.handleCommand(ns
, m
);
204 } catch (IOException e
) {
205 logger
.error("Cleanup failed", e
);
210 private int handleMultiLocalCommand(
211 final MultiLocalCommand command
,
213 final SignalServiceConfiguration serviceConfiguration
,
214 final List
<String
> usernames
216 final List
<Manager
> managers
= usernames
.stream()
217 .map(u
-> loadManager(u
, dataPath
, serviceConfiguration
))
218 .filter(Objects
::nonNull
)
219 .collect(Collectors
.toList());
221 int result
= command
.handleCommand(ns
, managers
);
223 for (Manager m
: managers
) {
226 } catch (IOException e
) {
227 logger
.warn("Cleanup failed", e
);
233 private Manager
loadManager(
234 final String username
, final File dataPath
, final SignalServiceConfiguration serviceConfiguration
238 manager
= Manager
.init(username
, dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
239 } catch (NotRegisteredException e
) {
240 logger
.error("User " + username
+ " is not registered.");
242 } catch (Throwable e
) {
243 logger
.error("Error loading state file for user " + username
+ ": {}", e
.getMessage());
248 manager
.checkAccountState();
249 } catch (IOException e
) {
250 logger
.error("Error while checking account " + username
+ ": {}", e
.getMessage());
257 private int initDbusClient(final Command command
, final String username
, final boolean systemBus
) {
259 DBusConnection
.DBusBusType busType
;
261 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
263 busType
= DBusConnection
.DBusBusType
.SESSION
;
265 try (DBusConnection dBusConn
= DBusConnection
.getConnection(busType
)) {
266 Signal ts
= dBusConn
.getRemoteObject(DbusConfig
.getBusname(),
267 DbusConfig
.getObjectPath(username
),
270 return handleCommand(command
, ts
, dBusConn
);
272 } catch (DBusException
| IOException e
) {
273 logger
.error("Dbus client failed", e
);
278 private int handleCommand(Command command
, Signal ts
, DBusConnection dBusConn
) {
279 if (command
instanceof ExtendedDbusCommand
) {
280 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
281 } else if (command
instanceof DbusCommand
) {
282 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
284 System
.err
.println("Command is not yet implemented via dbus");
290 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
291 * - $HOME/.config/signal
292 * - $HOME/.config/textsecure
294 * @return the data directory to be used by signal-cli.
296 private static File
getDefaultDataPath() {
297 File dataPath
= new File(IOUtils
.getDataHomeDir(), "signal-cli");
298 if (dataPath
.exists()) {
302 File configPath
= new File(System
.getProperty("user.home"), ".config");
304 File legacySettingsPath
= new File(configPath
, "signal");
305 if (legacySettingsPath
.exists()) {
306 return legacySettingsPath
;
309 legacySettingsPath
= new File(configPath
, "textsecure");
310 if (legacySettingsPath
.exists()) {
311 return legacySettingsPath
;