From b1a9ed2bd5bea7fe2f1a597a84111f6688e8dfe5 Mon Sep 17 00:00:00 2001 From: John Freed Date: Tue, 21 Sep 2021 16:42:51 +0200 Subject: [PATCH] Dbus group methods And update documentation ==== Reimplemented methods ==== removeDevice() -> <>:: - becomes a device method (implemented by AsamK) updateDeviceName(deviceName) -> <>:: - becomes setDeviceName and is a device method (implemented by AsamK) setDeviceName(deviceName) -> <>:: - allow changing local name for device (updating AsamK's implementation) getDevice(deviceId) -> devicePath:: - prevent crash when negative number is given for deviceId (updating AsamK's implementation) sendTyping(typingAction, groupIdStrings, numbers) -> <>:: - conform to sendTyping command and flesh it out link() -> deviceLinkUri:: - restore variant where default name is "cli" ==== New device methods ==== getDeviceName() -> deviceName:: ==== Already implemented, newly documented methods ==== getSelfNumber() -> number:: (implemented by AsamK) ==== Other new methods ==== getGroupId(groupName) -> groupId:: getGroupIdStrings() -> groupIdStrings:: getGroupIdString(groupName) -> groupIdString:: getGroupNames() -> groupNames:: updateGroup(groupId, name, description, addMembers, removeMembers, addAdmins, removeAdmins, resetGroupLink, groupLinkState, addMemberPermission, editDetailsPermission, avatar, expiration, isAnnouncementGroup) -> groupId:: updateGroup(groupId, name, description, addMembers, removeMembers, addAdmins, removeAdmins, resetGroupLink, groupLinkState, addMemberPermission, editDetailsPermission, avatar, expiration) -> groupId:: getGroupInviteUri(groupId) -> inviteUri:: getGroupAdminMembers(groupId) -> groupAdminMembers:: getGroupPendingMembers(groupId) -> groupPendingMembers:: getGroupRequestingMembers(groupId) -> groupRequestingMembers:: updateMembers(groupId, members, addToMembers) -> memberList:: updateAdmins(groupId, admins, addToAdmins) -> adminList:: setGroupAnnounceOnly(groupId, isAnnouncementGroup) -> <>:: isGroupAnnounceOnly(groupId) -> isAnnouncementGroup:: isAdmin(groupId) -> isAdmin:: --- .../org/asamk/signal/manager/api/Device.java | 6 +- man/signal-cli-dbus.5.adoc | 320 +++++++--- src/main/java/org/asamk/Signal.java | 139 +++-- src/main/java/org/asamk/SignalControl.java | 1 + .../signal/commands/UpdateGroupCommand.java | 4 +- .../asamk/signal/dbus/DbusManagerImpl.java | 9 +- .../signal/dbus/DbusSignalControlImpl.java | 5 + .../org/asamk/signal/dbus/DbusSignalImpl.java | 574 ++++++++++++++++-- 8 files changed, 879 insertions(+), 179 deletions(-) diff --git a/lib/src/main/java/org/asamk/signal/manager/api/Device.java b/lib/src/main/java/org/asamk/signal/manager/api/Device.java index 9ee0d36a..a1233970 100644 --- a/lib/src/main/java/org/asamk/signal/manager/api/Device.java +++ b/lib/src/main/java/org/asamk/signal/manager/api/Device.java @@ -3,7 +3,7 @@ package org.asamk.signal.manager.api; public class Device { private final long id; - private final String name; + private String name; private final long created; private final long lastSeen; private final boolean thisDevice; @@ -35,4 +35,8 @@ public class Device { public boolean isThisDevice() { return thisDevice; } + + public void setName(String deviceName) { + this.name = deviceName; + } } diff --git a/man/signal-cli-dbus.5.adoc b/man/signal-cli-dbus.5.adoc index e7cd083f..29066d5e 100755 --- a/man/signal-cli-dbus.5.adoc +++ b/man/signal-cli-dbus.5.adoc @@ -29,13 +29,13 @@ method(arg1, arg2, ...) -> return Where is according to DBus specification: +* : Array of ... * : String -* : Byte Array -* : Array of Byte Arrays -* : String Array -* : Array of signed 64 bit integer -* : Boolean (0|1) -* : Signed 64 bit integer +* : Boolean (false|true) +* : Signed int32 integer (int) +* : DBusPath object +* : Signed int64 integer (long) +* : Byte * <> : no return value Exceptions are the names of the Java Exceptions returned in the body field. They typically contain an additional message with details. All Exceptions begin with "org.asamk.Signal.Error." which is omitted here for better readability. @@ -45,9 +45,9 @@ Phone numbers always have the format + == Methods === Control methods -These methods are available if the daemon is started anonymously (without an explicit `-u USERNAME`). -Requests are sent to `/org/asamk/Signal`; requests related to individual accounts are sent to -`/org/asamk/Signal/_441234567890` where the + dialing code is replaced by an underscore (_). +These methods are available if the daemon is started anonymously (without an explicit `-u USERNAME`). +Requests are sent to `/org/asamk/Signal`; requests related to individual accounts are sent to +`/org/asamk/Signal/_441234567890` where the + dialing code is replaced by an underscore (_). Only `version()` is activated in single-user mode; the rest are disabled. link() -> deviceLinkUri:: @@ -60,7 +60,7 @@ can be captured by a Signal smartphone client. For example: `dbus-send --session --dest=org.asamk.Signal --type=method_call --print-reply /org/asamk/Signal org.asamk.Signal.link string:"My secondary client"|tr '\n' '\0'|sed 's/.*string //g'|sed 's/\"//g'|qrencode -s10 -tANSI256` -Exception: Failure +Exceptions: Failure listAccounts() -> accountList:: * accountList : Array of all attached accounts in DBus object path form @@ -86,29 +86,94 @@ verify(number, verificationCode) -> <>:: Command fails if PIN was set after previous registration; use verifyWithPin instead. -Exception: Failure, InvalidNumber +Exceptions: Failure, InvalidNumber verifyWithPin(number, verificationCode, pin) -> <>:: * number : Phone number * verificationCode : Code received from Signal after successful registration request * pin : PIN you set with setPin command after verifying previous registration -Exception: Failure, InvalidNumber +Exceptions: Failure, InvalidNumber version() -> version:: * version : Version string of signal-cli Exceptions: None +=== Device methods +Requests for these methods are sent to a specific device (main or linked); the list is available +from the listDevices() method (see below under "Other methods"). + +getDeviceName() -> deviceName:: +* deviceName : Name of this device + +Exceptions: None + +removeDevice() -> <>:: + +Exceptions: Failure + +setDeviceName(deviceName) -> <>:: +* deviceName : Name of this device + +Sets the name of this device locally. If this device is controlled by this signal-cli instance, also sets the name on the Signal servers. + +Exceptions: Failure + === Other methods +updateAdmins(groupId, admins, addToAdmins) -> adminList:: +* groupId : Byte array representing the internal group identifier +* admins : Phone numbers of users to grant or deny admin status +* addToAdmins : true for add to group admins; false for remove from group admins +* adminList : List of admins after command execution + +Exceptions: GroupNotFound, InvalidGroupId, Failure + +updateGroup(groupId, name, description, addMembers, removeMembers, addAdmins, removeAdmins, resetGroupLink, groupLinkState, addMemberPermission, editDetailsPermission, avatar, expiration, isAnnouncementGroup) -> groupId:: +updateGroup(groupId, name, description, addMembers, removeMembers, addAdmins, removeAdmins, resetGroupLink, groupLinkState, addMemberPermission, editDetailsPermission, avatar, expiration) -> groupId:: +* groupId : Byte array representing the internal group identifier +* name : Name of group (empty if unchanged) +* description : Description (empty if unchanged) +* addMembers : String array of new members to be invited to group (empty if unchanged) +* removeMembers : String array of members to be removed from group (empty if unchanged) +* addAdmins : String array of members granted admin privileges (empty if unchanged) +* removeAdmins : String array of members denied admin privileges (empty if unchanged) +* resetGroupLink : Boolean (true = change the group link, false = don't change) +* groupLinkState : String ("enabled", "enabled-with-approval", "disabled") (empty if unchanged) +* addMemberPermission : String of who may add members ("every-member", "only-admins") (empty if unchanged) +* editDetailsPermission : String of who may edit group details ("every-member", "only-admins") (empty if unchanged) +* avatar : Filename of avatar picture to be set for group (see below). +* expiration : Expiration time (in seconds) for messages sent to this group (see below). +* isAnnouncementGroup : true=only admins can post, false=any member can post (omit if unchanged) + +If groupId is empty or null, Signal creates a group with a random identifier +and returns it. To delete the group avatar, send the name of an empty file; leave field +empty if avatar is unchanged. Set expiration time to 0 to disable expirations, set to a +negative number to leave unchanged. Omit the isAnnouncementGroup parameter to leave it unchanged. + +Exceptions: InvalidGroupId, Failure + updateGroup(groupId, newName, members, avatar) -> groupId:: * groupId : Byte array representing the internal group identifier * newName : New name of group (empty if unchanged) * members : String array of new members to be invited to group * avatar : Filename of avatar picture to be set for group (empty if none) -Exceptions: AttachmentInvalid, Failure, InvalidNumber, GroupNotFound +If groupId is empty or null, Signal creates a group with a random identifier +and returns it. To delete the group avatar, send the name of an empty file; leave field +empty if avatar is unchanged. Set expiration time to 0 to disable expirations. + +Exceptions: AttachmentInvalid, Failure, GroupNotFound, InvalidGroupId, InvalidNumber + +updateMembers(groupId, members, addToMembers) -> memberList:: +* groupId : Byte array representing the internal group identifier +* members : Phone numbers of users to add to or remove from group +* addToMembers : true for add to group members; false for remove from group members +* active : Boolean representing whether you are a member of the group +* memberList : List of members after command execution + +Exceptions: GroupNotFound, Failure, InvalidGroupId, InvalidNumber updateProfile(name, about, aboutEmoji , avatar, remove) -> <>:: updateProfile(givenName, familyName, about, aboutEmoji , avatar, remove) -> <>:: @@ -122,16 +187,15 @@ updateProfile(givenName, familyName, about, aboutEmoji , avatar, Exceptions: Failure - setExpirationTimer(number, expiration) -> <>:: * number : Phone number of recipient * expiration : int32 for the number of seconds before messages to this recipient disappear. Set to 0 to disable expiration. -Exceptions: Failure +Exceptions: Failure, InvalidNumber setContactBlocked(number, block) -> <>:: * number : Phone number affected by method -* block : 0=remove block , 1=blocked +* block : false=remove block, true=blocked Messages from blocked numbers will no longer be forwarded via DBus. @@ -139,28 +203,36 @@ Exceptions: InvalidNumber setGroupBlocked(groupId, block) -> <>:: * groupId : Byte array representing the internal group identifier -* block : 0=remove block , 1=blocked +* block : false=remove block , true=blocked Messages from blocked groups will no longer be forwarded via DBus. -Exceptions: GroupNotFound +Exceptions: GroupNotFound, InvalidGroupId -joinGroup(inviteURI) -> <>:: -* inviteURI : String starting with https://signal.group which is generated when you share a group link via Signal App +getGroupInviteUri(groupId) -> inviteUri:: +* groupId : Byte array representing the internal group identifier +* inviteUri : String representing the URI of the group invitation link (starts with https://signal.group) + +Exceptions: Failure, InvalidGroupId + +joinGroup(inviteUri) -> groupId:: +* inviteUri : String representing the URI of the group invitation link (starts with https://signal.group) +* groupId : Byte array representing the internal group identifier Exceptions: Failure quitGroup(groupId) -> <>:: -* groupId : Byte array representing the internal group identifier +* groupId : Byte array representing the internal group identifier Note that quitting a group will not remove the group from the getGroupIds command, but set it inactive which can be tested with isMember() -Exceptions: GroupNotFound, Failure +Exceptions: GroupNotFound, Failure, InvalidGroupId -isMember(groupId) -> active:: -* groupId : Byte array representing the internal group identifier +isMember(groupId) -> isMember:: +* groupId : Byte array representing the internal group identifier +* isMember : true=you are a group member; false=you are not a group member -Note that this method does not raise an Exception for a non-existing/unknown group but will simply return 0 (false) +Exceptions: GroupNotFound, Failure, InvalidGroupId sendEndSessionMessage(recipients) -> <>:: * recipients : Array of phone numbers @@ -169,11 +241,11 @@ Exceptions: Failure, InvalidNumber, UntrustedIdentity sendGroupMessage(message, attachments, groupId) -> timestamp:: * message : Text to send (can be UTF8) -* attachments : String array of filenames to send as attachments (passed as filename, so need to be readable by the user signal-cli is running under) +* attachments : String array of filenames or URLs to send as attachments * groupId : Byte array representing the internal group identifier -* timestamp : Can be used to identify the corresponding signal reply +* timestamp : Long, can be used to identify the corresponding Signal reply -Exceptions: GroupNotFound, Failure, AttachmentInvalid +Exceptions: GroupNotFound, Failure, AttachmentInvalid, InvalidGroupId sendContacts() -> <>:: @@ -185,37 +257,37 @@ sendSyncRequest() -> <>:: Sends a synchronization request to the primary device (for group, contacts, ...). Only works if sent from a secondary device. -Exception: Failure +Exceptions: Failure sendNoteToSelfMessage(message, attachments) -> timestamp:: * message : Text to send (can be UTF8) -* attachments : String array of filenames to send as attachments (passed as filename, so need to be readable by the user signal-cli is running under) -* timestamp : Can be used to identify the corresponding signal reply +* attachments : String array of filenames or URLs to send as attachments +* timestamp : Long, can be used to identify the corresponding Signal reply Exceptions: Failure, AttachmentInvalid sendMessage(message, attachments, recipient) -> timestamp:: sendMessage(message, attachments, recipients) -> timestamp:: * message : Text to send (can be UTF8) -* attachments : String array of filenames to send as attachments (passed as filename, so need to be readable by the user signal-cli is running under) +* attachments : String array of filenames or URLs to send as attachments * recipient : Phone number of a single recipient -* recipients : Array of phone numbers -* timestamp : Can be used to identify the corresponding signal reply +* recipients : String array of phone numbers +* timestamp : Long, can be used to identify the corresponding Signal reply Depending on the type of the recipient field this sends a message to one or multiple recipients. Exceptions: AttachmentInvalid, Failure, InvalidNumber, UntrustedIdentity -sendTyping(recipient, stop) -> <>:: -* recipient : Phone number of a single recipient -* targetSentTimestamp : True, if typing state should be stopped +sendTyping(typingAction, groupIdStrings, numbers) -> <>:: +* typingAction : true = start typing, false = stop typing +* groupIdStrings : List of strings representing the internal group identifiers in Base64 format +* numbers : List of phone numbers for recipients Exceptions: Failure, GroupNotFound, UntrustedIdentity - -sendReadReceipt(recipient, targetSentTimestamp) -> <>:: +sendReadReceipt(recipient, targetSentTimestamps) -> <>:: * recipient : Phone number of a single recipient -* targetSentTimestamp : Array of Longs to identify the corresponding signal messages +* targetSentTimestamps : Array of Longs to identify the corresponding Signal messages Exceptions: Failure, UntrustedIdentity @@ -224,10 +296,10 @@ sendGroupMessageReaction(emoji, remove, targetAuthor, targetSentTimesta * remove : Boolean, whether a previously sent reaction (emoji) should be removed * targetAuthor : String with the phone number of the author of the message to which to react * targetSentTimestamp : Long representing timestamp of the message to which to react -* groupId : Byte array with base64 encoded group identifier +* groupId : Byte array representing the internal group identifier * timestamp : Long, can be used to identify the corresponding signal reply -Exceptions: Failure, InvalidNumber, GroupNotFound +Exceptions: Failure, InvalidNumber, GroupNotFound, InvalidGroupId sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipient) -> timestamp:: sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients) -> timestamp:: @@ -237,7 +309,7 @@ sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp * targetSentTimestamp : Long representing timestamp of the message to which to react * recipient : String with the phone number of a single recipient * recipients : Array of strings with phone numbers, should there be more recipients -* timestamp : Long, can be used to identify the corresponding signal reply +* timestamp : Long, can be used to identify the corresponding Signal reply Depending on the type of the recipient(s) field this sends a reaction to one or multiple recipients. @@ -248,7 +320,7 @@ sendGroupRemoteDeleteMessage(targetSentTimestamp, groupId) -> timestamp, recipient) -> timestamp:: sendRemoteDeleteMessage(targetSentTimestamp, recipients) -> timestamp:: @@ -265,66 +337,149 @@ getContactName(number) -> name:: * number : Phone number * name : Contact's name in local storage (from the master device for a linked account, or the one set with setContactName); if not set, contact's profile name is used +Exceptions: None + setContactName(number,name<>) -> <>:: * number : Phone number * name : Name to be set in contacts (in local storage with signal-cli) +Exceptions: InvalidNumber, Failure + getGroupIds() -> groupList:: groupList : Array of Byte arrays representing the internal group identifiers All groups known are returned, regardless of their active or blocked status. To query that use isMember() and isGroupBlocked() -getGroupName(groupId) -> groupName:: -groupName : The display name of the group -groupId : Byte array representing the internal group identifier +Exceptions: None -Exceptions: None, if the group name is not found an empty string is returned +getGroupId(groupName) -> groupId:: +* groupName : String representing name of a group +* groupId : Byte array representing the internal group identifier + +Exceptions: GroupNotFound + +getGroupIdStrings() -> groupIdStrings:: +* groupIdStrings : Array of strings representing the internal group identifiers + +All groups known are returned, regardless of their active or blocked status. To query that use isMember() and isGroupBlocked() + +Exceptions: None + +getGroupIdString(groupName) -> groupIdString:: +* groupName : String representing name of a group +* groupIdString : String representing the internal group identifier + +Exceptions: GroupNotFound + +getGroupName(groupId) -> groupName:: +* groupId : Byte array representing the internal group identifier +* groupName : The display name of the group + +Exceptions: InvalidGroupId, Failure + +getGroupNames() -> groupNames:: +* groupName : Array of strings representing names of groups + +All groups known are returned, regardless of their active or blocked status. To query that use isMember() and isGroupBlocked() + +Exceptions: None getGroupMembers(groupId) -> members:: -members : String array with the phone numbers of all active members of a group -groupId : Byte array representing the internal group identifier +* groupId : Byte array representing the internal group identifier +* members : String array with the phone numbers of all active members of a group -Exceptions: None, if the group name is not found an empty array is returned +Exceptions: InvalidGroupId, Failure + +getGroupAdminMembers(groupId) -> groupAdminMembers:: +* groupId : Byte array representing the internal group identifier +* groupAdminMembers : String array of phone numbers of members granted admin privileges + +Exceptions: InvalidGroupId, Failure + +getGroupPendingMembers(groupId) -> groupPendingMembers:: +* groupId : Byte array representing the internal group identifier +* groupPendingMembers : String array of phone numbers of pending members + +Exceptions: InvalidGroupId, Failure + +getGroupRequestingMembers(groupId) -> groupRequestingMembers:: +* groupId : Byte array representing the internal group identifier +* groupRequestingMembers : String array of phone numbers of requesting members + +Exceptions: InvalidGroupId, Failure + +isGroupAnnounceOnly(groupId) -> isAnnouncementGroup:: +* groupId : Byte array representing the internal group identifier +* isAnnouncementGroup : true=only admins can post, false=any member can post + +Exceptions: InvalidGroupId, Failure + +setGroupAnnounceOnly(groupId, isAnnouncementGroup) -> <>:: +* groupId : Byte array representing the internal group identifier +* isAnnouncementGroup : true=only admins can post, false=any member can post + +Exceptions: GroupNotFound, InvalidGroupId, Failure + +isAdmin(groupId) -> isAdmin:: +* groupId : Byte array representing the internal group identifier +* isAdmin : true=you are a group admin; false=you are not a group admin + +Exceptions: InvalidGroupId, Failure listNumbers() -> numbers:: -numbers : String array of all known numbers +* numbers : String array of all known numbers This is a concatenated list of all defined contacts as well of profiles known (e.g. peer group members or sender of received messages) +Exceptions: None + getContactNumber(name) -> numbers:: * numbers : Array of phone number * name : Contact or profile name ("firstname lastname") Searches contacts and known profiles for a given name and returns the list of all known numbers. May result in e.g. two entries if a contact and profile name is set. -isContactBlocked(number) -> state:: -* number : Phone number -* state : 1=blocked, 0=not blocked +Exceptions: None -Exceptions: None, for unknown numbers 0 (false) is returned +isContactBlocked(number) -> blocked:: +* number : Phone number +* blocked : true=blocked, false=not blocked -isGroupBlocked(groupId) -> state:: -* groupId : Byte array representing the internal group identifier -* state : 1=blocked, 0=not blocked +For unknown numbers false is returned but no exception is raised. -Exceptions: None, for unknown groups 0 (false) is returned +Exceptions: InvalidPhoneNumber + +isGroupBlocked(groupId) -> isGroupBlocked:: +* groupId : Byte array representing the internal group identifier +* isGroupBlocked : true=group is blocked; false=group is not blocked + +Dbus will not forward messages from a group when you have blocked it. + +Exceptions: InvalidGroupId, Failure removePin() -> <>:: Removes registration PIN protection. -Exception: Failure +Exceptions: Failure setPin(pin) -> <>:: * pin : PIN you set after registration (resets after 7 days of inactivity) Sets a registration lock PIN, to prevent others from registering your number. -Exception: Failure +Exceptions: Failure version() -> version:: * version : Version string of signal-cli +Exceptions: None + +getSelfNumber() -> number:: +* number : Your phone number + +Exceptions: None + isRegistered() -> result:: isRegistered(number) -> result:: isRegistered(numbers) -> results:: @@ -333,39 +488,44 @@ isRegistered(numbers) -> results:: * result : true=number is registered, false=number is not registered * results : Boolean array of results -Exception: InvalidNumber for an incorrectly formatted phone number. For unknown numbers, false is returned, but no exception is raised. If no number is given, returns whether you are registered (presumably true). +For unknown numbers, false is returned, but no exception is raised. If no number is given, returns true (indicating that you are registered). + +Exceptions: InvalidNumber addDevice(deviceUri) -> <>:: -* deviceUri : URI in the form of tsdevice:/?uuid=... Normally received from Signal desktop or smartphone app +* deviceUri : URI in the form of tsdevice:/?uuid=... Normally displayed by a Signal desktop app, smartphone app, or another signal-cli instance using the `link` control method. -Exception: InvalidUri +Exceptions: InvalidUri -listDevices() -> devices:: -* devices : String array of linked devices +getDevice(deviceId) -> devicePath:: +* deviceId : Long representing a (potential) deviceId +* devicePath : DBusPath object for the device -Exception: Failure +Note that this method returns a value even if the device does not exist. -removeDevice(deviceId) -> <>:: -* deviceId : Device ID to remove, obtained from listDevices() command +Exceptions: None -Exception: Failure +listDevices() -> devices:: +* devices : Array of DBusPath objects for main device and any linked devices -updateDeviceName(deviceName) -> <>:: -* deviceName : New name - -Set a new name for this device (main or linked). - -Exception: Failure +Exceptions: Failure uploadStickerPack(stickerPackPath) -> url:: * stickerPackPath : Path to the manifest.json file or a zip file in the same directory * url : URL of sticker pack after successful upload -Exception: Failure +Exceptions: Failure == Signals -SyncMessageReceived (timestamp, sender, destination, groupId,message, attachments):: +SyncMessageReceived (timestamp, sender, destination, groupId, message, attachments):: +* timestamp : Integer value that can be used to associate this e.g. with a sendMessage() +* sender : Phone number of the sender +* destination : DBus code for destination +* groupId : Byte array representing the internal group identifier (empty when private message) +* message : Message text +* attachments : String array of filenames in the signal-cli storage (~/.local/share/signal-cli/attachments/) + The sync message is received when the user sends a message from a linked device. ReceiptReceived (timestamp, sender):: @@ -379,7 +539,7 @@ MessageReceived(timestamp, sender, groupId, message, attachments attachments, String recipient ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity; - long sendMessage( - String message, List attachments, List recipients - ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity; + List getGroupAdminMembers(final byte[] groupId); - void sendTyping( - String recipient, boolean stop - ) throws Error.Failure, Error.GroupNotFound, Error.UntrustedIdentity; + List getGroupIds(); - void sendReadReceipt( - String recipient, List messageIds - ) throws Error.Failure, Error.UntrustedIdentity; + byte[] getGroupId(String groupName) throws Error.GroupNotFound; - long sendRemoteDeleteMessage( - long targetSentTimestamp, String recipient - ) throws Error.Failure, Error.InvalidNumber; + List getGroupIdStrings(); - long sendRemoteDeleteMessage( - long targetSentTimestamp, List recipients - ) throws Error.Failure, Error.InvalidNumber; + String getGroupIdString(String groupName) throws Error.GroupNotFound; - long sendGroupRemoteDeleteMessage( - long targetSentTimestamp, byte[] groupId - ) throws Error.Failure, Error.GroupNotFound, Error.InvalidGroupId; + String getGroupInviteUri(byte[] groupId); - long sendMessageReaction( - String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, String recipient - ) throws Error.InvalidNumber, Error.Failure; + List getGroupMembers(byte[] groupId) throws Error.InvalidGroupId; - long sendMessageReaction( - String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, List recipients - ) throws Error.InvalidNumber, Error.Failure; + String getGroupName(byte[] groupId) throws Error.InvalidGroupId; - void sendContacts() throws Error.Failure; + List getGroupNames(); - void sendSyncRequest() throws Error.Failure; + List getGroupPendingMembers(final byte[] groupId); - long sendNoteToSelfMessage( - String message, List attachments - ) throws Error.AttachmentInvalid, Error.Failure; + List getGroupRequestingMembers(final byte[] groupId); - void sendEndSessionMessage(List recipients) throws Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity; + boolean isAdmin(final byte[] groupId); + + boolean isGroupAnnounceOnly(byte[] groupId); + + boolean isGroupBlocked(final byte[] groupId) throws Error.InvalidGroupId; + + boolean isMember(final byte[] groupId) throws Error.InvalidGroupId; + + byte[] joinGroup(final String groupLink) throws Error.Failure; + + void quitGroup(final byte[] groupId) throws Error.GroupNotFound, Error.Failure, Error.InvalidGroupId; long sendGroupMessage( String message, List attachments, byte[] groupId @@ -72,33 +64,35 @@ public interface Signal extends DBusInterface { String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, byte[] groupId ) throws Error.GroupNotFound, Error.Failure, Error.InvalidNumber, Error.InvalidGroupId; - String getContactName(String number) throws Error.InvalidNumber; + long sendGroupRemoteDeleteMessage( + long targetSentTimestamp, byte[] groupId + ) throws Error.Failure, Error.GroupNotFound, Error.InvalidGroupId; - void setContactName(String number, String name) throws Error.InvalidNumber; + void setGroupAnnounceOnly(byte[] groupId, boolean isAnnouncementGroup); void setExpirationTimer(final String number, final int expiration) throws Error.Failure; - void setContactBlocked(String number, boolean blocked) throws Error.InvalidNumber; - void setGroupBlocked(byte[] groupId, boolean blocked) throws Error.GroupNotFound, Error.InvalidGroupId; - List getGroupIds(); - - String getGroupName(byte[] groupId) throws Error.InvalidGroupId; - - List getGroupMembers(byte[] groupId) throws Error.InvalidGroupId; + List updateAdmins(final byte[] groupId, Listadmins, boolean addToAdmins) throws Error.GroupNotFound, Error.InvalidGroupId, Error.Failure; byte[] updateGroup( byte[] groupId, String name, List members, String avatar ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound, Error.InvalidGroupId; - boolean isRegistered() throws Error.Failure, Error.InvalidNumber; + byte[] updateGroup( + byte[] groupId, String name, String description, List addMembers, List removeMembers, List addAdmins, List removeAdmins, boolean resetGroupLink, String groupLinkState, String addMemberPermission, String editDetailsPermission, String avatar, Integer expirationTimer, Boolean isAnnouncementGroup + ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound, Error.InvalidGroupId; - boolean isRegistered(String number) throws Error.Failure, Error.InvalidNumber; + byte[] updateGroup( + byte[] groupId, String name, String description, List addMembers, List removeMembers, List addAdmins, List removeAdmins, boolean resetGroupLink, String groupLinkState, String addMemberPermission, String editDetailsPermission, String avatar, Integer expirationTimer + ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound, Error.InvalidGroupId; - List isRegistered(List numbers) throws Error.Failure, Error.InvalidNumber; + List updateMembers(final byte[] groupId, List members, boolean addToMembers); - void addDevice(String uri) throws Error.InvalidUri; + void sendContacts() throws Error.Failure; + + void sendSyncRequest() throws Error.Failure; DBusPath getDevice(long deviceId); @@ -115,9 +109,13 @@ public interface Signal extends DBusInterface { boolean removeAvatar ) throws Error.Failure; - void updateProfile( - String name, String about, String aboutEmoji, String avatarPath, boolean removeAvatar - ) throws Error.Failure; + void addDevice(String uri) throws Error.Failure, Error.InvalidUri; + + void setContactBlocked(final String number, final boolean blocked) throws Error.Failure; + + boolean isRegistered(); + boolean isRegistered(String number); + List isRegistered(List numbers) throws Error.Failure; void removePin(); @@ -125,19 +123,51 @@ public interface Signal extends DBusInterface { String version(); - List listNumbers(); + String getContactName(String number) throws Error.InvalidNumber; List getContactNumber(final String name) throws Error.Failure; - void quitGroup(final byte[] groupId) throws Error.GroupNotFound, Error.Failure, Error.InvalidGroupId; - boolean isContactBlocked(final String number) throws Error.InvalidNumber; - boolean isGroupBlocked(final byte[] groupId) throws Error.InvalidGroupId; + void sendEndSessionMessage(List recipients) throws Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity; - boolean isMember(final byte[] groupId) throws Error.InvalidGroupId; + long sendMessage( + String message, List attachments, List recipients + ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity; - byte[] joinGroup(final String groupLink) throws Error.Failure; + long sendMessageReaction( + String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, String recipient + ) throws Error.InvalidNumber, Error.Failure; + + long sendMessageReaction( + String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, List recipients + ) throws Error.InvalidNumber, Error.Failure; + + void sendReadReceipt( + String recipient, List targetSentTimestamp + ) throws Error.Failure, Error.UntrustedIdentity; + + long sendRemoteDeleteMessage( + long targetSentTimestamp, String recipient + ) throws Error.Failure, Error.InvalidNumber; + + long sendRemoteDeleteMessage( + long targetSentTimestamp, List recipients + ) throws Error.Failure, Error.InvalidNumber; + + void setContactName(String number, String name) throws Error.InvalidNumber; + + List listNumbers(); + + long sendNoteToSelfMessage( + String message, List attachments + ) throws Error.AttachmentInvalid, Error.Failure; + + void sendTyping(boolean typingAction, List groupIdStrings, List numbers) throws Error.Failure, Error.GroupNotFound, Error.UntrustedIdentity; + + void updateProfile( + String name, String about, String aboutEmoji, String avatarPath, boolean removeAvatar + ) throws Error.Failure; String uploadStickerPack(String stickerPackPath) throws Error.Failure; @@ -265,6 +295,11 @@ public interface Signal extends DBusInterface { interface Device extends DBusInterface, Properties { void removeDevice() throws Error.Failure; + + String getDeviceName() throws Error.Failure; + + void setDeviceName(String deviceName) throws Error.Failure; + } interface Error { diff --git a/src/main/java/org/asamk/SignalControl.java b/src/main/java/org/asamk/SignalControl.java index 911ccb61..4e1b77a1 100644 --- a/src/main/java/org/asamk/SignalControl.java +++ b/src/main/java/org/asamk/SignalControl.java @@ -24,6 +24,7 @@ public interface SignalControl extends DBusInterface { void verifyWithPin(String number, String verificationCode, String pin) throws Error.Failure, Error.InvalidNumber; + String link() throws Error.Failure; String link(String newDeviceName) throws Error.Failure; public String version(); diff --git a/src/main/java/org/asamk/signal/commands/UpdateGroupCommand.java b/src/main/java/org/asamk/signal/commands/UpdateGroupCommand.java index 68bce2d2..d95be199 100644 --- a/src/main/java/org/asamk/signal/commands/UpdateGroupCommand.java +++ b/src/main/java/org/asamk/signal/commands/UpdateGroupCommand.java @@ -72,7 +72,7 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand { subparser.addArgument("-e", "--expiration").type(int.class).help("Set expiration time of messages (seconds)"); } - GroupLinkState getGroupLinkState(String value) throws UserErrorException { + public static GroupLinkState getGroupLinkState(String value) throws UserErrorException { if (value == null) { return null; } @@ -89,7 +89,7 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand { } } - GroupPermission getGroupPermission(String value) throws UserErrorException { + public static GroupPermission getGroupPermission(String value) throws UserErrorException { if (value == null) { return null; } diff --git a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java index 3124a5b0..17977f0c 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java @@ -247,14 +247,17 @@ public class DbusManagerImpl implements Manager { public void sendTypingMessage( final TypingAction action, final Set recipients ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { + List groupIdStrings = new ArrayList<>(); + List numbers = new ArrayList<>(); + boolean typingAction = (action == TypingAction.START); for (final var recipient : recipients) { if (recipient instanceof RecipientIdentifier.Single) { - signal.sendTyping(((RecipientIdentifier.Single) recipient).getIdentifier(), - action == TypingAction.STOP); + numbers.add(((RecipientIdentifier.Single) recipient).getIdentifier()); } else if (recipient instanceof RecipientIdentifier.Group) { - throw new UnsupportedOperationException(); + groupIdStrings.add(((RecipientIdentifier.Group) recipient).groupId.toBase64()); } } + signal.sendTyping(typingAction, groupIdStrings, numbers); } @Override diff --git a/src/main/java/org/asamk/signal/dbus/DbusSignalControlImpl.java b/src/main/java/org/asamk/signal/dbus/DbusSignalControlImpl.java index be628bde..94a0c526 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalControlImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalControlImpl.java @@ -131,6 +131,11 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl { } } + @Override + public String link() throws Error.Failure { + return link("cli"); + } + @Override public String link(final String newDeviceName) throws Error.Failure { try { diff --git a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java index ab9c89b2..81d6f95a 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -1,17 +1,23 @@ package org.asamk.signal.dbus; import org.asamk.Signal; +import org.asamk.Signal.Error; import org.asamk.signal.BaseConfig; +import org.asamk.signal.commands.UpdateGroupCommand; +import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.NotMasterDeviceException; import org.asamk.signal.manager.StickerPackInvalidException; import org.asamk.signal.manager.UntrustedIdentityException; +import org.asamk.signal.manager.api.Device; +import org.asamk.signal.manager.api.Group; import org.asamk.signal.manager.api.Identity; import org.asamk.signal.manager.api.Message; import org.asamk.signal.manager.api.RecipientIdentifier; 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.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupSendingNotAllowedException; @@ -19,7 +25,9 @@ import org.asamk.signal.manager.groups.LastGroupAdminException; import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.storage.recipients.Profile; import org.asamk.signal.manager.storage.recipients.RecipientAddress; +import org.asamk.signal.util.CommandUtil; import org.asamk.signal.util.ErrorUtils; + import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.exceptions.DBusException; @@ -37,7 +45,9 @@ import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Files; import java.util.ArrayList; +import java.util.Base64; import java.util.Collection; import java.util.HashSet; import java.util.List; @@ -96,6 +106,9 @@ public class DbusSignalImpl implements Signal { @Override public DBusPath getDevice(long deviceId) { + if (deviceId < 0) { + throw new Error.InvalidNumber("Invalid deviceId: Negative number"); + } updateDevices(); return new DBusPath(getDeviceObjectPath(objectPath, deviceId)); } @@ -249,26 +262,6 @@ public class DbusSignalImpl implements Signal { } } - @Override - public void sendTyping( - final String recipient, final boolean stop - ) throws Error.Failure, Error.GroupNotFound, Error.UntrustedIdentity { - try { - var recipients = new ArrayList(1); - recipients.add(recipient); - m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START, - getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() - .map(RecipientIdentifier.class::cast) - .collect(Collectors.toSet())); - } catch (IOException e) { - throw new Error.Failure(e.getMessage()); - } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { - throw new Error.GroupNotFound(e.getMessage()); - } catch (UntrustedIdentityException e) { - throw new Error.UntrustedIdentity(e.getMessage()); - } - } - @Override public void sendReadReceipt( final String recipient, final List messageIds @@ -367,6 +360,39 @@ public class DbusSignalImpl implements Signal { } } + @Override + public void sendTyping(boolean typingAction, List groupIdStrings, List numbers) { + final boolean noNumbers = numbers == null || numbers.isEmpty(); + final boolean noGroup = groupIdStrings == null || groupIdStrings.isEmpty(); + if (noNumbers && noGroup) { + throw new Error.Failure("No recipients given"); + } + + final TypingAction action = typingAction ? TypingAction.START : TypingAction.STOP; + final var timestamp = System.currentTimeMillis(); + final var localNumber = m.getSelfNumber(); + Set recipients = new HashSet(); + + try { + if (!noGroup) { + recipients.addAll(CommandUtil.getGroupIdentifiers(groupIdStrings)); + } + + if (!noNumbers) { + recipients.addAll(CommandUtil.getSingleRecipientIdentifiers(numbers, localNumber)); + } + m.sendTypingMessage(action, recipients); + } catch (IOException e) { + throw new Error.Failure("Failed to send message: " + e.getMessage()); + } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { + throw new Error.InvalidGroupId("Invalid group id: " + e.getMessage()); + } catch (UserErrorException e) { + throw new Error.Failure("Invalid number: " + e.getMessage()); + } catch (Exception e) { + throw new Error.UntrustedIdentity("Failed to send message: " + e.getMessage()); + } + } + // Since contact names might be empty if not defined, also potentially return // the profile name @Override @@ -408,8 +434,17 @@ public class DbusSignalImpl implements Signal { @Override public void setGroupBlocked(final byte[] groupId, final boolean blocked) { + GroupId group = null; try { - m.setGroupBlocked(getGroupId(groupId), blocked); + group = getGroupId(groupId); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } + try { + m.setGroupBlocked(group, blocked); } catch (GroupNotFoundException e) { throw new Error.GroupNotFound(e.getMessage()); } catch (IOException e) { @@ -427,11 +462,58 @@ public class DbusSignalImpl implements Signal { return ids; } + @Override + public byte[] getGroupId(String groupName) { + List groups = m.getGroups(); + for (Group group : groups) { + if (groupName.equals(group.getTitle())) { + return group.getGroupId().serialize(); + } + } + throw new Error.GroupNotFound(groupName); + } + + @Override + public List getGroupIdStrings() { + List groups = m.getGroups(); + List idStrings = new ArrayList<>(groups.size()); + for (Group group : groups) { + idStrings.add(group.getGroupId().toBase64()); + } + return idStrings; + } + + @Override + public String getGroupIdString(String groupName) { + List groups = m.getGroups(); + for (Group group : groups) { + if (groupName.equals(group.getTitle())) { + return group.getGroupId().toBase64(); + } + } + throw new Error.GroupNotFound(groupName); + } + + @Override + public List getGroupNames() { + List groups = m.getGroups(); + List groupNames = new ArrayList<>(groups.size()); + for (Group group : groups) { + groupNames.add(group.getTitle()); + } + return groupNames; + } + @Override public String getGroupName(final byte[] groupId) { - var group = m.getGroup(getGroupId(groupId)); - if (group == null || group.getTitle() == null) { - return ""; + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); } else { return group.getTitle(); } @@ -439,9 +521,14 @@ public class DbusSignalImpl implements Signal { @Override public List getGroupMembers(final byte[] groupId) { - var group = m.getGroup(getGroupId(groupId)); + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } if (group == null) { - return List.of(); + throw new Error.InvalidGroupId("GroupId is null."); } else { return group.getMembers().stream().map(RecipientAddress::getLegacyIdentifier).collect(Collectors.toList()); } @@ -487,6 +574,227 @@ public class DbusSignalImpl implements Signal { } } + @Override + public byte[] updateGroup( + byte[] groupId, + String name, + String description, + List addMembers, + List removeMembers, + List addAdmins, + List removeAdmins, + boolean resetGroupLink, + String groupLinkState, + String addMemberPermission, + String editDetailsPermission, + String avatar, + Integer expirationTimer + ) { + return updateGroup( + groupId, + name, + description, + addMembers, + removeMembers, + addAdmins, + removeAdmins, + resetGroupLink, + groupLinkState, + addMemberPermission, + editDetailsPermission, + avatar, + expirationTimer, + isGroupAnnounceOnly(groupId)); + } + + @Override + public byte[] updateGroup( + byte[] groupId, + String name, + String description, + List addMembers, + List removeMembers, + List addAdmins, + List removeAdmins, + boolean resetGroupLink, + String groupLinkState, + String addMemberPermission, + String editDetailsPermission, + String avatar, + Integer expirationTimer, + Boolean isAnnouncementGroup + ) { + try { + File avatarFile = null; + if (name.isEmpty()) { + name = null; + } + if (description.isEmpty()) { + description= null; + } + if (addMembers.isEmpty()) { + addMembers = null; + } + if (removeMembers.isEmpty()) { + removeMembers = null; + } + if (addAdmins.isEmpty()) { + addAdmins = null; + } + if (removeAdmins.isEmpty()) { + removeAdmins = null; + } + if (groupLinkState.isEmpty()) { + groupLinkState = null; + } + if (addMemberPermission.isEmpty()) { + addMemberPermission = null; + } + if (editDetailsPermission.isEmpty()) { + editDetailsPermission = null; + } + if (avatar.isEmpty()) { + avatarFile = null; + } else { + avatarFile = new File(avatar); + //TODO: check if we are sending an empty file. If so, this tells Signal + // to delete the avatar, so we should delete it from the local AvatarStore + long fileSize = avatarFile.length(); + if (fileSize == 0) { + try { + if (avatarFile.exists()) { + Files.delete(avatarFile.toPath()); + } + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } + } + } + + String localNumber = m.getSelfNumber(); + final var memberIdentifiers = getSingleRecipientIdentifiers(addMembers, localNumber); + if (groupId == null) { + final var results = m.createGroup(name, memberIdentifiers, avatar == null ? null : new File(avatar)); + checkSendMessageResults(results.second().getTimestamp(), results.second().getResults()); + return results.first().serialize(); + } else { + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } + + final var results = m.updateGroup(getGroupId(groupId), + name, + description, + getSingleRecipientIdentifiers(addMembers, localNumber), + getSingleRecipientIdentifiers(removeMembers, localNumber), + getSingleRecipientIdentifiers(addAdmins, localNumber), + getSingleRecipientIdentifiers(removeAdmins, localNumber), + resetGroupLink, + groupLinkState == null ? null : UpdateGroupCommand.getGroupLinkState(groupLinkState), + addMemberPermission == null ? null : UpdateGroupCommand.getGroupPermission(addMemberPermission), + editDetailsPermission == null ? null : UpdateGroupCommand.getGroupPermission(editDetailsPermission), + avatarFile, + expirationTimer, + isAnnouncementGroup + ); + if (results != null) { + checkSendMessageResults(results.getTimestamp(), results.getResults()); + } + return groupId; + } + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { + throw new Error.InvalidGroupId("Invalid group id: " + e.getMessage()); + } catch (UserErrorException e) { + throw new Error.InvalidNumber(e.getMessage()); + } catch (AttachmentInvalidException e) { + throw new Error.AttachmentInvalid(e.getMessage()); + } + } + + @Override + public String getGroupInviteUri(byte[] groupId) { + Group group = null; + GroupInviteLinkUrl groupInviteUri = null; + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } + groupInviteUri = group.getGroupInviteLinkUrl(); + if (groupInviteUri == null) { + return ""; + } + return groupInviteUri.getUrl(); + } + + @Override + public List getGroupPendingMembers(final byte[] groupId) { + Group group = null; + List members = new ArrayList<>(); + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } else { + for (RecipientAddress addr : group.getPendingMembers()) { + members.add(addr.getNumber().orElse("")); + } + return members; + } + } + + @Override + public List getGroupRequestingMembers(final byte[] groupId) { + Group group = null; + List members = new ArrayList<>(); + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } else { + for (RecipientAddress addr : group.getRequestingMembers()) { + members.add(addr.getNumber().orElse("")); + } + return members; + } + } + + @Override + public List getGroupAdminMembers(final byte[] groupId) { + Group group = null; + List members = new ArrayList<>(); + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } else { + for (RecipientAddress addr : group.getAdminMembers()) { + members.add(addr.getNumber().orElse("")); + } + return members; + } + } + @Override public boolean isRegistered() { return true; @@ -624,7 +932,15 @@ public class DbusSignalImpl implements Signal { @Override public void quitGroup(final byte[] groupId) { - var group = getGroupId(groupId); + GroupId group = null; + try { + group = getGroupId(groupId); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } try { m.quitGroup(group, Set.of()); } catch (GroupNotFoundException | NotAGroupMemberException e) { @@ -659,9 +975,14 @@ public class DbusSignalImpl implements Signal { @Override public boolean isGroupBlocked(final byte[] groupId) { - var group = m.getGroup(getGroupId(groupId)); + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } if (group == null) { - return false; + throw new Error.InvalidGroupId("GroupId is null."); } else { return group.isBlocked(); } @@ -669,9 +990,14 @@ public class DbusSignalImpl implements Signal { @Override public boolean isMember(final byte[] groupId) { - var group = m.getGroup(getGroupId(groupId)); + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } if (group == null) { - return false; + throw new Error.InvalidGroupId("GroupId is null."); } else { return group.isMember(); } @@ -689,6 +1015,166 @@ public class DbusSignalImpl implements Signal { } } + public List updateMembers(final byte[] groupId, Listmembers, boolean addToMembers) { + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } + var localNumber = m.getSelfNumber(); + if (addToMembers) { + var results = m.updateGroup(getGroupId(groupId), + null, + null, + getSingleRecipientIdentifiers(members, localNumber), + null, + null, + null, + false, + null, + null, + null, + null, + null, + isGroupAnnounceOnly(groupId) + ); + } else { + var results = m.updateGroup(getGroupId(groupId), + null, + null, + null, + getSingleRecipientIdentifiers(members, localNumber), + null, + null, + false, + null, + null, + null, + null, + null, + isGroupAnnounceOnly(groupId) + ); + } + return getGroupMembers(groupId); + } catch (NotAGroupMemberException | AssertionError | IOException | AttachmentInvalidException | GroupSendingNotAllowedException e) { + throw new Error.Failure(e.getMessage()); + } catch (GroupNotFoundException e) { + throw new Error.InvalidGroupId("Invalid group id: " + e.getMessage()); + } + } + + @Override + public void setGroupAnnounceOnly(byte[] groupId, boolean isAnnouncementGroup) { + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } + var results = m.updateGroup(getGroupId(groupId), + null, + null, + null, + null, + null, + null, + false, + null, + null, + null, + null, + null, + isAnnouncementGroup + ); + } catch (NotAGroupMemberException | AssertionError | IOException | AttachmentInvalidException | GroupSendingNotAllowedException e) { + throw new Error.Failure(e.getMessage()); + } catch (GroupNotFoundException e) { + throw new Error.InvalidGroupId("Invalid group id: " + e.getMessage()); + } + + } + + @Override + public boolean isGroupAnnounceOnly(byte[] groupId) { + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } else { + return group.isAnnouncementGroup(); + } + } + + @Override + public boolean isAdmin(final byte[] groupId) { + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + } catch (AssertionError e) { + throw new Error.Failure(e.getMessage()); + } + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } else { + return getGroupAdminMembers(groupId).contains(m.getSelfNumber()); + } + } + + @Override + public List updateAdmins(final byte[] groupId, Listadmins, boolean addToAdmins) { + Group group = null; + try { + group = m.getGroup(getGroupId(groupId)); + if (group == null) { + throw new Error.InvalidGroupId("GroupId is null."); + } + var localNumber = m.getSelfNumber(); + if (addToAdmins) { + var results = m.updateGroup(getGroupId(groupId), + null, + null, + null, + null, + getSingleRecipientIdentifiers(admins, localNumber), + null, + false, + null, + null, + null, + null, + null, + isGroupAnnounceOnly(groupId) + ); + } else { + var results = m.updateGroup(getGroupId(groupId), + null, + null, + null, + null, + null, + getSingleRecipientIdentifiers(admins, localNumber), + false, + null, + null, + null, + null, + null, + isGroupAnnounceOnly(groupId) + ); + } + return getGroupAdminMembers(groupId); + } catch (NotAGroupMemberException | AssertionError | IOException | AttachmentInvalidException | GroupSendingNotAllowedException e) { + throw new Error.Failure(e.getMessage()); + } catch (GroupNotFoundException e) { + throw new Error.GroupNotFound("Group not found: " + e.getMessage()); + } + } + private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException { var error = ErrorUtils.getErrorMessageFromSendMessageResult(result); @@ -823,17 +1309,23 @@ public class DbusSignalImpl implements Signal { } } - private void setDeviceName(String name) { - if (!device.isThisDevice()) { - throw new Error.Failure("Only the name of this device can be changed"); - } - try { - m.updateAccountAttributes(name); - // update device list - updateDevices(); - } catch (IOException e) { - throw new Error.Failure(e.getMessage()); + @Override + public String getDeviceName() { + return device.getName(); + } + + @Override + public void setDeviceName(String deviceName) { + if (device.isThisDevice()) { + try { + m.updateAccountAttributes(deviceName); + // update device list + updateDevices(); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } } + device.setName(deviceName); } } }