1 package org
.asamk
.signal
.commands
;
3 import com
.fasterxml
.jackson
.annotation
.JsonInclude
;
5 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
6 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
7 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
9 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
10 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
11 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
12 import org
.asamk
.signal
.manager
.Manager
;
13 import org
.asamk
.signal
.manager
.api
.InvalidUsernameException
;
14 import org
.asamk
.signal
.output
.JsonWriter
;
15 import org
.asamk
.signal
.output
.OutputWriter
;
16 import org
.asamk
.signal
.output
.PlainTextWriter
;
18 import java
.io
.IOException
;
20 public class UpdateAccountCommand
implements JsonRpcLocalCommand
{
23 public String
getName() {
24 return "updateAccount";
28 public void attachToSubparser(final Subparser subparser
) {
29 subparser
.help("Update the account attributes on the signal server.");
30 subparser
.addArgument("-n", "--device-name").help("Specify a name to describe this device.");
31 var mut
= subparser
.addMutuallyExclusiveGroup();
32 mut
.addArgument("-u", "--username").help("Specify a username that can then be used to contact this account.");
33 mut
.addArgument("--delete-username")
34 .action(Arguments
.storeTrue())
35 .help("Delete the username associated with this account.");
39 public void handleCommand(
40 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
41 ) throws CommandException
{
42 var deviceName
= ns
.getString("device-name");
44 m
.updateAccountAttributes(deviceName
);
45 } catch (IOException e
) {
46 throw new IOErrorException("UpdateAccount error: " + e
.getMessage(), e
);
49 var username
= ns
.getString("username");
50 if (username
!= null) {
52 m
.setUsername(username
);
53 final var newUsername
= m
.getUsername();
54 final var newUsernameLink
= m
.getUsernameLink();
55 switch (outputWriter
) {
56 case PlainTextWriter w
-> w
.println("Your new username: {} ({})",
58 newUsernameLink
== null ?
"-" : newUsernameLink
.getUrl());
59 case JsonWriter w
-> w
.write(new JsonAccountResponse(newUsername
,
60 newUsernameLink
== null ?
null : newUsernameLink
.getUrl()));
62 } catch (IOException e
) {
63 throw new IOErrorException("Failed to set username: " + e
.getMessage(), e
);
64 } catch (InvalidUsernameException e
) {
65 throw new UserErrorException("Invalid username: " + e
.getMessage(), e
);
69 var deleteUsername
= Boolean
.TRUE
.equals(ns
.getBoolean("delete-username"));
73 } catch (IOException e
) {
74 throw new IOErrorException("Failed to delete username: " + e
.getMessage(), e
);
79 private record JsonAccountResponse(
80 @JsonInclude(JsonInclude
.Include
.NON_NULL
) String username
,
81 @JsonInclude(JsonInclude
.Include
.NON_NULL
) String usernameLink