Allow calling signal-cli without -u flag

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.
This commit is contained in:
AsamK 2021-01-15 18:28:54 +01:00
parent a97bbf8608
commit ca86c421eb
19 changed files with 356 additions and 172 deletions

View file

@ -8,9 +8,9 @@ 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.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotRegisteredException;
import org.asamk.signal.manager.ProvisioningManager;
@ -21,10 +21,14 @@ 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 {
@ -40,15 +44,16 @@ public class Cli {
Command command = getCommand();
if (command == null) {
logger.error("Command not implemented!");
return 2;
return 1;
}
String username = ns.getString("username");
if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) {
return initDbusClient(command, 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 String username = ns.getString("username");
final File dataPath;
String config = ns.getString("config");
if (config != null) {
@ -65,59 +70,150 @@ public class Cli {
+ " because the required native library dependency is missing: libzkgroup");
}
if (username == null) {
ProvisioningManager pm = new ProvisioningManager(dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
return handleCommand(command, pm);
}
if (command instanceof RegistrationCommand) {
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());
if (command instanceof ProvisioningCommand) {
if (username != null) {
System.err.println("You cannot specify a username (phone number) when linking");
return 1;
}
try (RegistrationManager m = manager) {
return handleCommand(command, m);
} catch (Exception e) {
logger.error("Cleanup failed", e);
return 2;
}
return handleProvisioningCommand((ProvisioningCommand) command, dataPath, serviceConfiguration);
}
Manager manager;
try {
manager = Manager.init(username, dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
} catch (NotRegisteredException e) {
System.err.println("User is not registered.");
return 0;
} catch (Throwable e) {
logger.error("Error loading state file: {}", e.getMessage());
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;
}
try (Manager m = manager) {
try {
m.checkAccountState();
} catch (IOException e) {
logger.error("Error while checking account: {}", e.getMessage());
return 1;
}
if (command instanceof RegistrationCommand) {
return handleRegistrationCommand((RegistrationCommand) command, username, dataPath, serviceConfiguration);
}
return handleCommand(command, m);
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 boolean systemBus) {
private int initDbusClient(final Command command, final String username, final boolean systemBus) {
try {
DBusConnection.DBusBusType busType;
if (systemBus) {
@ -126,8 +222,8 @@ public class Cli {
busType = DBusConnection.DBusBusType.SESSION;
}
try (DBusConnection dBusConn = DBusConnection.getConnection(busType)) {
Signal ts = dBusConn.getRemoteObject(DbusConfig.SIGNAL_BUSNAME,
DbusConfig.SIGNAL_OBJECTPATH,
Signal ts = dBusConn.getRemoteObject(DbusConfig.getBusname(),
DbusConfig.getObjectPath(username),
Signal.class);
return handleCommand(command, ts, dBusConn);
@ -149,33 +245,6 @@ public class Cli {
}
}
private int handleCommand(Command command, ProvisioningManager pm) {
if (command instanceof ProvisioningCommand) {
return ((ProvisioningCommand) command).handleCommand(ns, pm);
} else {
System.err.println("Command only works with a username");
return 1;
}
}
private int handleCommand(Command command, RegistrationManager m) {
if (command instanceof RegistrationCommand) {
return ((RegistrationCommand) command).handleCommand(ns, m);
}
return 1;
}
private int handleCommand(Command command, Manager m) {
if (command instanceof LocalCommand) {
return ((LocalCommand) command).handleCommand(ns, m);
} else if (command instanceof DbusCommand) {
return ((DbusCommand) command).handleCommand(ns, new DbusSignalImpl(m));
} else {
System.err.println("Command only works via dbus");
return 1;
}
}
/**
* Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
* - $HOME/.config/signal