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(
41 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
42 ) throws CommandException
{
45 linkUri
= new URI(ns
.getString("uri"));
46 } catch (URISyntaxException e
) {
47 throw new UserErrorException("Device link uri has invalid format: " + e
.getMessage());
51 var deviceLinkUrl
= DeviceLinkUrl
.parseDeviceLinkUri(linkUri
);
52 m
.addDeviceLink(deviceLinkUrl
);
53 } catch (IOException e
) {
54 logger
.error("Add device link failed: {}", e
.getMessage());
55 throw new IOErrorException("Add device link failed", e
);
56 } catch (InvalidDeviceLinkException e
) {
57 logger
.info("Invalid device link");
58 throw new UserErrorException("Invalid device link", e
);
59 } catch (DeviceLimitExceededException e
) {
60 throw new UserErrorException("Account has too many linked devices already", e
);
61 } catch (NotPrimaryDeviceException e
) {
62 throw new UserErrorException("This command doesn't work on linked devices.");