Prevent sending to groups that the user has quit

Fixes #23
This commit is contained in:
AsamK 2016-08-12 18:24:30 +02:00
parent ee5062a2cc
commit 1efdf04394
4 changed files with 46 additions and 13 deletions

View file

@ -5,7 +5,6 @@ import org.asamk.signal.GroupNotFoundException;
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusSignal;
import org.freedesktop.dbus.exceptions.DBusException;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
import java.io.IOException;

View file

@ -310,6 +310,9 @@ public class Main {
} catch (GroupNotFoundException e) {
handleGroupNotFoundException(e);
return 1;
} catch (NotAGroupMemberException e) {
handleNotAGroupMemberException(e);
return 1;
} catch (AttachmentInvalidException e) {
System.err.println("Failed to add attachment: " + e.getMessage());
System.err.println("Aborting sending.");
@ -401,6 +404,9 @@ public class Main {
} catch (GroupNotFoundException e) {
handleGroupNotFoundException(e);
return 1;
} catch (NotAGroupMemberException e) {
handleNotAGroupMemberException(e);
return 1;
}
break;
@ -433,6 +439,9 @@ public class Main {
} catch (GroupNotFoundException e) {
handleGroupNotFoundException(e);
return 1;
} catch (NotAGroupMemberException e) {
handleNotAGroupMemberException(e);
return 1;
} catch (EncapsulatedExceptions e) {
handleEncapsulatedExceptions(e);
return 3;
@ -553,6 +562,13 @@ public class Main {
System.err.println("Aborting sending.");
}
private static void handleNotAGroupMemberException(NotAGroupMemberException e) {
System.err.println("Failed to send to group: " + e.getMessage());
System.err.println("Update the group on another device to readd the user to this group.");
System.err.println("Aborting sending.");
}
private static void handleDBusExecutionException(DBusExecutionException e) {
System.err.println("Cannot connect to dbus: " + e.getMessage());
System.err.println("Aborting.");

View file

@ -515,6 +515,19 @@ class Manager implements Signal {
return Optional.of(createAttachment(file));
}
private GroupInfo getGroupForSending(byte[] groupId) throws GroupNotFoundException, NotAGroupMemberException {
GroupInfo g = groupStore.getGroup(groupId);
if (g == null) {
throw new GroupNotFoundException(groupId);
}
for (String member : g.members) {
if (member.equals(this.username)) {
return g;
}
}
throw new NotAGroupMemberException(groupId, g.name);
}
@Override
public void sendGroupMessage(String messageText, List<String> attachments,
byte[] groupId)
@ -531,10 +544,7 @@ class Manager implements Signal {
}
SignalServiceDataMessage message = messageBuilder.build();
GroupInfo g = groupStore.getGroup(groupId);
if (g == null) {
throw new GroupNotFoundException(groupId);
}
final GroupInfo g = getGroupForSending(groupId);
// Don't send group message to ourself
final List<String> membersSend = new ArrayList<>(g.members);
@ -551,10 +561,7 @@ class Manager implements Signal {
.asGroupMessage(group)
.build();
final GroupInfo g = groupStore.getGroup(groupId);
if (g == null) {
throw new GroupNotFoundException(groupId);
}
final GroupInfo g = getGroupForSending(groupId);
g.members.remove(this.username);
groupStore.updateGroup(g);
@ -568,10 +575,7 @@ class Manager implements Signal {
g = new GroupInfo(Util.getSecretBytes(16));
g.members.add(username);
} else {
g = groupStore.getGroup(groupId);
if (g == null) {
throw new GroupNotFoundException(groupId);
}
g = getGroupForSending(groupId);
}
if (name != null) {

View file

@ -0,0 +1,14 @@
package org.asamk.signal;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
public class NotAGroupMemberException extends DBusExecutionException {
public NotAGroupMemberException(String message) {
super(message);
}
public NotAGroupMemberException(byte[] groupId, String groupName) {
super("User is not a member in group: " + groupName + " (" + Base64.encodeBytes(groupId) + ")");
}
}