import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.SignatureException;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
private Thread receiveThread;
private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
+ private final List<Runnable> closedListeners = new ArrayList<>();
private boolean isReceivingSynchronous;
ManagerImpl(
this.serviceEnvironmentConfig = serviceEnvironmentConfig;
final var credentialsProvider = new DynamicCredentialsProvider(account.getAci(),
- account.getUsername(),
+ account.getAccount(),
account.getPassword(),
account.getDeviceId());
final var sessionLock = new SignalSessionLock() {
@Override
public String getSelfNumber() {
- return account.getUsername();
+ return account.getAccount();
}
@Override
*
* @param numbers The set of phone number in question
* @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null.
- * @throws IOException if its unable to get the contacts to check if they're registered
+ * @throws IOException if it's unable to get the contacts to check if they're registered
*/
@Override
public Map<String, Pair<String, UUID>> areUsersRegistered(Set<String> numbers) throws IOException {
Map<String, String> canonicalizedNumbers = numbers.stream().collect(Collectors.toMap(n -> n, n -> {
try {
- return PhoneNumberFormatter.formatNumber(n, account.getUsername());
+ final var canonicalizedNumber = PhoneNumberFormatter.formatNumber(n, account.getAccount());
+ if (!canonicalizedNumber.equals(n)) {
+ logger.debug("Normalized number {} to {}.", n, canonicalizedNumber);
+ }
+ return canonicalizedNumber;
} catch (InvalidNumberException e) {
return "";
}
dependencies.getAccountManager().setGcmId(Optional.absent());
account.setRegistered(false);
+ close();
}
@Override
dependencies.getAccountManager().deleteAccount();
account.setRegistered(false);
+ close();
}
@Override
public void submitRateLimitRecaptchaChallenge(String challenge, String captcha) throws IOException {
+ captcha = captcha == null ? null : captcha.replace("signalcaptcha://", "");
+
dependencies.getAccountManager().submitRateLimitRecaptchaChallenge(challenge, captcha);
}
return new SendMessageResults(timestamp, results);
}
- private void sendTypingMessage(
+ private SendMessageResults sendTypingMessage(
SignalServiceTypingMessage.Action action, Set<RecipientIdentifier> recipients
- ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
+ ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
+ var results = new HashMap<RecipientIdentifier, List<SendMessageResult>>();
final var timestamp = System.currentTimeMillis();
for (var recipient : recipients) {
if (recipient instanceof RecipientIdentifier.Single) {
final var message = new SignalServiceTypingMessage(action, timestamp, Optional.absent());
final var recipientId = resolveRecipient((RecipientIdentifier.Single) recipient);
- sendHelper.sendTypingMessage(message, recipientId);
+ final var result = sendHelper.sendTypingMessage(message, recipientId);
+ results.put(recipient,
+ List.of(SendMessageResult.from(result,
+ account.getRecipientStore(),
+ account.getRecipientStore()::resolveRecipientAddress)));
} else if (recipient instanceof RecipientIdentifier.Group) {
final var groupId = ((RecipientIdentifier.Group) recipient).groupId();
final var message = new SignalServiceTypingMessage(action, timestamp, Optional.of(groupId.serialize()));
- sendHelper.sendGroupTypingMessage(message, groupId);
+ final var result = sendHelper.sendGroupTypingMessage(message, groupId);
+ results.put(recipient,
+ result.stream()
+ .map(r -> SendMessageResult.from(r,
+ account.getRecipientStore(),
+ account.getRecipientStore()::resolveRecipientAddress))
+ .collect(Collectors.toList()));
}
}
+ return new SendMessageResults(timestamp, results);
}
@Override
- public void sendTypingMessage(
+ public SendMessageResults sendTypingMessage(
TypingAction action, Set<RecipientIdentifier> recipients
- ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
- sendTypingMessage(action.toSignalService(), recipients);
+ ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
+ return sendTypingMessage(action.toSignalService(), recipients);
}
@Override
- public void sendReadReceipt(
+ public SendMessageResults sendReadReceipt(
RecipientIdentifier.Single sender, List<Long> messageIds
- ) throws IOException, UntrustedIdentityException {
+ ) throws IOException {
+ final var timestamp = System.currentTimeMillis();
var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ,
messageIds,
- System.currentTimeMillis());
+ timestamp);
- sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender));
+ final var result = sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender));
+ return new SendMessageResults(timestamp,
+ Map.of(sender,
+ List.of(SendMessageResult.from(result,
+ account.getRecipientStore(),
+ account.getRecipientStore()::resolveRecipientAddress))));
}
@Override
- public void sendViewedReceipt(
+ public SendMessageResults sendViewedReceipt(
RecipientIdentifier.Single sender, List<Long> messageIds
- ) throws IOException, UntrustedIdentityException {
+ ) throws IOException {
+ final var timestamp = System.currentTimeMillis();
var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.VIEWED,
messageIds,
- System.currentTimeMillis());
+ timestamp);
- sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender));
+ final var result = sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender));
+ return new SendMessageResults(timestamp,
+ Map.of(sender,
+ List.of(SendMessageResult.from(result,
+ account.getRecipientStore(),
+ account.getRecipientStore()::resolveRecipientAddress))));
}
@Override
if (attachments != null) {
messageBuilder.withAttachments(attachmentHelper.uploadAttachments(attachments));
}
+ if (message.mentions().size() > 0) {
+ messageBuilder.withMentions(resolveMentions(message.mentions()));
+ }
+ if (message.quote().isPresent()) {
+ final var quote = message.quote().get();
+ messageBuilder.withQuote(new SignalServiceDataMessage.Quote(quote.timestamp(),
+ resolveSignalServiceAddress(resolveRecipient(quote.author())),
+ quote.message(),
+ List.of(),
+ resolveMentions(quote.mentions())));
+ }
+ }
+
+ private ArrayList<SignalServiceDataMessage.Mention> resolveMentions(final List<Message.Mention> mentionList) throws IOException {
+ final var mentions = new ArrayList<SignalServiceDataMessage.Mention>();
+ for (final var m : mentionList) {
+ final var recipientId = resolveRecipient(m.recipient());
+ mentions.add(new SignalServiceDataMessage.Mention(resolveSignalServiceAddress(recipientId).getAci(),
+ m.start(),
+ m.length()));
+ }
+ return mentions;
}
@Override
}
}
+ @Override
+ public void deleteRecipient(final RecipientIdentifier.Single recipient) throws IOException {
+ account.removeRecipient(resolveRecipient(recipient));
+ }
+
+ @Override
+ public void deleteContact(final RecipientIdentifier.Single recipient) throws IOException {
+ account.getContactStore().deleteContact(resolveRecipient(recipient));
+ }
+
@Override
public void setContactName(
RecipientIdentifier.Single recipient, String name
) throws IOException {
retryFailedReceivedMessages(handler);
- Set<HandleAction> queuedActions = new HashSet<>();
+ // Use a Map here because java Set doesn't have a get method ...
+ Map<HandleAction, HandleAction> queuedActions = new HashMap<>();
final var signalWebSocket = dependencies.getSignalWebSocket();
signalWebSocket.connect();
while (!Thread.interrupted()) {
SignalServiceEnvelope envelope;
final CachedMessage[] cachedMessage = {null};
- account.setLastReceiveTimestamp(System.currentTimeMillis());
+ final var nowMillis = System.currentTimeMillis();
+ if (nowMillis - account.getLastReceiveTimestamp() > 60000) {
+ account.setLastReceiveTimestamp(nowMillis);
+ }
logger.debug("Checking for new message from server");
try {
var result = signalWebSocket.readOrEmpty(unit.toMillis(timeout), envelope1 -> {
logger.debug("New message received from server");
} else {
logger.debug("Received indicator that server queue is empty");
- handleQueuedActions(queuedActions);
+ handleQueuedActions(queuedActions.keySet());
queuedActions.clear();
hasCaughtUpWithOldMessages = true;
}
final var result = incomingMessageHandler.handleEnvelope(envelope, ignoreAttachments, handler);
- queuedActions.addAll(result.first());
+ for (final var h : result.first()) {
+ final var existingAction = queuedActions.get(h);
+ if (existingAction == null) {
+ queuedActions.put(h, h);
+ } else {
+ existingAction.mergeOther(h);
+ }
+ }
final var exception = result.second();
if (hasCaughtUpWithOldMessages) {
- handleQueuedActions(queuedActions);
+ handleQueuedActions(queuedActions.keySet());
queuedActions.clear();
}
if (cachedMessage[0] != null) {
}
}
}
- handleQueuedActions(queuedActions);
+ handleQueuedActions(queuedActions.keySet());
queuedActions.clear();
dependencies.getSignalWebSocket().disconnect();
}
/**
* Trust this the identity with this fingerprint
*
- * @param recipient username of the identity
+ * @param recipient account of the identity
* @param fingerprint Fingerprint
*/
@Override
/**
* Trust this the identity with this safety number
*
- * @param recipient username of the identity
+ * @param recipient account of the identity
* @param safetyNumber Safety number
*/
@Override
/**
* Trust this the identity with this scannable safety number
*
- * @param recipient username of the identity
+ * @param recipient account of the identity
* @param safetyNumber Scannable safety number
*/
@Override
/**
* Trust all keys of this identity without verification
*
- * @param recipient username of the identity
+ * @param recipient account of the identity
*/
@Override
public boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) {
return identityHelper.trustIdentityAllKeys(recipientId);
}
+ @Override
+ public void addClosedListener(final Runnable listener) {
+ synchronized (closedListeners) {
+ closedListeners.add(listener);
+ }
+ }
+
private void handleIdentityFailure(
final RecipientId recipientId,
final org.whispersystems.signalservice.api.messages.SendMessageResult.IdentityFailure identityFailure
@Override
public void close() throws IOException {
- close(true);
- }
-
- private void close(boolean closeAccount) throws IOException {
Thread thread;
synchronized (messageHandlers) {
weakHandlers.clear();
dependencies.getSignalWebSocket().disconnect();
- if (closeAccount && account != null) {
+ synchronized (closedListeners) {
+ closedListeners.forEach(Runnable::run);
+ closedListeners.clear();
+ }
+
+ if (account != null) {
account.close();
}
account = null;