X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/2879b31abab0d10a5f3f40a810f4f1689ecd5833..73d44ba3fee81e5a19f1055bd90366e37f0341ec:/src/main/java/org/asamk/signal/App.java diff --git a/src/main/java/org/asamk/signal/App.java b/src/main/java/org/asamk/signal/App.java index 060572aa..d06cd798 100644 --- a/src/main/java/org/asamk/signal/App.java +++ b/src/main/java/org/asamk/signal/App.java @@ -6,27 +6,36 @@ import net.sourceforge.argparse4j.inf.ArgumentParser; import net.sourceforge.argparse4j.inf.Namespace; import org.asamk.Signal; +import org.asamk.SignalControl; import org.asamk.signal.commands.Command; import org.asamk.signal.commands.Commands; 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.commands.SignalCreator; import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.dbus.DbusManagerImpl; +import org.asamk.signal.dbus.DbusMultiAccountManagerImpl; +import org.asamk.signal.dbus.DbusProvisioningManagerImpl; +import org.asamk.signal.dbus.DbusRegistrationManagerImpl; import org.asamk.signal.manager.Manager; +import org.asamk.signal.manager.MultiAccountManagerImpl; import org.asamk.signal.manager.NotRegisteredException; import org.asamk.signal.manager.ProvisioningManager; import org.asamk.signal.manager.RegistrationManager; import org.asamk.signal.manager.config.ServiceConfig; import org.asamk.signal.manager.config.ServiceEnvironment; import org.asamk.signal.manager.storage.identities.TrustNewIdentity; +import org.asamk.signal.output.JsonWriterImpl; +import org.asamk.signal.output.OutputWriter; +import org.asamk.signal.output.PlainTextWriterImpl; import org.asamk.signal.util.IOUtils; import org.freedesktop.dbus.connections.impl.DBusConnection; +import org.freedesktop.dbus.errors.ServiceUnknown; +import org.freedesktop.dbus.errors.UnknownMethod; import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.slf4j.Logger; @@ -39,8 +48,6 @@ import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; -import java.util.function.Consumer; -import java.util.stream.Collectors; import static net.sourceforge.argparse4j.DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS; @@ -60,9 +67,12 @@ public class App { parser.addArgument("-v", "--version").help("Show package version.").action(Arguments.version()); parser.addArgument("--verbose") - .help("Raise log level and include lib signal logs.") - .action(Arguments.storeTrue()); - parser.addArgument("--config") + .help("Raise log level and include lib signal logs. Specify multiple times for even more logs.") + .action(Arguments.count()); + parser.addArgument("--log-file") + .type(File.class) + .help("Write log output to the given file. If --verbose is also given, the detailed logs will only be written to the log file."); + parser.addArgument("-c", "--config") .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli)."); parser.addArgument("-a", "--account", "-u", "--username") @@ -141,11 +151,6 @@ public class App { dataPath = getDefaultDataPath(); } - if (!ServiceConfig.isZkgroupAvailable()) { - logger.warn("WARNING: Support for new group V2 is disabled," - + " because the required native library dependency is missing: libzkgroup"); - } - if (!ServiceConfig.isSignalClientAvailable()) { throw new UserErrorException("Missing required native library dependency: libsignal-client"); } @@ -153,7 +158,7 @@ public class App { final var serviceEnvironmentCli = ns.get("service-environment"); final var serviceEnvironment = serviceEnvironmentCli == ServiceEnvironmentCli.LIVE ? ServiceEnvironment.LIVE - : ServiceEnvironment.SANDBOX; + : ServiceEnvironment.STAGING; final var trustNewIdentityCli = ns.get("trust-new-identities"); final var trustNewIdentity = trustNewIdentityCli == TrustNewIdentityCli.ON_FIRST_USE @@ -186,7 +191,7 @@ public class App { throw new UserErrorException("No local users found, you first need to register or link an account"); } else if (accounts.size() > 1) { throw new UserErrorException( - "Multiple users found, you need to specify a account (phone number) with -u"); + "Multiple users found, you need to specify an account (phone number) with -a"); } account = accounts.get(0); @@ -200,7 +205,7 @@ public class App { } if (!(command instanceof LocalCommand)) { - throw new UserErrorException("Command only works via dbus"); + throw new UserErrorException("Command only works in multi-account mode"); } handleLocalCommand((LocalCommand) command, @@ -221,6 +226,22 @@ public class App { command.handleCommand(ns, pm, outputWriter); } + private void handleProvisioningCommand( + final ProvisioningCommand c, final DBusConnection dBusConn, final OutputWriter outputWriter + ) throws CommandException, DBusException { + final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(), + DbusConfig.getObjectPath(), + SignalControl.class); + final var provisioningManager = new DbusProvisioningManagerImpl(signalControl, dBusConn); + try { + c.handleCommand(ns, provisioningManager, outputWriter); + } catch (UnsupportedOperationException e) { + throw new UserErrorException("Command is not yet implemented via dbus", e); + } catch (DBusExecutionException e) { + throw new UnexpectedErrorException(e.getMessage(), e); + } + } + private void handleRegistrationCommand( final RegistrationCommand command, final String account, @@ -237,13 +258,28 @@ public class App { + e.getClass().getSimpleName() + ")", e); } - try (var m = manager) { - command.handleCommand(ns, m); + try (manager) { + command.handleCommand(ns, manager); } catch (IOException e) { logger.warn("Cleanup failed", e); } } + private void handleRegistrationCommand( + final RegistrationCommand c, String account, final DBusConnection dBusConn, final OutputWriter outputWriter + ) throws CommandException, DBusException { + final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(), + DbusConfig.getObjectPath(), + SignalControl.class); + try (final var registrationManager = new DbusRegistrationManagerImpl(account, signalControl, dBusConn)) { + c.handleCommand(ns, registrationManager); + } catch (UnsupportedOperationException e) { + throw new UserErrorException("Command is not yet implemented via dbus", e); + } catch (DBusExecutionException e) { + throw new UnexpectedErrorException(e.getMessage(), e); + } + } + private void handleLocalCommand( final LocalCommand command, final String account, @@ -259,6 +295,22 @@ public class App { } } + private void handleLocalCommand( + final LocalCommand c, + String accountObjectPath, + final DBusConnection dBusConn, + final OutputWriter outputWriter + ) throws CommandException, DBusException { + var signal = dBusConn.getRemoteObject(DbusConfig.getBusname(), accountObjectPath, Signal.class); + try (final var m = new DbusManagerImpl(signal, dBusConn)) { + c.handleCommand(ns, m, outputWriter); + } catch (UnsupportedOperationException e) { + throw new UserErrorException("Command is not yet implemented via dbus", e); + } catch (DBusExecutionException e) { + throw new UnexpectedErrorException(e.getMessage(), e); + } + } + private void handleMultiLocalCommand( final MultiLocalCommand command, final File dataPath, @@ -268,73 +320,32 @@ public class App { final TrustNewIdentity trustNewIdentity ) throws CommandException { final var managers = new ArrayList(); - try { - for (String u : accounts) { - try { - managers.add(loadManager(u, dataPath, serviceEnvironment, trustNewIdentity)); - } catch (CommandException e) { - logger.warn("Ignoring {}: {}", u, e.getMessage()); - } + for (String a : accounts) { + try { + managers.add(loadManager(a, dataPath, serviceEnvironment, trustNewIdentity)); + } catch (CommandException e) { + logger.warn("Ignoring {}: {}", a, e.getMessage()); } + } - command.handleCommand(ns, new SignalCreator() { - private final List> onManagerAddedHandlers = new ArrayList<>(); - - @Override - public List getAccountNumbers() { - synchronized (managers) { - return managers.stream().map(Manager::getSelfNumber).collect(Collectors.toList()); - } - } - - @Override - public void addManager(final Manager m) { - synchronized (managers) { - if (!managers.contains(m)) { - managers.add(m); - for (final var handler : onManagerAddedHandlers) { - handler.accept(m); - } - } - } - } - - @Override - public void addOnManagerAddedHandler(final Consumer handler) { - onManagerAddedHandlers.add(handler); - } - - @Override - public Manager getManager(final String phoneNumber) { - synchronized (managers) { - return managers.stream() - .filter(m -> m.getSelfNumber().equals(phoneNumber)) - .findFirst() - .orElse(null); - } - } - - @Override - public ProvisioningManager getNewProvisioningManager() { - return ProvisioningManager.init(dataPath, serviceEnvironment, BaseConfig.USER_AGENT); - } + try (var multiAccountManager = new MultiAccountManagerImpl(managers, + dataPath, + serviceEnvironment, + BaseConfig.USER_AGENT)) { + command.handleCommand(ns, multiAccountManager, outputWriter); + } + } - @Override - public RegistrationManager getNewRegistrationManager(String account) throws IOException { - return RegistrationManager.init(account, dataPath, serviceEnvironment, BaseConfig.USER_AGENT); - } - }, outputWriter); - } finally { - synchronized (managers) { - for (var m : managers) { - try { - m.close(); - } catch (IOException e) { - logger.warn("Cleanup failed", e); - } - } - managers.clear(); - } + private void handleMultiLocalCommand( + final MultiLocalCommand c, final DBusConnection dBusConn, final OutputWriter outputWriter + ) throws CommandException, DBusException { + final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(), + DbusConfig.getObjectPath(), + SignalControl.class); + try (final var multiAccountManager = new DbusMultiAccountManagerImpl(signalControl, dBusConn)) { + c.handleCommand(ns, multiAccountManager, outputWriter); + } catch (UnsupportedOperationException e) { + throw new UserErrorException("Command is not yet implemented via dbus", e); } } @@ -345,6 +356,7 @@ public class App { final TrustNewIdentity trustNewIdentity ) throws CommandException { Manager manager; + logger.trace("Loading account file for {}", account); try { manager = Manager.init(account, dataPath, serviceEnvironment, BaseConfig.USER_AGENT, trustNewIdentity); } catch (NotRegisteredException e) { @@ -359,6 +371,7 @@ public class App { + ")", e); } + logger.trace("Checking account state"); try { manager.checkAccountState(); } catch (IOException e) { @@ -384,31 +397,60 @@ public class App { busType = DBusConnection.DBusBusType.SESSION; } try (var dBusConn = DBusConnection.getConnection(busType)) { - var ts = dBusConn.getRemoteObject(DbusConfig.getBusname(), - DbusConfig.getObjectPath(account), - Signal.class); + if (command instanceof ProvisioningCommand c) { + if (account != null) { + throw new UserErrorException("You cannot specify a account (phone number) when linking"); + } - handleCommand(command, ts, dBusConn, outputWriter); + handleProvisioningCommand(c, dBusConn, outputWriter); + return; + } + + if (account == null && command instanceof MultiLocalCommand c) { + handleMultiLocalCommand(c, dBusConn, outputWriter); + return; + } + if (account != null && command instanceof RegistrationCommand c) { + handleRegistrationCommand(c, account, dBusConn, outputWriter); + return; + } + if (!(command instanceof LocalCommand localCommand)) { + throw new UserErrorException("Command only works in multi-account mode"); + } + + var accountObjectPath = account == null ? tryGetSingleAccountObjectPath(dBusConn) : null; + if (accountObjectPath == null) { + accountObjectPath = DbusConfig.getObjectPath(account); + } + handleLocalCommand(localCommand, accountObjectPath, dBusConn, outputWriter); } - } catch (DBusException | IOException e) { - logger.error("Dbus client failed", e); - throw new UnexpectedErrorException("Dbus client failed", e); + } catch (ServiceUnknown e) { + throw new UserErrorException("signal-cli DBus daemon not running on " + + (systemBus ? "system" : "session") + + " bus: " + + e.getMessage(), e); + } catch (DBusExecutionException | DBusException | IOException e) { + throw new UnexpectedErrorException("Dbus client failed: " + e.getMessage(), e); } } - private void handleCommand( - Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter - ) throws CommandException { - if (command instanceof LocalCommand localCommand) { - try (final var m = new DbusManagerImpl(ts, dBusConn)) { - localCommand.handleCommand(ns, m, outputWriter); - } catch (UnsupportedOperationException e) { - throw new UserErrorException("Command is not yet implemented via dbus", e); - } catch (IOException | DBusExecutionException e) { - throw new UnexpectedErrorException(e.getMessage(), e); + private String tryGetSingleAccountObjectPath(final DBusConnection dBusConn) throws DBusException, CommandException { + var control = dBusConn.getRemoteObject(DbusConfig.getBusname(), + DbusConfig.getObjectPath(), + SignalControl.class); + try { + final var accounts = control.listAccounts(); + if (accounts.size() == 0) { + throw new UserErrorException("No local users found, you first need to register or link an account"); + } else if (accounts.size() > 1) { + throw new UserErrorException( + "Multiple users found, you need to specify an account (phone number) with -a"); } - } else { - throw new UserErrorException("Command is not yet implemented via dbus"); + + return accounts.get(0).getPath(); + } catch (UnknownMethod e) { + // dbus daemon not running in multi-account mode + return null; } }