Add RecipientIdentifier as external Manager interface

This commit is contained in:
AsamK 2021-08-25 12:22:53 +02:00
parent cd7172ee57
commit 467a48bac5
25 changed files with 958 additions and 595 deletions

View file

@ -18,6 +18,9 @@ package org.asamk.signal.manager;
import org.asamk.signal.manager.api.Device; import org.asamk.signal.manager.api.Device;
import org.asamk.signal.manager.api.Message; import org.asamk.signal.manager.api.Message;
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.TypingAction; import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.config.ServiceConfig; import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironment; import org.asamk.signal.manager.config.ServiceEnvironment;
@ -156,6 +159,7 @@ import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -354,9 +358,6 @@ public class Manager implements Closeable {
.filter(s -> !s.isEmpty()) .filter(s -> !s.isEmpty())
.collect(Collectors.toSet())); .collect(Collectors.toSet()));
// Store numbers as recipients so we have the number/uuid association
contactDetails.forEach((number, uuid) -> resolveRecipientTrusted(new SignalServiceAddress(uuid, number)));
return numbers.stream().collect(Collectors.toMap(n -> n, n -> { return numbers.stream().collect(Collectors.toMap(n -> n, n -> {
final var number = canonicalizedNumbers.get(n); final var number = canonicalizedNumbers.get(n);
final var uuid = contactDetails.get(number); final var uuid = contactDetails.get(number);
@ -697,31 +698,9 @@ public class Manager implements Closeable {
return account.getGroupStore().getGroups(); return account.getGroupStore().getGroups();
} }
public Pair<Long, List<SendMessageResult>> sendGroupMessage( public SendGroupMessageResults sendQuitGroupMessage(
Message message, GroupId groupId GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException { ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException {
final var messageBuilder = createMessageBuilder();
applyMessage(messageBuilder, message);
return sendHelper.sendAsGroupMessage(messageBuilder, groupId);
}
public Pair<Long, List<SendMessageResult>> sendGroupMessageReaction(
String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, GroupId groupId
) throws IOException, InvalidNumberException, NotAGroupMemberException, GroupNotFoundException {
var targetAuthorRecipientId = canonicalizeAndResolveRecipient(targetAuthor);
var reaction = new SignalServiceDataMessage.Reaction(emoji,
remove,
resolveSignalServiceAddress(targetAuthorRecipientId),
targetSentTimestamp);
final var messageBuilder = createMessageBuilder().withReaction(reaction);
return sendHelper.sendAsGroupMessage(messageBuilder, groupId);
}
public Pair<Long, List<SendMessageResult>> sendQuitGroupMessage(
GroupId groupId, Set<String> groupAdmins
) throws GroupNotFoundException, IOException, NotAGroupMemberException, InvalidNumberException, LastGroupAdminException {
var group = getGroupForUpdating(groupId); var group = getGroupForUpdating(groupId);
if (group instanceof GroupInfoV1) { if (group instanceof GroupInfoV1) {
return quitGroupV1((GroupInfoV1) group); return quitGroupV1((GroupInfoV1) group);
@ -737,19 +716,19 @@ public class Manager implements Closeable {
} }
} }
private Pair<Long, List<SendMessageResult>> quitGroupV1(final GroupInfoV1 groupInfoV1) throws IOException { private SendGroupMessageResults quitGroupV1(final GroupInfoV1 groupInfoV1) throws IOException {
var group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT) var group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT)
.withId(groupInfoV1.getGroupId().serialize()) .withId(groupInfoV1.getGroupId().serialize())
.build(); .build();
var messageBuilder = createMessageBuilder().asGroupMessage(group); var messageBuilder = SignalServiceDataMessage.newBuilder().asGroupMessage(group);
groupInfoV1.removeMember(account.getSelfRecipientId()); groupInfoV1.removeMember(account.getSelfRecipientId());
account.getGroupStore().updateGroup(groupInfoV1); account.getGroupStore().updateGroup(groupInfoV1);
return sendHelper.sendGroupMessage(messageBuilder.build(), return sendGroupMessage(messageBuilder,
groupInfoV1.getMembersIncludingPendingWithout(account.getSelfRecipientId())); groupInfoV1.getMembersIncludingPendingWithout(account.getSelfRecipientId()));
} }
private Pair<Long, List<SendMessageResult>> quitGroupV2( private SendGroupMessageResults quitGroupV2(
final GroupInfoV2 groupInfoV2, final Set<RecipientId> newAdmins final GroupInfoV2 groupInfoV2, final Set<RecipientId> newAdmins
) throws LastGroupAdminException, IOException { ) throws LastGroupAdminException, IOException {
final var currentAdmins = groupInfoV2.getAdminMembers(); final var currentAdmins = groupInfoV2.getAdminMembers();
@ -764,9 +743,10 @@ public class Manager implements Closeable {
} }
final var groupGroupChangePair = groupV2Helper.leaveGroup(groupInfoV2, newAdmins); final var groupGroupChangePair = groupV2Helper.leaveGroup(groupInfoV2, newAdmins);
groupInfoV2.setGroup(groupGroupChangePair.first(), this::resolveRecipient); groupInfoV2.setGroup(groupGroupChangePair.first(), this::resolveRecipient);
var messageBuilder = getGroupUpdateMessageBuilder(groupInfoV2, groupGroupChangePair.second().toByteArray());
account.getGroupStore().updateGroup(groupInfoV2); account.getGroupStore().updateGroup(groupInfoV2);
return sendHelper.sendGroupMessage(messageBuilder.build(),
var messageBuilder = getGroupUpdateMessageBuilder(groupInfoV2, groupGroupChangePair.second().toByteArray());
return sendGroupMessage(messageBuilder,
groupInfoV2.getMembersIncludingPendingWithout(account.getSelfRecipientId())); groupInfoV2.getMembersIncludingPendingWithout(account.getSelfRecipientId()));
} }
@ -775,13 +755,13 @@ public class Manager implements Closeable {
avatarStore.deleteGroupAvatar(groupId); avatarStore.deleteGroupAvatar(groupId);
} }
public Pair<GroupId, List<SendMessageResult>> createGroup( public Pair<GroupId, SendGroupMessageResults> createGroup(
String name, List<String> members, File avatarFile String name, Set<RecipientIdentifier.Single> members, File avatarFile
) throws IOException, AttachmentInvalidException, InvalidNumberException { ) throws IOException, AttachmentInvalidException {
return createGroup(name, members == null ? null : getRecipientIds(members), avatarFile); return createGroupInternal(name, members == null ? null : getRecipientIds(members), avatarFile);
} }
private Pair<GroupId, List<SendMessageResult>> createGroup( private Pair<GroupId, SendGroupMessageResults> createGroupInternal(
String name, Set<RecipientId> members, File avatarFile String name, Set<RecipientId> members, File avatarFile
) throws IOException, AttachmentInvalidException { ) throws IOException, AttachmentInvalidException {
final var selfRecipientId = account.getSelfRecipientId(); final var selfRecipientId = account.getSelfRecipientId();
@ -794,13 +774,12 @@ public class Manager implements Closeable {
members == null ? Set.of() : members, members == null ? Set.of() : members,
avatarFile); avatarFile);
SignalServiceDataMessage.Builder messageBuilder;
if (gv2Pair == null) { if (gv2Pair == null) {
// Failed to create v2 group, creating v1 group instead // Failed to create v2 group, creating v1 group instead
var gv1 = new GroupInfoV1(GroupIdV1.createRandom()); var gv1 = new GroupInfoV1(GroupIdV1.createRandom());
gv1.addMembers(List.of(selfRecipientId)); gv1.addMembers(List.of(selfRecipientId));
final var result = updateGroupV1(gv1, name, members, avatarFile); final var result = updateGroupV1(gv1, name, members, avatarFile);
return new Pair<>(gv1.getGroupId(), result.second()); return new Pair<>(gv1.getGroupId(), result);
} }
final var gv2 = gv2Pair.first(); final var gv2 = gv2Pair.first();
@ -811,22 +790,23 @@ public class Manager implements Closeable {
avatarStore.storeGroupAvatar(gv2.getGroupId(), avatarStore.storeGroupAvatar(gv2.getGroupId(),
outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream)); outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream));
} }
messageBuilder = getGroupUpdateMessageBuilder(gv2, null);
account.getGroupStore().updateGroup(gv2); account.getGroupStore().updateGroup(gv2);
final var result = sendHelper.sendGroupMessage(messageBuilder.build(), final var messageBuilder = getGroupUpdateMessageBuilder(gv2, null);
gv2.getMembersIncludingPendingWithout(selfRecipientId));
return new Pair<>(gv2.getGroupId(), result.second()); final var result = sendGroupMessage(messageBuilder, gv2.getMembersIncludingPendingWithout(selfRecipientId));
return new Pair<>(gv2.getGroupId(), result);
} }
public Pair<Long, List<SendMessageResult>> updateGroup( public SendGroupMessageResults updateGroup(
GroupId groupId, GroupId groupId,
String name, String name,
String description, String description,
List<String> members, Set<RecipientIdentifier.Single> members,
List<String> removeMembers, Set<RecipientIdentifier.Single> removeMembers,
List<String> admins, Set<RecipientIdentifier.Single> admins,
List<String> removeAdmins, Set<RecipientIdentifier.Single> removeAdmins,
boolean resetGroupLink, boolean resetGroupLink,
GroupLinkState groupLinkState, GroupLinkState groupLinkState,
GroupPermission addMemberPermission, GroupPermission addMemberPermission,
@ -834,8 +814,8 @@ public class Manager implements Closeable {
File avatarFile, File avatarFile,
Integer expirationTimer, Integer expirationTimer,
Boolean isAnnouncementGroup Boolean isAnnouncementGroup
) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException { ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
return updateGroup(groupId, return updateGroupInternal(groupId,
name, name,
description, description,
members == null ? null : getRecipientIds(members), members == null ? null : getRecipientIds(members),
@ -851,7 +831,7 @@ public class Manager implements Closeable {
isAnnouncementGroup); isAnnouncementGroup);
} }
private Pair<Long, List<SendMessageResult>> updateGroup( private SendGroupMessageResults updateGroupInternal(
final GroupId groupId, final GroupId groupId,
final String name, final String name,
final String description, final String description,
@ -913,16 +893,15 @@ public class Manager implements Closeable {
return result; return result;
} }
private Pair<Long, List<SendMessageResult>> updateGroupV1( private SendGroupMessageResults updateGroupV1(
final GroupInfoV1 gv1, final String name, final Set<RecipientId> members, final File avatarFile final GroupInfoV1 gv1, final String name, final Set<RecipientId> members, final File avatarFile
) throws IOException, AttachmentInvalidException { ) throws IOException, AttachmentInvalidException {
updateGroupV1Details(gv1, name, members, avatarFile); updateGroupV1Details(gv1, name, members, avatarFile);
var messageBuilder = getGroupUpdateMessageBuilder(gv1);
account.getGroupStore().updateGroup(gv1); account.getGroupStore().updateGroup(gv1);
return sendHelper.sendGroupMessage(messageBuilder.build(), var messageBuilder = getGroupUpdateMessageBuilder(gv1);
gv1.getMembersIncludingPendingWithout(account.getSelfRecipientId())); return sendGroupMessage(messageBuilder, gv1.getMembersIncludingPendingWithout(account.getSelfRecipientId()));
} }
private void updateGroupV1Details( private void updateGroupV1Details(
@ -963,7 +942,7 @@ public class Manager implements Closeable {
} }
} }
private Pair<Long, List<SendMessageResult>> updateGroupV2( private SendGroupMessageResults updateGroupV2(
final GroupInfoV2 group, final GroupInfoV2 group,
final String name, final String name,
final String description, final String description,
@ -979,7 +958,7 @@ public class Manager implements Closeable {
final Integer expirationTimer, final Integer expirationTimer,
final Boolean isAnnouncementGroup final Boolean isAnnouncementGroup
) throws IOException { ) throws IOException {
Pair<Long, List<SendMessageResult>> result = null; SendGroupMessageResults result = null;
if (group.isPendingMember(account.getSelfRecipientId())) { if (group.isPendingMember(account.getSelfRecipientId())) {
var groupGroupChangePair = groupV2Helper.acceptInvite(group); var groupGroupChangePair = groupV2Helper.acceptInvite(group);
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second()); result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
@ -1080,7 +1059,7 @@ public class Manager implements Closeable {
return result; return result;
} }
public Pair<GroupId, List<SendMessageResult>> joinGroup( public Pair<GroupId, SendGroupMessageResults> joinGroup(
GroupInviteLinkUrl inviteLinkUrl GroupInviteLinkUrl inviteLinkUrl
) throws IOException, GroupLinkNotActiveException { ) throws IOException, GroupLinkNotActiveException {
final var groupJoinInfo = groupV2Helper.getDecryptedGroupJoinInfo(inviteLinkUrl.getGroupMasterKey(), final var groupJoinInfo = groupV2Helper.getDecryptedGroupJoinInfo(inviteLinkUrl.getGroupMasterKey(),
@ -1094,25 +1073,74 @@ public class Manager implements Closeable {
if (group.getGroup() == null) { if (group.getGroup() == null) {
// Only requested member, can't send update to group members // Only requested member, can't send update to group members
return new Pair<>(group.getGroupId(), List.of()); return new Pair<>(group.getGroupId(), new SendGroupMessageResults(0, List.of()));
} }
final var result = sendUpdateGroupV2Message(group, group.getGroup(), groupChange); final var result = sendUpdateGroupV2Message(group, group.getGroup(), groupChange);
return new Pair<>(group.getGroupId(), result.second()); return new Pair<>(group.getGroupId(), result);
} }
private Pair<Long, List<SendMessageResult>> sendUpdateGroupV2Message( private SendGroupMessageResults sendUpdateGroupV2Message(
GroupInfoV2 group, DecryptedGroup newDecryptedGroup, GroupChange groupChange GroupInfoV2 group, DecryptedGroup newDecryptedGroup, GroupChange groupChange
) throws IOException { ) throws IOException {
final var selfRecipientId = account.getSelfRecipientId(); final var selfRecipientId = account.getSelfRecipientId();
final var members = group.getMembersIncludingPendingWithout(selfRecipientId); final var members = group.getMembersIncludingPendingWithout(selfRecipientId);
group.setGroup(newDecryptedGroup, this::resolveRecipient); group.setGroup(newDecryptedGroup, this::resolveRecipient);
members.addAll(group.getMembersIncludingPendingWithout(selfRecipientId)); members.addAll(group.getMembersIncludingPendingWithout(selfRecipientId));
account.getGroupStore().updateGroup(group);
final var messageBuilder = getGroupUpdateMessageBuilder(group, groupChange.toByteArray()); final var messageBuilder = getGroupUpdateMessageBuilder(group, groupChange.toByteArray());
account.getGroupStore().updateGroup(group); return sendGroupMessage(messageBuilder, members);
return sendHelper.sendGroupMessage(messageBuilder.build(), members); }
public SendMessageResults sendMessage(
SignalServiceDataMessage.Builder messageBuilder, Set<RecipientIdentifier> recipients
) throws IOException, NotAGroupMemberException, GroupNotFoundException {
var results = new HashMap<RecipientIdentifier, List<SendMessageResult>>();
long timestamp = System.currentTimeMillis();
messageBuilder.withTimestamp(timestamp);
for (final var recipient : recipients) {
if (recipient instanceof RecipientIdentifier.Single) {
final var recipientId = resolveRecipient((RecipientIdentifier.Single) recipient);
final var result = sendHelper.sendMessage(messageBuilder, recipientId);
results.put(recipient, List.of(result));
} else if (recipient instanceof RecipientIdentifier.NoteToSelf) {
final var result = sendHelper.sendSelfMessage(messageBuilder);
results.put(recipient, List.of(result));
} else if (recipient instanceof RecipientIdentifier.Group) {
final var groupId = ((RecipientIdentifier.Group) recipient).groupId;
final var result = sendHelper.sendAsGroupMessage(messageBuilder, groupId);
results.put(recipient, result);
}
}
return new SendMessageResults(timestamp, results);
}
public void sendTypingMessage(
SignalServiceTypingMessage.Action action, Set<RecipientIdentifier> recipients
) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException {
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);
} 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);
}
}
}
private SendGroupMessageResults sendGroupMessage(
final SignalServiceDataMessage.Builder messageBuilder, final Set<RecipientId> members
) throws IOException {
final var timestamp = System.currentTimeMillis();
messageBuilder.withTimestamp(timestamp);
final var results = sendHelper.sendGroupMessage(messageBuilder.build(), members);
return new SendGroupMessageResults(timestamp, results);
} }
private static int currentTimeDays() { private static int currentTimeDays() {
@ -1138,7 +1166,7 @@ public class Manager implements Closeable {
} }
} }
Pair<Long, List<SendMessageResult>> sendGroupInfoMessage( SendGroupMessageResults sendGroupInfoMessage(
GroupIdV1 groupId, SignalServiceAddress recipient GroupIdV1 groupId, SignalServiceAddress recipient
) throws IOException, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException { ) throws IOException, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException {
GroupInfoV1 g; GroupInfoV1 g;
@ -1156,7 +1184,7 @@ public class Manager implements Closeable {
var messageBuilder = getGroupUpdateMessageBuilder(g); var messageBuilder = getGroupUpdateMessageBuilder(g);
// Send group message only to the recipient who requested it // Send group message only to the recipient who requested it
return sendHelper.sendGroupMessage(messageBuilder.build(), Set.of(recipientId)); return sendGroupMessage(messageBuilder, Set.of(recipientId));
} }
private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV1 g) throws AttachmentInvalidException { private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV1 g) throws AttachmentInvalidException {
@ -1177,45 +1205,49 @@ public class Manager implements Closeable {
throw new AttachmentInvalidException(g.getGroupId().toBase64(), e); throw new AttachmentInvalidException(g.getGroupId().toBase64(), e);
} }
return createMessageBuilder().asGroupMessage(group.build()).withExpiration(g.getMessageExpirationTime()); return SignalServiceDataMessage.newBuilder()
.asGroupMessage(group.build())
.withExpiration(g.getMessageExpirationTime());
} }
private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV2 g, byte[] signedGroupChange) { private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV2 g, byte[] signedGroupChange) {
var group = SignalServiceGroupV2.newBuilder(g.getMasterKey()) var group = SignalServiceGroupV2.newBuilder(g.getMasterKey())
.withRevision(g.getGroup().getRevision()) .withRevision(g.getGroup().getRevision())
.withSignedGroupChange(signedGroupChange); .withSignedGroupChange(signedGroupChange);
return createMessageBuilder().asGroupMessage(group.build()).withExpiration(g.getMessageExpirationTime()); return SignalServiceDataMessage.newBuilder()
.asGroupMessage(group.build())
.withExpiration(g.getMessageExpirationTime());
} }
Pair<Long, List<SendMessageResult>> sendGroupInfoRequest( SendGroupMessageResults sendGroupInfoRequest(
GroupIdV1 groupId, SignalServiceAddress recipient GroupIdV1 groupId, SignalServiceAddress recipient
) throws IOException { ) throws IOException {
var group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.REQUEST_INFO).withId(groupId.serialize()); var group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.REQUEST_INFO).withId(groupId.serialize());
var messageBuilder = createMessageBuilder().asGroupMessage(group.build()); var messageBuilder = SignalServiceDataMessage.newBuilder().asGroupMessage(group.build());
// Send group info request message to the recipient who sent us a message with this groupId // Send group info request message to the recipient who sent us a message with this groupId
return sendHelper.sendGroupMessage(messageBuilder.build(), Set.of(resolveRecipient(recipient))); return sendGroupMessage(messageBuilder, Set.of(resolveRecipient(recipient)));
} }
public void sendReadReceipt( public void sendReadReceipt(
String sender, List<Long> messageIds RecipientIdentifier.Single sender, List<Long> messageIds
) throws IOException, UntrustedIdentityException, InvalidNumberException { ) throws IOException, UntrustedIdentityException {
var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ, var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ,
messageIds, messageIds,
System.currentTimeMillis()); System.currentTimeMillis());
sendHelper.sendReceiptMessage(receiptMessage, canonicalizeAndResolveRecipient(sender)); sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender));
} }
public void sendViewedReceipt( public void sendViewedReceipt(
String sender, List<Long> messageIds RecipientIdentifier.Single sender, List<Long> messageIds
) throws IOException, UntrustedIdentityException, InvalidNumberException { ) throws IOException, UntrustedIdentityException {
var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.VIEWED, var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.VIEWED,
messageIds, messageIds,
System.currentTimeMillis()); System.currentTimeMillis());
sendHelper.sendReceiptMessage(receiptMessage, canonicalizeAndResolveRecipient(sender)); sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender));
} }
void sendDeliveryReceipt( void sendDeliveryReceipt(
@ -1228,12 +1260,12 @@ public class Manager implements Closeable {
sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(remoteAddress)); sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(remoteAddress));
} }
public Pair<Long, List<SendMessageResult>> sendMessage( public SendMessageResults sendMessage(
Message message, List<String> recipients Message message, Set<RecipientIdentifier> recipients
) throws IOException, AttachmentInvalidException, InvalidNumberException { ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException {
final var messageBuilder = createMessageBuilder(); final var messageBuilder = SignalServiceDataMessage.newBuilder();
applyMessage(messageBuilder, message); applyMessage(messageBuilder, message);
return sendHelper.sendMessage(messageBuilder, getRecipientIds(recipients)); return sendMessage(messageBuilder, recipients);
} }
private void applyMessage( private void applyMessage(
@ -1258,48 +1290,41 @@ public class Manager implements Closeable {
} }
} }
public Pair<Long, SendMessageResult> sendSelfMessage(final Message message) throws IOException, AttachmentInvalidException { public SendMessageResults sendRemoteDeleteMessage(
final var messageBuilder = createMessageBuilder(); long targetSentTimestamp, Set<RecipientIdentifier> recipients
applyMessage(messageBuilder, message);
return sendHelper.sendSelfMessage(messageBuilder);
}
public Pair<Long, List<SendMessageResult>> sendRemoteDeleteMessage(
long targetSentTimestamp, List<String> recipients
) throws IOException, InvalidNumberException {
var delete = new SignalServiceDataMessage.RemoteDelete(targetSentTimestamp);
final var messageBuilder = createMessageBuilder().withRemoteDelete(delete);
return sendHelper.sendMessage(messageBuilder, getRecipientIds(recipients));
}
public Pair<Long, List<SendMessageResult>> sendGroupRemoteDeleteMessage(
long targetSentTimestamp, GroupId groupId
) throws IOException, NotAGroupMemberException, GroupNotFoundException { ) throws IOException, NotAGroupMemberException, GroupNotFoundException {
var delete = new SignalServiceDataMessage.RemoteDelete(targetSentTimestamp); var delete = new SignalServiceDataMessage.RemoteDelete(targetSentTimestamp);
final var messageBuilder = createMessageBuilder().withRemoteDelete(delete); final var messageBuilder = SignalServiceDataMessage.newBuilder().withRemoteDelete(delete);
return sendHelper.sendAsGroupMessage(messageBuilder, groupId); return sendMessage(messageBuilder, recipients);
} }
public Pair<Long, List<SendMessageResult>> sendMessageReaction( public SendMessageResults sendMessageReaction(
String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, List<String> recipients String emoji,
) throws IOException, InvalidNumberException { boolean remove,
var targetAuthorRecipientId = canonicalizeAndResolveRecipient(targetAuthor); RecipientIdentifier.Single targetAuthor,
long targetSentTimestamp,
Set<RecipientIdentifier> recipients
) throws IOException, NotAGroupMemberException, GroupNotFoundException {
var targetAuthorRecipientId = resolveRecipient(targetAuthor);
var reaction = new SignalServiceDataMessage.Reaction(emoji, var reaction = new SignalServiceDataMessage.Reaction(emoji,
remove, remove,
resolveSignalServiceAddress(targetAuthorRecipientId), resolveSignalServiceAddress(targetAuthorRecipientId),
targetSentTimestamp); targetSentTimestamp);
final var messageBuilder = createMessageBuilder().withReaction(reaction); final var messageBuilder = SignalServiceDataMessage.newBuilder().withReaction(reaction);
return sendHelper.sendMessage(messageBuilder, getRecipientIds(recipients)); return sendMessage(messageBuilder, recipients);
} }
public Pair<Long, List<SendMessageResult>> sendEndSessionMessage(List<String> recipients) throws IOException, InvalidNumberException { public SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException {
var messageBuilder = createMessageBuilder().asEndSessionMessage(); var messageBuilder = SignalServiceDataMessage.newBuilder().asEndSessionMessage();
final var recipientIds = getRecipientIds(recipients);
try { try {
return sendHelper.sendMessage(messageBuilder, recipientIds); return sendMessage(messageBuilder,
recipients.stream().map(RecipientIdentifier.class::cast).collect(Collectors.toSet()));
} catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new AssertionError(e);
} finally { } finally {
for (var recipientId : recipientIds) { for (var recipient : recipients) {
final var recipientId = resolveRecipient((RecipientIdentifier.Single) recipient);
handleEndSession(recipientId); handleEndSession(recipientId);
} }
} }
@ -1312,23 +1337,25 @@ public class Manager implements Closeable {
} }
} }
public void setContactName(String number, String name) throws InvalidNumberException, NotMasterDeviceException { public void setContactName(
RecipientIdentifier.Single recipient, String name
) throws NotMasterDeviceException {
if (!account.isMasterDevice()) { if (!account.isMasterDevice()) {
throw new NotMasterDeviceException(); throw new NotMasterDeviceException();
} }
final var recipientId = canonicalizeAndResolveRecipient(number); final var recipientId = resolveRecipient(recipient);
var contact = account.getContactStore().getContact(recipientId); var contact = account.getContactStore().getContact(recipientId);
final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact); final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
account.getContactStore().storeContact(recipientId, builder.withName(name).build()); account.getContactStore().storeContact(recipientId, builder.withName(name).build());
} }
public void setContactBlocked( public void setContactBlocked(
String number, boolean blocked RecipientIdentifier.Single recipient, boolean blocked
) throws InvalidNumberException, NotMasterDeviceException { ) throws NotMasterDeviceException {
if (!account.isMasterDevice()) { if (!account.isMasterDevice()) {
throw new NotMasterDeviceException(); throw new NotMasterDeviceException();
} }
setContactBlocked(canonicalizeAndResolveRecipient(number), blocked); setContactBlocked(resolveRecipient(recipient), blocked);
} }
private void setContactBlocked(RecipientId recipientId, boolean blocked) { private void setContactBlocked(RecipientId recipientId, boolean blocked) {
@ -1357,20 +1384,20 @@ public class Manager implements Closeable {
.storeContact(recipientId, builder.withMessageExpirationTime(messageExpirationTimer).build()); .storeContact(recipientId, builder.withMessageExpirationTime(messageExpirationTimer).build());
} }
private void sendExpirationTimerUpdate(RecipientId recipientId) throws IOException {
final var messageBuilder = createMessageBuilder().asExpirationUpdate();
sendHelper.sendMessage(messageBuilder, Set.of(recipientId));
}
/** /**
* Change the expiration timer for a contact * Change the expiration timer for a contact
*/ */
public void setExpirationTimer( public void setExpirationTimer(
String number, int messageExpirationTimer RecipientIdentifier.Single recipient, int messageExpirationTimer
) throws IOException, InvalidNumberException { ) throws IOException {
var recipientId = canonicalizeAndResolveRecipient(number); var recipientId = resolveRecipient(recipient);
setExpirationTimer(recipientId, messageExpirationTimer); setExpirationTimer(recipientId, messageExpirationTimer);
sendExpirationTimerUpdate(recipientId); final var messageBuilder = SignalServiceDataMessage.newBuilder().asExpirationUpdate();
try {
sendMessage(messageBuilder, Set.of(recipient));
} catch (NotAGroupMemberException | GroupNotFoundException e) {
throw new AssertionError(e);
}
} }
/** /**
@ -1385,7 +1412,7 @@ public class Manager implements Closeable {
} }
private void sendExpirationTimerUpdate(GroupIdV1 groupId) throws IOException, NotAGroupMemberException, GroupNotFoundException { private void sendExpirationTimerUpdate(GroupIdV1 groupId) throws IOException, NotAGroupMemberException, GroupNotFoundException {
final var messageBuilder = createMessageBuilder().asExpirationUpdate(); final var messageBuilder = SignalServiceDataMessage.newBuilder().asExpirationUpdate();
sendHelper.sendAsGroupMessage(messageBuilder, groupId); sendHelper.sendAsGroupMessage(messageBuilder, groupId);
} }
@ -1395,7 +1422,7 @@ public class Manager implements Closeable {
* @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
* @return if successful, returns the URL to install the sticker pack in the signal app * @return if successful, returns the URL to install the sticker pack in the signal app
*/ */
public String uploadStickerPack(File path) throws IOException, StickerPackInvalidException { public URI uploadStickerPack(File path) throws IOException, StickerPackInvalidException {
var manifest = StickerUtils.getSignalServiceStickerManifestUpload(path); var manifest = StickerUtils.getSignalServiceStickerManifestUpload(path);
var messageSender = dependencies.getMessageSender(); var messageSender = dependencies.getMessageSender();
@ -1414,7 +1441,7 @@ public class Manager implements Closeable {
"pack_id=" "pack_id="
+ URLEncoder.encode(Hex.toStringCondensed(packId.serialize()), StandardCharsets.UTF_8) + URLEncoder.encode(Hex.toStringCondensed(packId.serialize()), StandardCharsets.UTF_8)
+ "&pack_key=" + "&pack_key="
+ URLEncoder.encode(Hex.toStringCondensed(packKey), StandardCharsets.UTF_8)).toString(); + URLEncoder.encode(Hex.toStringCondensed(packKey), StandardCharsets.UTF_8));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
@ -1484,12 +1511,12 @@ public class Manager implements Closeable {
return certificate; return certificate;
} }
private Set<RecipientId> getRecipientIds(Collection<String> numbers) throws InvalidNumberException { private Set<RecipientId> getRecipientIds(Collection<RecipientIdentifier.Single> recipients) {
final var signalServiceAddresses = new HashSet<SignalServiceAddress>(numbers.size()); final var signalServiceAddresses = new HashSet<SignalServiceAddress>(recipients.size());
final var addressesMissingUuid = new HashSet<SignalServiceAddress>(); final var addressesMissingUuid = new HashSet<SignalServiceAddress>();
for (var number : numbers) { for (var number : recipients) {
final var resolvedAddress = resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number)); final var resolvedAddress = resolveSignalServiceAddress(resolveRecipient(number));
if (resolvedAddress.getUuid().isPresent()) { if (resolvedAddress.getUuid().isPresent()) {
signalServiceAddresses.add(resolvedAddress); signalServiceAddresses.add(resolvedAddress);
} else { } else {
@ -1534,40 +1561,26 @@ public class Manager implements Closeable {
} }
private Map<String, UUID> getRegisteredUsers(final Set<String> numbers) throws IOException { private Map<String, UUID> getRegisteredUsers(final Set<String> numbers) throws IOException {
final Map<String, UUID> registeredUsers;
try { try {
return dependencies.getAccountManager() registeredUsers = dependencies.getAccountManager()
.getRegisteredUsers(ServiceConfig.getIasKeyStore(), .getRegisteredUsers(ServiceConfig.getIasKeyStore(),
numbers, numbers,
serviceEnvironmentConfig.getCdsMrenclave()); serviceEnvironmentConfig.getCdsMrenclave());
} catch (Quote.InvalidQuoteFormatException | UnauthenticatedQuoteException | SignatureException | UnauthenticatedResponseException | InvalidKeyException e) { } catch (Quote.InvalidQuoteFormatException | UnauthenticatedQuoteException | SignatureException | UnauthenticatedResponseException | InvalidKeyException e) {
throw new IOException(e); throw new IOException(e);
} }
// Store numbers as recipients so we have the number/uuid association
registeredUsers.forEach((number, uuid) -> resolveRecipientTrusted(new SignalServiceAddress(uuid, number)));
return registeredUsers;
} }
public void sendTypingMessage( public void sendTypingMessage(
TypingAction action, Set<String> recipients TypingAction action, Set<RecipientIdentifier> recipients
) throws IOException, UntrustedIdentityException, InvalidNumberException { ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException {
final var timestamp = System.currentTimeMillis(); sendTypingMessage(action.toSignalService(), recipients);
var message = new SignalServiceTypingMessage(action.toSignalService(), timestamp, Optional.absent());
sendHelper.sendTypingMessage(message, getRecipientIds(recipients));
}
public void sendGroupTypingMessage(
TypingAction action, GroupId groupId
) throws IOException, NotAGroupMemberException, GroupNotFoundException {
final var timestamp = System.currentTimeMillis();
final var message = new SignalServiceTypingMessage(action.toSignalService(),
timestamp,
Optional.of(groupId.serialize()));
sendHelper.sendGroupTypingMessage(message, groupId);
}
private SignalServiceDataMessage.Builder createMessageBuilder() {
final var timestamp = System.currentTimeMillis();
var messageBuilder = SignalServiceDataMessage.newBuilder();
messageBuilder.withTimestamp(timestamp);
return messageBuilder;
} }
private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, ProtocolUntrustedIdentityException, InvalidMessageStructureException { private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, ProtocolUntrustedIdentityException, InvalidMessageStructureException {
@ -2005,8 +2018,8 @@ public class Manager implements Closeable {
return false; return false;
} }
public boolean isContactBlocked(final String identifier) throws InvalidNumberException { public boolean isContactBlocked(final RecipientIdentifier.Single recipient) {
final var recipientId = canonicalizeAndResolveRecipient(identifier); final var recipientId = resolveRecipient(recipient);
return isContactBlocked(recipientId); return isContactBlocked(recipientId);
} }
@ -2607,8 +2620,8 @@ public class Manager implements Closeable {
return account.getContactStore().getContacts(); return account.getContactStore().getContacts();
} }
public String getContactOrProfileName(String number) throws InvalidNumberException { public String getContactOrProfileName(RecipientIdentifier.Single recipientIdentifier) {
final var recipientId = canonicalizeAndResolveRecipient(number); final var recipientId = resolveRecipient(recipientIdentifier);
final var recipient = account.getRecipientStore().getRecipient(recipientId); final var recipient = account.getRecipientStore().getRecipient(recipientId);
if (recipient == null) { if (recipient == null) {
return null; return null;
@ -2643,19 +2656,19 @@ public class Manager implements Closeable {
return account.getIdentityKeyStore().getIdentities(); return account.getIdentityKeyStore().getIdentities();
} }
public List<IdentityInfo> getIdentities(String number) throws InvalidNumberException { public List<IdentityInfo> getIdentities(RecipientIdentifier.Single recipient) {
final var identity = account.getIdentityKeyStore().getIdentity(canonicalizeAndResolveRecipient(number)); final var identity = account.getIdentityKeyStore().getIdentity(resolveRecipient(recipient));
return identity == null ? List.of() : List.of(identity); return identity == null ? List.of() : List.of(identity);
} }
/** /**
* Trust this the identity with this fingerprint * Trust this the identity with this fingerprint
* *
* @param name username of the identity * @param recipient username of the identity
* @param fingerprint Fingerprint * @param fingerprint Fingerprint
*/ */
public boolean trustIdentityVerified(String name, byte[] fingerprint) throws InvalidNumberException { public boolean trustIdentityVerified(RecipientIdentifier.Single recipient, byte[] fingerprint) {
var recipientId = canonicalizeAndResolveRecipient(name); var recipientId = resolveRecipient(recipient);
return trustIdentity(recipientId, return trustIdentity(recipientId,
identityKey -> Arrays.equals(identityKey.serialize(), fingerprint), identityKey -> Arrays.equals(identityKey.serialize(), fingerprint),
TrustLevel.TRUSTED_VERIFIED); TrustLevel.TRUSTED_VERIFIED);
@ -2664,11 +2677,11 @@ public class Manager implements Closeable {
/** /**
* Trust this the identity with this safety number * Trust this the identity with this safety number
* *
* @param name username of the identity * @param recipient username of the identity
* @param safetyNumber Safety number * @param safetyNumber Safety number
*/ */
public boolean trustIdentityVerifiedSafetyNumber(String name, String safetyNumber) throws InvalidNumberException { public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, String safetyNumber) {
var recipientId = canonicalizeAndResolveRecipient(name); var recipientId = resolveRecipient(recipient);
var address = account.getRecipientStore().resolveServiceAddress(recipientId); var address = account.getRecipientStore().resolveServiceAddress(recipientId);
return trustIdentity(recipientId, return trustIdentity(recipientId,
identityKey -> safetyNumber.equals(computeSafetyNumber(address, identityKey)), identityKey -> safetyNumber.equals(computeSafetyNumber(address, identityKey)),
@ -2678,11 +2691,11 @@ public class Manager implements Closeable {
/** /**
* Trust this the identity with this scannable safety number * Trust this the identity with this scannable safety number
* *
* @param name username of the identity * @param recipient username of the identity
* @param safetyNumber Scannable safety number * @param safetyNumber Scannable safety number
*/ */
public boolean trustIdentityVerifiedSafetyNumber(String name, byte[] safetyNumber) throws InvalidNumberException { public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, byte[] safetyNumber) {
var recipientId = canonicalizeAndResolveRecipient(name); var recipientId = resolveRecipient(recipient);
var address = account.getRecipientStore().resolveServiceAddress(recipientId); var address = account.getRecipientStore().resolveServiceAddress(recipientId);
return trustIdentity(recipientId, identityKey -> { return trustIdentity(recipientId, identityKey -> {
final var fingerprint = computeSafetyNumberFingerprint(address, identityKey); final var fingerprint = computeSafetyNumberFingerprint(address, identityKey);
@ -2697,10 +2710,10 @@ public class Manager implements Closeable {
/** /**
* Trust all keys of this identity without verification * Trust all keys of this identity without verification
* *
* @param name username of the identity * @param recipient username of the identity
*/ */
public boolean trustIdentityAllKeys(String name) throws InvalidNumberException { public boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) {
var recipientId = canonicalizeAndResolveRecipient(name); var recipientId = resolveRecipient(recipient);
return trustIdentity(recipientId, identityKey -> true, TrustLevel.TRUSTED_UNVERIFIED); return trustIdentity(recipientId, identityKey -> true, TrustLevel.TRUSTED_UNVERIFIED);
} }
@ -2782,12 +2795,6 @@ public class Manager implements Closeable {
return account.getRecipientStore().resolveServiceAddress(recipientId); return account.getRecipientStore().resolveServiceAddress(recipientId);
} }
private RecipientId canonicalizeAndResolveRecipient(String identifier) throws InvalidNumberException {
var canonicalizedNumber = UuidUtil.isUuid(identifier) ? identifier : canonicalizePhoneNumber(identifier);
return resolveRecipient(canonicalizedNumber);
}
private String canonicalizePhoneNumber(final String number) throws InvalidNumberException { private String canonicalizePhoneNumber(final String number) throws InvalidNumberException {
return PhoneNumberFormatter.formatNumber(number, account.getUsername()); return PhoneNumberFormatter.formatNumber(number, account.getUsername());
} }
@ -2798,6 +2805,17 @@ public class Manager implements Closeable {
return resolveRecipient(address); return resolveRecipient(address);
} }
private RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) {
final SignalServiceAddress address;
if (recipient instanceof RecipientIdentifier.Uuid) {
address = new SignalServiceAddress(((RecipientIdentifier.Uuid) recipient).uuid, null);
} else {
address = new SignalServiceAddress(null, ((RecipientIdentifier.Number) recipient).number);
}
return resolveRecipient(address);
}
public RecipientId resolveRecipient(SignalServiceAddress address) { public RecipientId resolveRecipient(SignalServiceAddress address) {
return account.getRecipientStore().resolveRecipient(address); return account.getRecipientStore().resolveRecipient(address);
} }

View file

@ -0,0 +1,112 @@
package org.asamk.signal.manager.api;
import org.asamk.signal.manager.groups.GroupId;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
import org.whispersystems.signalservice.api.util.UuidUtil;
import java.util.UUID;
public abstract class RecipientIdentifier {
public static class NoteToSelf extends RecipientIdentifier {
@Override
public boolean equals(final Object obj) {
return obj instanceof NoteToSelf;
}
@Override
public int hashCode() {
return 5;
}
}
public abstract static class Single extends RecipientIdentifier {
public static Single fromString(String identifier, String localNumber) throws InvalidNumberException {
return UuidUtil.isUuid(identifier)
? new Uuid(UUID.fromString(identifier))
: new Number(PhoneNumberFormatter.formatNumber(identifier, localNumber));
}
public static Single fromAddress(SignalServiceAddress address) {
return address.getUuid().isPresent()
? new Uuid(address.getUuid().get())
: new Number(address.getNumber().get());
}
}
public static class Uuid extends Single {
public final UUID uuid;
public Uuid(final UUID uuid) {
this.uuid = uuid;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Uuid uuid1 = (Uuid) o;
return uuid.equals(uuid1.uuid);
}
@Override
public int hashCode() {
return uuid.hashCode();
}
}
public static class Number extends Single {
public final String number;
public Number(final String number) {
this.number = number;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Number number1 = (Number) o;
return number.equals(number1.number);
}
@Override
public int hashCode() {
return number.hashCode();
}
}
public static class Group extends RecipientIdentifier {
public final GroupId groupId;
public Group(final GroupId groupId) {
this.groupId = groupId;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Group group = (Group) o;
return groupId.equals(group.groupId);
}
@Override
public int hashCode() {
return groupId.hashCode();
}
}
}

View file

@ -0,0 +1,26 @@
package org.asamk.signal.manager.api;
import org.whispersystems.signalservice.api.messages.SendMessageResult;
import java.util.List;
public class SendGroupMessageResults {
private final long timestamp;
private final List<SendMessageResult> results;
public SendGroupMessageResults(
final long timestamp, final List<SendMessageResult> results
) {
this.timestamp = timestamp;
this.results = results;
}
public long getTimestamp() {
return timestamp;
}
public List<SendMessageResult> getResults() {
return results;
}
}

View file

@ -0,0 +1,27 @@
package org.asamk.signal.manager.api;
import org.whispersystems.signalservice.api.messages.SendMessageResult;
import java.util.List;
import java.util.Map;
public class SendMessageResults {
private final long timestamp;
private final Map<RecipientIdentifier, List<SendMessageResult>> results;
public SendMessageResults(
final long timestamp, final Map<RecipientIdentifier, List<SendMessageResult>> results
) {
this.timestamp = timestamp;
this.results = results;
}
public long getTimestamp() {
return timestamp;
}
public Map<RecipientIdentifier, List<SendMessageResult>> getResults() {
return results;
}
}

View file

@ -11,7 +11,6 @@ import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.storage.recipients.RecipientResolver; import org.asamk.signal.manager.storage.recipients.RecipientResolver;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.util.Pair;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.crypto.ContentHint; import org.whispersystems.signalservice.api.crypto.ContentHint;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException; import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
@ -67,36 +66,34 @@ public class SendHelper {
* Send a single message to one or multiple recipients. * Send a single message to one or multiple recipients.
* The message is extended with the current expiration timer for each recipient. * The message is extended with the current expiration timer for each recipient.
*/ */
public Pair<Long, List<SendMessageResult>> sendMessage( public SendMessageResult sendMessage(
SignalServiceDataMessage.Builder messageBuilder, Set<RecipientId> recipientIds final SignalServiceDataMessage.Builder messageBuilder, final RecipientId recipientId
) throws IOException { ) throws IOException {
// Send to all individually, so sync messages are sent correctly
messageBuilder.withProfileKey(account.getProfileKey().serialize());
var results = new ArrayList<SendMessageResult>(recipientIds.size());
long timestamp = 0;
for (var recipientId : recipientIds) {
final var contact = account.getContactStore().getContact(recipientId); final var contact = account.getContactStore().getContact(recipientId);
final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0; final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0;
messageBuilder.withExpiration(expirationTime); messageBuilder.withExpiration(expirationTime);
messageBuilder.withProfileKey(account.getProfileKey().serialize());
final var singleMessage = messageBuilder.build(); final var message = messageBuilder.build();
timestamp = singleMessage.getTimestamp(); final var result = sendMessage(message, recipientId);
final var result = sendMessage(singleMessage, recipientId);
handlePossibleIdentityFailure(result); handlePossibleIdentityFailure(result);
return result;
results.add(result);
}
return new Pair<>(timestamp, results);
} }
/** /**
* Send a group message to the given group * Send a group message to the given group
* The message is extended with the current expiration timer for the group and the group context. * The message is extended with the current expiration timer for the group and the group context.
*/ */
public Pair<Long, List<SendMessageResult>> sendAsGroupMessage( public List<SendMessageResult> sendAsGroupMessage(
SignalServiceDataMessage.Builder messageBuilder, GroupId groupId SignalServiceDataMessage.Builder messageBuilder, GroupId groupId
) throws IOException, GroupNotFoundException, NotAGroupMemberException { ) throws IOException, GroupNotFoundException, NotAGroupMemberException {
final var g = getGroupForSending(groupId); final var g = getGroupForSending(groupId);
return sendAsGroupMessage(messageBuilder, g);
}
private List<SendMessageResult> sendAsGroupMessage(
final SignalServiceDataMessage.Builder messageBuilder, final GroupInfo g
) throws IOException {
GroupUtils.setGroupContext(messageBuilder, g); GroupUtils.setGroupContext(messageBuilder, g);
messageBuilder.withExpiration(g.getMessageExpirationTime()); messageBuilder.withExpiration(g.getMessageExpirationTime());
@ -108,7 +105,7 @@ public class SendHelper {
* Send a complete group message to the given recipients (should be current/old/new members) * Send a complete group message to the given recipients (should be current/old/new members)
* This method should only be used for create/update/quit group messages. * This method should only be used for create/update/quit group messages.
*/ */
public Pair<Long, List<SendMessageResult>> sendGroupMessage( public List<SendMessageResult> sendGroupMessage(
final SignalServiceDataMessage message, final Set<RecipientId> recipientIds final SignalServiceDataMessage message, final Set<RecipientId> recipientIds
) throws IOException { ) throws IOException {
List<SendMessageResult> result = sendGroupMessageInternal(message, recipientIds); List<SendMessageResult> result = sendGroupMessageInternal(message, recipientIds);
@ -117,7 +114,7 @@ public class SendHelper {
handlePossibleIdentityFailure(r); handlePossibleIdentityFailure(r);
} }
return new Pair<>(message.getTimestamp(), result); return result;
} }
public void sendReceiptMessage( public void sendReceiptMessage(
@ -146,7 +143,7 @@ public class SendHelper {
} }
} }
public Pair<Long, SendMessageResult> sendSelfMessage( public SendMessageResult sendSelfMessage(
SignalServiceDataMessage.Builder messageBuilder SignalServiceDataMessage.Builder messageBuilder
) throws IOException { ) throws IOException {
final var recipientId = account.getSelfRecipientId(); final var recipientId = account.getSelfRecipientId();
@ -155,8 +152,7 @@ public class SendHelper {
messageBuilder.withExpiration(expirationTime); messageBuilder.withExpiration(expirationTime);
var message = messageBuilder.build(); var message = messageBuilder.build();
final var result = sendSelfMessage(message); return sendSelfMessage(message);
return new Pair<>(message.getTimestamp(), result);
} }
public SendMessageResult sendSyncMessage(SignalServiceSyncMessage message) throws IOException { public SendMessageResult sendSyncMessage(SignalServiceSyncMessage message) throws IOException {
@ -170,10 +166,9 @@ public class SendHelper {
} }
public void sendTypingMessage( public void sendTypingMessage(
SignalServiceTypingMessage message, Set<RecipientId> recipientIds SignalServiceTypingMessage message, RecipientId recipientId
) throws IOException, UntrustedIdentityException { ) throws IOException, UntrustedIdentityException {
var messageSender = dependencies.getMessageSender(); var messageSender = dependencies.getMessageSender();
for (var recipientId : recipientIds) {
final var address = addressResolver.resolveSignalServiceAddress(recipientId); final var address = addressResolver.resolveSignalServiceAddress(recipientId);
try { try {
messageSender.sendTyping(address, unidentifiedAccessHelper.getAccessFor(recipientId), message); messageSender.sendTyping(address, unidentifiedAccessHelper.getAccessFor(recipientId), message);
@ -183,7 +178,6 @@ public class SendHelper {
messageSender.sendTyping(newAddress, unidentifiedAccessHelper.getAccessFor(newRecipientId), message); messageSender.sendTyping(newAddress, unidentifiedAccessHelper.getAccessFor(newRecipientId), message);
} }
} }
}
public void sendGroupTypingMessage( public void sendGroupTypingMessage(
SignalServiceTypingMessage message, GroupId groupId SignalServiceTypingMessage message, GroupId groupId

View file

@ -31,7 +31,7 @@ public interface Signal extends DBusInterface {
long sendGroupRemoteDeleteMessage( long sendGroupRemoteDeleteMessage(
long targetSentTimestamp, byte[] groupId long targetSentTimestamp, byte[] groupId
) throws Error.Failure, Error.GroupNotFound; ) throws Error.Failure, Error.GroupNotFound, Error.InvalidGroupId;
long sendMessageReaction( long sendMessageReaction(
String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, String recipient String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, String recipient
@ -49,11 +49,11 @@ public interface Signal extends DBusInterface {
long sendGroupMessage( long sendGroupMessage(
String message, List<String> attachments, byte[] groupId String message, List<String> attachments, byte[] groupId
) throws Error.GroupNotFound, Error.Failure, Error.AttachmentInvalid; ) throws Error.GroupNotFound, Error.Failure, Error.AttachmentInvalid, Error.InvalidGroupId;
long sendGroupMessageReaction( long sendGroupMessageReaction(
String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, byte[] groupId String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, byte[] groupId
) throws Error.GroupNotFound, Error.Failure, Error.InvalidNumber; ) throws Error.GroupNotFound, Error.Failure, Error.InvalidNumber, Error.InvalidGroupId;
String getContactName(String number) throws Error.InvalidNumber; String getContactName(String number) throws Error.InvalidNumber;
@ -61,17 +61,17 @@ public interface Signal extends DBusInterface {
void setContactBlocked(String number, boolean blocked) throws Error.InvalidNumber; void setContactBlocked(String number, boolean blocked) throws Error.InvalidNumber;
void setGroupBlocked(byte[] groupId, boolean blocked) throws Error.GroupNotFound; void setGroupBlocked(byte[] groupId, boolean blocked) throws Error.GroupNotFound, Error.InvalidGroupId;
List<byte[]> getGroupIds(); List<byte[]> getGroupIds();
String getGroupName(byte[] groupId); String getGroupName(byte[] groupId) throws Error.InvalidGroupId;
List<String> getGroupMembers(byte[] groupId); List<String> getGroupMembers(byte[] groupId) throws Error.InvalidGroupId;
byte[] updateGroup( byte[] updateGroup(
byte[] groupId, String name, List<String> members, String avatar byte[] groupId, String name, List<String> members, String avatar
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound; ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound, Error.InvalidGroupId;
boolean isRegistered(); boolean isRegistered();
@ -85,15 +85,15 @@ public interface Signal extends DBusInterface {
List<String> getContactNumber(final String name) throws Error.Failure; List<String> getContactNumber(final String name) throws Error.Failure;
void quitGroup(final byte[] groupId) throws Error.GroupNotFound, Error.Failure; void quitGroup(final byte[] groupId) throws Error.GroupNotFound, Error.Failure, Error.InvalidGroupId;
boolean isContactBlocked(final String number) throws Error.InvalidNumber; boolean isContactBlocked(final String number) throws Error.InvalidNumber;
boolean isGroupBlocked(final byte[] groupId); boolean isGroupBlocked(final byte[] groupId) throws Error.InvalidGroupId;
boolean isMember(final byte[] groupId); boolean isMember(final byte[] groupId) throws Error.InvalidGroupId;
void joinGroup(final String groupLink) throws Error.Failure; byte[] joinGroup(final String groupLink) throws Error.Failure;
class MessageReceived extends DBusSignal { class MessageReceived extends DBusSignal {
@ -235,6 +235,13 @@ public interface Signal extends DBusInterface {
} }
} }
class InvalidGroupId extends DBusExecutionException {
public InvalidGroupId(final String message) {
super(message);
}
}
class InvalidNumber extends DBusExecutionException { class InvalidNumber extends DBusExecutionException {
public InvalidNumber(final String message) { public InvalidNumber(final String message) {

View file

@ -1,6 +1,7 @@
package org.asamk.signal; package org.asamk.signal;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupUtils; import org.asamk.signal.manager.groups.GroupUtils;
import org.asamk.signal.util.DateUtils; import org.asamk.signal.util.DateUtils;
@ -18,7 +19,6 @@ import org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMess
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage; import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.messages.shared.SharedContact; import org.whispersystems.signalservice.api.messages.shared.SharedContact;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
@ -450,7 +450,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
final PlainTextWriter writer, final SignalServiceDataMessage.Reaction reaction final PlainTextWriter writer, final SignalServiceDataMessage.Reaction reaction
) { ) {
writer.println("Emoji: {}", reaction.getEmoji()); writer.println("Emoji: {}", reaction.getEmoji());
writer.println("Target author: {}", formatContact(m.resolveSignalServiceAddress(reaction.getTargetAuthor()))); writer.println("Target author: {}", formatContact(reaction.getTargetAuthor()));
writer.println("Target timestamp: {}", DateUtils.formatTimestamp(reaction.getTargetSentTimestamp())); writer.println("Target timestamp: {}", DateUtils.formatTimestamp(reaction.getTargetSentTimestamp()));
writer.println("Is remove: {}", reaction.isRemove()); writer.println("Is remove: {}", reaction.isRemove());
} }
@ -459,7 +459,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
final PlainTextWriter writer, final SignalServiceDataMessage.Quote quote final PlainTextWriter writer, final SignalServiceDataMessage.Quote quote
) { ) {
writer.println("Id: {}", quote.getId()); writer.println("Id: {}", quote.getId());
writer.println("Author: {}", getLegacyIdentifier(m.resolveSignalServiceAddress(quote.getAuthor()))); writer.println("Author: {}", formatContact(quote.getAuthor()));
writer.println("Text: {}", quote.getText()); writer.println("Text: {}", quote.getText());
if (quote.getMentions() != null && quote.getMentions().size() > 0) { if (quote.getMentions() != null && quote.getMentions().size() > 0) {
writer.println("Mentions:"); writer.println("Mentions:");
@ -678,12 +678,9 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
} }
private String formatContact(SignalServiceAddress address) { private String formatContact(SignalServiceAddress address) {
address = m.resolveSignalServiceAddress(address);
final var number = getLegacyIdentifier(address); final var number = getLegacyIdentifier(address);
String name = null; final var name = m.getContactOrProfileName(RecipientIdentifier.Single.fromAddress(address));
try {
name = m.getContactOrProfileName(number);
} catch (InvalidNumberException ignored) {
}
if (name == null || name.isEmpty()) { if (name == null || name.isEmpty()) {
return number; return number;
} else { } else {

View file

@ -8,12 +8,10 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException; import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.CommandUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
public class BlockCommand implements JsonRpcLocalCommand { public class BlockCommand implements JsonRpcLocalCommand {
@ -35,23 +33,22 @@ public class BlockCommand implements JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
for (var contactNumber : ns.<String>getList("contact")) { final var contacts = ns.<String>getList("contact");
for (var contact : CommandUtil.getSingleRecipientIdentifiers(contacts, m.getUsername())) {
try { try {
m.setContactBlocked(contactNumber, true); m.setContactBlocked(contact, true);
} catch (InvalidNumberException e) {
logger.warn("Invalid number {}: {}", contactNumber, e.getMessage());
} catch (NotMasterDeviceException e) { } catch (NotMasterDeviceException e) {
throw new UserErrorException("This command doesn't work on linked devices."); throw new UserErrorException("This command doesn't work on linked devices.");
} }
} }
if (ns.<String>getList("group-id") != null) { final var groupIdStrings = ns.<String>getList("group-id");
for (var groupIdString : ns.<String>getList("group-id")) { if (groupIdStrings != null) {
for (var groupId : CommandUtil.getGroupIds(groupIdStrings)) {
try { try {
var groupId = Util.decodeGroupId(groupIdString);
m.setGroupBlocked(groupId, true); m.setGroupBlocked(groupId, true);
} catch (GroupIdFormatException | GroupNotFoundException e) { } catch (GroupNotFoundException e) {
logger.warn("Invalid group id {}: {}", groupIdString, e.getMessage()); logger.warn("Group not found {}: {}", groupId.toBase64(), e.getMessage());
} }
} }
} }

View file

@ -70,7 +70,7 @@ public class JoinGroupCommand implements JsonRpcLocalCommand {
writer.println("Joined group \"{}\"", newGroupId.toBase64()); writer.println("Joined group \"{}\"", newGroupId.toBase64());
} }
} }
handleSendMessageResults(results.second()); handleSendMessageResults(results.second().getResults());
} catch (GroupPatchNotAcceptedException e) { } catch (GroupPatchNotAcceptedException e) {
throw new UserErrorException("Failed to join group, maybe already a member"); throw new UserErrorException("Failed to join group, maybe already a member");
} catch (IOException e) { } catch (IOException e) {

View file

@ -7,15 +7,14 @@ import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.storage.identities.IdentityInfo; import org.asamk.signal.manager.storage.identities.IdentityInfo;
import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.Hex; import org.asamk.signal.util.Hex;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
@ -58,11 +57,7 @@ public class ListIdentitiesCommand implements JsonRpcLocalCommand {
if (number == null) { if (number == null) {
identities = m.getIdentities(); identities = m.getIdentities();
} else { } else {
try { identities = m.getIdentities(CommandUtil.getSingleRecipientIdentifier(number, m.getUsername()));
identities = m.getIdentities(number);
} catch (InvalidNumberException e) {
throw new UserErrorException("Invalid number: " + e.getMessage());
}
} }
if (outputWriter instanceof PlainTextWriter) { if (outputWriter instanceof PlainTextWriter) {

View file

@ -11,20 +11,15 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.LastGroupAdminException; import org.asamk.signal.manager.groups.LastGroupAdminException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.CommandUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import static org.asamk.signal.util.ErrorUtils.handleSendMessageResults; import static org.asamk.signal.util.ErrorUtils.handleSendMessageResults;
@ -53,22 +48,16 @@ public class QuitGroupCommand implements JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
final GroupId groupId; final var groupId = CommandUtil.getGroupId(ns.getString("group-id"));
try {
groupId = Util.decodeGroupId(ns.getString("group-id"));
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id: " + e.getMessage());
}
var groupAdmins = ns.<String>getList("admin"); var groupAdmins = CommandUtil.getSingleRecipientIdentifiers(ns.getList("admin"), m.getUsername());
try { try {
try { try {
final var results = m.sendQuitGroupMessage(groupId, final var results = m.sendQuitGroupMessage(groupId, groupAdmins);
groupAdmins == null ? Set.of() : new HashSet<>(groupAdmins)); final var timestamp = results.getTimestamp();
final var timestamp = results.first();
outputResult(outputWriter, timestamp); outputResult(outputWriter, timestamp);
handleSendMessageResults(results.second()); handleSendMessageResults(results.getResults());
} catch (NotAGroupMemberException e) { } catch (NotAGroupMemberException e) {
logger.info("User is not a group member"); logger.info("User is not a group member");
} }
@ -80,8 +69,6 @@ public class QuitGroupCommand implements JsonRpcLocalCommand {
throw new IOErrorException("Failed to send message: " + e.getMessage()); throw new IOErrorException("Failed to send message: " + e.getMessage());
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
throw new UserErrorException("Failed to send to group: " + e.getMessage()); throw new UserErrorException("Failed to send to group: " + e.getMessage());
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse admin number: " + e.getMessage());
} catch (LastGroupAdminException e) { } catch (LastGroupAdminException e) {
throw new UserErrorException("You need to specify a new admin with --admin: " + e.getMessage()); throw new UserErrorException("You need to specify a new admin with --admin: " + e.getMessage());
} }

View file

@ -1,5 +1,6 @@
package org.asamk.signal.commands; package org.asamk.signal.commands;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
@ -10,14 +11,15 @@ import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.util.Util; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils;
import org.freedesktop.dbus.errors.UnknownObject; import org.freedesktop.dbus.errors.UnknownObject;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import java.util.List; import java.io.IOException;
import java.util.Map; import java.util.Map;
public class RemoteDeleteCommand implements DbusCommand, JsonRpcLocalCommand { public class RemoteDeleteCommand implements DbusCommand, JsonRpcLocalCommand {
@ -34,40 +36,62 @@ public class RemoteDeleteCommand implements DbusCommand, JsonRpcLocalCommand {
.required(true) .required(true)
.type(long.class) .type(long.class)
.help("Specify the timestamp of the message to delete."); .help("Specify the timestamp of the message to delete.");
subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID."); subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*"); subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
subparser.addArgument("--note-to-self").action(Arguments.storeTrue());
}
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var isNoteToSelf = ns.getBoolean("note-to-self");
final var recipientStrings = ns.<String>getList("recipient");
final var groupIdStrings = ns.<String>getList("group-id");
final var recipientIdentifiers = CommandUtil.getRecipientIdentifiers(m,
isNoteToSelf,
recipientStrings,
groupIdStrings);
final long targetTimestamp = ns.getLong("target-timestamp");
try {
final var results = m.sendRemoteDeleteMessage(targetTimestamp, recipientIdentifiers);
outputResult(outputWriter, results.getTimestamp());
ErrorUtils.handleSendMessageResults(results.getResults());
} catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new UserErrorException(e.getMessage());
} catch (IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
}
} }
@Override @Override
public void handleCommand( public void handleCommand(
final Namespace ns, final Signal signal, final OutputWriter outputWriter final Namespace ns, final Signal signal, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
final List<String> recipients = ns.getList("recipient"); final var recipients = ns.<String>getList("recipient");
final var groupIdString = ns.getString("group-id"); final var groupIdStrings = ns.<String>getList("group-id");
final var noRecipients = recipients == null || recipients.isEmpty(); final var noRecipients = recipients == null || recipients.isEmpty();
if (noRecipients && groupIdString == null) { final var noGroups = groupIdStrings == null || groupIdStrings.isEmpty();
if (noRecipients && noGroups) {
throw new UserErrorException("No recipients given"); throw new UserErrorException("No recipients given");
} }
if (!noRecipients && groupIdString != null) { if (!noRecipients && !noGroups) {
throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time"); throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
} }
final long targetTimestamp = ns.getLong("target-timestamp"); final long targetTimestamp = ns.getLong("target-timestamp");
byte[] groupId = null;
if (groupIdString != null) {
try { try {
groupId = Util.decodeGroupId(groupIdString).serialize(); long timestamp = 0;
} catch (GroupIdFormatException e) { if (!noGroups) {
throw new UserErrorException("Invalid group id: " + e.getMessage()); final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
for (final var groupId : groupIds) {
timestamp = signal.sendGroupRemoteDeleteMessage(targetTimestamp, groupId.serialize());
} }
}
try {
long timestamp;
if (groupId != null) {
timestamp = signal.sendGroupRemoteDeleteMessage(targetTimestamp, groupId);
} else { } else {
timestamp = signal.sendRemoteDeleteMessage(targetTimestamp, recipients); timestamp = signal.sendRemoteDeleteMessage(targetTimestamp, recipients);
} }
@ -83,13 +107,6 @@ public class RemoteDeleteCommand implements DbusCommand, JsonRpcLocalCommand {
} }
} }
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
handleCommand(ns, new DbusSignalImpl(m, null), outputWriter);
}
private void outputResult(final OutputWriter outputWriter, final long timestamp) { private void outputResult(final OutputWriter outputWriter, final long timestamp) {
if (outputWriter instanceof PlainTextWriter) { if (outputWriter instanceof PlainTextWriter) {
final var writer = (PlainTextWriter) outputWriter; final var writer = (PlainTextWriter) outputWriter;

View file

@ -12,11 +12,15 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException; import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.dbus.DbusSignalImpl; import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils;
import org.asamk.signal.util.IOUtils; import org.asamk.signal.util.IOUtils;
import org.asamk.signal.util.Util;
import org.freedesktop.dbus.errors.UnknownObject; import org.freedesktop.dbus.errors.UnknownObject;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -26,6 +30,7 @@ import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
public class SendCommand implements DbusCommand, JsonRpcLocalCommand { public class SendCommand implements DbusCommand, JsonRpcLocalCommand {
@ -40,9 +45,8 @@ public class SendCommand implements DbusCommand, JsonRpcLocalCommand {
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.help("Send a message to another user or group."); subparser.help("Send a message to another user or group.");
subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*"); subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
final var mutuallyExclusiveGroup = subparser.addMutuallyExclusiveGroup(); subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
mutuallyExclusiveGroup.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID."); subparser.addArgument("--note-to-self")
mutuallyExclusiveGroup.addArgument("--note-to-self")
.help("Send the message to self without notification.") .help("Send the message to self without notification.")
.action(Arguments.storeTrue()); .action(Arguments.storeTrue());
@ -53,20 +57,77 @@ public class SendCommand implements DbusCommand, JsonRpcLocalCommand {
.action(Arguments.storeTrue()); .action(Arguments.storeTrue());
} }
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var isNoteToSelf = ns.getBoolean("note-to-self");
final var recipientStrings = ns.<String>getList("recipient");
final var groupIdStrings = ns.<String>getList("group-id");
final var recipientIdentifiers = CommandUtil.getRecipientIdentifiers(m,
isNoteToSelf,
recipientStrings,
groupIdStrings);
final var isEndSession = ns.getBoolean("end-session");
if (isEndSession) {
final var singleRecipients = recipientIdentifiers.stream()
.filter(r -> r instanceof RecipientIdentifier.Single)
.map(RecipientIdentifier.Single.class::cast)
.collect(Collectors.toSet());
if (singleRecipients.isEmpty()) {
throw new UserErrorException("No recipients given");
}
try {
m.sendEndSessionMessage(singleRecipients);
return;
} catch (IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
}
}
var messageText = ns.getString("message");
if (messageText == null) {
try {
messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
} catch (IOException e) {
throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
}
}
List<String> attachments = ns.getList("attachment");
if (attachments == null) {
attachments = List.of();
}
try {
var results = m.sendMessage(new Message(messageText, attachments), recipientIdentifiers);
outputResult(outputWriter, results.getTimestamp());
ErrorUtils.handleSendMessageResults(results.getResults());
} catch (AttachmentInvalidException | IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new UserErrorException(e.getMessage());
}
}
@Override @Override
public void handleCommand( public void handleCommand(
final Namespace ns, final Signal signal, final OutputWriter outputWriter final Namespace ns, final Signal signal, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
final List<String> recipients = ns.getList("recipient"); final var recipients = ns.<String>getList("recipient");
final var isEndSession = ns.getBoolean("end-session"); final var isEndSession = ns.getBoolean("end-session");
final var groupIdString = ns.getString("group-id"); final var groupIdStrings = ns.<String>getList("group-id");
final var isNoteToSelf = ns.getBoolean("note-to-self"); final var isNoteToSelf = ns.getBoolean("note-to-self");
final var noRecipients = recipients == null || recipients.isEmpty(); final var noRecipients = recipients == null || recipients.isEmpty();
if ((noRecipients && isEndSession) || (noRecipients && groupIdString == null && !isNoteToSelf)) { final var noGroups = groupIdStrings == null || groupIdStrings.isEmpty();
if ((noRecipients && isEndSession) || (noRecipients && noGroups && !isNoteToSelf)) {
throw new UserErrorException("No recipients given"); throw new UserErrorException("No recipients given");
} }
if (!noRecipients && groupIdString != null) { if (!noRecipients && !noGroups) {
throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time"); throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
} }
if (!noRecipients && isNoteToSelf) { if (!noRecipients && isNoteToSelf) {
@ -99,16 +160,14 @@ public class SendCommand implements DbusCommand, JsonRpcLocalCommand {
attachments = List.of(); attachments = List.of();
} }
if (groupIdString != null) { if (!noGroups) {
byte[] groupId; final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
try {
groupId = Util.decodeGroupId(groupIdString).serialize();
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id: " + e.getMessage());
}
try { try {
var timestamp = signal.sendGroupMessage(messageText, attachments, groupId); long timestamp = 0;
for (final var groupId : groupIds) {
timestamp = signal.sendGroupMessage(messageText, attachments, groupId.serialize());
}
outputResult(outputWriter, timestamp); outputResult(outputWriter, timestamp);
return; return;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
@ -149,11 +208,4 @@ public class SendCommand implements DbusCommand, JsonRpcLocalCommand {
writer.write(Map.of("timestamp", timestamp)); writer.write(Map.of("timestamp", timestamp));
} }
} }
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
handleCommand(ns, new DbusSignalImpl(m, null), outputWriter);
}
} }

View file

@ -11,14 +11,15 @@ import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.util.Util; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils;
import org.freedesktop.dbus.errors.UnknownObject; import org.freedesktop.dbus.errors.UnknownObject;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import java.util.List; import java.io.IOException;
import java.util.Map; import java.util.Map;
public class SendReactionCommand implements DbusCommand, JsonRpcLocalCommand { public class SendReactionCommand implements DbusCommand, JsonRpcLocalCommand {
@ -31,8 +32,11 @@ public class SendReactionCommand implements DbusCommand, JsonRpcLocalCommand {
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.help("Send reaction to a previously received or sent message."); subparser.help("Send reaction to a previously received or sent message.");
subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID."); subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*"); subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
subparser.addArgument("--note-to-self")
.help("Send the reaction to self without notification.")
.action(Arguments.storeTrue());
subparser.addArgument("-e", "--emoji") subparser.addArgument("-e", "--emoji")
.required(true) .required(true)
.help("Specify the emoji, should be a single unicode grapheme cluster."); .help("Specify the emoji, should be a single unicode grapheme cluster.");
@ -46,39 +50,71 @@ public class SendReactionCommand implements DbusCommand, JsonRpcLocalCommand {
subparser.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments.storeTrue()); subparser.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments.storeTrue());
} }
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var isNoteToSelf = ns.getBoolean("note-to-self");
final var recipientStrings = ns.<String>getList("recipient");
final var groupIdStrings = ns.<String>getList("group-id");
final var recipientIdentifiers = CommandUtil.getRecipientIdentifiers(m,
isNoteToSelf,
recipientStrings,
groupIdStrings);
final var emoji = ns.getString("emoji");
final var isRemove = ns.getBoolean("remove");
final var targetAuthor = ns.getString("target-author");
final var targetTimestamp = ns.getLong("target-timestamp");
try {
final var results = m.sendMessageReaction(emoji,
isRemove,
CommandUtil.getSingleRecipientIdentifier(targetAuthor, m.getUsername()),
targetTimestamp,
recipientIdentifiers);
outputResult(outputWriter, results.getTimestamp());
ErrorUtils.handleSendMessageResults(results.getResults());
} catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new UserErrorException(e.getMessage());
} catch (IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
}
}
@Override @Override
public void handleCommand( public void handleCommand(
final Namespace ns, final Signal signal, final OutputWriter outputWriter final Namespace ns, final Signal signal, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
final List<String> recipients = ns.getList("recipient"); final var recipients = ns.<String>getList("recipient");
final var groupIdString = ns.getString("group-id"); final var groupIdStrings = ns.<String>getList("group-id");
final var noRecipients = recipients == null || recipients.isEmpty(); final var noRecipients = recipients == null || recipients.isEmpty();
if (noRecipients && groupIdString == null) { final var noGroups = groupIdStrings == null || groupIdStrings.isEmpty();
if (noRecipients && noGroups) {
throw new UserErrorException("No recipients given"); throw new UserErrorException("No recipients given");
} }
if (!noRecipients && groupIdString != null) { if (!noRecipients && !noGroups) {
throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time"); throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
} }
final var emoji = ns.getString("emoji"); final var emoji = ns.getString("emoji");
final boolean isRemove = ns.getBoolean("remove"); final var isRemove = ns.getBoolean("remove");
final var targetAuthor = ns.getString("target-author"); final var targetAuthor = ns.getString("target-author");
final long targetTimestamp = ns.getLong("target-timestamp"); final var targetTimestamp = ns.getLong("target-timestamp");
byte[] groupId = null;
if (groupIdString != null) {
try {
groupId = Util.decodeGroupId(groupIdString).serialize();
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id: " + e.getMessage());
}
}
try { try {
long timestamp; long timestamp = 0;
if (groupId != null) { if (!noGroups) {
timestamp = signal.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId); final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
for (final var groupId : groupIds) {
timestamp = signal.sendGroupMessageReaction(emoji,
isRemove,
targetAuthor,
targetTimestamp,
groupId.serialize());
}
} else { } else {
timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients); timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
} }
@ -94,13 +130,6 @@ public class SendReactionCommand implements DbusCommand, JsonRpcLocalCommand {
} }
} }
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
handleCommand(ns, new DbusSignalImpl(m, null), outputWriter);
}
private void outputResult(final OutputWriter outputWriter, final long timestamp) { private void outputResult(final OutputWriter outputWriter, final long timestamp) {
if (outputWriter instanceof PlainTextWriter) { if (outputWriter instanceof PlainTextWriter) {
final var writer = (PlainTextWriter) outputWriter; final var writer = (PlainTextWriter) outputWriter;

View file

@ -7,8 +7,8 @@ import org.asamk.signal.OutputWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.CommandUtil;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException; import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException; import java.io.IOException;
@ -34,7 +34,8 @@ public class SendReceiptCommand implements JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
final var recipient = ns.getString("recipient"); final var recipientString = ns.getString("recipient");
final var recipient = CommandUtil.getSingleRecipientIdentifier(recipientString, m.getUsername());
final var targetTimestamps = ns.<Long>getList("target-timestamp"); final var targetTimestamps = ns.<Long>getList("target-timestamp");
final var type = ns.getString("type"); final var type = ns.getString("type");
@ -49,8 +50,6 @@ public class SendReceiptCommand implements JsonRpcLocalCommand {
} }
} catch (IOException | UntrustedIdentityException e) { } catch (IOException | UntrustedIdentityException e) {
throw new UserErrorException("Failed to send message: " + e.getMessage()); throw new UserErrorException("Failed to send message: " + e.getMessage());
} catch (InvalidNumberException e) {
throw new UserErrorException("Invalid number: " + e.getMessage());
} }
} }
} }

View file

@ -8,14 +8,12 @@ import org.asamk.signal.OutputWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.TypingAction; import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.CommandUtil;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException; import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
@ -31,7 +29,7 @@ public class SendTypingCommand implements JsonRpcLocalCommand {
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.help( subparser.help(
"Send typing message to trigger a typing indicator for the recipient. Indicator will be shown for 15seconds unless a typing STOP message is sent first."); "Send typing message to trigger a typing indicator for the recipient. Indicator will be shown for 15seconds unless a typing STOP message is sent first.");
subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID."); subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*"); subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
subparser.addArgument("-s", "--stop").help("Send a typing STOP message.").action(Arguments.storeTrue()); subparser.addArgument("-s", "--stop").help("Send a typing STOP message.").action(Arguments.storeTrue());
} }
@ -40,40 +38,29 @@ public class SendTypingCommand implements JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
final var recipients = ns.<String>getList("recipient"); final var recipientStrings = ns.<String>getList("recipient");
final var groupIdString = ns.getString("group-id"); final var groupIdStrings = ns.<String>getList("group-id");
final var noRecipients = recipients == null || recipients.isEmpty();
if (noRecipients && groupIdString == null) {
throw new UserErrorException("No recipients given");
}
if (!noRecipients && groupIdString != null) {
throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
}
final var action = ns.getBoolean("stop") ? TypingAction.STOP : TypingAction.START; final var action = ns.getBoolean("stop") ? TypingAction.STOP : TypingAction.START;
GroupId groupId = null; final var recipientIdentifiers = new HashSet<RecipientIdentifier>();
if (groupIdString != null) { if (recipientStrings != null) {
try { final var localNumber = m.getUsername();
groupId = Util.decodeGroupId(groupIdString); recipientIdentifiers.addAll(CommandUtil.getSingleRecipientIdentifiers(recipientStrings, localNumber));
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id: " + e.getMessage());
} }
if (groupIdStrings != null) {
recipientIdentifiers.addAll(CommandUtil.getGroupIdentifiers(groupIdStrings));
}
if (recipientIdentifiers.isEmpty()) {
throw new UserErrorException("No recipients given");
} }
try { try {
if (groupId != null) { m.sendTypingMessage(action, recipientIdentifiers);
m.sendGroupTypingMessage(action, groupId);
} else {
m.sendTypingMessage(action, new HashSet<>(recipients));
}
} catch (IOException | UntrustedIdentityException e) { } catch (IOException | UntrustedIdentityException e) {
throw new UserErrorException("Failed to send message: " + e.getMessage()); throw new UserErrorException("Failed to send message: " + e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) { } catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new UserErrorException("Failed to send to group: " + e.getMessage()); throw new UserErrorException("Failed to send to group: " + e.getMessage());
} catch (InvalidNumberException e) {
throw new UserErrorException("Invalid number: " + e.getMessage());
} }
} }
} }

View file

@ -8,8 +8,8 @@ import org.asamk.signal.OutputWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.Hex; import org.asamk.signal.util.Hex;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.util.Base64; import java.util.Base64;
import java.util.Locale; import java.util.Locale;
@ -37,14 +37,10 @@ public class TrustCommand implements JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
var number = ns.getString("number"); var recipentString = ns.getString("number");
var recipient = CommandUtil.getSingleRecipientIdentifier(recipentString, m.getUsername());
if (ns.getBoolean("trust-all-known-keys")) { if (ns.getBoolean("trust-all-known-keys")) {
boolean res; boolean res = m.trustIdentityAllKeys(recipient);
try {
res = m.trustIdentityAllKeys(number);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) { if (!res) {
throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct."); throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
} }
@ -64,23 +60,13 @@ public class TrustCommand implements JsonRpcLocalCommand {
throw new UserErrorException( throw new UserErrorException(
"Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters."); "Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
} }
boolean res; boolean res = m.trustIdentityVerified(recipient, fingerprintBytes);
try {
res = m.trustIdentityVerified(number, fingerprintBytes);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) { if (!res) {
throw new UserErrorException( throw new UserErrorException(
"Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct."); "Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
} }
} else if (safetyNumber.length() == 60) { } else if (safetyNumber.length() == 60) {
boolean res; boolean res = m.trustIdentityVerifiedSafetyNumber(recipient, safetyNumber);
try {
res = m.trustIdentityVerifiedSafetyNumber(number, safetyNumber);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) { if (!res) {
throw new UserErrorException( throw new UserErrorException(
"Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct."); "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
@ -93,12 +79,7 @@ public class TrustCommand implements JsonRpcLocalCommand {
throw new UserErrorException( throw new UserErrorException(
"Safety number has invalid format, either specify the old hex fingerprint or the new safety number"); "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
} }
boolean res; boolean res = m.trustIdentityVerifiedSafetyNumber(recipient, scannableSafetyNumber);
try {
res = m.trustIdentityVerifiedSafetyNumber(number, scannableSafetyNumber);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) { if (!res) {
throw new UserErrorException( throw new UserErrorException(
"Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct."); "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");

View file

@ -8,12 +8,10 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException; import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.CommandUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
public class UnblockCommand implements JsonRpcLocalCommand { public class UnblockCommand implements JsonRpcLocalCommand {
@ -35,26 +33,20 @@ public class UnblockCommand implements JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
for (var contactNumber : ns.<String>getList("contact")) { for (var contactNumber : CommandUtil.getSingleRecipientIdentifiers(ns.getList("contact"), m.getUsername())) {
try { try {
m.setContactBlocked(contactNumber, false); m.setContactBlocked(contactNumber, false);
} catch (InvalidNumberException e) {
logger.warn("Invalid number: {}", contactNumber);
} catch (NotMasterDeviceException e) { } catch (NotMasterDeviceException e) {
throw new UserErrorException("This command doesn't work on linked devices."); throw new UserErrorException("This command doesn't work on linked devices.");
} }
} }
if (ns.<String>getList("group-id") != null) { final var groupIdStrings = ns.<String>getList("group-id");
for (var groupIdString : ns.<String>getList("group-id")) { for (var groupId : CommandUtil.getGroupIds(groupIdStrings)) {
try { try {
var groupId = Util.decodeGroupId(groupIdString);
m.setGroupBlocked(groupId, false); m.setGroupBlocked(groupId, false);
} catch (GroupIdFormatException e) {
logger.warn("Invalid group id: {}", groupIdString);
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
logger.warn("Unknown group id: {}", groupIdString); logger.warn("Unknown group id: {}", groupId);
}
} }
} }
} }

View file

@ -9,7 +9,7 @@ import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException; import org.asamk.signal.manager.NotMasterDeviceException;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.asamk.signal.util.CommandUtil;
import java.io.IOException; import java.io.IOException;
@ -32,20 +32,19 @@ public class UpdateContactCommand implements JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
var number = ns.getString("number"); var recipientString = ns.getString("number");
var recipient = CommandUtil.getSingleRecipientIdentifier(recipientString, m.getUsername());
try { try {
var expiration = ns.getInt("expiration"); var expiration = ns.getInt("expiration");
if (expiration != null) { if (expiration != null) {
m.setExpirationTimer(number, expiration); m.setExpirationTimer(recipient, expiration);
} }
var name = ns.getString("name"); var name = ns.getString("name");
if (name != null) { if (name != null) {
m.setContactName(number, name); m.setContactName(recipient, name);
} }
} catch (InvalidNumberException e) {
throw new UserErrorException("Invalid contact number: " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
throw new IOErrorException("Update contact error: " + e.getMessage()); throw new IOErrorException("Update contact error: " + e.getMessage());
} catch (NotMasterDeviceException e) { } catch (NotMasterDeviceException e) {

View file

@ -14,17 +14,15 @@ import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupLinkState; import org.asamk.signal.manager.groups.GroupLinkState;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.GroupPermission; import org.asamk.signal.manager.groups.GroupPermission;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils; import org.asamk.signal.util.ErrorUtils;
import org.asamk.signal.util.Util;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -114,22 +112,17 @@ public class UpdateGroupCommand implements DbusCommand, JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
GroupId groupId = null;
final var groupIdString = ns.getString("group-id"); final var groupIdString = ns.getString("group-id");
if (groupIdString != null) { var groupId = CommandUtil.getGroupId(groupIdString);
try {
groupId = Util.decodeGroupId(groupIdString); final var localNumber = m.getUsername();
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id: " + e.getMessage());
}
}
var groupName = ns.getString("name"); var groupName = ns.getString("name");
var groupDescription = ns.getString("description"); var groupDescription = ns.getString("description");
var groupMembers = ns.<String>getList("member"); var groupMembers = CommandUtil.getSingleRecipientIdentifiers(ns.getList("member"), localNumber);
var groupRemoveMembers = ns.<String>getList("remove-member"); var groupRemoveMembers = CommandUtil.getSingleRecipientIdentifiers(ns.getList("remove-member"), localNumber);
var groupAdmins = ns.<String>getList("admin"); var groupAdmins = CommandUtil.getSingleRecipientIdentifiers(ns.getList("admin"), localNumber);
var groupRemoveAdmins = ns.<String>getList("remove-admin"); var groupRemoveAdmins = CommandUtil.getSingleRecipientIdentifiers(ns.getList("remove-admin"), localNumber);
var groupAvatar = ns.getString("avatar"); var groupAvatar = ns.getString("avatar");
var groupResetLink = ns.getBoolean("reset-link"); var groupResetLink = ns.getBoolean("reset-link");
var groupLinkState = getGroupLinkState(ns.getString("link")); var groupLinkState = getGroupLinkState(ns.getString("link"));
@ -140,12 +133,14 @@ public class UpdateGroupCommand implements DbusCommand, JsonRpcLocalCommand {
try { try {
boolean isNewGroup = false; boolean isNewGroup = false;
Long timestamp = null;
if (groupId == null) { if (groupId == null) {
isNewGroup = true; isNewGroup = true;
var results = m.createGroup(groupName, var results = m.createGroup(groupName,
groupMembers, groupMembers,
groupAvatar == null ? null : new File(groupAvatar)); groupAvatar == null ? null : new File(groupAvatar));
ErrorUtils.handleSendMessageResults(results.second()); timestamp = results.second().getTimestamp();
ErrorUtils.handleSendMessageResults(results.second().getResults());
groupId = results.first(); groupId = results.first();
groupName = null; groupName = null;
groupMembers = null; groupMembers = null;
@ -168,20 +163,15 @@ public class UpdateGroupCommand implements DbusCommand, JsonRpcLocalCommand {
groupSendMessagesPermission == null groupSendMessagesPermission == null
? null ? null
: groupSendMessagesPermission == GroupPermission.ONLY_ADMINS); : groupSendMessagesPermission == GroupPermission.ONLY_ADMINS);
Long timestamp = null;
if (results != null) { if (results != null) {
timestamp = results.first(); timestamp = results.getTimestamp();
ErrorUtils.handleSendMessageResults(results.second()); ErrorUtils.handleSendMessageResults(results.getResults());
} }
outputResult(outputWriter, timestamp, isNewGroup ? groupId : null); outputResult(outputWriter, timestamp, isNewGroup ? groupId : null);
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage()); throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage());
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException | NotAGroupMemberException e) {
logger.warn("Unknown group id: {}", groupIdString); throw new UserErrorException(e.getMessage());
} catch (NotAGroupMemberException e) {
logger.warn("You're not a group member");
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse member number: " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
} }
@ -191,17 +181,7 @@ public class UpdateGroupCommand implements DbusCommand, JsonRpcLocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Signal signal, final OutputWriter outputWriter final Namespace ns, final Signal signal, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
byte[] groupId = null; var groupId = CommandUtil.getGroupId(ns.getString("group-id"));
if (ns.getString("group-id") != null) {
try {
groupId = Util.decodeGroupId(ns.getString("group-id")).serialize();
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id: " + e.getMessage());
}
}
if (groupId == null) {
groupId = new byte[0];
}
var groupName = ns.getString("name"); var groupName = ns.getString("name");
if (groupName == null) { if (groupName == null) {
@ -219,8 +199,11 @@ public class UpdateGroupCommand implements DbusCommand, JsonRpcLocalCommand {
} }
try { try {
var newGroupId = signal.updateGroup(groupId, groupName, groupMembers, groupAvatar); var newGroupId = signal.updateGroup(groupId == null ? new byte[0] : groupId.serialize(),
if (groupId.length != newGroupId.length) { groupName,
groupMembers,
groupAvatar);
if (groupId == null) {
outputResult(outputWriter, null, GroupId.unknownVersion(newGroupId)); outputResult(outputWriter, null, GroupId.unknownVersion(newGroupId));
} }
} catch (Signal.Error.AttachmentInvalid e) { } catch (Signal.Error.AttachmentInvalid e) {

View file

@ -6,6 +6,7 @@ import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException; import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.Message; import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
@ -24,7 +25,10 @@ import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -59,57 +63,22 @@ public class DbusSignalImpl implements Signal {
return sendMessage(message, attachments, recipients); return sendMessage(message, attachments, recipients);
} }
private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException {
var error = ErrorUtils.getErrorMessageFromSendMessageResult(result);
if (error == null) {
return;
}
final var message = timestamp + "\nFailed to send message:\n" + error + '\n';
if (result.getIdentityFailure() != null) {
throw new Error.UntrustedIdentity(message);
} else {
throw new Error.Failure(message);
}
}
private static void checkSendMessageResults(
long timestamp, List<SendMessageResult> results
) throws DBusExecutionException {
if (results.size() == 1) {
checkSendMessageResult(timestamp, results.get(0));
return;
}
var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results);
if (errors.size() == 0) {
return;
}
var message = new StringBuilder();
message.append(timestamp).append('\n');
message.append("Failed to send (some) messages:\n");
for (var error : errors) {
message.append(error).append('\n');
}
throw new Error.Failure(message.toString());
}
@Override @Override
public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) { public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) {
try { try {
final var results = m.sendMessage(new Message(message, attachments), recipients); final var results = m.sendMessage(new Message(message, attachments),
checkSendMessageResults(results.first(), results.second()); getSingleRecipientIdentifiers(recipients, m.getUsername()).stream()
return results.first(); .map(RecipientIdentifier.class::cast)
} catch (InvalidNumberException e) { .collect(Collectors.toSet()));
throw new Error.InvalidNumber(e.getMessage());
checkSendMessageResults(results.getTimestamp(), results.getResults());
return results.getTimestamp();
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
throw new Error.AttachmentInvalid(e.getMessage()); throw new Error.AttachmentInvalid(e.getMessage());
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new Error.GroupNotFound(e.getMessage());
} }
} }
@ -127,13 +96,16 @@ public class DbusSignalImpl implements Signal {
final long targetSentTimestamp, final List<String> recipients final long targetSentTimestamp, final List<String> recipients
) { ) {
try { try {
final var results = m.sendRemoteDeleteMessage(targetSentTimestamp, recipients); final var results = m.sendRemoteDeleteMessage(targetSentTimestamp,
checkSendMessageResults(results.first(), results.second()); getSingleRecipientIdentifiers(recipients, m.getUsername()).stream()
return results.first(); .map(RecipientIdentifier.class::cast)
.collect(Collectors.toSet()));
checkSendMessageResults(results.getTimestamp(), results.getResults());
return results.getTimestamp();
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (InvalidNumberException e) { } catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new Error.InvalidNumber(e.getMessage()); throw new Error.GroupNotFound(e.getMessage());
} }
} }
@ -142,9 +114,10 @@ public class DbusSignalImpl implements Signal {
final long targetSentTimestamp, final byte[] groupId final long targetSentTimestamp, final byte[] groupId
) { ) {
try { try {
final var results = m.sendGroupRemoteDeleteMessage(targetSentTimestamp, GroupId.unknownVersion(groupId)); final var results = m.sendRemoteDeleteMessage(targetSentTimestamp,
checkSendMessageResults(results.first(), results.second()); Set.of(new RecipientIdentifier.Group(getGroupId(groupId))));
return results.first(); checkSendMessageResults(results.getTimestamp(), results.getResults());
return results.getTimestamp();
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) { } catch (GroupNotFoundException | NotAGroupMemberException e) {
@ -174,13 +147,19 @@ public class DbusSignalImpl implements Signal {
final List<String> recipients final List<String> recipients
) { ) {
try { try {
final var results = m.sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients); final var results = m.sendMessageReaction(emoji,
checkSendMessageResults(results.first(), results.second()); remove,
return results.first(); getSingleRecipientIdentifier(targetAuthor, m.getUsername()),
} catch (InvalidNumberException e) { targetSentTimestamp,
throw new Error.InvalidNumber(e.getMessage()); getSingleRecipientIdentifiers(recipients, m.getUsername()).stream()
.map(RecipientIdentifier.class::cast)
.collect(Collectors.toSet()));
checkSendMessageResults(results.getTimestamp(), results.getResults());
return results.getTimestamp();
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new Error.GroupNotFound(e.getMessage());
} }
} }
@ -189,34 +168,36 @@ public class DbusSignalImpl implements Signal {
final String message, final List<String> attachments final String message, final List<String> attachments
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity { ) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
try { try {
final var results = m.sendSelfMessage(new Message(message, attachments)); final var results = m.sendMessage(new Message(message, attachments),
checkSendMessageResult(results.first(), results.second()); Set.of(new RecipientIdentifier.NoteToSelf()));
return results.first(); checkSendMessageResults(results.getTimestamp(), results.getResults());
return results.getTimestamp();
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
throw new Error.AttachmentInvalid(e.getMessage()); throw new Error.AttachmentInvalid(e.getMessage());
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new Error.GroupNotFound(e.getMessage());
} }
} }
@Override @Override
public void sendEndSessionMessage(final List<String> recipients) { public void sendEndSessionMessage(final List<String> recipients) {
try { try {
final var results = m.sendEndSessionMessage(recipients); final var results = m.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients, m.getUsername()));
checkSendMessageResults(results.first(), results.second()); checkSendMessageResults(results.getTimestamp(), results.getResults());
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
} }
} }
@Override @Override
public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) { public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
try { try {
var results = m.sendGroupMessage(new Message(message, attachments), GroupId.unknownVersion(groupId)); var results = m.sendMessage(new Message(message, attachments),
checkSendMessageResults(results.first(), results.second()); Set.of(new RecipientIdentifier.Group(getGroupId(groupId))));
return results.first(); checkSendMessageResults(results.getTimestamp(), results.getResults());
return results.getTimestamp();
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) { } catch (GroupNotFoundException | NotAGroupMemberException e) {
@ -235,17 +216,15 @@ public class DbusSignalImpl implements Signal {
final byte[] groupId final byte[] groupId
) { ) {
try { try {
final var results = m.sendGroupMessageReaction(emoji, final var results = m.sendMessageReaction(emoji,
remove, remove,
targetAuthor, getSingleRecipientIdentifier(targetAuthor, m.getUsername()),
targetSentTimestamp, targetSentTimestamp,
GroupId.unknownVersion(groupId)); Set.of(new RecipientIdentifier.Group(getGroupId(groupId))));
checkSendMessageResults(results.first(), results.second()); checkSendMessageResults(results.getTimestamp(), results.getResults());
return results.first(); return results.getTimestamp();
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) { } catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new Error.GroupNotFound(e.getMessage()); throw new Error.GroupNotFound(e.getMessage());
} }
@ -255,19 +234,13 @@ public class DbusSignalImpl implements Signal {
// the profile name // the profile name
@Override @Override
public String getContactName(final String number) { public String getContactName(final String number) {
try { return m.getContactOrProfileName(getSingleRecipientIdentifier(number, m.getUsername()));
return m.getContactOrProfileName(number);
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
}
} }
@Override @Override
public void setContactName(final String number, final String name) { public void setContactName(final String number, final String name) {
try { try {
m.setContactName(number, name); m.setContactName(getSingleRecipientIdentifier(number, m.getUsername()), name);
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
} catch (NotMasterDeviceException e) { } catch (NotMasterDeviceException e) {
throw new Error.Failure("This command doesn't work on linked devices."); throw new Error.Failure("This command doesn't work on linked devices.");
} }
@ -276,9 +249,7 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public void setContactBlocked(final String number, final boolean blocked) { public void setContactBlocked(final String number, final boolean blocked) {
try { try {
m.setContactBlocked(number, blocked); m.setContactBlocked(getSingleRecipientIdentifier(number, m.getUsername()), blocked);
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
} catch (NotMasterDeviceException e) { } catch (NotMasterDeviceException e) {
throw new Error.Failure("This command doesn't work on linked devices."); throw new Error.Failure("This command doesn't work on linked devices.");
} }
@ -287,7 +258,7 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public void setGroupBlocked(final byte[] groupId, final boolean blocked) { public void setGroupBlocked(final byte[] groupId, final boolean blocked) {
try { try {
m.setGroupBlocked(GroupId.unknownVersion(groupId), blocked); m.setGroupBlocked(getGroupId(groupId), blocked);
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
throw new Error.GroupNotFound(e.getMessage()); throw new Error.GroupNotFound(e.getMessage());
} }
@ -305,7 +276,7 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public String getGroupName(final byte[] groupId) { public String getGroupName(final byte[] groupId) {
var group = m.getGroup(GroupId.unknownVersion(groupId)); var group = m.getGroup(getGroupId(groupId));
if (group == null) { if (group == null) {
return ""; return "";
} else { } else {
@ -315,7 +286,7 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public List<String> getGroupMembers(final byte[] groupId) { public List<String> getGroupMembers(final byte[] groupId) {
var group = m.getGroup(GroupId.unknownVersion(groupId)); var group = m.getGroup(getGroupId(groupId));
if (group == null) { if (group == null) {
return List.of(); return List.of();
} else { } else {
@ -336,21 +307,19 @@ public class DbusSignalImpl implements Signal {
if (name.isEmpty()) { if (name.isEmpty()) {
name = null; name = null;
} }
if (members.isEmpty()) {
members = null;
}
if (avatar.isEmpty()) { if (avatar.isEmpty()) {
avatar = null; avatar = null;
} }
final var memberIdentifiers = getSingleRecipientIdentifiers(members, m.getUsername());
if (groupId == null) { if (groupId == null) {
final var results = m.createGroup(name, members, avatar == null ? null : new File(avatar)); final var results = m.createGroup(name, memberIdentifiers, avatar == null ? null : new File(avatar));
checkSendMessageResults(0, results.second()); checkSendMessageResults(results.second().getTimestamp(), results.second().getResults());
return results.first().serialize(); return results.first().serialize();
} else { } else {
final var results = m.updateGroup(GroupId.unknownVersion(groupId), final var results = m.updateGroup(getGroupId(groupId),
name, name,
null, null,
members, memberIdentifiers,
null, null,
null, null,
null, null,
@ -362,7 +331,7 @@ public class DbusSignalImpl implements Signal {
null, null,
null); null);
if (results != null) { if (results != null) {
checkSendMessageResults(results.first(), results.second()); checkSendMessageResults(results.getTimestamp(), results.getResults());
} }
return groupId; return groupId;
} }
@ -370,8 +339,6 @@ public class DbusSignalImpl implements Signal {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) { } catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new Error.GroupNotFound(e.getMessage()); throw new Error.GroupNotFound(e.getMessage());
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
throw new Error.AttachmentInvalid(e.getMessage()); throw new Error.AttachmentInvalid(e.getMessage());
} }
@ -450,26 +417,25 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public void quitGroup(final byte[] groupId) { public void quitGroup(final byte[] groupId) {
var group = GroupId.unknownVersion(groupId); var group = getGroupId(groupId);
try { try {
m.sendQuitGroupMessage(group, Set.of()); m.sendQuitGroupMessage(group, Set.of());
} catch (GroupNotFoundException | NotAGroupMemberException e) { } catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new Error.GroupNotFound(e.getMessage()); throw new Error.GroupNotFound(e.getMessage());
} catch (IOException | LastGroupAdminException e) { } catch (IOException | LastGroupAdminException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
} }
} }
@Override @Override
public void joinGroup(final String groupLink) { public byte[] joinGroup(final String groupLink) {
try { try {
final var linkUrl = GroupInviteLinkUrl.fromUri(groupLink); final var linkUrl = GroupInviteLinkUrl.fromUri(groupLink);
if (linkUrl == null) { if (linkUrl == null) {
throw new Error.Failure("Group link is invalid:"); throw new Error.Failure("Group link is invalid:");
} }
m.joinGroup(linkUrl); final var result = m.joinGroup(linkUrl);
return result.first().serialize();
} catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) { } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) {
throw new Error.Failure("Group link is invalid: " + e.getMessage()); throw new Error.Failure("Group link is invalid: " + e.getMessage());
} catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) { } catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
@ -481,16 +447,12 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public boolean isContactBlocked(final String number) { public boolean isContactBlocked(final String number) {
try { return m.isContactBlocked(getSingleRecipientIdentifier(number, m.getUsername()));
return m.isContactBlocked(number);
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
}
} }
@Override @Override
public boolean isGroupBlocked(final byte[] groupId) { public boolean isGroupBlocked(final byte[] groupId) {
var group = m.getGroup(GroupId.unknownVersion(groupId)); var group = m.getGroup(getGroupId(groupId));
if (group == null) { if (group == null) {
return false; return false;
} else { } else {
@ -500,11 +462,102 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public boolean isMember(final byte[] groupId) { public boolean isMember(final byte[] groupId) {
var group = m.getGroup(GroupId.unknownVersion(groupId)); var group = m.getGroup(getGroupId(groupId));
if (group == null) { if (group == null) {
return false; return false;
} else { } else {
return group.isMember(m.getSelfRecipientId()); return group.isMember(m.getSelfRecipientId());
} }
} }
private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException {
var error = ErrorUtils.getErrorMessageFromSendMessageResult(result);
if (error == null) {
return;
}
final var message = timestamp + "\nFailed to send message:\n" + error + '\n';
if (result.getIdentityFailure() != null) {
throw new Error.UntrustedIdentity(message);
} else {
throw new Error.Failure(message);
}
}
private static void checkSendMessageResults(
long timestamp, Map<RecipientIdentifier, List<SendMessageResult>> results
) throws DBusExecutionException {
final var sendMessageResults = results.values().stream().findFirst();
if (results.size() == 1 && sendMessageResults.get().size() == 1) {
checkSendMessageResult(timestamp, sendMessageResults.get().stream().findFirst().get());
return;
}
var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results);
if (errors.size() == 0) {
return;
}
var message = new StringBuilder();
message.append(timestamp).append('\n');
message.append("Failed to send (some) messages:\n");
for (var error : errors) {
message.append(error).append('\n');
}
throw new Error.Failure(message.toString());
}
private static void checkSendMessageResults(
long timestamp, Collection<SendMessageResult> results
) throws DBusExecutionException {
if (results.size() == 1) {
checkSendMessageResult(timestamp, results.stream().findFirst().get());
return;
}
var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results);
if (errors.size() == 0) {
return;
}
var message = new StringBuilder();
message.append(timestamp).append('\n');
message.append("Failed to send (some) messages:\n");
for (var error : errors) {
message.append(error).append('\n');
}
throw new Error.Failure(message.toString());
}
private static Set<RecipientIdentifier.Single> getSingleRecipientIdentifiers(
final Collection<String> recipientStrings, final String localNumber
) throws DBusExecutionException {
final var identifiers = new HashSet<RecipientIdentifier.Single>();
for (var recipientString : recipientStrings) {
identifiers.add(getSingleRecipientIdentifier(recipientString, localNumber));
}
return identifiers;
}
private static RecipientIdentifier.Single getSingleRecipientIdentifier(
final String recipientString, final String localNumber
) throws DBusExecutionException {
try {
return RecipientIdentifier.Single.fromString(recipientString, localNumber);
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
}
}
private static GroupId getGroupId(byte[] groupId) throws DBusExecutionException {
try {
return GroupId.unknownVersion(groupId);
} catch (Throwable e) {
throw new Error.InvalidGroupId("Invalid group id: " + e.getMessage());
}
}
} }

View file

@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException; import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException;
import org.whispersystems.signalservice.api.messages.SignalServiceContent; import org.whispersystems.signalservice.api.messages.SignalServiceContent;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
@ -94,7 +95,7 @@ public class JsonMessageEnvelope {
} }
String name; String name;
try { try {
name = m.getContactOrProfileName(this.source); name = m.getContactOrProfileName(RecipientIdentifier.Single.fromString(this.source, m.getUsername()));
} catch (InvalidNumberException | NullPointerException e) { } catch (InvalidNumberException | NullPointerException e) {
name = null; name = null;
} }

View file

@ -0,0 +1,99 @@
package org.asamk.signal.util;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CommandUtil {
private CommandUtil() {
}
public static Set<RecipientIdentifier> getRecipientIdentifiers(
final Manager m,
final boolean isNoteToSelf,
final List<String> recipientStrings,
final List<String> groupIdStrings
) throws UserErrorException {
final var recipientIdentifiers = new HashSet<RecipientIdentifier>();
if (isNoteToSelf) {
recipientIdentifiers.add(new RecipientIdentifier.NoteToSelf());
}
if (recipientStrings != null) {
final var localNumber = m.getUsername();
recipientIdentifiers.addAll(CommandUtil.getSingleRecipientIdentifiers(recipientStrings, localNumber));
}
if (groupIdStrings != null) {
recipientIdentifiers.addAll(CommandUtil.getGroupIdentifiers(groupIdStrings));
}
if (recipientIdentifiers.isEmpty()) {
throw new UserErrorException("No recipients given");
}
return recipientIdentifiers;
}
public static Set<RecipientIdentifier.Group> getGroupIdentifiers(Collection<String> groupIdStrings) throws UserErrorException {
if (groupIdStrings == null) {
return Set.of();
}
final var groupIds = new HashSet<RecipientIdentifier.Group>();
for (final var groupIdString : groupIdStrings) {
groupIds.add(new RecipientIdentifier.Group(getGroupId(groupIdString)));
}
return groupIds;
}
public static Set<GroupId> getGroupIds(Collection<String> groupIdStrings) throws UserErrorException {
if (groupIdStrings == null) {
return Set.of();
}
final var groupIds = new HashSet<GroupId>();
for (final var groupIdString : groupIdStrings) {
groupIds.add(getGroupId(groupIdString));
}
return groupIds;
}
public static GroupId getGroupId(String groupId) throws UserErrorException {
if (groupId == null) {
return null;
}
try {
return GroupId.fromBase64(groupId);
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id: " + e.getMessage());
}
}
public static Set<RecipientIdentifier.Single> getSingleRecipientIdentifiers(
final Collection<String> recipientStrings, final String localNumber
) throws UserErrorException {
if (recipientStrings == null) {
return Set.of();
}
final var identifiers = new HashSet<RecipientIdentifier.Single>();
for (var recipientString : recipientStrings) {
identifiers.add(getSingleRecipientIdentifier(recipientString, localNumber));
}
return identifiers;
}
public static RecipientIdentifier.Single getSingleRecipientIdentifier(
final String recipientString, final String localNumber
) throws UserErrorException {
try {
return RecipientIdentifier.Single.fromString(recipientString, localNumber);
} catch (InvalidNumberException e) {
throw new UserErrorException("Invalid phone number '" + recipientString + "': " + e.getMessage());
}
}
}

View file

@ -2,13 +2,16 @@ package org.asamk.signal.util;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.messages.SendMessageResult; import org.whispersystems.signalservice.api.messages.SendMessageResult;
import org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException; import org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.asamk.signal.util.Util.getLegacyIdentifier; import static org.asamk.signal.util.Util.getLegacyIdentifier;
@ -21,13 +24,27 @@ public class ErrorUtils {
} }
public static void handleSendMessageResults( public static void handleSendMessageResults(
List<SendMessageResult> results Map<RecipientIdentifier, List<SendMessageResult>> mapResults
) throws CommandException {
List<String> errors = getErrorMessagesFromSendMessageResults(mapResults);
handleSendMessageResultErrors(errors);
}
public static void handleSendMessageResults(
Collection<SendMessageResult> results
) throws CommandException { ) throws CommandException {
var errors = getErrorMessagesFromSendMessageResults(results); var errors = getErrorMessagesFromSendMessageResults(results);
handleSendMessageResultErrors(errors); handleSendMessageResultErrors(errors);
} }
public static List<String> getErrorMessagesFromSendMessageResults(List<SendMessageResult> results) { public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
return mapResults.values()
.stream()
.flatMap(results -> getErrorMessagesFromSendMessageResults(results).stream())
.collect(Collectors.toList());
}
public static List<String> getErrorMessagesFromSendMessageResults(Collection<SendMessageResult> results) {
var errors = new ArrayList<String>(); var errors = new ArrayList<String>();
for (var result : results) { for (var result : results) {
var error = getErrorMessageFromSendMessageResult(result); var error = getErrorMessageFromSendMessageResult(result);

View file

@ -5,8 +5,6 @@ import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
@ -61,10 +59,6 @@ public class Util {
return f.toString(); return f.toString();
} }
public static GroupId decodeGroupId(String groupId) throws GroupIdFormatException {
return GroupId.fromBase64(groupId);
}
public static String getLegacyIdentifier(final SignalServiceAddress address) { public static String getLegacyIdentifier(final SignalServiceAddress address) {
return address.getNumber().or(() -> address.getUuid().get().toString()); return address.getNumber().or(() -> address.getUuid().get().toString());
} }