Add command to retrieve avatars and stickers

Fixes #1125
This commit is contained in:
AsamK 2024-02-09 22:06:46 +01:00
parent d486563099
commit 7cf3a989bf
13 changed files with 296 additions and 3 deletions

View file

@ -17,6 +17,8 @@ public class Commands {
addCommand(new FinishChangeNumberCommand());
addCommand(new FinishLinkCommand());
addCommand(new GetAttachmentCommand());
addCommand(new GetAvatarCommand());
addCommand(new GetStickerCommand());
addCommand(new GetUserStatusCommand());
addCommand(new AddStickerPackCommand());
addCommand(new JoinGroupCommand());

View file

@ -26,6 +26,7 @@ public class GetAttachmentCommand implements JsonRpcLocalCommand {
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.help("Retrieve an already downloaded attachment base64 encoded.");
subparser.addArgument("--id").required(true).help("The ID of the attachment file.");
var mut = subparser.addMutuallyExclusiveGroup().required(true);
mut.addArgument("--recipient").help("Sender of the attachment");

View file

@ -0,0 +1,76 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.json.JsonAttachmentData;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.CommandUtil;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
public class GetAvatarCommand implements JsonRpcLocalCommand {
@Override
public String getName() {
return "getAvatar";
}
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.help("Retrieve the avatar of a contact, contact's profile or group base64 encoded.");
var mut = subparser.addMutuallyExclusiveGroup().required(true);
mut.addArgument("-c", "--contact").help("Get a contact avatar");
mut.addArgument("-p", "--profile").help("Get a profile avatar");
mut.addArgument("-g", "--group-id").help("Get a group avatar");
}
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var contactRecipient = ns.getString("contact");
final var profileRecipient = ns.getString("profile");
final var groupId = ns.getString("groupId");
final InputStream data;
try {
if (contactRecipient != null) {
data = m.retrieveContactAvatar(CommandUtil.getSingleRecipientIdentifier(contactRecipient,
m.getSelfNumber()));
} else if (profileRecipient != null) {
data = m.retrieveProfileAvatar(CommandUtil.getSingleRecipientIdentifier(profileRecipient,
m.getSelfNumber()));
} else {
data = m.retrieveGroupAvatar(CommandUtil.getGroupId(groupId));
}
} catch (FileNotFoundException ex) {
throw new UserErrorException("Could not find avatar", ex);
} catch (IOException ex) {
throw new UnexpectedErrorException("An error occurred reading avatar", ex);
} catch (UnregisteredRecipientException e) {
throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
}
try (data) {
final var bytes = data.readAllBytes();
final var base64 = Base64.getEncoder().encodeToString(bytes);
switch (outputWriter) {
case PlainTextWriter writer -> writer.println(base64);
case JsonWriter writer -> writer.write(new JsonAttachmentData(base64));
}
} catch (IOException ex) {
throw new UnexpectedErrorException("An error occurred reading avatar", ex);
}
}
}

View file

@ -0,0 +1,60 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.json.JsonAttachmentData;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.Hex;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
public class GetStickerCommand implements JsonRpcLocalCommand {
@Override
public String getName() {
return "getSticker";
}
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.help("Retrieve the sticker of a sticker pack base64 encoded.");
subparser.addArgument("--pack-id").required(true).help("The ID of the sticker pack.");
subparser.addArgument("--sticker-id").type(int.class).required(true).help("The ID of the sticker.");
}
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var packId = StickerPackId.deserialize(Hex.toByteArray(ns.getString("pack-id")));
final var stickerId = ns.getInt("sticker-id");
try (InputStream data = m.retrieveSticker(packId, stickerId)) {
final var bytes = data.readAllBytes();
final var base64 = Base64.getEncoder().encodeToString(bytes);
switch (outputWriter) {
case PlainTextWriter writer -> writer.println(base64);
case JsonWriter writer -> writer.write(new JsonAttachmentData(base64));
}
} catch (FileNotFoundException ex) {
throw new UserErrorException("Could not find sticker with ID: " + stickerId + " in pack " + packId, ex);
} catch (IOException ex) {
throw new UnexpectedErrorException("An error occurred reading sticker with ID: "
+ stickerId
+ " in pack "
+ packId, ex);
}
}
}

View file

@ -38,6 +38,7 @@ 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.StickerPack;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
@ -1069,6 +1070,26 @@ public class DbusManagerImpl implements Manager {
throw new UnsupportedOperationException();
}
@Override
public InputStream retrieveContactAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException {
throw new UnsupportedOperationException();
}
@Override
public InputStream retrieveProfileAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException {
throw new UnsupportedOperationException();
}
@Override
public InputStream retrieveGroupAvatar(final GroupId groupId) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public InputStream retrieveSticker(final StickerPackId stickerPackId, final int stickerId) throws IOException {
throw new UnsupportedOperationException();
}
@SuppressWarnings("unchecked")
private <T> T getValue(
final Map<String, Variant<?>> stringVariantMap, final String field