mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 02:20:39 +00:00
For daemon command all local users will be exposed as dbus objects If only one local user exists, all other commands will use that user, otherwise a user has to be specified.
275 lines
9.6 KiB
Java
275 lines
9.6 KiB
Java
package org.asamk.signal;
|
|
|
|
import net.sourceforge.argparse4j.inf.Namespace;
|
|
|
|
import org.asamk.Signal;
|
|
import org.asamk.signal.commands.Command;
|
|
import org.asamk.signal.commands.Commands;
|
|
import org.asamk.signal.commands.DbusCommand;
|
|
import org.asamk.signal.commands.ExtendedDbusCommand;
|
|
import org.asamk.signal.commands.LocalCommand;
|
|
import org.asamk.signal.commands.MultiLocalCommand;
|
|
import org.asamk.signal.commands.ProvisioningCommand;
|
|
import org.asamk.signal.commands.RegistrationCommand;
|
|
import org.asamk.signal.manager.Manager;
|
|
import org.asamk.signal.manager.NotRegisteredException;
|
|
import org.asamk.signal.manager.ProvisioningManager;
|
|
import org.asamk.signal.manager.RegistrationManager;
|
|
import org.asamk.signal.manager.ServiceConfig;
|
|
import org.asamk.signal.util.IOUtils;
|
|
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
|
import org.freedesktop.dbus.exceptions.DBusException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
|
|
import org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class Cli {
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(Main.class);
|
|
|
|
private final Namespace ns;
|
|
|
|
public Cli(final Namespace ns) {
|
|
this.ns = ns;
|
|
}
|
|
|
|
public int init() {
|
|
Command command = getCommand();
|
|
if (command == null) {
|
|
logger.error("Command not implemented!");
|
|
return 1;
|
|
}
|
|
|
|
String username = ns.getString("username");
|
|
|
|
if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) {
|
|
// If username is null, it will connect to the default object path
|
|
return initDbusClient(command, username, ns.getBoolean("dbus_system"));
|
|
}
|
|
|
|
final File dataPath;
|
|
String config = ns.getString("config");
|
|
if (config != null) {
|
|
dataPath = new File(config);
|
|
} else {
|
|
dataPath = getDefaultDataPath();
|
|
}
|
|
|
|
final SignalServiceConfiguration serviceConfiguration = ServiceConfig.createDefaultServiceConfiguration(
|
|
BaseConfig.USER_AGENT);
|
|
|
|
if (!ServiceConfig.getCapabilities().isGv2()) {
|
|
logger.warn("WARNING: Support for new group V2 is disabled,"
|
|
+ " because the required native library dependency is missing: libzkgroup");
|
|
}
|
|
|
|
if (command instanceof ProvisioningCommand) {
|
|
if (username != null) {
|
|
System.err.println("You cannot specify a username (phone number) when linking");
|
|
return 1;
|
|
}
|
|
|
|
return handleProvisioningCommand((ProvisioningCommand) command, dataPath, serviceConfiguration);
|
|
}
|
|
|
|
if (username == null) {
|
|
List<String> usernames = Manager.getAllLocalUsernames(dataPath);
|
|
if (usernames.size() == 0) {
|
|
System.err.println("No local users found, you first need to register or link an account");
|
|
return 1;
|
|
}
|
|
|
|
if (command instanceof MultiLocalCommand) {
|
|
return handleMultiLocalCommand((MultiLocalCommand) command, dataPath, serviceConfiguration, usernames);
|
|
}
|
|
|
|
if (usernames.size() > 1) {
|
|
System.err.println("Multiple users found, you need to specify a username (phone number) with -u");
|
|
return 1;
|
|
}
|
|
|
|
username = usernames.get(0);
|
|
} else if (!PhoneNumberFormatter.isValidNumber(username, null)) {
|
|
System.err.println("Invalid username (phone number), make sure you include the country code.");
|
|
return 1;
|
|
}
|
|
|
|
if (command instanceof RegistrationCommand) {
|
|
return handleRegistrationCommand((RegistrationCommand) command, username, dataPath, serviceConfiguration);
|
|
}
|
|
|
|
if (!(command instanceof LocalCommand)) {
|
|
System.err.println("Command only works via dbus");
|
|
return 1;
|
|
}
|
|
|
|
return handleLocalCommand((LocalCommand) command, username, dataPath, serviceConfiguration);
|
|
}
|
|
|
|
private int handleProvisioningCommand(
|
|
final ProvisioningCommand command,
|
|
final File dataPath,
|
|
final SignalServiceConfiguration serviceConfiguration
|
|
) {
|
|
ProvisioningManager pm = new ProvisioningManager(dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
|
|
return command.handleCommand(ns, pm);
|
|
}
|
|
|
|
private int handleRegistrationCommand(
|
|
final RegistrationCommand command,
|
|
final String username,
|
|
final File dataPath,
|
|
final SignalServiceConfiguration serviceConfiguration
|
|
) {
|
|
final RegistrationManager manager;
|
|
try {
|
|
manager = RegistrationManager.init(username, dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
|
|
} catch (Throwable e) {
|
|
logger.error("Error loading or creating state file: {}", e.getMessage());
|
|
return 2;
|
|
}
|
|
try (RegistrationManager m = manager) {
|
|
return command.handleCommand(ns, m);
|
|
} catch (IOException e) {
|
|
logger.error("Cleanup failed", e);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
private int handleLocalCommand(
|
|
final LocalCommand command,
|
|
final String username,
|
|
final File dataPath,
|
|
final SignalServiceConfiguration serviceConfiguration
|
|
) {
|
|
try (Manager m = loadManager(username, dataPath, serviceConfiguration)) {
|
|
if (m == null) {
|
|
return 2;
|
|
}
|
|
|
|
return command.handleCommand(ns, m);
|
|
} catch (IOException e) {
|
|
logger.error("Cleanup failed", e);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
private int handleMultiLocalCommand(
|
|
final MultiLocalCommand command,
|
|
final File dataPath,
|
|
final SignalServiceConfiguration serviceConfiguration,
|
|
final List<String> usernames
|
|
) {
|
|
final List<Manager> managers = usernames.stream()
|
|
.map(u -> loadManager(u, dataPath, serviceConfiguration))
|
|
.filter(Objects::nonNull)
|
|
.collect(Collectors.toList());
|
|
|
|
int result = command.handleCommand(ns, managers);
|
|
|
|
for (Manager m : managers) {
|
|
try {
|
|
m.close();
|
|
} catch (IOException e) {
|
|
logger.warn("Cleanup failed", e);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private Manager loadManager(
|
|
final String username, final File dataPath, final SignalServiceConfiguration serviceConfiguration
|
|
) {
|
|
Manager manager;
|
|
try {
|
|
manager = Manager.init(username, dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
|
|
} catch (NotRegisteredException e) {
|
|
logger.error("User " + username + " is not registered.");
|
|
return null;
|
|
} catch (Throwable e) {
|
|
logger.error("Error loading state file for user " + username + ": {}", e.getMessage());
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
manager.checkAccountState();
|
|
} catch (IOException e) {
|
|
logger.error("Error while checking account " + username + ": {}", e.getMessage());
|
|
return null;
|
|
}
|
|
|
|
return manager;
|
|
}
|
|
|
|
private Command getCommand() {
|
|
String commandKey = ns.getString("command");
|
|
return Commands.getCommand(commandKey);
|
|
}
|
|
|
|
private int initDbusClient(final Command command, final String username, final boolean systemBus) {
|
|
try {
|
|
DBusConnection.DBusBusType busType;
|
|
if (systemBus) {
|
|
busType = DBusConnection.DBusBusType.SYSTEM;
|
|
} else {
|
|
busType = DBusConnection.DBusBusType.SESSION;
|
|
}
|
|
try (DBusConnection dBusConn = DBusConnection.getConnection(busType)) {
|
|
Signal ts = dBusConn.getRemoteObject(DbusConfig.getBusname(),
|
|
DbusConfig.getObjectPath(username),
|
|
Signal.class);
|
|
|
|
return handleCommand(command, ts, dBusConn);
|
|
}
|
|
} catch (DBusException | IOException e) {
|
|
logger.error("Dbus client failed", e);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
private int handleCommand(Command command, Signal ts, DBusConnection dBusConn) {
|
|
if (command instanceof ExtendedDbusCommand) {
|
|
return ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn);
|
|
} else if (command instanceof DbusCommand) {
|
|
return ((DbusCommand) command).handleCommand(ns, ts);
|
|
} else {
|
|
System.err.println("Command is not yet implemented via dbus");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
|
|
* - $HOME/.config/signal
|
|
* - $HOME/.config/textsecure
|
|
*
|
|
* @return the data directory to be used by signal-cli.
|
|
*/
|
|
private static File getDefaultDataPath() {
|
|
File dataPath = new File(IOUtils.getDataHomeDir(), "signal-cli");
|
|
if (dataPath.exists()) {
|
|
return dataPath;
|
|
}
|
|
|
|
File configPath = new File(System.getProperty("user.home"), ".config");
|
|
|
|
File legacySettingsPath = new File(configPath, "signal");
|
|
if (legacySettingsPath.exists()) {
|
|
return legacySettingsPath;
|
|
}
|
|
|
|
legacySettingsPath = new File(configPath, "textsecure");
|
|
if (legacySettingsPath.exists()) {
|
|
return legacySettingsPath;
|
|
}
|
|
|
|
return dataPath;
|
|
}
|
|
}
|