import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage;
import org.whispersystems.signalservice.api.push.ACI;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
+import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
import org.whispersystems.signalservice.api.util.DeviceNameUtil;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
+import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState;
import org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException;
import org.whispersystems.signalservice.internal.contacts.crypto.Quote;
import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedQuoteException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.SignatureException;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
+import io.reactivex.rxjava3.core.Observable;
+import io.reactivex.rxjava3.schedulers.Schedulers;
+
import static org.asamk.signal.manager.config.ServiceConfig.capabilities;
public class ManagerImpl implements Manager {
unidentifiedAccessHelper::getAccessFor,
this::resolveSignalServiceAddress);
final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential,
- this::getRecipientProfile,
+ profileHelper::getRecipientProfile,
account::getSelfRecipientId,
dependencies.getGroupsV2Operations(),
dependencies.getGroupsV2Api(),
account.getRecipientStore(),
this::handleIdentityFailure,
this::getGroupInfo,
+ profileHelper::getRecipientProfile,
this::refreshRegisteredUser);
this.groupHelper = new GroupHelper(account,
dependencies,
contactHelper,
attachmentHelper,
syncHelper,
- this::getRecipientProfile,
+ profileHelper::getRecipientProfile,
jobExecutor);
this.identityHelper = new IdentityHelper(account,
dependencies,
days);
}
}
- preKeyHelper.refreshPreKeysIfNecessary();
- if (account.getAci() == null) {
- account.setAci(dependencies.getAccountManager().getOwnAci());
+ try {
+ preKeyHelper.refreshPreKeysIfNecessary();
+ if (account.getAci() == null) {
+ account.setAci(ACI.parseOrNull(dependencies.getAccountManager().getWhoAmI().getAci()));
+ }
+ updateAccountAttributes(null);
+ } catch (AuthorizationFailedException e) {
+ account.setRegistered(false);
+ throw e;
}
- updateAccountAttributes(null);
}
/**
*
* @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.getAccount());
+ 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 "";
}
d.getCreated(),
d.getLastSeen(),
d.getId() == account.getDeviceId());
- }).collect(Collectors.toList());
+ }).toList();
}
@Override
@Override
public List<Group> getGroups() {
- return account.getGroupStore().getGroups().stream().map(this::toGroup).collect(Collectors.toList());
+ return account.getGroupStore().getGroups().stream().map(this::toGroup).toList();
}
private Group toGroup(final GroupInfo groupInfo) {
.map(sendMessageResult -> SendMessageResult.from(sendMessageResult,
account.getRecipientStore(),
account.getRecipientStore()::resolveRecipientAddress))
- .collect(Collectors.toList()));
+ .toList());
}
}
return new SendMessageResults(timestamp, results);
.map(r -> SendMessageResult.from(r,
account.getRecipientStore(),
account.getRecipientStore()::resolveRecipientAddress))
- .collect(Collectors.toList()));
+ .toList());
}
}
return new SendMessageResults(timestamp, results);
messageBuilder.withAttachments(attachmentHelper.uploadAttachments(attachments));
}
if (message.mentions().size() > 0) {
- final var mentions = new ArrayList<SignalServiceDataMessage.Mention>();
- for (final var m : message.mentions()) {
- final var recipientId = resolveRecipient(m.recipient());
- mentions.add(new SignalServiceDataMessage.Mention(resolveSignalServiceAddress(recipientId).getAci(),
- m.start(),
- m.length()));
- }
- messageBuilder.withMentions(mentions);
+ 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
public SendMessageResults sendRemoteDeleteMessage(
long targetSentTimestamp, Set<RecipientIdentifier> recipients
}
}
+ @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
logger.debug("Starting receiving messages");
while (!Thread.interrupted()) {
try {
- receiveMessagesInternal(1L, TimeUnit.HOURS, false, (envelope, e) -> {
+ receiveMessagesInternal(Duration.ofMinutes(1), false, (envelope, e) -> {
synchronized (messageHandlers) {
Stream.concat(messageHandlers.stream(), weakHandlers.stream()).forEach(h -> {
try {
}
@Override
- public void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException {
- receiveMessages(timeout, unit, true, handler);
+ public void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException {
+ receiveMessages(timeout, true, handler);
}
@Override
public void receiveMessages(ReceiveMessageHandler handler) throws IOException {
- receiveMessages(1L, TimeUnit.HOURS, false, handler);
+ receiveMessages(Duration.ofMinutes(1), false, handler);
}
private void receiveMessages(
- long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler
+ Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler
) throws IOException {
if (isReceiving()) {
throw new IllegalStateException("Already receiving message.");
isReceivingSynchronous = true;
receiveThread = Thread.currentThread();
try {
- receiveMessagesInternal(timeout, unit, returnOnTimeout, handler);
+ receiveMessagesInternal(timeout, returnOnTimeout, handler);
} finally {
receiveThread = null;
hasCaughtUpWithOldMessages = false;
}
private void receiveMessagesInternal(
- long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler
+ Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler
) throws IOException {
retryFailedReceivedMessages(handler);
Map<HandleAction, HandleAction> queuedActions = new HashMap<>();
final var signalWebSocket = dependencies.getSignalWebSocket();
+ final var webSocketStateDisposable = Observable.merge(signalWebSocket.getUnidentifiedWebSocketState(),
+ signalWebSocket.getWebSocketState())
+ .subscribeOn(Schedulers.computation())
+ .observeOn(Schedulers.computation())
+ .distinctUntilChanged()
+ .subscribe(this::onWebSocketStateChange);
signalWebSocket.connect();
hasCaughtUpWithOldMessages = false;
}
logger.debug("Checking for new message from server");
try {
- var result = signalWebSocket.readOrEmpty(unit.toMillis(timeout), envelope1 -> {
+ var result = signalWebSocket.readOrEmpty(timeout.toMillis(), envelope1 -> {
final var recipientId = envelope1.hasSourceUuid()
? resolveRecipient(envelope1.getSourceAddress())
: null;
handleQueuedActions(queuedActions.keySet());
queuedActions.clear();
dependencies.getSignalWebSocket().disconnect();
+ webSocketStateDisposable.dispose();
+ }
+
+ private void onWebSocketStateChange(final WebSocketConnectionState s) {
+ if (s.equals(WebSocketConnectionState.AUTHENTICATION_FAILED)) {
+ account.setRegistered(false);
+ try {
+ close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
@Override
.getContacts()
.stream()
.map(p -> new Pair<>(account.getRecipientStore().resolveRecipientAddress(p.first()), p.second()))
- .collect(Collectors.toList());
+ .toList();
}
@Override
@Override
public List<Identity> getIdentities() {
- return account.getIdentityKeyStore()
- .getIdentities()
- .stream()
- .map(this::toIdentity)
- .collect(Collectors.toList());
+ return account.getIdentityKeyStore().getIdentities().stream().map(this::toIdentity).toList();
}
private Identity toIdentity(final IdentityInfo identityInfo) {
private SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId) {
final var address = account.getRecipientStore().resolveRecipientAddress(recipientId);
- if (address.getUuid().isPresent()) {
+ if (address.uuid().isPresent()) {
return address.toSignalServiceAddress();
}
// Address in recipient store doesn't have a uuid, this shouldn't happen
// Try to retrieve the uuid from the server
- final var number = address.getNumber().get();
+ final var number = address.number().get();
final ACI aci;
try {
aci = getRegisteredUser(number);