- Namespace ns = parseArgs(args);
- if (ns == null) {
- System.exit(1);
- }
-
- int res = handleCommands(ns);
- 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");
-
- 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);
-
- 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();
- }
-
- 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;
- }
- } 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;
- }
- }
- }
-
- 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;
- }