X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/861f47d734a2c416ca3231ad72f9f129da5a706f..f207c2abc390d1b6292de6e86379d1513dc358cf:/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java diff --git a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java index 5421d4ad..51323395 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java @@ -2,10 +2,8 @@ package org.asamk.signal.dbus; import org.asamk.Signal; import org.asamk.signal.DbusConfig; -import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.Manager; -import org.asamk.signal.manager.NotMasterDeviceException; -import org.asamk.signal.manager.StickerPackInvalidException; +import org.asamk.signal.manager.api.AttachmentInvalidException; import org.asamk.signal.manager.api.Configuration; import org.asamk.signal.manager.api.Device; import org.asamk.signal.manager.api.Group; @@ -14,10 +12,14 @@ import org.asamk.signal.manager.api.InactiveGroupLinkException; import org.asamk.signal.manager.api.InvalidDeviceLinkException; import org.asamk.signal.manager.api.Message; import org.asamk.signal.manager.api.MessageEnvelope; +import org.asamk.signal.manager.api.NotMasterDeviceException; import org.asamk.signal.manager.api.Pair; import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.SendGroupMessageResults; import org.asamk.signal.manager.api.SendMessageResults; +import org.asamk.signal.manager.api.StickerPack; +import org.asamk.signal.manager.api.StickerPackInvalidException; +import org.asamk.signal.manager.api.StickerPackUrl; import org.asamk.signal.manager.api.TypingAction; import org.asamk.signal.manager.api.UpdateGroup; import org.asamk.signal.manager.groups.GroupId; @@ -42,6 +44,7 @@ import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -50,7 +53,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.UUID; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -82,11 +85,6 @@ public class DbusManagerImpl implements Manager { return signal.getSelfNumber(); } - @Override - public void checkAccountState() throws IOException { - throw new UnsupportedOperationException(); - } - @Override public Map> areUsersRegistered(final Set numbers) throws IOException { final var numbersList = new ArrayList<>(numbers); @@ -110,12 +108,28 @@ public class DbusManagerImpl implements Manager { @Override public Configuration getConfiguration() { - throw new UnsupportedOperationException(); + final var configuration = getRemoteObject(new DBusPath(signal.getObjectPath() + "/Configuration"), + Signal.Configuration.class).GetAll("org.asamk.Signal.Configuration"); + return new Configuration(Optional.of((Boolean) configuration.get("ReadReceipts").getValue()), + Optional.of((Boolean) configuration.get("UnidentifiedDeliveryIndicators").getValue()), + Optional.of((Boolean) configuration.get("TypingIndicators").getValue()), + Optional.of((Boolean) configuration.get("LinkPreviews").getValue())); } @Override - public void updateConfiguration(Configuration configuration) throws IOException { - throw new UnsupportedOperationException(); + public void updateConfiguration(Configuration newConfiguration) throws IOException { + final var configuration = getRemoteObject(new DBusPath(signal.getObjectPath() + "/Configuration"), + Signal.Configuration.class); + newConfiguration.readReceipts() + .ifPresent(v -> configuration.Set("org.asamk.Signal.Configuration", "ReadReceipts", v)); + newConfiguration.unidentifiedDeliveryIndicators() + .ifPresent(v -> configuration.Set("org.asamk.Signal.Configuration", + "UnidentifiedDeliveryIndicators", + v)); + newConfiguration.typingIndicators() + .ifPresent(v -> configuration.Set("org.asamk.Signal.Configuration", "TypingIndicators", v)); + newConfiguration.linkPreviews() + .ifPresent(v -> configuration.Set("org.asamk.Signal.Configuration", "LinkPreviews", v)); } @Override @@ -136,12 +150,12 @@ public class DbusManagerImpl implements Manager { @Override public void unregister() throws IOException { - throw new UnsupportedOperationException(); + signal.unregister(); } @Override public void deleteAccount() throws IOException { - throw new UnsupportedOperationException(); + signal.deleteAccount(); } @Override @@ -155,16 +169,16 @@ public class DbusManagerImpl implements Manager { return signal.listDevices().stream().map(d -> { final var device = getRemoteObject(d.getObjectPath(), Signal.Device.class).GetAll("org.asamk.Signal.Device"); - return new Device((long) device.get("Id").getValue(), + return new Device((Integer) device.get("Id").getValue(), (String) device.get("Name").getValue(), (long) device.get("Created").getValue(), (long) device.get("LastSeen").getValue(), thisDevice.equals(d.getObjectPath())); - }).collect(Collectors.toList()); + }).toList(); } @Override - public void removeLinkedDevices(final long deviceId) throws IOException { + public void removeLinkedDevices(final int deviceId) throws IOException { final var devicePath = signal.getDevice(deviceId); getRemoteObject(devicePath, Signal.Device.class).removeDevice(); } @@ -191,7 +205,7 @@ public class DbusManagerImpl implements Manager { @Override public List getGroups() { final var groups = signal.listGroups(); - return groups.stream().map(Signal.StructGroup::getObjectPath).map(this::getGroup).collect(Collectors.toList()); + return groups.stream().map(Signal.StructGroup::getObjectPath).map(this::getGroup).toList(); } @Override @@ -208,7 +222,8 @@ public class DbusManagerImpl implements Manager { @Override public void deleteGroup(final GroupId groupId) throws IOException { - throw new UnsupportedOperationException(); + final var group = getRemoteObject(signal.getGroup(groupId.serialize()), Signal.Group.class); + group.deleteGroup(); } @Override @@ -216,7 +231,7 @@ public class DbusManagerImpl implements Manager { final String name, final Set members, final File avatarFile ) throws IOException, AttachmentInvalidException { final var newGroupId = signal.createGroup(emptyIfNull(name), - members.stream().map(RecipientIdentifier.Single::getIdentifier).collect(Collectors.toList()), + members.stream().map(RecipientIdentifier.Single::getIdentifier).toList(), avatarFile == null ? "" : avatarFile.getPath()); return new Pair<>(GroupId.unknownVersion(newGroupId), new SendGroupMessageResults(0, List.of())); } @@ -254,28 +269,22 @@ public class DbusManagerImpl implements Manager { : GroupPermission.EVERY_MEMBER.name()); } if (updateGroup.getMembers() != null) { - group.addMembers(updateGroup.getMembers() - .stream() - .map(RecipientIdentifier.Single::getIdentifier) - .collect(Collectors.toList())); + group.addMembers(updateGroup.getMembers().stream().map(RecipientIdentifier.Single::getIdentifier).toList()); } if (updateGroup.getRemoveMembers() != null) { group.removeMembers(updateGroup.getRemoveMembers() .stream() .map(RecipientIdentifier.Single::getIdentifier) - .collect(Collectors.toList())); + .toList()); } if (updateGroup.getAdmins() != null) { - group.addAdmins(updateGroup.getAdmins() - .stream() - .map(RecipientIdentifier.Single::getIdentifier) - .collect(Collectors.toList())); + group.addAdmins(updateGroup.getAdmins().stream().map(RecipientIdentifier.Single::getIdentifier).toList()); } if (updateGroup.getRemoveAdmins() != null) { group.removeAdmins(updateGroup.getRemoveAdmins() .stream() .map(RecipientIdentifier.Single::getIdentifier) - .collect(Collectors.toList())); + .toList()); } if (updateGroup.isResetGroupLink()) { group.resetLink(); @@ -307,7 +316,8 @@ public class DbusManagerImpl implements Manager { signal.sendTyping(signal.getSelfNumber(), action == TypingAction.STOP); return 0L; }, groupId -> { - throw new UnsupportedOperationException(); + signal.sendGroupTyping(groupId, action == TypingAction.STOP); + return 0L; }); } @@ -375,19 +385,17 @@ public class DbusManagerImpl implements Manager { @Override public SendMessageResults sendEndSessionMessage(final Set recipients) throws IOException { - signal.sendEndSessionMessage(recipients.stream() - .map(RecipientIdentifier.Single::getIdentifier) - .collect(Collectors.toList())); + signal.sendEndSessionMessage(recipients.stream().map(RecipientIdentifier.Single::getIdentifier).toList()); return new SendMessageResults(0, Map.of()); } @Override - public void deleteRecipient(final RecipientIdentifier.Single recipient) throws IOException { + public void deleteRecipient(final RecipientIdentifier.Single recipient) { signal.deleteRecipient(recipient.getIdentifier()); } @Override - public void deleteContact(final RecipientIdentifier.Single recipient) throws IOException { + public void deleteContact(final RecipientIdentifier.Single recipient) { signal.deleteContact(recipient.getIdentifier()); } @@ -425,14 +433,19 @@ public class DbusManagerImpl implements Manager { } @Override - public URI uploadStickerPack(final File path) throws IOException, StickerPackInvalidException { + public StickerPackUrl uploadStickerPack(final File path) throws IOException, StickerPackInvalidException { try { - return new URI(signal.uploadStickerPack(path.getPath())); - } catch (URISyntaxException e) { + return StickerPackUrl.fromUri(new URI(signal.uploadStickerPack(path.getPath()))); + } catch (URISyntaxException | StickerPackUrl.InvalidStickerPackLinkException e) { throw new AssertionError(e); } } + @Override + public List getStickerPacks() { + throw new UnsupportedOperationException(); + } + @Override public void requestAllSyncData() throws IOException { signal.sendSyncRequest(); @@ -484,14 +497,26 @@ public class DbusManagerImpl implements Manager { @Override public void receiveMessages( - final long timeout, final TimeUnit unit, final ReceiveMessageHandler handler + final Duration timeout, final ReceiveMessageHandler handler ) throws IOException { - addReceiveHandler(handler); - try { - Thread.sleep(unit.toMillis(timeout)); - } catch (InterruptedException ignored) { + final var lastMessage = new AtomicLong(System.currentTimeMillis()); + + final ReceiveMessageHandler receiveHandler = (envelope, e) -> { + lastMessage.set(System.currentTimeMillis()); + handler.handleMessage(envelope, e); + }; + addReceiveHandler(receiveHandler); + while (true) { + try { + final var sleepTimeRemaining = timeout.toMillis() - (System.currentTimeMillis() - lastMessage.get()); + if (sleepTimeRemaining < 0) { + break; + } + Thread.sleep(sleepTimeRemaining); + } catch (InterruptedException ignored) { + } } - removeReceiveHandler(handler); + removeReceiveHandler(receiveHandler); } @Override @@ -596,6 +621,10 @@ public class DbusManagerImpl implements Manager { throw new UnsupportedOperationException(); } + @Override + public void addAddressChangedListener(final Runnable listener) { + } + @Override public void addClosedListener(final Runnable listener) { synchronized (closedListeners) { @@ -604,7 +633,7 @@ public class DbusManagerImpl implements Manager { } @Override - public void close() throws IOException { + public void close() { synchronized (this) { this.notify(); } @@ -632,7 +661,7 @@ public class DbusManagerImpl implements Manager { .filter(r -> r instanceof RecipientIdentifier.Single) .map(RecipientIdentifier.Single.class::cast) .map(RecipientIdentifier.Single::getIdentifier) - .collect(Collectors.toList()); + .toList(); if (singleRecipients.size() > 0) { timestamp = recipientsHandler.apply(singleRecipients); } @@ -644,7 +673,7 @@ public class DbusManagerImpl implements Manager { .filter(r -> r instanceof RecipientIdentifier.Group) .map(RecipientIdentifier.Group.class::cast) .map(RecipientIdentifier.Group::groupId) - .collect(Collectors.toList()); + .toList(); for (final var groupId : groupRecipients) { timestamp = groupHandler.apply(groupId.serialize()); } @@ -655,9 +684,9 @@ public class DbusManagerImpl implements Manager { return string == null ? "" : string; } - private T getRemoteObject(final DBusPath devicePath, final Class type) { + private T getRemoteObject(final DBusPath path, final Class type) { try { - return connection.getRemoteObject(DbusConfig.getBusname(), devicePath.getPath(), type); + return connection.getRemoteObject(DbusConfig.getBusname(), path.getPath(), type); } catch (DBusException e) { throw new AssertionError(e); } @@ -826,7 +855,7 @@ public class DbusManagerImpl implements Manager { getValue(a, "isVoiceNote"), getValue(a, "isGif"), getValue(a, "isBorderless")); - }).collect(Collectors.toList()); + }).toList(); } @SuppressWarnings("unchecked")