]> nmode's Git Repositories - signal-cli/commitdiff
Support sending and receiving group info requests
authorAsamK <asamk@gmx.de>
Fri, 25 Nov 2016 13:03:58 +0000 (14:03 +0100)
committerAsamK <asamk@gmx.de>
Fri, 25 Nov 2016 13:03:58 +0000 (14:03 +0100)
build.gradle
src/main/java/org/asamk/signal/Manager.java

index 3e7bbb71cbf0952efd0da720165e40ee4979c6ed..e61d0d99f9b73b9e0489825d1ac7b8ea611bcd9c 100644 (file)
@@ -18,7 +18,7 @@ repositories {
 }
 
 dependencies {
-    compile 'com.github.turasa:signal-service-java:2.4.0_unofficial_1'
+    compile 'com.github.turasa:signal-service-java:2.4.1_unofficial_1'
     compile 'org.bouncycastle:bcprov-jdk15on:1.55'
     compile 'net.sourceforge.argparse4j:argparse4j:0.7.0'
     compile 'org.freedesktop.dbus:dbus-java:2.7.0'
index 1aefc252d552d5d0087ab44d9aa2558b2a809f6c..b16c8eaeb46166414b704aa621453c207ffa0d92 100644 (file)
@@ -663,34 +663,75 @@ class Manager implements Signal {
             }
         }
 
+        if (avatarFile != null) {
+            createPrivateDirectories(avatarsPath);
+            File aFile = getGroupAvatarFile(g.groupId);
+            Files.copy(Paths.get(avatarFile), aFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+        }
+
+        groupStore.updateGroup(g);
+
+        SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
+
+        // Don't send group message to ourself
+        final List<String> membersSend = new ArrayList<>(g.members);
+        membersSend.remove(this.username);
+        sendMessage(messageBuilder, membersSend);
+        return g.groupId;
+    }
+
+    private void sendUpdateGroupMessage(byte[] groupId, String recipient) throws IOException, EncapsulatedExceptions {
+        if (groupId == null) {
+            return;
+        }
+        GroupInfo g = getGroupForSending(groupId);
+
+        if (!g.members.contains(recipient)) {
+            return;
+        }
+
+        SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
+
+        // Send group message only to the recipient who requested it
+        final List<String> membersSend = new ArrayList<>();
+        membersSend.add(recipient);
+        sendMessage(messageBuilder, membersSend);
+    }
+
+    private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfo g) {
         SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
                 .withId(g.groupId)
                 .withName(g.name)
                 .withMembers(new ArrayList<>(g.members));
 
         File aFile = getGroupAvatarFile(g.groupId);
-        if (avatarFile != null) {
-            createPrivateDirectories(avatarsPath);
-            Files.copy(Paths.get(avatarFile), aFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
-        }
         if (aFile.exists()) {
             try {
                 group.withAvatar(createAttachment(aFile));
             } catch (IOException e) {
-                throw new AttachmentInvalidException(avatarFile, e);
+                throw new AttachmentInvalidException(aFile.toString(), e);
             }
         }
 
-        groupStore.updateGroup(g);
+        return SignalServiceDataMessage.newBuilder()
+                .asGroupMessage(group.build());
+    }
+
+    private void sendGroupInfoRequest(byte[] groupId, String recipient) throws IOException, EncapsulatedExceptions {
+        if (groupId == null) {
+            return;
+        }
+
+        SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.REQUEST_INFO)
+                .withId(groupId);
 
         SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
                 .asGroupMessage(group.build());
 
-        // Don't send group message to ourself
-        final List<String> membersSend = new ArrayList<>(g.members);
-        membersSend.remove(this.username);
+        // Send group info request message to the recipient who sent us a message with this groupId
+        final List<String> membersSend = new ArrayList<>();
+        membersSend.add(recipient);
         sendMessage(messageBuilder, membersSend);
-        return g.groupId;
     }
 
     @Override
@@ -847,10 +888,9 @@ class Manager implements Signal {
         if (message.getGroupInfo().isPresent()) {
             SignalServiceGroup groupInfo = message.getGroupInfo().get();
             threadId = Base64.encodeBytes(groupInfo.getGroupId());
+            GroupInfo group = groupStore.getGroup(groupInfo.getGroupId());
             switch (groupInfo.getType()) {
                 case UPDATE:
-                    GroupInfo group;
-                    group = groupStore.getGroup(groupInfo.getGroupId());
                     if (group == null) {
                         group = new GroupInfo(groupInfo.getGroupId());
                     }
@@ -877,14 +917,33 @@ class Manager implements Signal {
                     groupStore.updateGroup(group);
                     break;
                 case DELIVER:
+                    if (group == null) {
+                        try {
+                            sendGroupInfoRequest(groupInfo.getGroupId(), source);
+                        } catch (IOException | EncapsulatedExceptions e) {
+                            e.printStackTrace();
+                        }
+                    }
                     break;
                 case QUIT:
-                    group = groupStore.getGroup(groupInfo.getGroupId());
-                    if (group != null) {
+                    if (group == null) {
+                        try {
+                            sendGroupInfoRequest(groupInfo.getGroupId(), source);
+                        } catch (IOException | EncapsulatedExceptions e) {
+                            e.printStackTrace();
+                        }
+                    } else {
                         group.members.remove(source);
                         groupStore.updateGroup(group);
                     }
                     break;
+                case REQUEST_INFO:
+                    try {
+                        sendUpdateGroupMessage(groupInfo.getGroupId(), source);
+                    } catch (IOException | EncapsulatedExceptions e) {
+                        e.printStackTrace();
+                    }
+                    break;
             }
         } else {
             if (isSync) {