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
.UserErrorException
;
9 import org
.asamk
.signal
.manager
.Manager
;
10 import org
.asamk
.signal
.manager
.api
.DeviceLimitExceededException
;
11 import org
.asamk
.signal
.manager
.api
.DeviceLinkUrl
;
12 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
13 import org
.asamk
.signal
.manager
.api
.NotPrimaryDeviceException
;
14 import org
.asamk
.signal
.output
.OutputWriter
;
15 import org
.slf4j
.Logger
;
16 import org
.slf4j
.LoggerFactory
;
18 import java
.io
.IOException
;
20 import java
.net
.URISyntaxException
;
22 public class AddDeviceCommand
implements JsonRpcLocalCommand
{
24 private static final Logger logger
= LoggerFactory
.getLogger(AddDeviceCommand
.class);
27 public String
getName() {
32 public void attachToSubparser(final Subparser subparser
) {
33 subparser
.help("Link another device to this device. Only works, if this is the primary device.");
34 subparser
.addArgument("--uri")
36 .help("Specify the uri contained in the QR code shown by the new device.");
40 public void handleCommand(
43 final OutputWriter outputWriter
44 ) throws CommandException
{
47 linkUri
= new URI(ns
.getString("uri"));
48 } catch (URISyntaxException e
) {
49 throw new UserErrorException("Device link uri has invalid format: " + e
.getMessage());
53 var deviceLinkUrl
= DeviceLinkUrl
.parseDeviceLinkUri(linkUri
);
54 m
.addDeviceLink(deviceLinkUrl
);
55 } catch (IOException e
) {
56 logger
.error("Add device link failed: {}", e
.getMessage());
57 throw new IOErrorException("Add device link failed", e
);
58 } catch (InvalidDeviceLinkException e
) {
59 logger
.info("Invalid device link");
60 throw new UserErrorException("Invalid device link", e
);
61 } catch (DeviceLimitExceededException e
) {
62 throw new UserErrorException("Account has too many linked devices already", e
);
63 } catch (NotPrimaryDeviceException e
) {
64 throw new UserErrorException("This command doesn't work on linked devices.");