import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.util.TextUtils;
import org.asamk.Signal;
+import org.asamk.signal.storage.contacts.ContactInfo;
+import org.asamk.signal.storage.contacts.JsonContactsStore;
+import org.asamk.signal.storage.groups.GroupInfo;
+import org.asamk.signal.storage.groups.JsonGroupStore;
+import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
+import org.asamk.signal.storage.protocol.JsonSignalProtocolStore;
+import org.asamk.signal.storage.threads.JsonThreadStore;
+import org.asamk.signal.storage.threads.ThreadInfo;
+import org.asamk.signal.util.Base64;
+import org.asamk.signal.util.Util;
import org.whispersystems.libsignal.*;
import org.whispersystems.libsignal.ecc.Curve;
import org.whispersystems.libsignal.ecc.ECKeyPair;
private final static int PREKEY_MINIMUM_COUNT = 20;
private static final int PREKEY_BATCH_SIZE = 100;
+ private static final int MAX_ATTACHMENT_SIZE = 150 * 1024 * 1024;
private final String settingsPath;
private final String dataPath;
}
public void updateAccountAttributes() throws IOException {
- accountManager.setAccountAttributes(signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false,true);
+ accountManager.setAccountAttributes(signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false, true);
}
public void unregister() throws IOException {
public void verifyAccount(String verificationCode) throws IOException {
verificationCode = verificationCode.replace("-", "");
signalingKey = Util.getSecret(52);
- accountManager.verifyAccountWithCode(verificationCode, signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false,true);
+ accountManager.verifyAccountWithCode(verificationCode, signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false, true);
//accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
registered = true;
if (mime == null) {
mime = "application/octet-stream";
}
- return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, null);
+ // TODO mabybe add a parameter to set the voiceNote and preview option
+ return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, Optional.of(attachmentFile.getName()), false, Optional.<byte[]>absent(), null);
}
private Optional<SignalServiceAttachmentStream> createGroupAvatarAttachment(byte[] groupId) throws IOException {
throw new NotAGroupMemberException(groupId, g.name);
}
+ public List<GroupInfo> getGroups() {
+ return groupStore.getGroups();
+ }
+
@Override
public void sendGroupMessage(String messageText, List<String> attachments,
byte[] groupId)
sendMessage(messageBuilder, recipients);
}
+ @Override
+ public String getContactName(String number) {
+ ContactInfo contact = contactStore.getContact(number);
+ if (contact == null) {
+ return "";
+ } else {
+ return contact.name;
+ }
+ }
+
+ @Override
+ public void setContactName(String number, String name) {
+ ContactInfo contact = contactStore.getContact(number);
+ if (contact == null) {
+ contact = new ContactInfo();
+ contact.number = number;
+ System.out.println("Add contact " + number + " named " + name);
+ } else {
+ System.out.println("Updating contact " + number + " name " + contact.name + " -> " + name);
+ }
+ contact.name = name;
+ contactStore.updateContact(contact);
+ save();
+ }
+
+ @Override
+ public String getGroupName(byte[] groupId) {
+ GroupInfo group = getGroup(groupId);
+ if (group == null) {
+ return "";
+ } else {
+ return group.name;
+ }
+ }
+
+ @Override
+ public List<String> getGroupMembers(byte[] groupId) {
+ GroupInfo group = getGroup(groupId);
+ if (group == null) {
+ return new ArrayList<String>();
+ } else {
+ return new ArrayList<String>(group.members);
+ }
+ }
+
+ @Override
+ public byte[] updateGroup(byte[] groupId, String name, List<String> members, String avatar) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
+ if (groupId.length == 0) {
+ groupId = null;
+ }
+ if (name.isEmpty()) {
+ name = null;
+ }
+ if (members.size() == 0) {
+ members = null;
+ }
+ if (avatar.isEmpty()) {
+ avatar = null;
+ }
+ return sendUpdateGroupMessage(groupId, name, members, avatar);
+ }
+
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));
if (rm.isContactsRequest()) {
try {
sendContacts();
+ sendVerifiedMessage();
} catch (UntrustedIdentityException | IOException e) {
e.printStackTrace();
}
File tmpFile = null;
try {
tmpFile = Util.createTempFile();
- DeviceContactsInputStream s = new DeviceContactsInputStream(retrieveAttachmentAsStream(syncMessage.getContacts().get().asPointer(), tmpFile));
+ final ContactsMessage contactsMessage = syncMessage.getContacts().get();
+ DeviceContactsInputStream s = new DeviceContactsInputStream(retrieveAttachmentAsStream(contactsMessage.getContactsStream().asPointer(), tmpFile));
+ if (contactsMessage.isComplete()) {
+ contactStore.clear();
+ }
DeviceContact c;
while ((c = s.read()) != null) {
ContactInfo contact = contactStore.getContact(c.getNumber());
}
}
}
+ if (syncMessage.getVerified().isPresent()) {
+ final List<VerifiedMessage> verifiedList = syncMessage.getVerified().get();
+ for (VerifiedMessage v : verifiedList) {
+ signalProtocolStore.saveIdentity(v.getDestination(), v.getIdentityKey(), TrustLevel.fromVerifiedState(v.getVerified()));
+ }
+ }
}
}
}
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT);
File tmpFile = Util.createTempFile();
- try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile)) {
+ try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile, MAX_ATTACHMENT_SIZE)) {
try (OutputStream output = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[4096];
int read;
private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException {
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT);
- return messageReceiver.retrieveAttachment(pointer, tmpFile);
+ return messageReceiver.retrieveAttachment(pointer, tmpFile, MAX_ATTACHMENT_SIZE);
}
private String canonicalizeNumber(String number) throws InvalidNumberException {
.withLength(contactsFile.length())
.build();
- sendSyncMessage(SignalServiceSyncMessage.forContacts(attachmentStream));
+ sendSyncMessage(SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, true)));
}
}
} finally {
}
}
+ private void sendVerifiedMessage() throws IOException, UntrustedIdentityException {
+ List<VerifiedMessage> verifiedMessages = new LinkedList<>();
+ for (Map.Entry<String, List<JsonIdentityKeyStore.Identity>> x : getIdentities().entrySet()) {
+ final String name = x.getKey();
+ for (JsonIdentityKeyStore.Identity id : x.getValue()) {
+ if (id.getTrustLevel() == TrustLevel.TRUSTED_UNVERIFIED) {
+ continue;
+ }
+ VerifiedMessage verifiedMessage = new VerifiedMessage(name, id.getIdentityKey(), id.getTrustLevel().toVerifiedState());
+ verifiedMessages.add(verifiedMessage);
+ }
+ }
+ sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessages));
+ }
+
+ private void sendVerifiedMessage(String destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException {
+ VerifiedMessage verifiedMessage = new VerifiedMessage(destination, identityKey, trustLevel.toVerifiedState());
+ sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
+ }
+
public ContactInfo getContact(String number) {
return contactStore.getContact(number);
}
return false;
}
for (JsonIdentityKeyStore.Identity id : ids) {
- if (!Arrays.equals(id.identityKey.serialize(), fingerprint)) {
+ if (!Arrays.equals(id.getIdentityKey().serialize(), fingerprint)) {
continue;
}
- signalProtocolStore.saveIdentity(name, id.identityKey, TrustLevel.TRUSTED_VERIFIED);
+ signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
+ try {
+ sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
+ } catch (IOException | UntrustedIdentityException e) {
+ e.printStackTrace();
+ }
save();
return true;
}
return false;
}
for (JsonIdentityKeyStore.Identity id : ids) {
- if (!safetyNumber.equals(computeSafetyNumber(name, id.identityKey))) {
+ if (!safetyNumber.equals(computeSafetyNumber(name, id.getIdentityKey()))) {
continue;
}
- signalProtocolStore.saveIdentity(name, id.identityKey, TrustLevel.TRUSTED_VERIFIED);
+ signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
+ try {
+ sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
+ } catch (IOException | UntrustedIdentityException e) {
+ e.printStackTrace();
+ }
save();
return true;
}
return false;
}
for (JsonIdentityKeyStore.Identity id : ids) {
- if (id.trustLevel == TrustLevel.UNTRUSTED) {
- signalProtocolStore.saveIdentity(name, id.identityKey, TrustLevel.TRUSTED_UNVERIFIED);
+ if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
+ signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
+ try {
+ sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
+ } catch (IOException | UntrustedIdentityException e) {
+ e.printStackTrace();
+ }
}
}
save();