- // Workaround for BKS truststore
- Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
-
- Namespace ns = parseArgs(args);
- if (ns == null) {
- System.exit(1);
- }
-
- 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 = (Signal) dBusConn.getRemoteObject(
- SIGNAL_BUSNAME, SIGNAL_OBJECTPATH,
- Signal.class);
- } catch (DBusException e) {
- e.printStackTrace();
- if (dBusConn != null) {
- dBusConn.disconnect();
- }
- System.exit(3);
- return;
- }
- } 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;
- }
- }
- }
-
- m = new Manager(username, settingsPath);
- ts = m;
- if (m.userExists()) {
- try {
- m.load();
- } catch (Exception e) {
- System.err.println("Error loading state file \"" + m.getFileName() + "\": " + e.getMessage());
- System.exit(2);
- return;
- }
- }
- }
-
- switch (ns.getString("command")) {
- case "register":
- if (dBusConn != null) {
- System.err.println("register is not yet implementd via dbus");
- System.exit(1);
- }
- if (!m.userHasKeys()) {
- m.createNewIdentity();
- }
- try {
- m.register(ns.getBoolean("voice"));
- } catch (IOException e) {
- System.err.println("Request verify error: " + e.getMessage());
- System.exit(3);
- }
- break;
- case "verify":
- if (dBusConn != null) {
- System.err.println("verify is not yet implementd via dbus");
- System.exit(1);
- }
- if (!m.userHasKeys()) {
- System.err.println("User has no keys, first call register.");
- System.exit(1);
- }
- if (m.isRegistered()) {
- System.err.println("User registration is already verified");
- System.exit(1);
- }
- try {
- m.verifyAccount(ns.getString("verificationCode"));
- } catch (IOException e) {
- System.err.println("Verify error: " + e.getMessage());
- System.exit(3);
- }
- break;
- case "send":
- if (dBusConn == null && !m.isRegistered()) {
- System.err.println("User is not registered.");
- System.exit(1);
- }
-
- if (ns.getBoolean("endsession")) {
- if (ns.getList("recipient") == null) {
- System.err.println("No recipients given");
- System.err.println("Aborting sending.");
- System.exit(1);
- }
- try {
- ts.sendEndSessionMessage(ns.<String>getList("recipient"));
- } catch (IOException e) {
- handleIOException(e);
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- } catch (AssertionError e) {
- handleAssertionError(e);
- } catch (DBusExecutionException e) {
- handleDBusExecutionException(e);
- }
- } else {
- String messageText = ns.getString("message");
- if (messageText == null) {
- try {
- messageText = IOUtils.toString(System.in);
- } catch (IOException e) {
- System.err.println("Failed to read message from stdin: " + e.getMessage());
- System.err.println("Aborting sending.");
- System.exit(1);
- }
- }
-
- try {
- List<String> attachments = ns.getList("attachment");
- if (attachments == null) {
- attachments = new ArrayList<>();
- }
- if (ns.getString("group") != null) {
- byte[] groupId = decodeGroupId(ns.getString("group"));
- ts.sendGroupMessage(messageText, attachments, groupId);
- } else {
- ts.sendMessage(messageText, attachments, ns.<String>getList("recipient"));
- }
- } catch (IOException e) {
- handleIOException(e);
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- } catch (AssertionError e) {
- handleAssertionError(e);
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- } catch (AttachmentInvalidException e) {
- System.err.println("Failed to add attachment: " + e.getMessage());
- System.err.println("Aborting sending.");
- System.exit(1);
- } catch (DBusExecutionException e) {
- handleDBusExecutionException(e);
- }
- }
-
- break;
- case "receive":
- if (dBusConn != null) {
- System.err.println("receive is not yet implementd via dbus");
- System.exit(1);
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- System.exit(1);
- }
- int timeout = 5;
- if (ns.getInt("timeout") != null) {
- timeout = ns.getInt("timeout");
- }
- boolean returnOnTimeout = true;
- if (timeout < 0) {
- returnOnTimeout = false;
- timeout = 3600;
- }
- try {
- m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
- } catch (IOException e) {
- System.err.println("Error while receiving messages: " + e.getMessage());
- System.exit(3);
- } catch (AssertionError e) {
- handleAssertionError(e);
- }
- break;
- case "quitGroup":
- if (dBusConn != null) {
- System.err.println("quitGroup is not yet implementd via dbus");
- System.exit(1);
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- System.exit(1);
- }
-
- try {
- m.sendQuitGroupMessage(decodeGroupId(ns.getString("group")));
- } catch (IOException e) {
- handleIOException(e);
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- } catch (AssertionError e) {
- handleAssertionError(e);
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- }