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 used for verification.");
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 var subparsers
= parser
.addSubparsers().title("subcommands").dest("command");
71 final var commands
= Commands
.getCommands();
72 for (var entry
: commands
.entrySet()) {
73 var subparser
= subparsers
.addParser(entry
.getKey());
74 entry
.getValue().attachToSubparser(subparser
);
80 public App(final Namespace ns
) {
84 public void init() throws CommandException
{
85 var commandKey
= ns
.getString("command");
86 var command
= Commands
.getCommand(commandKey
);
87 if (command
== null) {
88 throw new UserErrorException("Command not implemented!");
91 OutputType outputType
= ns
.get("output");
92 if (!command
.getSupportedOutputTypes().contains(outputType
)) {
93 throw new UserErrorException("Command doesn't support output type " + outputType
.toString());
96 var username
= ns
.getString("username");
98 final boolean useDbus
= ns
.getBoolean("dbus");
99 final boolean useDbusSystem
= ns
.getBoolean("dbus_system");
100 if (useDbus
|| useDbusSystem
) {
101 // If username is null, it will connect to the default object path
102 initDbusClient(command
, username
, useDbusSystem
);
107 var config
= ns
.getString("config");
108 if (config
!= null) {
109 dataPath
= new File(config
);
111 dataPath
= getDefaultDataPath();
114 final var serviceEnvironment
= ServiceEnvironment
.LIVE
;
116 if (!ServiceConfig
.getCapabilities().isGv2()) {
117 logger
.warn("WARNING: Support for new group V2 is disabled,"
118 + " because the required native library dependency is missing: libzkgroup");
121 if (!ServiceConfig
.isSignalClientAvailable()) {
122 throw new UserErrorException("Missing required native library dependency: libsignal-client");
125 if (command
instanceof ProvisioningCommand
) {
126 if (username
!= null) {
127 throw new UserErrorException("You cannot specify a username (phone number) when linking");
130 handleProvisioningCommand((ProvisioningCommand
) command
, dataPath
, serviceEnvironment
);
134 if (username
== null) {
135 var usernames
= Manager
.getAllLocalUsernames(dataPath
);
136 if (usernames
.size() == 0) {
137 throw new UserErrorException("No local users found, you first need to register or link an account");
140 if (command
instanceof MultiLocalCommand
) {
141 handleMultiLocalCommand((MultiLocalCommand
) command
, dataPath
, serviceEnvironment
, usernames
);
145 if (usernames
.size() > 1) {
146 throw new UserErrorException(
147 "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 throw new UserErrorException("Invalid username (phone number), make sure you include the country code.");
155 if (command
instanceof RegistrationCommand
) {
156 handleRegistrationCommand((RegistrationCommand
) command
, username
, dataPath
, serviceEnvironment
);
160 if (!(command
instanceof LocalCommand
)) {
161 throw new UserErrorException("Command only works via dbus");
164 handleLocalCommand((LocalCommand
) command
, username
, dataPath
, serviceEnvironment
);
167 private void handleProvisioningCommand(
168 final ProvisioningCommand command
, final File dataPath
, final ServiceEnvironment serviceEnvironment
169 ) throws CommandException
{
170 var pm
= ProvisioningManager
.init(dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
171 command
.handleCommand(ns
, pm
);
174 private void handleRegistrationCommand(
175 final RegistrationCommand command
,
176 final String username
,
178 final ServiceEnvironment serviceEnvironment
179 ) throws CommandException
{
180 final RegistrationManager manager
;
182 manager
= RegistrationManager
.init(username
, dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
183 } catch (Throwable e
) {
184 throw new UnexpectedErrorException("Error loading or creating state file: "
187 + e
.getClass().getSimpleName()
190 try (var m
= manager
) {
191 command
.handleCommand(ns
, m
);
192 } catch (IOException e
) {
193 logger
.warn("Cleanup failed", e
);
197 private void handleLocalCommand(
198 final LocalCommand command
,
199 final String username
,
201 final ServiceEnvironment serviceEnvironment
202 ) throws CommandException
{
203 try (var m
= loadManager(username
, dataPath
, serviceEnvironment
)) {
204 command
.handleCommand(ns
, m
);
205 } catch (IOException e
) {
206 logger
.warn("Cleanup failed", e
);
210 private void handleMultiLocalCommand(
211 final MultiLocalCommand command
,
213 final ServiceEnvironment serviceEnvironment
,
214 final List
<String
> usernames
215 ) throws CommandException
{
216 final var managers
= new ArrayList
<Manager
>();
217 for (String u
: usernames
) {
218 managers
.add(loadManager(u
, dataPath
, serviceEnvironment
));
221 command
.handleCommand(ns
, managers
);
223 for (var m
: managers
) {
226 } catch (IOException e
) {
227 logger
.warn("Cleanup failed", e
);
232 private Manager
loadManager(
233 final String username
, final File dataPath
, final ServiceEnvironment serviceEnvironment
234 ) throws CommandException
{
237 manager
= Manager
.init(username
, dataPath
, serviceEnvironment
, BaseConfig
.USER_AGENT
);
238 } catch (NotRegisteredException e
) {
239 throw new UserErrorException("User " + username
+ " is not registered.");
240 } catch (Throwable e
) {
241 throw new UnexpectedErrorException("Error loading state file for user "
246 + e
.getClass().getSimpleName()
251 manager
.checkAccountState();
252 } catch (IOException e
) {
253 throw new UnexpectedErrorException("Error while checking account " + username
+ ": " + e
.getMessage());
259 private void initDbusClient(
260 final Command command
, final String username
, final boolean systemBus
261 ) throws CommandException
{
263 DBusConnection
.DBusBusType busType
;
265 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
267 busType
= DBusConnection
.DBusBusType
.SESSION
;
269 try (var dBusConn
= DBusConnection
.getConnection(busType
)) {
270 var ts
= dBusConn
.getRemoteObject(DbusConfig
.getBusname(),
271 DbusConfig
.getObjectPath(username
),
274 handleCommand(command
, ts
, dBusConn
);
276 } catch (DBusException
| IOException e
) {
277 logger
.error("Dbus client failed", e
);
278 throw new UnexpectedErrorException("Dbus client failed");
282 private void handleCommand(Command command
, Signal ts
, DBusConnection dBusConn
) throws CommandException
{
283 if (command
instanceof ExtendedDbusCommand
) {
284 ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
285 } else if (command
instanceof DbusCommand
) {
286 ((DbusCommand
) command
).handleCommand(ns
, ts
);
288 throw new UserErrorException("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 var dataPath
= new File(IOUtils
.getDataHomeDir(), "signal-cli");
301 if (dataPath
.exists()) {
305 var configPath
= new File(System
.getProperty("user.home"), ".config");
307 var 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
;