- private static int handleCommands(Namespace ns) {
- final String username = ns.getString("username");
- Manager m;
- Signal ts;
- DBusConnection dBusConn = null;
- try {
- if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) {
- try {
- m = null;
- int busType;
- if (ns.getBoolean("dbus_system")) {
- busType = DBusConnection.SYSTEM;
- } else {
- busType = DBusConnection.SESSION;
- }
- dBusConn = DBusConnection.getConnection(busType);
- 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();
- }
-
- m = new Manager(username, dataPath);
- 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;
- }
- }
-
- 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 DbusCommand) {
- return ((DbusCommand) command).handleCommand(ns, ts);
- } else {
- System.err.println(commandKey + " is only works via dbus");
- return 1;
- }
- }
- }
- return 0;
- } finally {
- if (dBusConn != null) {
- dBusConn.disconnect();
- }
- }
- }
-
- /**
- * 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()
- .defaultHelp(true)
- .description("Commandline interface for Signal.")
- .version(BaseConfig.PROJECT_NAME + " " + BaseConfig.PROJECT_VERSION);
-
- parser.addArgument("-v", "--version")
- .help("Show package version.")
- .action(Arguments.version());
- parser.addArgument("--config")
- .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")
- .help("Specify your phone number, that will be used for verification.");
- mut.addArgument("--dbus")
- .help("Make request via user dbus.")
- .action(Arguments.storeTrue());
- mut.addArgument("--dbus-system")
- .help("Make request via system dbus.")
- .action(Arguments.storeTrue());
-
- Subparsers subparsers = parser.addSubparsers()
- .title("subcommands")
- .dest("command")
- .description("valid subcommands")
- .help("additional help");
-
- final Map<String, Command> commands = Commands.getCommands();
- for (Map.Entry<String, Command> entry : commands.entrySet()) {
- Subparser subparser = subparsers.addParser(entry.getKey());
- entry.getValue().attachToSubparser(subparser);
- }