]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/Manager.java
Add ListGroups command
[signal-cli] / src / main / java / org / asamk / signal / Manager.java
index e5214302700dfce1491ce3ccb0e4226b69b1fa9a..2b2d1d70d4c37d699eab31f061260b8985e44a49 100644 (file)
@@ -53,6 +53,7 @@ import org.whispersystems.signalservice.api.push.exceptions.*;
 import org.whispersystems.signalservice.api.util.InvalidNumberException;
 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
 import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
+import org.whispersystems.signalservice.internal.push.SignalServiceUrl;
 
 import java.io.*;
 import java.net.URI;
@@ -77,6 +78,7 @@ import static java.nio.file.attribute.PosixFilePermission.*;
 class Manager implements Signal {
     private final static String URL = "https://textsecure-service.whispersystems.org";
     private final static TrustStore TRUST_STORE = new WhisperTrustStore();
+    private final static SignalServiceUrl[] serviceUrls = new SignalServiceUrl[]{new SignalServiceUrl(URL, TRUST_STORE)};
 
     public final static String PROJECT_NAME = Manager.class.getPackage().getImplementationTitle();
     public final static String PROJECT_VERSION = Manager.class.getPackage().getImplementationVersion();
@@ -108,6 +110,7 @@ class Manager implements Signal {
     private JsonGroupStore groupStore;
     private JsonContactsStore contactStore;
     private JsonThreadStore threadStore;
+    private SignalServiceMessagePipe messagePipe = null;
 
     public Manager(String username, String settingsPath) {
         this.username = username;
@@ -217,7 +220,7 @@ class Manager implements Signal {
 
         migrateLegacyConfigs();
 
-        accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, deviceId, USER_AGENT);
+        accountManager = new SignalServiceAccountManager(serviceUrls, username, password, deviceId, USER_AGENT);
         try {
             if (registered && accountManager.getPreKeysCount() < PREKEY_MINIMUM_COUNT) {
                 refreshPreKeys();
@@ -342,7 +345,7 @@ class Manager implements Signal {
     public void register(boolean voiceVerification) throws IOException {
         password = Util.getSecret(18);
 
-        accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, USER_AGENT);
+        accountManager = new SignalServiceAccountManager(serviceUrls, username, password, USER_AGENT);
 
         if (voiceVerification)
             accountManager.requestVoiceVerificationCode();
@@ -353,10 +356,21 @@ class Manager implements Signal {
         save();
     }
 
+    public void updateAccountAttributes() throws IOException {
+        accountManager.setAccountAttributes(signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false,true);
+    }
+
+    public void unregister() throws IOException {
+        // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
+        // If this is the master device, other users can't send messages to this number anymore.
+        // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
+        accountManager.setGcmId(Optional.<String>absent());
+    }
+
     public URI getDeviceLinkUri() throws TimeoutException, IOException {
         password = Util.getSecret(18);
 
-        accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, USER_AGENT);
+        accountManager = new SignalServiceAccountManager(serviceUrls, username, password, USER_AGENT);
         String uuid = accountManager.getNewDeviceUuid();
 
         registered = false;
@@ -493,7 +507,7 @@ class Manager implements Signal {
     public void verifyAccount(String verificationCode) throws IOException {
         verificationCode = verificationCode.replace("-", "");
         signalingKey = Util.getSecret(52);
-        accountManager.verifyAccountWithCode(verificationCode, signalingKey, signalProtocolStore.getLocalRegistrationId(), false, true);
+        accountManager.verifyAccountWithCode(verificationCode, signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false,true);
 
         //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
         registered = true;
@@ -566,6 +580,10 @@ class Manager implements Signal {
         }
         throw new NotAGroupMemberException(groupId, g.name);
     }
+    
+    public List<GroupInfo> getGroups() {
+       return groupStore.getGroups();
+    }
 
     @Override
     public void sendGroupMessage(String messageText, List<String> attachments,
@@ -783,8 +801,8 @@ class Manager implements Signal {
 
     private void sendSyncMessage(SignalServiceSyncMessage message)
             throws IOException, UntrustedIdentityException {
-        SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password,
-                deviceId, signalProtocolStore, USER_AGENT, Optional.<SignalServiceMessageSender.EventListener>absent());
+        SignalServiceMessageSender messageSender = new SignalServiceMessageSender(serviceUrls, username, password,
+                deviceId, signalProtocolStore, USER_AGENT, Optional.fromNullable(messagePipe), Optional.<SignalServiceMessageSender.EventListener>absent());
         try {
             messageSender.sendMessage(message);
         } catch (UntrustedIdentityException e) {
@@ -800,8 +818,8 @@ class Manager implements Signal {
 
         SignalServiceDataMessage message = null;
         try {
-            SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password,
-                    deviceId, signalProtocolStore, USER_AGENT, Optional.<SignalServiceMessageSender.EventListener>absent());
+            SignalServiceMessageSender messageSender = new SignalServiceMessageSender(serviceUrls, username, password,
+                    deviceId, signalProtocolStore, USER_AGENT, Optional.fromNullable(messagePipe), Optional.<SignalServiceMessageSender.EventListener>absent());
 
             message = messageBuilder.build();
             if (message.getGroupInfo().isPresent()) {
@@ -1029,11 +1047,12 @@ class Manager implements Signal {
 
     public void receiveMessages(long timeout, TimeUnit unit, boolean returnOnTimeout, boolean ignoreAttachments, ReceiveMessageHandler handler) throws IOException {
         retryFailedReceivedMessages(handler, ignoreAttachments);
-        final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
-        SignalServiceMessagePipe messagePipe = null;
+        final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT);
 
         try {
-            messagePipe = messageReceiver.createMessagePipe();
+            if (messagePipe == null) {
+                messagePipe = messageReceiver.createMessagePipe();
+            }
 
             while (true) {
                 SignalServiceEnvelope envelope;
@@ -1082,8 +1101,10 @@ class Manager implements Signal {
                 }
             }
         } finally {
-            if (messagePipe != null)
+            if (messagePipe != null) {
                 messagePipe.shutdown();
+                messagePipe = null;
+            }
         }
     }
 
@@ -1314,7 +1335,7 @@ class Manager implements Signal {
             }
         }
 
-        final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
+        final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT);
 
         File tmpFile = Util.createTempFile();
         try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile)) {
@@ -1340,7 +1361,7 @@ class Manager implements Signal {
     }
 
     private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException {
-        final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
+        final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT);
         return messageReceiver.retrieveAttachment(pointer, tmpFile);
     }