]>
nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/LinkCommand.java
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
.PlainTextWriterImpl
;
7 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
8 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
9 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
10 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
11 import org
.asamk
.signal
.manager
.ProvisioningManager
;
12 import org
.asamk
.signal
.manager
.UserAlreadyExists
;
13 import org
.slf4j
.Logger
;
14 import org
.slf4j
.LoggerFactory
;
15 import org
.whispersystems
.libsignal
.InvalidKeyException
;
17 import java
.io
.IOException
;
18 import java
.util
.concurrent
.TimeoutException
;
20 public class LinkCommand
implements ProvisioningCommand
{
22 private final static Logger logger
= LoggerFactory
.getLogger(LinkCommand
.class);
25 public void attachToSubparser(final Subparser subparser
) {
26 subparser
.addArgument("-n", "--name").help("Specify a name to describe this new device.");
30 public void handleCommand(final Namespace ns
, final ProvisioningManager m
) throws CommandException
{
31 final var writer
= new PlainTextWriterImpl(System
.out
);
33 var deviceName
= ns
.getString("name");
34 if (deviceName
== null) {
38 writer
.println("{}", m
.getDeviceLinkUri());
39 try (var manager
= m
.finishDeviceLink(deviceName
)) {
40 writer
.println("Associated with: {}", manager
.getUsername());
42 } catch (TimeoutException e
) {
43 throw new UserErrorException("Link request timed out, please try again.");
44 } catch (IOException e
) {
45 throw new IOErrorException("Link request error: " + e
.getMessage());
46 } catch (InvalidKeyException e
) {
47 logger
.debug("Finish device link failed", e
);
48 throw new UnexpectedErrorException("Invalid key: " + e
.getMessage());
49 } catch (UserAlreadyExists e
) {
50 throw new UserErrorException("The user "
52 + " already exists\nDelete \""
54 + "\" before trying again.");