X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/c68cfe7d7c15eef53ae047e80672282e546759dd..24ab58cc1405344dedbf67eaf5f4314bdae747eb:/src/main/java/org/asamk/signal/Manager.java diff --git a/src/main/java/org/asamk/signal/Manager.java b/src/main/java/org/asamk/signal/Manager.java index 578565ef..dc979930 100644 --- a/src/main/java/org/asamk/signal/Manager.java +++ b/src/main/java/org/asamk/signal/Manager.java @@ -27,6 +27,15 @@ import com.fasterxml.jackson.databind.SerializationFeature; 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.Util; import org.whispersystems.libsignal.*; import org.whispersystems.libsignal.ecc.Curve; import org.whispersystems.libsignal.ecc.ECKeyPair; @@ -54,6 +63,7 @@ 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 org.whispersystems.signalservice.internal.util.Base64; import java.io.*; import java.net.URI; @@ -86,6 +96,7 @@ class Manager implements Signal { 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; @@ -357,7 +368,7 @@ class Manager implements Signal { } 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 { @@ -507,7 +518,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, false,true); + accountManager.verifyAccountWithCode(verificationCode, signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false, true); //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID))); registered = true; @@ -547,7 +558,8 @@ class Manager implements Signal { 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.absent(), null); } private Optional createGroupAvatarAttachment(byte[] groupId) throws IOException { @@ -581,6 +593,10 @@ class Manager implements Signal { throw new NotAGroupMemberException(groupId, g.name); } + public List getGroups() { + return groupStore.getGroups(); + } + @Override public void sendGroupMessage(String messageText, List attachments, byte[] groupId) @@ -775,6 +791,68 @@ class Manager implements Signal { 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.err.println("Add contact " + number + " named " + name); + } else { + System.err.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 getGroupMembers(byte[] groupId) { + GroupInfo group = getGroup(groupId); + if (group == null) { + return new ArrayList(); + } else { + return new ArrayList(group.members); + } + } + + @Override + public byte[] updateGroup(byte[] groupId, String name, List 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)); @@ -1035,9 +1113,11 @@ class Manager implements Signal { try { Files.delete(fileEntry.toPath()); } catch (IOException e) { - System.out.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage()); + System.err.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage()); } } + // Try to delete directory if empty + dir.delete(); } } @@ -1091,8 +1171,10 @@ class Manager implements Signal { try { cacheFile = getMessageCacheFile(envelope.getSource(), now, envelope.getTimestamp()); Files.delete(cacheFile.toPath()); + // Try to delete directory if empty + new File(getMessageCachePath()).delete(); } catch (IOException e) { - System.out.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage()); + System.err.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage()); } } } @@ -1121,6 +1203,7 @@ class Manager implements Signal { if (rm.isContactsRequest()) { try { sendContacts(); + sendVerifiedMessage(); } catch (UntrustedIdentityException | IOException e) { e.printStackTrace(); } @@ -1137,23 +1220,25 @@ class Manager implements Signal { File tmpFile = null; try { tmpFile = Util.createTempFile(); - DeviceGroupsInputStream s = new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile)); - DeviceGroup g; - while ((g = s.read()) != null) { - GroupInfo syncGroup = groupStore.getGroup(g.getId()); - if (syncGroup == null) { - syncGroup = new GroupInfo(g.getId()); + try (InputStream attachmentAsStream = retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile)) { + DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream); + DeviceGroup g; + while ((g = s.read()) != null) { + GroupInfo syncGroup = groupStore.getGroup(g.getId()); + if (syncGroup == null) { + 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()) { + retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId); + } + groupStore.updateGroup(syncGroup); } - if (g.getName().isPresent()) { - syncGroup.name = g.getName().get(); - } - syncGroup.members.addAll(g.getMembers()); - syncGroup.active = g.isActive(); - - if (g.getAvatar().isPresent()) { - retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId); - } - groupStore.updateGroup(syncGroup); } } catch (Exception e) { e.printStackTrace(); @@ -1162,7 +1247,7 @@ class Manager implements Signal { try { Files.delete(tmpFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage()); + System.err.println("Failed to delete received groups temp file “" + tmpFile + "”: " + e.getMessage()); } } } @@ -1174,24 +1259,30 @@ class Manager implements Signal { File tmpFile = null; try { tmpFile = Util.createTempFile(); - DeviceContactsInputStream s = new DeviceContactsInputStream(retrieveAttachmentAsStream(syncMessage.getContacts().get().asPointer(), tmpFile)); - DeviceContact c; - while ((c = s.read()) != null) { - ContactInfo contact = contactStore.getContact(c.getNumber()); - if (contact == null) { - contact = new ContactInfo(); - contact.number = c.getNumber(); - } - if (c.getName().isPresent()) { - contact.name = c.getName().get(); - } - if (c.getColor().isPresent()) { - contact.color = c.getColor().get(); + final ContactsMessage contactsMessage = syncMessage.getContacts().get(); + try (InputStream attachmentAsStream = retrieveAttachmentAsStream(contactsMessage.getContactsStream().asPointer(), tmpFile)) { + DeviceContactsInputStream s = new DeviceContactsInputStream(attachmentAsStream); + if (contactsMessage.isComplete()) { + contactStore.clear(); } - contactStore.updateContact(contact); - - if (c.getAvatar().isPresent()) { - retrieveContactAvatarAttachment(c.getAvatar().get(), contact.number); + DeviceContact c; + while ((c = s.read()) != null) { + ContactInfo contact = contactStore.getContact(c.getNumber()); + if (contact == null) { + contact = new ContactInfo(); + contact.number = c.getNumber(); + } + if (c.getName().isPresent()) { + contact.name = c.getName().get(); + } + if (c.getColor().isPresent()) { + contact.color = c.getColor().get(); + } + contactStore.updateContact(contact); + + if (c.getAvatar().isPresent()) { + retrieveContactAvatarAttachment(c.getAvatar().get(), contact.number); + } } } } catch (Exception e) { @@ -1201,11 +1292,17 @@ class Manager implements Signal { try { Files.delete(tmpFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage()); + System.err.println("Failed to delete received contacts temp file “" + tmpFile + "”: " + e.getMessage()); } } } } + if (syncMessage.getVerified().isPresent()) { + final List verifiedList = syncMessage.getVerified().get(); + for (VerifiedMessage v : verifiedList) { + signalProtocolStore.saveIdentity(v.getDestination(), v.getIdentityKey(), TrustLevel.fromVerifiedState(v.getVerified())); + } + } } } } @@ -1334,7 +1431,7 @@ class Manager implements Signal { 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; @@ -1350,7 +1447,7 @@ class Manager implements Signal { try { Files.delete(tmpFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage()); + System.err.println("Failed to delete received attachment temp file “" + tmpFile + "”: " + e.getMessage()); } } return outputFile; @@ -1358,7 +1455,7 @@ class Manager implements Signal { 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 { @@ -1404,7 +1501,7 @@ class Manager implements Signal { try { Files.delete(groupsFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + groupsFile + "”: " + e.getMessage()); + System.err.println("Failed to delete groups temp file “" + groupsFile + "”: " + e.getMessage()); } } } @@ -1429,18 +1526,38 @@ class Manager implements Signal { .withLength(contactsFile.length()) .build(); - sendSyncMessage(SignalServiceSyncMessage.forContacts(attachmentStream)); + sendSyncMessage(SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, true))); } } } finally { try { Files.delete(contactsFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + contactsFile + "”: " + e.getMessage()); + System.err.println("Failed to delete contacts temp file “" + contactsFile + "”: " + e.getMessage()); } } } + private void sendVerifiedMessage() throws IOException, UntrustedIdentityException { + List verifiedMessages = new LinkedList<>(); + for (Map.Entry> 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); } @@ -1469,11 +1586,16 @@ class Manager implements Signal { 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; } @@ -1492,11 +1614,16 @@ class Manager implements Signal { 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; } @@ -1514,8 +1641,13 @@ class Manager implements Signal { 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();