X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/bfb12b0872f89e073de3f1790ae8c5d8503a602a..f96770df3c6602ba45cb7e1ff10faa81602988c7:/src/main/java/org/asamk/signal/manager/Manager.java diff --git a/src/main/java/org/asamk/signal/manager/Manager.java b/src/main/java/org/asamk/signal/manager/Manager.java index 324c4045..2b561304 100644 --- a/src/main/java/org/asamk/signal/manager/Manager.java +++ b/src/main/java/org/asamk/signal/manager/Manager.java @@ -425,7 +425,7 @@ public class Manager implements Closeable { account.getDeviceId(), account.getSignalProtocolStore(), userAgent, account.isMultiDevice(), attachmentsV3, Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent(), clientZkProfileOperations); } - private SignalServiceProfile getRecipientProfile(SignalServiceAddress address, Optional unidentifiedAccess) throws IOException { + private SignalServiceProfile getEncryptedRecipientProfile(SignalServiceAddress address, Optional unidentifiedAccess) throws IOException { SignalServiceMessagePipe pipe = unidentifiedMessagePipe != null && unidentifiedAccess.isPresent() ? unidentifiedMessagePipe : messagePipe; @@ -444,6 +444,10 @@ public class Manager implements Closeable { } } + private SignalProfile getRecipientProfile(SignalServiceAddress address, Optional unidentifiedAccess, ProfileKey profileKey) throws IOException { + return decryptProfile(getEncryptedRecipientProfile(address, unidentifiedAccess), profileKey); + } + private Optional createGroupAvatarAttachment(byte[] groupId) throws IOException { File file = getGroupAvatarFile(groupId); if (!file.exists()) { @@ -580,7 +584,7 @@ public class Manager implements Closeable { return g.groupId; } - private void sendUpdateGroupMessage(byte[] groupId, SignalServiceAddress recipient) throws IOException, EncapsulatedExceptions, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException { + void sendUpdateGroupMessage(byte[] groupId, SignalServiceAddress recipient) throws IOException, EncapsulatedExceptions, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException { if (groupId == null) { return; } @@ -616,7 +620,7 @@ public class Manager implements Closeable { .withExpiration(g.messageExpirationTime); } - private void sendGroupInfoRequest(byte[] groupId, SignalServiceAddress recipient) throws IOException, EncapsulatedExceptions { + void sendGroupInfoRequest(byte[] groupId, SignalServiceAddress recipient) throws IOException, EncapsulatedExceptions { if (groupId == null) { return; } @@ -631,7 +635,7 @@ public class Manager implements Closeable { sendMessageLegacy(messageBuilder, Collections.singleton(recipient)); } - private void sendReceipt(SignalServiceAddress remoteAddress, long messageId) throws IOException, UntrustedIdentityException { + void sendReceipt(SignalServiceAddress remoteAddress, long messageId) throws IOException, UntrustedIdentityException { SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.DELIVERY, Collections.singletonList(messageId), System.currentTimeMillis()); @@ -746,7 +750,7 @@ public class Manager implements Closeable { if (name.isEmpty()) { name = null; } - if (members.size() == 0) { + if (members.isEmpty()) { members = null; } if (avatar.isEmpty()) { @@ -980,7 +984,7 @@ public class Manager implements Closeable { } SignalProfile targetProfile; try { - targetProfile = decryptProfile(getRecipientProfile(recipient, Optional.absent()), theirProfileKey); + targetProfile = getRecipientProfile(recipient, Optional.absent(), theirProfileKey); } catch (IOException e) { System.err.println("Failed to get recipient profile: " + e); return null; @@ -1209,7 +1213,8 @@ public class Manager implements Closeable { account.getSignalProtocolStore().deleteAllSessions(source); } - private void handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, SignalServiceAddress source, SignalServiceAddress destination, boolean ignoreAttachments) { + private List handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, SignalServiceAddress source, SignalServiceAddress destination, boolean ignoreAttachments) { + List actions = new ArrayList<>(); if (message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent()) { SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get(); GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId()); @@ -1244,12 +1249,8 @@ public class Manager implements Closeable { account.getGroupStore().updateGroup(group); break; case DELIVER: - if (group == null) { - try { - sendGroupInfoRequest(groupInfo.getGroupId(), source); - } catch (IOException | EncapsulatedExceptions e) { - e.printStackTrace(); - } + if (group == null && !isSync) { + actions.add(new SendGroupInfoRequestAction(source, groupInfo.getGroupId())); } break; case QUIT: @@ -1259,14 +1260,8 @@ public class Manager implements Closeable { } break; case REQUEST_INFO: - if (group != null) { - try { - sendUpdateGroupMessage(groupInfo.getGroupId(), source); - } catch (IOException | EncapsulatedExceptions | AttachmentInvalidException e) { - e.printStackTrace(); - } catch (GroupNotFoundException | NotAGroupMemberException e) { - // We have left this group, so don't send a group update message - } + if (group != null && !isSync) { + actions.add(new SendGroupUpdateAction(source, group.groupId)); } break; } @@ -1341,6 +1336,7 @@ public class Manager implements Closeable { } } } + return actions; } private void retryFailedReceivedMessages(ReceiveMessageHandler handler, boolean ignoreAttachments) { @@ -1383,7 +1379,14 @@ public class Manager implements Closeable { } catch (Exception e) { return; } - handleMessage(envelope, content, ignoreAttachments); + List actions = handleMessage(envelope, content, ignoreAttachments); + for (HandleAction action : actions) { + try { + action.execute(this); + } catch (Throwable e) { + e.printStackTrace(); + } + } } account.save(); handler.handleMessage(envelope, content, null); @@ -1398,17 +1401,21 @@ public class Manager implements Closeable { retryFailedReceivedMessages(handler, ignoreAttachments); final SignalServiceMessageReceiver messageReceiver = getMessageReceiver(); + Set queuedActions = null; + if (messagePipe == null) { messagePipe = messageReceiver.createMessagePipe(); } + boolean hasCaughtUpWithOldMessages = false; + while (true) { SignalServiceEnvelope envelope; SignalServiceContent content = null; Exception exception = null; final long now = new Date().getTime(); try { - envelope = messagePipe.read(timeout, unit, envelope1 -> { + Optional result = messagePipe.readOrEmpty(timeout, unit, envelope1 -> { // store message on disk, before acknowledging receipt to the server try { String source = envelope1.getSourceE164().isPresent() ? envelope1.getSourceE164().get() : ""; @@ -1418,6 +1425,27 @@ public class Manager implements Closeable { System.err.println("Failed to store encrypted message in disk cache, ignoring: " + e.getMessage()); } }); + if (result.isPresent()) { + envelope = result.get(); + } else { + // Received indicator that server queue is empty + hasCaughtUpWithOldMessages = true; + + if (queuedActions != null) { + for (HandleAction action : queuedActions) { + try { + action.execute(this); + } catch (Throwable e) { + e.printStackTrace(); + } + } + queuedActions.clear(); + queuedActions = null; + } + + // Continue to wait another timeout for new messages + continue; + } } catch (TimeoutException e) { if (returnOnTimeout) return; @@ -1437,7 +1465,21 @@ public class Manager implements Closeable { } catch (Exception e) { exception = e; } - handleMessage(envelope, content, ignoreAttachments); + List actions = handleMessage(envelope, content, ignoreAttachments); + if (hasCaughtUpWithOldMessages) { + for (HandleAction action : actions) { + try { + action.execute(this); + } catch (Throwable e) { + e.printStackTrace(); + } + } + } else { + if (queuedActions == null) { + queuedActions = new HashSet<>(); + } + queuedActions.addAll(actions); + } } account.save(); if (!isMessageBlocked(envelope, content)) { @@ -1484,7 +1526,8 @@ public class Manager implements Closeable { return false; } - private void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments) { + private List handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments) { + List actions = new ArrayList<>(); if (content != null) { SignalServiceAddress sender; if (!envelope.isUnidentifiedSender() && envelope.hasSource()) { @@ -1499,44 +1542,28 @@ public class Manager implements Closeable { SignalServiceDataMessage message = content.getDataMessage().get(); if (content.isNeedsReceipt()) { - try { - sendReceipt(sender, message.getTimestamp()); - } catch (IOException | UntrustedIdentityException | IllegalArgumentException e) { - e.printStackTrace(); - } + actions.add(new SendReceiptAction(sender, message.getTimestamp())); } - handleSignalServiceDataMessage(message, false, sender, account.getSelfAddress(), ignoreAttachments); + actions.addAll(handleSignalServiceDataMessage(message, false, sender, account.getSelfAddress(), ignoreAttachments)); } if (content.getSyncMessage().isPresent()) { account.setMultiDevice(true); SignalServiceSyncMessage syncMessage = content.getSyncMessage().get(); if (syncMessage.getSent().isPresent()) { SentTranscriptMessage message = syncMessage.getSent().get(); - handleSignalServiceDataMessage(message.getMessage(), true, sender, message.getDestination().orNull(), ignoreAttachments); + actions.addAll(handleSignalServiceDataMessage(message.getMessage(), true, sender, message.getDestination().orNull(), ignoreAttachments)); } if (syncMessage.getRequest().isPresent()) { RequestMessage rm = syncMessage.getRequest().get(); if (rm.isContactsRequest()) { - try { - sendContacts(); - } catch (UntrustedIdentityException | IOException | IllegalArgumentException e) { - e.printStackTrace(); - } + actions.add(SendSyncContactsAction.create()); } if (rm.isGroupsRequest()) { - try { - sendGroups(); - } catch (UntrustedIdentityException | IOException | IllegalArgumentException e) { - e.printStackTrace(); - } + actions.add(SendSyncGroupsAction.create()); } if (rm.isBlockedListRequest()) { - try { - sendBlockedList(); - } catch (UntrustedIdentityException | IOException | IllegalArgumentException e) { - e.printStackTrace(); - } + actions.add(SendSyncBlockedListAction.create()); } // TODO Handle rm.isConfigurationRequest(); } @@ -1670,6 +1697,7 @@ public class Manager implements Closeable { } } } + return actions; } private File getContactAvatarFile(String number) { @@ -1753,7 +1781,7 @@ public class Manager implements Closeable { return messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE); } - private void sendGroups() throws IOException, UntrustedIdentityException { + void sendGroups() throws IOException, UntrustedIdentityException { File groupsFile = IOUtils.createTempFile(); try { @@ -1842,7 +1870,7 @@ public class Manager implements Closeable { } } - private void sendBlockedList() throws IOException, UntrustedIdentityException { + void sendBlockedList() throws IOException, UntrustedIdentityException { List addresses = new ArrayList<>(); for (ContactInfo record : account.getContactStore().getContacts()) { if (record.blocked) {