]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/Main.java
Switch to hypfvieh dbus-java
[signal-cli] / src / main / java / org / asamk / signal / Main.java
index e598d8e5c2cf143b2bd314b9ba3917d6cdd762c9..38c7a68d93e9c6f90e9bcd24bd492f212620ba28 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2015-2018 AsamK
+  Copyright (C) 2015-2020 AsamK and contributors
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -18,29 +18,39 @@ package org.asamk.signal;
 
 import net.sourceforge.argparse4j.ArgumentParsers;
 import net.sourceforge.argparse4j.impl.Arguments;
-import net.sourceforge.argparse4j.inf.*;
-import org.apache.http.util.TextUtils;
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
+import net.sourceforge.argparse4j.inf.Namespace;
+import net.sourceforge.argparse4j.inf.Subparser;
+import net.sourceforge.argparse4j.inf.Subparsers;
+
 import org.asamk.Signal;
-import org.asamk.signal.commands.*;
+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.manager.BaseConfig;
 import org.asamk.signal.manager.Manager;
 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;
 import java.security.Security;
 import java.util.Map;
 
+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) {
@@ -51,6 +61,12 @@ 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;
@@ -60,11 +76,11 @@ public class Main {
             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(
@@ -82,7 +98,7 @@ public class Main {
                 }
             } else {
                 String dataPath = ns.getString("config");
-                if (TextUtils.isEmpty(dataPath)) {
+                if (isEmpty(dataPath)) {
                     dataPath = getDefaultDataPath();
                 }
 
@@ -90,6 +106,12 @@ public class Main {
                 ts = m;
                 try {
                     m.init();
+                } 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 (Exception e) {
                     System.err.println("Error loading state file: " + e.getMessage());
                     return 2;
@@ -210,13 +232,13 @@ public class Main {
                 System.err.println("You need to specify a username (phone number)");
                 System.exit(2);
             }
-            if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"))) {
+            if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"), null)) {
                 System.err.println("Invalid username (phone number), make sure you include the country code.");
                 System.exit(2);
             }
         }
         if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
-            System.err.println("You cannot specify recipients by phone number and groups a the same time");
+            System.err.println("You cannot specify recipients by phone number and groups at the same time");
             System.exit(2);
         }
         return ns;