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
.commands
.exceptions
.CommandException
;
7 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
8 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
9 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
10 import org
.asamk
.signal
.manager
.Manager
;
11 import org
.asamk
.signal
.manager
.api
.InactiveGroupLinkException
;
12 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
13 import org
.asamk
.signal
.output
.JsonWriter
;
14 import org
.asamk
.signal
.output
.OutputWriter
;
15 import org
.asamk
.signal
.output
.PlainTextWriter
;
16 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
18 import java
.io
.IOException
;
21 import static org
.asamk
.signal
.util
.ErrorUtils
.handleSendMessageResults
;
23 public class JoinGroupCommand
implements JsonRpcLocalCommand
{
26 public String
getName() {
31 public void attachToSubparser(final Subparser subparser
) {
32 subparser
.help("Join a group via an invitation link.");
33 subparser
.addArgument("--uri").required(true).help("Specify the uri with the group invitation link.");
37 public void handleCommand(
38 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
39 ) throws CommandException
{
40 final GroupInviteLinkUrl linkUrl
;
41 var uri
= ns
.getString("uri");
43 linkUrl
= GroupInviteLinkUrl
.fromUri(uri
);
44 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException e
) {
45 throw new UserErrorException("Group link is invalid: " + e
.getMessage());
46 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
47 throw new UserErrorException("Group link was created with an incompatible version: " + e
.getMessage());
50 if (linkUrl
== null) {
51 throw new UserErrorException("Link is not a signal group invitation link");
55 final var results
= m
.joinGroup(linkUrl
);
56 var newGroupId
= results
.first();
57 if (outputWriter
instanceof JsonWriter writer
) {
58 if (!m
.getGroup(newGroupId
).isMember()) {
59 writer
.write(Map
.of("groupId", newGroupId
.toBase64(), "onlyRequested", true));
61 writer
.write(Map
.of("groupId", newGroupId
.toBase64()));
64 final var writer
= (PlainTextWriter
) outputWriter
;
65 if (!m
.getGroup(newGroupId
).isMember()) {
66 writer
.println("Requested to join group \"{}\"", newGroupId
.toBase64());
68 writer
.println("Joined group \"{}\"", newGroupId
.toBase64());
71 handleSendMessageResults(results
.second().results());
72 } catch (IOException e
) {
73 throw new IOErrorException("Failed to send message: "
76 + e
.getClass().getSimpleName()
78 } catch (DBusExecutionException e
) {
79 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
80 .getSimpleName() + ")", e
);
81 } catch (InactiveGroupLinkException e
) {
82 throw new UserErrorException("Group link is not valid: " + e
.getMessage());