1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
4 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
6 import org
.asamk
.signal
.JsonWriter
;
7 import org
.asamk
.signal
.OutputWriter
;
8 import org
.asamk
.signal
.PlainTextWriter
;
9 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
10 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
11 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
12 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
13 import org
.asamk
.signal
.manager
.Manager
;
14 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
15 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
16 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
17 import org
.whispersystems
.signalservice
.internal
.push
.exceptions
.GroupPatchNotAcceptedException
;
19 import java
.io
.IOException
;
22 import static org
.asamk
.signal
.util
.ErrorUtils
.handleSendMessageResults
;
24 public class JoinGroupCommand
implements JsonRpcLocalCommand
{
27 public String
getName() {
32 public void attachToSubparser(final Subparser subparser
) {
33 subparser
.help("Join a group via an invitation link.");
34 subparser
.addArgument("--uri").required(true).help("Specify the uri with the group invitation link.");
38 public void handleCommand(
39 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
40 ) throws CommandException
{
41 final GroupInviteLinkUrl linkUrl
;
42 var uri
= ns
.getString("uri");
44 linkUrl
= GroupInviteLinkUrl
.fromUri(uri
);
45 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException e
) {
46 throw new UserErrorException("Group link is invalid: " + e
.getMessage());
47 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
48 throw new UserErrorException("Group link was created with an incompatible version: " + e
.getMessage());
51 if (linkUrl
== null) {
52 throw new UserErrorException("Link is not a signal group invitation link");
56 final var results
= m
.joinGroup(linkUrl
);
57 var newGroupId
= results
.first();
58 if (outputWriter
instanceof JsonWriter
) {
59 final var writer
= (JsonWriter
) outputWriter
;
60 if (!m
.getGroup(newGroupId
).isMember(m
.getSelfRecipientId())) {
61 writer
.write(Map
.of("groupId", newGroupId
.toBase64(), "onlyRequested", true));
63 writer
.write(Map
.of("groupId", newGroupId
.toBase64()));
66 final var writer
= (PlainTextWriter
) outputWriter
;
67 if (!m
.getGroup(newGroupId
).isMember(m
.getSelfRecipientId())) {
68 writer
.println("Requested to join group \"{}\"", newGroupId
.toBase64());
70 writer
.println("Joined group \"{}\"", newGroupId
.toBase64());
73 handleSendMessageResults(results
.second());
74 } catch (GroupPatchNotAcceptedException e
) {
75 throw new UserErrorException("Failed to join group, maybe already a member");
76 } catch (IOException e
) {
77 throw new IOErrorException("Failed to send message: " + e
.getMessage());
78 } catch (DBusExecutionException e
) {
79 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
80 } catch (GroupLinkNotActiveException e
) {
81 throw new UserErrorException("Group link is not valid: " + e
.getMessage());