1 package org
.asamk
.signal
;
3 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import org
.asamk
.Signal
;
6 import org
.asamk
.signal
.commands
.Command
;
7 import org
.asamk
.signal
.commands
.Commands
;
8 import org
.asamk
.signal
.commands
.DbusCommand
;
9 import org
.asamk
.signal
.commands
.ExtendedDbusCommand
;
10 import org
.asamk
.signal
.commands
.LocalCommand
;
11 import org
.asamk
.signal
.commands
.ProvisioningCommand
;
12 import org
.asamk
.signal
.commands
.RegistrationCommand
;
13 import org
.asamk
.signal
.dbus
.DbusSignalImpl
;
14 import org
.asamk
.signal
.manager
.Manager
;
15 import org
.asamk
.signal
.manager
.NotRegisteredException
;
16 import org
.asamk
.signal
.manager
.ProvisioningManager
;
17 import org
.asamk
.signal
.manager
.RegistrationManager
;
18 import org
.asamk
.signal
.manager
.ServiceConfig
;
19 import org
.asamk
.signal
.util
.IOUtils
;
20 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
21 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
22 import org
.slf4j
.Logger
;
23 import org
.slf4j
.LoggerFactory
;
24 import org
.whispersystems
.signalservice
.internal
.configuration
.SignalServiceConfiguration
;
27 import java
.io
.IOException
;
31 private final static Logger logger
= LoggerFactory
.getLogger(Main
.class);
33 private final Namespace ns
;
35 public Cli(final Namespace ns
) {
40 Command command
= getCommand();
41 if (command
== null) {
42 logger
.error("Command not implemented!");
46 if (ns
.getBoolean("dbus") || ns
.getBoolean("dbus_system")) {
47 return initDbusClient(command
, ns
.getBoolean("dbus_system"));
50 final String username
= ns
.getString("username");
53 String config
= ns
.getString("config");
55 dataPath
= new File(config
);
57 dataPath
= getDefaultDataPath();
60 final SignalServiceConfiguration serviceConfiguration
= ServiceConfig
.createDefaultServiceConfiguration(
61 BaseConfig
.USER_AGENT
);
63 if (!ServiceConfig
.getCapabilities().isGv2()) {
64 logger
.warn("WARNING: Support for new group V2 is disabled,"
65 + " because the required native library dependency is missing: libzkgroup");
68 if (username
== null) {
69 ProvisioningManager pm
= new ProvisioningManager(dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
70 return handleCommand(command
, pm
);
73 if (command
instanceof RegistrationCommand
) {
74 final RegistrationManager manager
;
76 manager
= RegistrationManager
.init(username
, dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
77 } catch (Throwable e
) {
78 logger
.error("Error loading or creating state file: {}", e
.getMessage());
81 try (RegistrationManager m
= manager
) {
82 return handleCommand(command
, m
);
83 } catch (Exception e
) {
84 logger
.error("Cleanup failed", e
);
91 manager
= Manager
.init(username
, dataPath
, serviceConfiguration
, BaseConfig
.USER_AGENT
);
92 } catch (NotRegisteredException e
) {
93 System
.err
.println("User is not registered.");
95 } catch (Throwable e
) {
96 logger
.error("Error loading state file: {}", e
.getMessage());
100 try (Manager m
= manager
) {
102 m
.checkAccountState();
103 } catch (IOException e
) {
104 logger
.error("Error while checking account: {}", e
.getMessage());
108 return handleCommand(command
, m
);
109 } catch (IOException e
) {
110 logger
.error("Cleanup failed", e
);
115 private Command
getCommand() {
116 String commandKey
= ns
.getString("command");
117 return Commands
.getCommand(commandKey
);
120 private int initDbusClient(final Command command
, final boolean systemBus
) {
122 DBusConnection
.DBusBusType busType
;
124 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
126 busType
= DBusConnection
.DBusBusType
.SESSION
;
128 try (DBusConnection dBusConn
= DBusConnection
.getConnection(busType
)) {
129 Signal ts
= dBusConn
.getRemoteObject(DbusConfig
.SIGNAL_BUSNAME
,
130 DbusConfig
.SIGNAL_OBJECTPATH
,
133 return handleCommand(command
, ts
, dBusConn
);
135 } catch (DBusException
| IOException e
) {
136 logger
.error("Dbus client failed", e
);
141 private int handleCommand(Command command
, Signal ts
, DBusConnection dBusConn
) {
142 if (command
instanceof ExtendedDbusCommand
) {
143 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
144 } else if (command
instanceof DbusCommand
) {
145 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
147 System
.err
.println("Command is not yet implemented via dbus");
152 private int handleCommand(Command command
, ProvisioningManager pm
) {
153 if (command
instanceof ProvisioningCommand
) {
154 return ((ProvisioningCommand
) command
).handleCommand(ns
, pm
);
156 System
.err
.println("Command only works with a username");
161 private int handleCommand(Command command
, RegistrationManager m
) {
162 if (command
instanceof RegistrationCommand
) {
163 return ((RegistrationCommand
) command
).handleCommand(ns
, m
);
168 private int handleCommand(Command command
, Manager m
) {
169 if (command
instanceof LocalCommand
) {
170 return ((LocalCommand
) command
).handleCommand(ns
, m
);
171 } else if (command
instanceof DbusCommand
) {
172 return ((DbusCommand
) command
).handleCommand(ns
, new DbusSignalImpl(m
));
174 System
.err
.println("Command only works via dbus");
180 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
181 * - $HOME/.config/signal
182 * - $HOME/.config/textsecure
184 * @return the data directory to be used by signal-cli.
186 private static File
getDefaultDataPath() {
187 File dataPath
= new File(IOUtils
.getDataHomeDir(), "signal-cli");
188 if (dataPath
.exists()) {
192 File configPath
= new File(System
.getProperty("user.home"), ".config");
194 File legacySettingsPath
= new File(configPath
, "signal");
195 if (legacySettingsPath
.exists()) {
196 return legacySettingsPath
;
199 legacySettingsPath
= new File(configPath
, "textsecure");
200 if (legacySettingsPath
.exists()) {
201 return legacySettingsPath
;