X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/4bafc7212ae24d62c5f2d37514180179dabb139c..a02031aa807d45816398955c9667215baf5d06dc:/src/main/java/org/asamk/signal/Main.java diff --git a/src/main/java/org/asamk/signal/Main.java b/src/main/java/org/asamk/signal/Main.java index 969e72bc..0958b9a8 100644 --- a/src/main/java/org/asamk/signal/Main.java +++ b/src/main/java/org/asamk/signal/Main.java @@ -31,13 +31,16 @@ 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.manager.BaseConfig; +import org.asamk.signal.commands.ProvisioningCommand; import org.asamk.signal.manager.Manager; +import org.asamk.signal.manager.ProvisioningManager; +import org.asamk.signal.manager.ServiceConfig; import org.asamk.signal.util.IOUtils; import org.asamk.signal.util.SecurityProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.freedesktop.dbus.DBusConnection; +import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.exceptions.DBusException; +import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException; import org.whispersystems.signalservice.api.util.PhoneNumberFormatter; import java.io.File; @@ -49,9 +52,7 @@ import static org.whispersystems.signalservice.internal.util.Util.isEmpty; public class Main { public static void main(String[] args) { - // Register our own security provider - Security.insertProviderAt(new SecurityProvider(), 1); - Security.addProvider(new BouncyCastleProvider()); + installSecurityProviderWorkaround(); Namespace ns = parseArgs(args); if (ns == null) { @@ -62,20 +63,26 @@ public class Main { System.exit(res); } + public static void installSecurityProviderWorkaround() { + // Register our own security provider + Security.insertProviderAt(new SecurityProvider(), 1); + Security.addProvider(new BouncyCastleProvider()); + } + private static int handleCommands(Namespace ns) { final String username = ns.getString("username"); - Manager m; + Manager m = null; + ProvisioningManager pm = null; Signal ts; DBusConnection dBusConn = null; try { if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) { try { - m = null; - int busType; + DBusConnection.DBusBusType busType; if (ns.getBoolean("dbus_system")) { - busType = DBusConnection.SYSTEM; + busType = DBusConnection.DBusBusType.SYSTEM; } else { - busType = DBusConnection.SESSION; + busType = DBusConnection.DBusBusType.SESSION; } dBusConn = DBusConnection.getConnection(busType); ts = dBusConn.getRemoteObject( @@ -97,13 +104,23 @@ public class Main { dataPath = getDefaultDataPath(); } - m = new Manager(username, dataPath); - ts = m; - try { - m.init(); - } catch (Exception e) { - System.err.println("Error loading state file: " + e.getMessage()); - return 2; + if (username == null) { + pm = new ProvisioningManager(dataPath, ServiceConfig.createDefaultServiceConfiguration(BaseConfig.USER_AGENT), BaseConfig.USER_AGENT); + ts = null; + } else { + try { + m = Manager.init(username, dataPath, ServiceConfig.createDefaultServiceConfiguration(BaseConfig.USER_AGENT), BaseConfig.USER_AGENT); + } catch (AuthorizationFailedException e) { + if (!"register".equals(ns.getString("command"))) { + // Register command should still be possible, if current authorization fails + System.err.println("Authorization failed, was the number registered elsewhere?"); + return 2; + } + } catch (Throwable e) { + System.err.println("Error loading state file: " + e.getMessage()); + return 2; + } + ts = m; } } @@ -124,6 +141,8 @@ public class Main { } else { if (command instanceof LocalCommand) { return ((LocalCommand) command).handleCommand(ns, m); + } else if (command instanceof ProvisioningCommand) { + return ((ProvisioningCommand) command).handleCommand(ns, pm); } else if (command instanceof DbusCommand) { return ((DbusCommand) command).handleCommand(ns, ts); } else {