import org.asamk.Signal;
import org.asamk.signal.BaseConfig;
-import org.asamk.signal.commands.exceptions.IOErrorException;
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.UntrustedIdentityException;
import org.asamk.signal.manager.api.Identity;
+import org.asamk.signal.manager.api.InactiveGroupLinkException;
+import org.asamk.signal.manager.api.InvalidDeviceLinkException;
+import org.asamk.signal.manager.api.InvalidNumberException;
import org.asamk.signal.manager.api.Message;
+import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.RecipientIdentifier;
+import org.asamk.signal.manager.api.SendMessageResult;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UpdateGroup;
import org.asamk.signal.manager.groups.GroupId;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
+import org.freedesktop.dbus.interfaces.DBusInterface;
import org.freedesktop.dbus.types.Variant;
-import org.whispersystems.libsignal.InvalidKeyException;
-import org.whispersystems.libsignal.util.Pair;
-import org.whispersystems.libsignal.util.guava.Optional;
-import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
-import org.whispersystems.signalservice.api.messages.SendMessageResult;
-import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
-import org.whispersystems.signalservice.api.util.InvalidNumberException;
-import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
private final Manager m;
private final DBusConnection connection;
private final String objectPath;
+ private final boolean noReceiveOnStart;
private DBusPath thisDevice;
private final List<StructDevice> devices = new ArrayList<>();
private final List<StructGroup> groups = new ArrayList<>();
+ private DbusReceiveMessageHandler dbusMessageHandler;
+ private int subscriberCount;
- public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) {
+ private final static Logger logger = LoggerFactory.getLogger(DbusSignalImpl.class);
+
+ public DbusSignalImpl(
+ final Manager m, DBusConnection connection, final String objectPath, final boolean noReceiveOnStart
+ ) {
this.m = m;
this.connection = connection;
this.objectPath = objectPath;
+ this.noReceiveOnStart = noReceiveOnStart;
}
public void initObjects() {
+ if (!noReceiveOnStart) {
+ subscribeReceive();
+ }
+
updateDevices();
updateGroups();
+ updateConfiguration();
}
public void close() {
+ if (dbusMessageHandler != null) {
+ m.removeReceiveHandler(dbusMessageHandler);
+ dbusMessageHandler = null;
+ }
unExportDevices();
unExportGroups();
+ unExportConfiguration();
}
@Override
}
@Override
- public void submitRateLimitChallenge(String challenge, String captchaString) throws IOErrorException {
- final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", "");
+ public void subscribeReceive() {
+ if (dbusMessageHandler == null) {
+ dbusMessageHandler = new DbusReceiveMessageHandler(m, connection, objectPath);
+ m.addReceiveHandler(dbusMessageHandler);
+ }
+ subscriberCount++;
+ }
+ @Override
+ public void unsubscribeReceive() {
+ subscriberCount = Math.max(0, subscriberCount - 1);
+ if (subscriberCount == 0 && dbusMessageHandler != null) {
+ m.removeReceiveHandler(dbusMessageHandler);
+ dbusMessageHandler = null;
+ }
+ }
+
+ @Override
+ public void submitRateLimitChallenge(String challenge, String captcha) {
try {
m.submitRateLimitRecaptchaChallenge(challenge, captcha);
} catch (IOException e) {
- throw new IOErrorException("Submit challenge error: " + e.getMessage(), e);
+ throw new Error.Failure("Submit challenge error: " + e.getMessage());
}
}
+ @Override
+ public void unregister() throws Error.Failure {
+ try {
+ m.unregister();
+ } catch (IOException e) {
+ throw new Error.Failure("Failed to unregister: " + e.getMessage());
+ }
+ }
+
+ @Override
+ public void deleteAccount() throws Error.Failure {
+ try {
+ m.deleteAccount();
+ } catch (IOException e) {
+ throw new Error.Failure("Failed to delete account: " + e.getMessage());
+ }
+ }
+
@Override
public void addDevice(String uri) {
try {
m.addDeviceLink(new URI(uri));
- } catch (IOException | InvalidKeyException e) {
+ } catch (IOException | InvalidDeviceLinkException e) {
throw new Error.Failure(e.getClass().getSimpleName() + " Add device link failed. " + e.getMessage());
} catch (URISyntaxException e) {
throw new Error.InvalidUri(e.getClass().getSimpleName()
@Override
public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) {
try {
- final var results = m.sendMessage(new Message(message, attachments),
+ final var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
.map(RecipientIdentifier.class::cast)
.collect(Collectors.toSet()));
try {
var recipients = new ArrayList<String>(1);
recipients.add(recipient);
- m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START,
+ final var results = m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START,
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
.map(RecipientIdentifier.class::cast)
.collect(Collectors.toSet()));
+ checkSendMessageResults(results.timestamp(), results.results());
} catch (IOException e) {
throw new Error.Failure(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
throw new Error.GroupNotFound(e.getMessage());
- } catch (UntrustedIdentityException e) {
- throw new Error.UntrustedIdentity(e.getMessage());
}
}
final String recipient, final List<Long> messageIds
) throws Error.Failure, Error.UntrustedIdentity {
try {
- m.sendReadReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), messageIds);
+ final var results = m.sendReadReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()),
+ messageIds);
+ checkSendMessageResults(results.timestamp(), results.results());
} catch (IOException e) {
throw new Error.Failure(e.getMessage());
- } catch (UntrustedIdentityException e) {
- throw new Error.UntrustedIdentity(e.getMessage());
}
}
final String recipient, final List<Long> messageIds
) throws Error.Failure, Error.UntrustedIdentity {
try {
- m.sendViewedReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), messageIds);
+ final var results = m.sendViewedReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()),
+ messageIds);
+ checkSendMessageResults(results.timestamp(), results.results());
} catch (IOException e) {
throw new Error.Failure(e.getMessage());
- } catch (UntrustedIdentityException e) {
- throw new Error.UntrustedIdentity(e.getMessage());
}
}
final String message, final List<String> attachments
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
try {
- final var results = m.sendMessage(new Message(message, attachments),
+ final var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
checkSendMessageResults(results.timestamp(), results.results());
return results.timestamp();
@Override
public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
try {
- var results = m.sendMessage(new Message(message, attachments),
+ var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
Set.of(new RecipientIdentifier.Group(getGroupId(groupId))));
checkSendMessageResults(results.timestamp(), results.results());
return results.timestamp();
m.setContactName(getSingleRecipientIdentifier(number, m.getSelfNumber()), name);
} catch (NotMasterDeviceException e) {
throw new Error.Failure("This command doesn't work on linked devices.");
- } catch (UnregisteredUserException e) {
+ } catch (IOException e) {
throw new Error.Failure("Contact is not registered.");
}
}
aboutEmoji = nullIfEmpty(aboutEmoji);
avatarPath = nullIfEmpty(avatarPath);
Optional<File> avatarFile = removeAvatar
- ? Optional.absent()
+ ? Optional.empty()
: avatarPath == null ? null : Optional.of(new File(avatarPath));
m.setProfile(givenName, familyName, about, aboutEmoji, avatarFile);
} catch (IOException e) {
@Override
public void removePin() {
try {
- m.setRegistrationLockPin(Optional.absent());
- } catch (UnauthenticatedResponseException e) {
- throw new Error.Failure("Remove pin failed with unauthenticated response: " + e.getMessage());
+ m.setRegistrationLockPin(Optional.empty());
} catch (IOException e) {
throw new Error.Failure("Remove pin error: " + e.getMessage());
}
public void setPin(String registrationLockPin) {
try {
m.setRegistrationLockPin(Optional.of(registrationLockPin));
- } catch (UnauthenticatedResponseException e) {
- throw new Error.Failure("Set pin error failed with unauthenticated response: " + e.getMessage());
} catch (IOException e) {
throw new Error.Failure("Set pin error: " + e.getMessage());
}
Profile profile = null;
try {
profile = m.getRecipientProfile(RecipientIdentifier.Single.fromAddress(address));
- } catch (UnregisteredUserException ignored) {
+ } catch (IOException ignored) {
}
if (profile != null && profile.getDisplayName().equals(name)) {
numbers.add(number);
}
final var result = m.joinGroup(linkUrl);
return result.first().serialize();
- } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) {
+ } catch (GroupInviteLinkUrl.InvalidGroupLinkException | InactiveGroupLinkException e) {
throw new Error.Failure("Group link is invalid: " + e.getMessage());
} catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
throw new Error.Failure("Group link was created with an incompatible version: " + e.getMessage());
final var message = timestamp + "\nFailed to send message:\n" + error + '\n';
- if (result.getIdentityFailure() != null) {
+ if (result.isIdentityFailure()) {
throw new Error.UntrustedIdentity(message);
} else {
throw new Error.Failure(message);
linkedDevices.forEach(d -> {
final var object = new DbusSignalDeviceImpl(d);
final var deviceObjectPath = object.getObjectPath();
- try {
- connection.exportObject(object);
- } catch (DBusException e) {
- e.printStackTrace();
- }
+ exportObject(object);
if (d.isThisDevice()) {
thisDevice = new DBusPath(deviceObjectPath);
}
groups.forEach(g -> {
final var object = new DbusSignalGroupImpl(g.groupId());
- try {
- connection.exportObject(object);
- } catch (DBusException e) {
- e.printStackTrace();
- }
+ exportObject(object);
this.groups.add(new StructGroup(new DBusPath(object.getObjectPath()),
g.groupId().serialize(),
emptyIfNull(g.title())));
this.groups.clear();
}
+ private static String getConfigurationObjectPath(String basePath) {
+ return basePath + "/Configuration";
+ }
+
+ private void updateConfiguration() {
+ unExportConfiguration();
+ final var object = new DbusSignalConfigurationImpl();
+ exportObject(object);
+ }
+
+ private void unExportConfiguration() {
+ final var objectPath = getConfigurationObjectPath(this.objectPath);
+ connection.unExportObject(objectPath);
+ }
+
+ private void exportObject(final DBusInterface object) {
+ try {
+ connection.exportObject(object);
+ logger.debug("Exported dbus object: " + object.getObjectPath());
+ } catch (DBusException e) {
+ e.printStackTrace();
+ }
+ }
+
public class DbusSignalDeviceImpl extends DbusProperties implements Signal.Device {
private final org.asamk.signal.manager.api.Device device;
}
}
+ public class DbusSignalConfigurationImpl extends DbusProperties implements Signal.Configuration {
+
+ public DbusSignalConfigurationImpl(
+ ) {
+ super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Configuration",
+ List.of(new DbusProperty<>("ReadReceipts", this::getReadReceipts, this::setReadReceipts),
+ new DbusProperty<>("UnidentifiedDeliveryIndicators",
+ this::getUnidentifiedDeliveryIndicators,
+ this::setUnidentifiedDeliveryIndicators),
+ new DbusProperty<>("TypingIndicators",
+ this::getTypingIndicators,
+ this::setTypingIndicators),
+ new DbusProperty<>("LinkPreviews", this::getLinkPreviews, this::setLinkPreviews))));
+
+ }
+
+ @Override
+ public String getObjectPath() {
+ return getConfigurationObjectPath(objectPath);
+ }
+
+ public void setReadReceipts(Boolean readReceipts) {
+ setConfiguration(readReceipts, null, null, null);
+ }
+
+ public void setUnidentifiedDeliveryIndicators(Boolean unidentifiedDeliveryIndicators) {
+ setConfiguration(null, unidentifiedDeliveryIndicators, null, null);
+ }
+
+ public void setTypingIndicators(Boolean typingIndicators) {
+ setConfiguration(null, null, typingIndicators, null);
+ }
+
+ public void setLinkPreviews(Boolean linkPreviews) {
+ setConfiguration(null, null, null, linkPreviews);
+ }
+
+ private void setConfiguration(
+ Boolean readReceipts,
+ Boolean unidentifiedDeliveryIndicators,
+ Boolean typingIndicators,
+ Boolean linkPreviews
+ ) {
+ try {
+ m.updateConfiguration(new org.asamk.signal.manager.api.Configuration(Optional.ofNullable(readReceipts),
+ Optional.ofNullable(unidentifiedDeliveryIndicators),
+ Optional.ofNullable(typingIndicators),
+ Optional.ofNullable(linkPreviews)));
+ } catch (IOException e) {
+ throw new Error.Failure("UpdateAccount error: " + e.getMessage());
+ } catch (NotMasterDeviceException e) {
+ throw new Error.Failure("This command doesn't work on linked devices.");
+ }
+ }
+
+ private boolean getReadReceipts() {
+ return m.getConfiguration().readReceipts().orElse(false);
+ }
+
+ private boolean getUnidentifiedDeliveryIndicators() {
+ return m.getConfiguration().unidentifiedDeliveryIndicators().orElse(false);
+ }
+
+ private boolean getTypingIndicators() {
+ return m.getConfiguration().typingIndicators().orElse(false);
+ }
+
+ private boolean getLinkPreviews() {
+ return m.getConfiguration().linkPreviews().orElse(false);
+ }
+ }
+
public class DbusSignalGroupImpl extends DbusProperties implements Signal.Group {
private final GroupId groupId;