- manager = Manager.init(username, dataPath, serviceConfiguration, BaseConfig.USER_AGENT);
- } catch (Throwable e) {
- logger.error("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) {
- logger.error("Error while checking account: {}", e.getMessage());
- return 2;
- }
-
- return handleCommands(ns, m);
- } catch (IOException e) {
- logger.error("Cleanup failed", e);
- return 3;
- }
- }
-
- private static int initDbusClient(final Namespace ns, final boolean systemBus) {
- try {
- DBusConnection.DBusBusType busType;
- if (systemBus) {
- 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 (DBusException | IOException e) {
- logger.error("Dbus client failed", e);
- 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;
- }
-
- 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;
- }
-
- 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");