]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/Main.java
Ignore closed channel exception when closing the channel lock
[signal-cli] / src / main / java / org / asamk / signal / Main.java
index 0958b9a809ac6d386b4d8aa8a5be4e0793f9b3b9..b95b0093c983a5fcc01c43996da31fd3bc4de710 100644 (file)
@@ -32,6 +32,7 @@ import org.asamk.signal.commands.DbusCommand;
 import org.asamk.signal.commands.ExtendedDbusCommand;
 import org.asamk.signal.commands.LocalCommand;
 import org.asamk.signal.commands.ProvisioningCommand;
+import org.asamk.signal.dbus.DbusSignalImpl;
 import org.asamk.signal.manager.Manager;
 import org.asamk.signal.manager.ProvisioningManager;
 import org.asamk.signal.manager.ServiceConfig;
@@ -42,8 +43,10 @@ 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 org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
 
 import java.io.File;
+import java.io.IOException;
 import java.security.Security;
 import java.util.Map;
 
@@ -71,92 +74,122 @@ public class Main {
 
     private static int handleCommands(Namespace ns) {
         final String username = ns.getString("username");
-        Manager m = null;
-        ProvisioningManager pm = null;
-        Signal ts;
-        DBusConnection dBusConn = null;
-        try {
-            if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) {
-                try {
-                    DBusConnection.DBusBusType busType;
-                    if (ns.getBoolean("dbus_system")) {
-                        busType = DBusConnection.DBusBusType.SYSTEM;
-                    } else {
-                        busType = DBusConnection.DBusBusType.SESSION;
-                    }
-                    dBusConn = DBusConnection.getConnection(busType);
-                    ts = dBusConn.getRemoteObject(
+
+        if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) {
+            try {
+                DBusConnection.DBusBusType busType;
+                if (ns.getBoolean("dbus_system")) {
+                    busType = DBusConnection.DBusBusType.SYSTEM;
+                } else {
+                    busType = DBusConnection.DBusBusType.SESSION;
+                }
+                try (DBusConnection dBusConn = DBusConnection.getConnection(busType)) {
+                    Signal ts = dBusConn.getRemoteObject(
                             DbusConfig.SIGNAL_BUSNAME, DbusConfig.SIGNAL_OBJECTPATH,
                             Signal.class);
-                } catch (UnsatisfiedLinkError e) {
-                    System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
-                    return 1;
-                } catch (DBusException e) {
-                    e.printStackTrace();
-                    if (dBusConn != null) {
-                        dBusConn.disconnect();
-                    }
-                    return 3;
-                }
-            } else {
-                String dataPath = ns.getString("config");
-                if (isEmpty(dataPath)) {
-                    dataPath = getDefaultDataPath();
+
+                    return handleCommands(ns, ts, dBusConn);
                 }
+            } catch (UnsatisfiedLinkError e) {
+                System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
+                return 1;
+            } catch (DBusException | IOException e) {
+                e.printStackTrace();
+                return 3;
+            }
+        } else {
+            String dataPath = ns.getString("config");
+            if (isEmpty(dataPath)) {
+                dataPath = getDefaultDataPath();
+            }
 
-                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());
+            final SignalServiceConfiguration serviceConfiguration = ServiceConfig.createDefaultServiceConfiguration(BaseConfig.USER_AGENT);
+
+            if (username == null) {
+                ProvisioningManager pm = new ProvisioningManager(dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
+                return handleCommands(ns, pm);
+            }
+
+            Manager manager;
+            try {
+                manager = Manager.init(username, dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
+            } catch (Throwable e) {
+                System.err.println("Error loading state file: " + e.getMessage());
+                return 2;
+            }
+
+            try (Manager m = manager) {
+                try {
+                    m.checkAccountState();
+                } 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;
                     }
-                    ts = m;
+                } catch (IOException e) {
+                    System.err.println("Error while checking account: " + e.getMessage());
+                    return 2;
                 }
+
+                return handleCommands(ns, m);
+            } catch (IOException e) {
+                e.printStackTrace();
+                return 3;
             }
+        }
+    }
 
-            String commandKey = ns.getString("command");
-            final Map<String, Command> commands = Commands.getCommands();
-            if (commands.containsKey(commandKey)) {
-                Command command = commands.get(commandKey);
-
-                if (dBusConn != null) {
-                    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(commandKey + " is not yet implemented via dbus");
-                        return 1;
-                    }
-                } 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 {
-                        System.err.println(commandKey + " is only works via dbus");
-                        return 1;
-                    }
-                }
+    private static int handleCommands(Namespace ns, Signal ts, DBusConnection dBusConn) {
+        String commandKey = ns.getString("command");
+        final Map<String, Command> commands = Commands.getCommands();
+        if (commands.containsKey(commandKey)) {
+            Command command = commands.get(commandKey);
+
+            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(commandKey + " is not yet implemented via dbus");
+                return 1;
+            }
+        }
+        return 0;
+    }
+
+    private static int handleCommands(Namespace ns, ProvisioningManager pm) {
+        String commandKey = ns.getString("command");
+        final Map<String, Command> commands = Commands.getCommands();
+        if (commands.containsKey(commandKey)) {
+            Command command = commands.get(commandKey);
+
+            if (command instanceof ProvisioningCommand) {
+                return ((ProvisioningCommand) command).handleCommand(ns, pm);
+            } else {
+                System.err.println(commandKey + " only works with a username");
+                return 1;
             }
-            return 0;
-        } finally {
-            if (dBusConn != null) {
-                dBusConn.disconnect();
+        }
+        return 0;
+    }
+
+    private static int handleCommands(Namespace ns, Manager m) {
+        String commandKey = ns.getString("command");
+        final Map<String, Command> commands = Commands.getCommands();
+        if (commands.containsKey(commandKey)) {
+            Command command = commands.get(commandKey);
+
+            if (command instanceof LocalCommand) {
+                return ((LocalCommand) command).handleCommand(ns, m);
+            } else if (command instanceof DbusCommand) {
+                return ((DbusCommand) command).handleCommand(ns, new DbusSignalImpl(m));
+            } else if (command instanceof ExtendedDbusCommand) {
+                System.err.println(commandKey + " only works via dbus");
             }
+            return 1;
         }
+        return 0;
     }
 
     /**