]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/Main.java
Remove ThreadStore and store message expiration time in group/contact store
[signal-cli] / src / main / java / org / asamk / signal / Main.java
index df22e63bddce027105444f9944db91e49e73ff15..81065c778c894afca0c43f004f6842214ff55b61 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,12 +18,22 @@ 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;
@@ -34,12 +44,12 @@ 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) {
@@ -50,6 +60,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;
@@ -80,18 +96,12 @@ public class Main {
                     return 3;
                 }
             } else {
-                String settingsPath = ns.getString("config");
-                if (TextUtils.isEmpty(settingsPath)) {
-                    settingsPath = System.getProperty("user.home") + "/.config/signal";
-                    if (!new File(settingsPath).exists()) {
-                        String legacySettingsPath = System.getProperty("user.home") + "/.config/textsecure";
-                        if (new File(legacySettingsPath).exists()) {
-                            settingsPath = legacySettingsPath;
-                        }
-                    }
+                String dataPath = ns.getString("config");
+                if (isEmpty(dataPath)) {
+                    dataPath = getDefaultDataPath();
                 }
 
-                m = new Manager(username, settingsPath);
+                m = new Manager(username, dataPath);
                 ts = m;
                 try {
                     m.init();
@@ -134,6 +144,32 @@ public class Main {
         }
     }
 
+    /**
+     * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
+     * - $HOME/.config/signal
+     * - $HOME/.config/textsecure
+     *
+     * @return the data directory to be used by signal-cli.
+     */
+    private static String getDefaultDataPath() {
+        String dataPath = IOUtils.getDataHomeDir() + "/signal-cli";
+        if (new File(dataPath).exists()) {
+            return dataPath;
+        }
+
+        String legacySettingsPath = System.getProperty("user.home") + "/.config/signal";
+        if (new File(legacySettingsPath).exists()) {
+            return legacySettingsPath;
+        }
+
+        legacySettingsPath = System.getProperty("user.home") + "/.config/textsecure";
+        if (new File(legacySettingsPath).exists()) {
+            return legacySettingsPath;
+        }
+
+        return dataPath;
+    }
+
     private static Namespace parseArgs(String[] args) {
         ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
                 .build()
@@ -145,7 +181,7 @@ public class Main {
                 .help("Show package version.")
                 .action(Arguments.version());
         parser.addArgument("--config")
-                .help("Set the path, where to store the config (Default: $HOME/.config/signal).");
+                .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
 
         MutuallyExclusiveGroup mut = parser.addMutuallyExclusiveGroup();
         mut.addArgument("-u", "--username")
@@ -189,13 +225,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;