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
.DeviceLinkUrl
;
11 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
12 import org
.asamk
.signal
.manager
.api
.NotPrimaryDeviceException
;
13 import org
.asamk
.signal
.output
.OutputWriter
;
14 import org
.slf4j
.Logger
;
15 import org
.slf4j
.LoggerFactory
;
17 import java
.io
.IOException
;
19 import java
.net
.URISyntaxException
;
21 public class AddDeviceCommand
implements JsonRpcLocalCommand
{
23 private static final Logger logger
= LoggerFactory
.getLogger(AddDeviceCommand
.class);
26 public String
getName() {
31 public void attachToSubparser(final Subparser subparser
) {
32 subparser
.help("Link another device to this device. Only works, if this is the primary device.");
33 subparser
.addArgument("--uri")
35 .help("Specify the uri contained in the QR code shown by the new device.");
39 public void handleCommand(
40 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
41 ) throws CommandException
{
44 linkUri
= new URI(ns
.getString("uri"));
45 } catch (URISyntaxException e
) {
46 throw new UserErrorException("Device link uri has invalid format: " + e
.getMessage());
50 var deviceLinkUrl
= DeviceLinkUrl
.parseDeviceLinkUri(linkUri
);
51 m
.addDeviceLink(deviceLinkUrl
);
52 } catch (IOException e
) {
53 logger
.error("Add device link failed: {}", e
.getMessage());
54 throw new IOErrorException("Add device link failed", e
);
55 } catch (InvalidDeviceLinkException e
) {
56 logger
.error("Invalid device link");
57 throw new UserErrorException("Invalid device link", e
);
58 } catch (NotPrimaryDeviceException e
) {
59 throw new UserErrorException("This command doesn't work on linked devices.");