1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
8 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
9 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
10 import org
.asamk
.signal
.manager
.Manager
;
11 import org
.asamk
.signal
.manager
.api
.InvalidUsernameException
;
12 import org
.asamk
.signal
.output
.JsonWriter
;
13 import org
.asamk
.signal
.output
.OutputWriter
;
14 import org
.asamk
.signal
.output
.PlainTextWriter
;
16 import java
.io
.IOException
;
18 public class UpdateAccountCommand
implements JsonRpcLocalCommand
{
21 public String
getName() {
22 return "updateAccount";
26 public void attachToSubparser(final Subparser subparser
) {
27 subparser
.help("Update the account attributes on the signal server.");
28 subparser
.addArgument("-n", "--device-name").help("Specify a name to describe this device.");
29 var mut
= subparser
.addMutuallyExclusiveGroup();
30 mut
.addArgument("-u", "--username").help("Specify a username that can then be used to contact this account.");
31 mut
.addArgument("--delete-username")
32 .action(Arguments
.storeTrue())
33 .help("Delete the username associated with this account.");
37 public void handleCommand(
38 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
39 ) throws CommandException
{
40 var deviceName
= ns
.getString("device-name");
42 m
.updateAccountAttributes(deviceName
);
43 } catch (IOException e
) {
44 throw new IOErrorException("UpdateAccount error: " + e
.getMessage(), e
);
47 var username
= ns
.getString("username");
48 if (username
!= null) {
50 final var newUsername
= m
.setUsername(username
);
51 if (outputWriter
instanceof PlainTextWriter w
) {
52 w
.println("Your new username: {}", newUsername
);
53 } else if (outputWriter
instanceof JsonWriter w
) {
54 w
.write(new JsonAccountResponse(newUsername
));
56 } catch (IOException e
) {
57 throw new IOErrorException("Failed to set username: " + e
.getMessage(), e
);
58 } catch (InvalidUsernameException e
) {
59 throw new UserErrorException("Invalid username: " + e
.getMessage(), e
);
63 var deleteUsername
= Boolean
.TRUE
.equals(ns
.getBoolean("delete-username"));
67 } catch (IOException e
) {
68 throw new IOErrorException("Failed to delete username: " + e
.getMessage(), e
);
73 private record JsonAccountResponse(