mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 02:20:39 +00:00
Implement requesting/sending groups when linking device
This commit is contained in:
parent
947818d317
commit
0ad42a72ab
4 changed files with 147 additions and 6 deletions
|
@ -19,6 +19,9 @@ public class GroupInfo {
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
public long avatarId;
|
public long avatarId;
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
public boolean active;
|
||||||
|
|
||||||
public GroupInfo(byte[] groupId) {
|
public GroupInfo(byte[] groupId) {
|
||||||
this.groupId = groupId;
|
this.groupId = groupId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,9 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class JsonGroupStore {
|
public class JsonGroupStore {
|
||||||
|
@ -31,6 +33,10 @@ public class JsonGroupStore {
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<GroupInfo> getGroups() {
|
||||||
|
return new ArrayList<>(groups.values());
|
||||||
|
}
|
||||||
|
|
||||||
public static class MapToListSerializer extends JsonSerializer<Map<?, ?>> {
|
public static class MapToListSerializer extends JsonSerializer<Map<?, ?>> {
|
||||||
@Override
|
@Override
|
||||||
public void serialize(final Map<?, ?> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
|
public void serialize(final Map<?, ?> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
|
||||||
|
|
|
@ -622,6 +622,7 @@ public class Main {
|
||||||
handleSignalServiceDataMessage(message, group);
|
handleSignalServiceDataMessage(message, group);
|
||||||
}
|
}
|
||||||
if (content.getSyncMessage().isPresent()) {
|
if (content.getSyncMessage().isPresent()) {
|
||||||
|
System.out.println("Received a sync message");
|
||||||
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
|
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
|
||||||
|
|
||||||
if (syncMessage.getContacts().isPresent()) {
|
if (syncMessage.getContacts().isPresent()) {
|
||||||
|
|
|
@ -39,11 +39,10 @@ import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
||||||
import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
|
import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
|
||||||
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
||||||
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
||||||
import org.whispersystems.signalservice.api.crypto.*;
|
import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
|
||||||
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
||||||
import org.whispersystems.signalservice.api.messages.*;
|
import org.whispersystems.signalservice.api.messages.*;
|
||||||
import org.whispersystems.signalservice.api.messages.multidevice.RequestMessage;
|
import org.whispersystems.signalservice.api.messages.multidevice.*;
|
||||||
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
|
||||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||||
import org.whispersystems.signalservice.api.push.TrustStore;
|
import org.whispersystems.signalservice.api.push.TrustStore;
|
||||||
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
|
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
|
||||||
|
@ -250,6 +249,10 @@ class Manager implements Signal {
|
||||||
|
|
||||||
registered = true;
|
registered = true;
|
||||||
refreshPreKeys();
|
refreshPreKeys();
|
||||||
|
|
||||||
|
requestSyncGroups();
|
||||||
|
requestSyncContacts();
|
||||||
|
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,7 +387,7 @@ class Manager implements Signal {
|
||||||
return SignalServiceAttachments;
|
return SignalServiceAttachments;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SignalServiceAttachmentStream createAttachment(String attachment) throws IOException {
|
private static SignalServiceAttachment createAttachment(String attachment) throws IOException {
|
||||||
File attachmentFile = new File(attachment);
|
File attachmentFile = new File(attachment);
|
||||||
InputStream attachmentStream = new FileInputStream(attachmentFile);
|
InputStream attachmentStream = new FileInputStream(attachmentFile);
|
||||||
final long attachmentSize = attachmentFile.length();
|
final long attachmentSize = attachmentFile.length();
|
||||||
|
@ -504,6 +507,37 @@ class Manager implements Signal {
|
||||||
sendMessage(message, recipients);
|
sendMessage(message, recipients);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void requestSyncGroups() throws IOException {
|
||||||
|
SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.GROUPS).build();
|
||||||
|
SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
|
||||||
|
try {
|
||||||
|
sendMessage(message);
|
||||||
|
} catch (EncapsulatedExceptions encapsulatedExceptions) {
|
||||||
|
encapsulatedExceptions.printStackTrace();
|
||||||
|
} catch (UntrustedIdentityException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requestSyncContacts() throws IOException {
|
||||||
|
SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.CONTACTS).build();
|
||||||
|
SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
|
||||||
|
try {
|
||||||
|
sendMessage(message);
|
||||||
|
} catch (EncapsulatedExceptions encapsulatedExceptions) {
|
||||||
|
encapsulatedExceptions.printStackTrace();
|
||||||
|
} catch (UntrustedIdentityException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessage(SignalServiceSyncMessage message)
|
||||||
|
throws IOException, EncapsulatedExceptions, UntrustedIdentityException {
|
||||||
|
SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password,
|
||||||
|
deviceId, signalProtocolStore, USER_AGENT, Optional.<SignalServiceMessageSender.EventListener>absent());
|
||||||
|
messageSender.sendMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
private void sendMessage(SignalServiceDataMessage message, Collection<String> recipients)
|
private void sendMessage(SignalServiceDataMessage message, Collection<String> recipients)
|
||||||
throws IOException, EncapsulatedExceptions, UntrustedIdentityException {
|
throws IOException, EncapsulatedExceptions, UntrustedIdentityException {
|
||||||
SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password,
|
SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password,
|
||||||
|
@ -575,6 +609,7 @@ class Manager implements Signal {
|
||||||
long avatarId = avatar.asPointer().getId();
|
long avatarId = avatar.asPointer().getId();
|
||||||
try {
|
try {
|
||||||
retrieveAttachment(avatar.asPointer());
|
retrieveAttachment(avatar.asPointer());
|
||||||
|
// TODO store group avatar in /avatar/groups folder
|
||||||
group.avatarId = avatarId;
|
group.avatarId = avatarId;
|
||||||
} catch (IOException | InvalidMessageException e) {
|
} catch (IOException | InvalidMessageException e) {
|
||||||
System.err.println("Failed to retrieve group avatar (" + avatarId + "): " + e.getMessage());
|
System.err.println("Failed to retrieve group avatar (" + avatarId + "): " + e.getMessage());
|
||||||
|
@ -651,10 +686,68 @@ class Manager implements Signal {
|
||||||
group = handleSignalServiceDataMessage(message, true, envelope.getSource(), syncMessage.getSent().get().getDestination().get());
|
group = handleSignalServiceDataMessage(message, true, envelope.getSource(), syncMessage.getSent().get().getDestination().get());
|
||||||
}
|
}
|
||||||
if (syncMessage.getRequest().isPresent()) {
|
if (syncMessage.getRequest().isPresent()) {
|
||||||
// TODO
|
RequestMessage rm = syncMessage.getRequest().get();
|
||||||
|
if (rm.isContactsRequest()) {
|
||||||
|
// TODO implement when we have contacts
|
||||||
|
}
|
||||||
|
if (rm.isGroupsRequest()) {
|
||||||
|
try {
|
||||||
|
sendGroups();
|
||||||
|
} catch (EncapsulatedExceptions encapsulatedExceptions) {
|
||||||
|
encapsulatedExceptions.printStackTrace();
|
||||||
|
} catch (UntrustedIdentityException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (syncMessage.getGroups().isPresent()) {
|
if (syncMessage.getGroups().isPresent()) {
|
||||||
// TODO
|
try {
|
||||||
|
DeviceGroupsInputStream s = new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer()));
|
||||||
|
DeviceGroup g;
|
||||||
|
while ((g = s.read()) != null) {
|
||||||
|
GroupInfo syncGroup;
|
||||||
|
try {
|
||||||
|
syncGroup = groupStore.getGroup(g.getId());
|
||||||
|
} catch (GroupNotFoundException e) {
|
||||||
|
syncGroup = new GroupInfo(g.getId());
|
||||||
|
}
|
||||||
|
if (g.getName().isPresent()) {
|
||||||
|
syncGroup.name = g.getName().get();
|
||||||
|
}
|
||||||
|
syncGroup.members.addAll(g.getMembers());
|
||||||
|
syncGroup.active = g.isActive();
|
||||||
|
|
||||||
|
if (g.getAvatar().isPresent()) {
|
||||||
|
byte[] ava = new byte[(int) g.getAvatar().get().getLength()];
|
||||||
|
org.whispersystems.signalservice.internal.util.Util.readFully(g.getAvatar().get().getInputStream(), ava);
|
||||||
|
// TODO store group avatar in /avatar/groups folder
|
||||||
|
}
|
||||||
|
groupStore.updateGroup(syncGroup);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (syncMessage.getContacts().isPresent()) {
|
||||||
|
try {
|
||||||
|
DeviceContactsInputStream s = new DeviceContactsInputStream(retrieveAttachmentAsStream(syncMessage.getContacts().get().asPointer()));
|
||||||
|
DeviceContact c;
|
||||||
|
while ((c = s.read()) != null) {
|
||||||
|
// TODO implement when we have contact storage
|
||||||
|
if (c.getName().isPresent()) {
|
||||||
|
c.getName().get();
|
||||||
|
}
|
||||||
|
c.getNumber();
|
||||||
|
|
||||||
|
if (c.getAvatar().isPresent()) {
|
||||||
|
byte[] ava = new byte[(int) c.getAvatar().get().getLength()];
|
||||||
|
org.whispersystems.signalservice.internal.util.Util.readFully(c.getAvatar().get().getInputStream(), ava);
|
||||||
|
// TODO store contact avatar in /avatar/contacts folder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -725,6 +818,14 @@ class Manager implements Signal {
|
||||||
return outputFile;
|
return outputFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer) throws IOException, InvalidMessageException {
|
||||||
|
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
|
||||||
|
File file = File.createTempFile("ts_tmp", "tmp");
|
||||||
|
file.deleteOnExit();
|
||||||
|
|
||||||
|
return messageReceiver.retrieveAttachment(pointer, file);
|
||||||
|
}
|
||||||
|
|
||||||
private String canonicalizeNumber(String number) throws InvalidNumberException {
|
private String canonicalizeNumber(String number) throws InvalidNumberException {
|
||||||
String localNumber = username;
|
String localNumber = username;
|
||||||
return PhoneNumberFormatter.formatNumber(number, localNumber);
|
return PhoneNumberFormatter.formatNumber(number, localNumber);
|
||||||
|
@ -739,4 +840,34 @@ class Manager implements Signal {
|
||||||
public boolean isRemote() {
|
public boolean isRemote() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendGroups() throws IOException, EncapsulatedExceptions, UntrustedIdentityException {
|
||||||
|
File contactsFile = File.createTempFile("multidevice-contact-update", ".tmp");
|
||||||
|
|
||||||
|
try {
|
||||||
|
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new FileOutputStream(contactsFile));
|
||||||
|
try {
|
||||||
|
for (GroupInfo record : groupStore.getGroups()) {
|
||||||
|
out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
|
||||||
|
new ArrayList<>(record.members), Optional.of(new SignalServiceAttachmentStream(new FileInputStream("/home/sebastian/Bilder/00026_150512_14-00-18.JPG"), "octet", new File("/home/sebastian/Bilder/00026_150512_14-00-18.JPG").length(), null)),
|
||||||
|
record.active));
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contactsFile.exists() && contactsFile.length() > 0) {
|
||||||
|
FileInputStream contactsFileStream = new FileInputStream(contactsFile);
|
||||||
|
SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
|
||||||
|
.withStream(contactsFileStream)
|
||||||
|
.withContentType("application/octet-stream")
|
||||||
|
.withLength(contactsFile.length())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
sendMessage(SignalServiceSyncMessage.forGroups(attachmentStream));
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (contactsFile != null) contactsFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue