Add blocking/unblocking for groups

This commit is contained in:
Daniel Schäufele 2020-01-06 23:55:24 +01:00
parent 5ab3152a40
commit ff478a5b0a
8 changed files with 116 additions and 62 deletions

View file

@ -26,6 +26,8 @@ public interface Signal extends DBusInterface {
void setContactBlocked(String number, boolean blocked); void setContactBlocked(String number, boolean blocked);
void setGroupBlocked(byte[] groupId, boolean blocked) throws GroupNotFoundException;
List<byte[]> getGroupIds(); List<byte[]> getGroupIds();
String getGroupName(byte[] groupId); String getGroupName(byte[] groupId);

View file

@ -0,0 +1,47 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.GroupIdFormatException;
import org.asamk.signal.GroupNotFoundException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.Util;
public class BlockCommand implements LocalCommand {
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("contact")
.help("Contact number")
.nargs("*");
subparser.addArgument("-g", "--group")
.help("Group ID")
.nargs("*");
subparser.help("Block the given contacts or groups (no messages will be received)");
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
if (!m.isRegistered()) {
System.err.println("User is not registered.");
return 1;
}
for (String contact_number : ns.<String>getList("contact")) {
m.setContactBlocked(contact_number, true);
}
if(ns.<String>getList("group") != null) {
for (String groupIdString : ns.<String>getList("group")) {
try {
byte[] groupId = Util.decodeGroupId(groupIdString);
m.setGroupBlocked(groupId, true);
} catch (GroupIdFormatException | GroupNotFoundException e) {
System.err.println(e.getMessage());
}
}
}
return 0;
}
}

View file

@ -1,29 +0,0 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.manager.Manager;
public class BlockContactCommand implements LocalCommand {
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("number")
.help("Contact number");
subparser.help("Block the given contact (no messages will be received)");
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
if (!m.isRegistered()) {
System.err.println("User is not registered.");
return 1;
}
String number = ns.getString("number");
m.setContactBlocked(number, true);
return 0;
}
}

View file

@ -9,7 +9,7 @@ public class Commands {
static { static {
addCommand("addDevice", new AddDeviceCommand()); addCommand("addDevice", new AddDeviceCommand());
addCommand("blockContact", new BlockContactCommand()); addCommand("block", new BlockCommand());
addCommand("daemon", new DaemonCommand()); addCommand("daemon", new DaemonCommand());
addCommand("link", new LinkCommand()); addCommand("link", new LinkCommand());
addCommand("listContacts", new ListContactsCommand()); addCommand("listContacts", new ListContactsCommand());
@ -26,7 +26,7 @@ public class Commands {
addCommand("updateContact", new UpdateContactCommand()); addCommand("updateContact", new UpdateContactCommand());
addCommand("setPin", new SetPinCommand()); addCommand("setPin", new SetPinCommand());
addCommand("trust", new TrustCommand()); addCommand("trust", new TrustCommand());
addCommand("unblockContact", new UnblockContactCommand()); addCommand("unblock", new UnblockCommand());
addCommand("unregister", new UnregisterCommand()); addCommand("unregister", new UnregisterCommand());
addCommand("updateAccount", new UpdateAccountCommand()); addCommand("updateAccount", new UpdateAccountCommand());
addCommand("updateGroup", new UpdateGroupCommand()); addCommand("updateGroup", new UpdateGroupCommand());

View file

@ -0,0 +1,47 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.GroupIdFormatException;
import org.asamk.signal.GroupNotFoundException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.Util;
public class UnblockCommand implements LocalCommand {
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("contact")
.help("Contact number")
.nargs("*");
subparser.addArgument("-g", "--group")
.help("Group ID")
.nargs("*");
subparser.help("Unblock the given contacts or groups (messages will be received again)");
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
if (!m.isRegistered()) {
System.err.println("User is not registered.");
return 1;
}
for (String contact_number : ns.<String>getList("contact")) {
m.setContactBlocked(contact_number, false);
}
if(ns.<String>getList("group") != null) {
for (String groupIdString : ns.<String>getList("group")) {
try {
byte[] groupId = Util.decodeGroupId(groupIdString);
m.setGroupBlocked(groupId, false);
} catch (GroupIdFormatException | GroupNotFoundException e) {
System.err.println(e.getMessage());
}
}
}
return 0;
}
}

View file

@ -1,29 +0,0 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.manager.Manager;
public class UnblockContactCommand implements LocalCommand {
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("number")
.help("Contact number");
subparser.help("Unblock the given contact (messages will be received again)");
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
if (!m.isRegistered()) {
System.err.println("User is not registered.");
return 1;
}
String number = ns.getString("number");
m.setContactBlocked(number, false);
return 0;
}
}

View file

@ -701,6 +701,19 @@ public class Manager implements Signal {
account.save(); account.save();
} }
@Override
public void setGroupBlocked(final byte[] groupId, final boolean blocked) throws GroupNotFoundException {
GroupInfo group = getGroup(groupId);
if (group == null) {
throw new GroupNotFoundException(groupId);
} else {
System.err.println((blocked ? "Blocking" : "Unblocking") + " group " + Base64.encodeBytes(groupId));
group.blocked = blocked;
account.getGroupStore().updateGroup(group);
account.save();
}
}
@Override @Override
public List<byte[]> getGroupIds() { public List<byte[]> getGroupIds() {
List<GroupInfo> groups = getGroups(); List<GroupInfo> groups = getGroups();
@ -1441,7 +1454,7 @@ public class Manager implements Signal {
out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name), out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
new ArrayList<>(record.getMembers()), createGroupAvatarAttachment(record.groupId), new ArrayList<>(record.getMembers()), createGroupAvatarAttachment(record.groupId),
record.active, Optional.fromNullable(info != null ? info.messageExpirationTime : null), record.active, Optional.fromNullable(info != null ? info.messageExpirationTime : null),
Optional.fromNullable(record.color), false)); Optional.fromNullable(record.color), record.blocked));
} }
} }

View file

@ -23,6 +23,8 @@ public class GroupInfo {
public boolean active; public boolean active;
@JsonProperty @JsonProperty
public String color; public String color;
@JsonProperty(defaultValue = "false")
public boolean blocked;
private long avatarId; private long avatarId;
@ -30,12 +32,13 @@ public class GroupInfo {
this.groupId = groupId; this.groupId = groupId;
} }
public GroupInfo(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection<String> members, @JsonProperty("avatarId") long avatarId, @JsonProperty("color") String color) { public GroupInfo(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection<String> members, @JsonProperty("avatarId") long avatarId, @JsonProperty("color") String color, @JsonProperty("blocked") boolean blocked) {
this.groupId = groupId; this.groupId = groupId;
this.name = name; this.name = name;
this.members.addAll(members); this.members.addAll(members);
this.avatarId = avatarId; this.avatarId = avatarId;
this.color = color; this.color = color;
this.blocked = blocked;
} }
@JsonIgnore @JsonIgnore