mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Implement updateGroup
This commit is contained in:
parent
1689dfcb38
commit
fb862e4dde
4 changed files with 144 additions and 25 deletions
|
@ -2,9 +2,9 @@ package cli;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class GroupInfo {
|
public class GroupInfo {
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
|
@ -14,11 +14,15 @@ public class GroupInfo {
|
||||||
public String name;
|
public String name;
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
public List<String> members = new ArrayList<>();
|
public Set<String> members = new HashSet<>();
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
public long avatarId;
|
public long avatarId;
|
||||||
|
|
||||||
|
public GroupInfo(byte[] groupId) {
|
||||||
|
this.groupId = groupId;
|
||||||
|
}
|
||||||
|
|
||||||
public GroupInfo(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection<String> members, @JsonProperty("avatarId") long avatarId) {
|
public GroupInfo(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection<String> members, @JsonProperty("avatarId") long avatarId) {
|
||||||
this.groupId = groupId;
|
this.groupId = groupId;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMess
|
||||||
import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
|
import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
|
||||||
import org.whispersystems.textsecure.api.push.exceptions.NetworkFailureException;
|
import org.whispersystems.textsecure.api.push.exceptions.NetworkFailureException;
|
||||||
import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException;
|
import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException;
|
||||||
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
@ -99,14 +100,14 @@ public class Main {
|
||||||
try {
|
try {
|
||||||
GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group")));
|
GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group")));
|
||||||
if (g == null) {
|
if (g == null) {
|
||||||
System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": Unknown group");
|
System.err.println("Failed to send to group \"" + ns.getString("group") + "\": Unknown group");
|
||||||
System.err.println("Aborting sending.");
|
System.err.println("Aborting sending.");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
groupId = g.groupId;
|
groupId = g.groupId;
|
||||||
recipients = g.members;
|
recipients = new ArrayList<>(g.members);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage());
|
System.err.println("Failed to send to group \"" + ns.getString("group") + "\": " + e.getMessage());
|
||||||
System.err.println("Aborting sending.");
|
System.err.println("Aborting sending.");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
@ -123,11 +124,7 @@ public class Main {
|
||||||
textSecureAttachments = new ArrayList<>(attachments.size());
|
textSecureAttachments = new ArrayList<>(attachments.size());
|
||||||
for (String attachment : attachments) {
|
for (String attachment : attachments) {
|
||||||
try {
|
try {
|
||||||
File attachmentFile = new File(attachment);
|
textSecureAttachments.add(createAttachment(attachment));
|
||||||
InputStream attachmentStream = new FileInputStream(attachmentFile);
|
|
||||||
final long attachmentSize = attachmentFile.length();
|
|
||||||
String mime = Files.probeContentType(Paths.get(attachment));
|
|
||||||
textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null));
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
|
System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
|
||||||
System.err.println("Aborting sending.");
|
System.err.println("Aborting sending.");
|
||||||
|
@ -186,14 +183,82 @@ public class Main {
|
||||||
try {
|
try {
|
||||||
GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group")));
|
GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group")));
|
||||||
if (g == null) {
|
if (g == null) {
|
||||||
System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": Unknown group");
|
System.err.println("Failed to send to group \"" + ns.getString("group") + "\": Unknown group");
|
||||||
System.err.println("Aborting sending.");
|
System.err.println("Aborting sending.");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendQuitGroupMessage(m, g.members, g.groupId);
|
sendQuitGroupMessage(m, new ArrayList<>(g.members), g.groupId);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage());
|
System.err.println("Failed to send to group \"" + ns.getString("group") + "\": " + e.getMessage());
|
||||||
|
System.err.println("Aborting sending.");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "updateGroup":
|
||||||
|
if (!m.isRegistered()) {
|
||||||
|
System.err.println("User is not registered.");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
GroupInfo g;
|
||||||
|
if (ns.getString("group") != null) {
|
||||||
|
g = m.getGroupInfo(Base64.decode(ns.getString("group")));
|
||||||
|
if (g == null) {
|
||||||
|
System.err.println("Failed to send to group \"" + ns.getString("group") + "\": Unknown group");
|
||||||
|
System.err.println("Aborting sending.");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Create new group
|
||||||
|
g = new GroupInfo(Util.getSecretBytes(16));
|
||||||
|
g.members.add(m.getUsername());
|
||||||
|
System.out.println("Creating new group \"" + Base64.encodeBytes(g.groupId) + "\" …");
|
||||||
|
}
|
||||||
|
|
||||||
|
String name = ns.getString("name");
|
||||||
|
if (name != null) {
|
||||||
|
g.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<String> members = ns.getList("member");
|
||||||
|
|
||||||
|
if (members != null) {
|
||||||
|
for (String member : members) {
|
||||||
|
try {
|
||||||
|
g.members.add(m.canonicalizeNumber(member));
|
||||||
|
} catch (InvalidNumberException e) {
|
||||||
|
System.err.println("Failed to add member \"" + member + "\" to group: " + e.getMessage());
|
||||||
|
System.err.println("Aborting…");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TextSecureGroup.Builder group = TextSecureGroup.newBuilder(TextSecureGroup.Type.UPDATE)
|
||||||
|
.withId(g.groupId)
|
||||||
|
.withName(g.name)
|
||||||
|
.withMembers(new ArrayList<>(g.members));
|
||||||
|
|
||||||
|
String avatar = ns.getString("avatar");
|
||||||
|
if (avatar != null) {
|
||||||
|
try {
|
||||||
|
group.withAvatar(createAttachment(avatar));
|
||||||
|
// TODO
|
||||||
|
g.avatarId = 0;
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Failed to add attachment \"" + avatar + "\": " + e.getMessage());
|
||||||
|
System.err.println("Aborting sending.");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.setGroupInfo(g);
|
||||||
|
|
||||||
|
sendUpdateGroupMessage(m, group.build());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Failed to send to group \"" + ns.getString("group") + "\": " + e.getMessage());
|
||||||
System.err.println("Aborting sending.");
|
System.err.println("Aborting sending.");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
@ -204,6 +269,14 @@ public class Main {
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static TextSecureAttachmentStream createAttachment(String attachment) throws IOException {
|
||||||
|
File attachmentFile = new File(attachment);
|
||||||
|
InputStream attachmentStream = new FileInputStream(attachmentFile);
|
||||||
|
final long attachmentSize = attachmentFile.length();
|
||||||
|
String mime = Files.probeContentType(Paths.get(attachment));
|
||||||
|
return new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null);
|
||||||
|
}
|
||||||
|
|
||||||
private static Namespace parseArgs(String[] args) {
|
private static Namespace parseArgs(String[] args) {
|
||||||
ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
|
ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
|
||||||
.defaultHelp(true)
|
.defaultHelp(true)
|
||||||
|
@ -251,6 +324,17 @@ public class Main {
|
||||||
.required(true)
|
.required(true)
|
||||||
.help("Specify the recipient group ID.");
|
.help("Specify the recipient group ID.");
|
||||||
|
|
||||||
|
Subparser parserUpdateGroup = subparsers.addParser("updateGroup");
|
||||||
|
parserUpdateGroup.addArgument("-g", "--group")
|
||||||
|
.help("Specify the recipient group ID.");
|
||||||
|
parserUpdateGroup.addArgument("-n", "--name")
|
||||||
|
.help("Specify the new group name.");
|
||||||
|
parserUpdateGroup.addArgument("-a", "--avatar")
|
||||||
|
.help("Specify a new group avatar image file");
|
||||||
|
parserUpdateGroup.addArgument("-m", "--member")
|
||||||
|
.nargs("*")
|
||||||
|
.help("Specify one or more members to add to the group");
|
||||||
|
|
||||||
Subparser parserReceive = subparsers.addParser("receive");
|
Subparser parserReceive = subparsers.addParser("receive");
|
||||||
parserReceive.addArgument("-t", "--timeout")
|
parserReceive.addArgument("-t", "--timeout")
|
||||||
.type(int.class)
|
.type(int.class)
|
||||||
|
@ -298,7 +382,10 @@ public class Main {
|
||||||
|
|
||||||
private static void sendQuitGroupMessage(Manager m, List<String> recipients, byte[] groupId) {
|
private static void sendQuitGroupMessage(Manager m, List<String> recipients, byte[] groupId) {
|
||||||
final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder();
|
final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder();
|
||||||
TextSecureGroup group = TextSecureGroup.newBuilder(TextSecureGroup.Type.QUIT).withId(groupId).build();
|
TextSecureGroup group = TextSecureGroup.newBuilder(TextSecureGroup.Type.QUIT)
|
||||||
|
.withId(groupId)
|
||||||
|
.build();
|
||||||
|
|
||||||
messageBuilder.asGroupMessage(group);
|
messageBuilder.asGroupMessage(group);
|
||||||
|
|
||||||
TextSecureDataMessage message = messageBuilder.build();
|
TextSecureDataMessage message = messageBuilder.build();
|
||||||
|
@ -306,6 +393,16 @@ public class Main {
|
||||||
sendMessage(m, message, recipients);
|
sendMessage(m, message, recipients);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void sendUpdateGroupMessage(Manager m, TextSecureGroup g) {
|
||||||
|
final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder();
|
||||||
|
|
||||||
|
messageBuilder.asGroupMessage(g);
|
||||||
|
|
||||||
|
TextSecureDataMessage message = messageBuilder.build();
|
||||||
|
|
||||||
|
sendMessage(m, message, g.getMembers().get());
|
||||||
|
}
|
||||||
|
|
||||||
private static void sendMessage(Manager m, TextSecureDataMessage message, List<String> recipients) {
|
private static void sendMessage(Manager m, TextSecureDataMessage message, List<String> recipients) {
|
||||||
try {
|
try {
|
||||||
m.sendMessage(recipients, message);
|
m.sendMessage(recipients, message);
|
||||||
|
|
|
@ -44,9 +44,7 @@ import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
||||||
import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
|
import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
@ -248,7 +246,7 @@ class Manager {
|
||||||
TextSecureMessageSender messageSender = new TextSecureMessageSender(URL, TRUST_STORE, username, password,
|
TextSecureMessageSender messageSender = new TextSecureMessageSender(URL, TRUST_STORE, username, password,
|
||||||
axolotlStore, USER_AGENT, Optional.<TextSecureMessageSender.EventListener>absent());
|
axolotlStore, USER_AGENT, Optional.<TextSecureMessageSender.EventListener>absent());
|
||||||
|
|
||||||
List<TextSecureAddress> recipientsTS = new ArrayList<>(recipients.size());
|
Set<TextSecureAddress> recipientsTS = new HashSet<>(recipients.size());
|
||||||
for (String recipient : recipients) {
|
for (String recipient : recipients) {
|
||||||
try {
|
try {
|
||||||
recipientsTS.add(getPushAddress(recipient));
|
recipientsTS.add(getPushAddress(recipient));
|
||||||
|
@ -259,7 +257,7 @@ class Manager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
messageSender.sendMessage(recipientsTS, message);
|
messageSender.sendMessage(new ArrayList<>(recipientsTS), message);
|
||||||
|
|
||||||
if (message.isEndSession()) {
|
if (message.isEndSession()) {
|
||||||
for (TextSecureAddress recipient : recipientsTS) {
|
for (TextSecureAddress recipient : recipientsTS) {
|
||||||
|
@ -309,20 +307,32 @@ class Manager {
|
||||||
TextSecureGroup groupInfo = message.getGroupInfo().get();
|
TextSecureGroup groupInfo = message.getGroupInfo().get();
|
||||||
switch (groupInfo.getType()) {
|
switch (groupInfo.getType()) {
|
||||||
case UPDATE:
|
case UPDATE:
|
||||||
long avatarId = 0;
|
group = groupStore.getGroup(groupInfo.getGroupId());
|
||||||
|
if (group == null) {
|
||||||
|
group = new GroupInfo(groupInfo.getGroupId());
|
||||||
|
}
|
||||||
|
|
||||||
if (groupInfo.getAvatar().isPresent()) {
|
if (groupInfo.getAvatar().isPresent()) {
|
||||||
TextSecureAttachment avatar = groupInfo.getAvatar().get();
|
TextSecureAttachment avatar = groupInfo.getAvatar().get();
|
||||||
if (avatar.isPointer()) {
|
if (avatar.isPointer()) {
|
||||||
avatarId = avatar.asPointer().getId();
|
long avatarId = avatar.asPointer().getId();
|
||||||
try {
|
try {
|
||||||
retrieveAttachment(avatar.asPointer());
|
retrieveAttachment(avatar.asPointer());
|
||||||
|
group.avatarId = avatarId;
|
||||||
} catch (IOException | InvalidMessageException e) {
|
} catch (IOException | InvalidMessageException e) {
|
||||||
System.err.println("Failed to retrieve group avatar (" + avatarId + "): " + e.getMessage());
|
System.err.println("Failed to retrieve group avatar (" + avatarId + "): " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
group = new GroupInfo(groupInfo.getGroupId(), groupInfo.getName().get(), groupInfo.getMembers().get(), avatarId);
|
if (groupInfo.getName().isPresent()) {
|
||||||
|
group.name = groupInfo.getName().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (groupInfo.getMembers().isPresent()) {
|
||||||
|
group.members.addAll(groupInfo.getMembers().get());
|
||||||
|
}
|
||||||
|
|
||||||
groupStore.updateGroup(group);
|
groupStore.updateGroup(group);
|
||||||
break;
|
break;
|
||||||
case DELIVER:
|
case DELIVER:
|
||||||
|
@ -419,7 +429,7 @@ class Manager {
|
||||||
return outputFile;
|
return outputFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String canonicalizeNumber(String number) throws InvalidNumberException {
|
public String canonicalizeNumber(String number) throws InvalidNumberException {
|
||||||
String localNumber = username;
|
String localNumber = username;
|
||||||
return PhoneNumberFormatter.formatNumber(number, localNumber);
|
return PhoneNumberFormatter.formatNumber(number, localNumber);
|
||||||
}
|
}
|
||||||
|
@ -432,4 +442,12 @@ class Manager {
|
||||||
public GroupInfo getGroupInfo(byte[] groupId) {
|
public GroupInfo getGroupInfo(byte[] groupId) {
|
||||||
return groupStore.getGroup(groupId);
|
return groupStore.getGroup(groupId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setGroupInfo(GroupInfo group) {
|
||||||
|
groupStore.updateGroup(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ class Util {
|
||||||
return Base64.encodeBytes(secret);
|
return Base64.encodeBytes(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] getSecretBytes(int size) {
|
public static byte[] getSecretBytes(int size) {
|
||||||
byte[] secret = new byte[size];
|
byte[] secret = new byte[size];
|
||||||
getSecureRandom().nextBytes(secret);
|
getSecureRandom().nextBytes(secret);
|
||||||
return secret;
|
return secret;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue