X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/6411b09aab88b891608bdb70c1a717c550f83052..6f2e8716c71ec9763ec7ca123b0c01c1f5dd63fe:/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 a272883b..4a5d4680 100644 --- a/src/main/java/org/asamk/signal/Manager.java +++ b/src/main/java/org/asamk/signal/Manager.java @@ -110,6 +110,7 @@ class Manager implements Signal { private JsonGroupStore groupStore; private JsonContactsStore contactStore; private JsonThreadStore threadStore; + private SignalServiceMessagePipe messagePipe = null; public Manager(String username, String settingsPath) { this.username = username; @@ -355,6 +356,17 @@ class Manager implements Signal { save(); } + public void updateAccountAttributes() throws IOException { + accountManager.setAccountAttributes(signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false,true); + } + + public void unregister() throws IOException { + // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false. + // If this is the master device, other users can't send messages to this number anymore. + // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore. + accountManager.setGcmId(Optional.absent()); + } + public URI getDeviceLinkUri() throws TimeoutException, IOException { password = Util.getSecret(18); @@ -495,7 +507,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, true); + accountManager.verifyAccountWithCode(verificationCode, signalingKey, signalProtocolStore.getLocalRegistrationId(), false, false,true); //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID))); registered = true; @@ -568,6 +580,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, @@ -763,6 +779,65 @@ 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.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 getGroupMembers(byte[] groupId) { + GroupInfo group = getGroup(groupId); + if (group == null) { + return new ArrayList(); + } else { + return new ArrayList(group.members); + } + } + + @Override + public void updateGroup(byte[] groupId, String name, List members, String avatar) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException { + if (name.isEmpty()) { + name = null; + } + if (members.size() == 0) { + members = null; + } + if (avatar.isEmpty()) { + avatar = null; + } + 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)); @@ -786,7 +861,7 @@ class Manager implements Signal { private void sendSyncMessage(SignalServiceSyncMessage message) throws IOException, UntrustedIdentityException { SignalServiceMessageSender messageSender = new SignalServiceMessageSender(serviceUrls, username, password, - deviceId, signalProtocolStore, USER_AGENT, Optional.absent()); + deviceId, signalProtocolStore, USER_AGENT, Optional.fromNullable(messagePipe), Optional.absent()); try { messageSender.sendMessage(message); } catch (UntrustedIdentityException e) { @@ -803,7 +878,7 @@ class Manager implements Signal { SignalServiceDataMessage message = null; try { SignalServiceMessageSender messageSender = new SignalServiceMessageSender(serviceUrls, username, password, - deviceId, signalProtocolStore, USER_AGENT, Optional.absent()); + deviceId, signalProtocolStore, USER_AGENT, Optional.fromNullable(messagePipe), Optional.absent()); message = messageBuilder.build(); if (message.getGroupInfo().isPresent()) { @@ -1032,10 +1107,11 @@ class Manager implements Signal { public void receiveMessages(long timeout, TimeUnit unit, boolean returnOnTimeout, boolean ignoreAttachments, ReceiveMessageHandler handler) throws IOException { retryFailedReceivedMessages(handler, ignoreAttachments); final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT); - SignalServiceMessagePipe messagePipe = null; try { - messagePipe = messageReceiver.createMessagePipe(); + if (messagePipe == null) { + messagePipe = messageReceiver.createMessagePipe(); + } while (true) { SignalServiceEnvelope envelope; @@ -1084,8 +1160,10 @@ class Manager implements Signal { } } } finally { - if (messagePipe != null) + if (messagePipe != null) { messagePipe.shutdown(); + messagePipe = null; + } } }