| 1 |
package backend |
| 2 |
|
| 3 |
import ( |
| 4 |
"flag" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"github.com/henrybear327/go-proton-api" |
| 8 |
) |
| 9 |
|
| 10 |
func (s *Backend) RunQuarkCommand(command string, args ...string) (any, error) { |
| 11 |
switch command { |
| 12 |
case "encryption:id": |
| 13 |
return s.quarkEncryptionID(args...) |
| 14 |
|
| 15 |
case "user:create": |
| 16 |
return s.quarkUserCreate(args...) |
| 17 |
|
| 18 |
case "user:create:address": |
| 19 |
return s.quarkUserCreateAddress(args...) |
| 20 |
|
| 21 |
case "user:create:subscription": |
| 22 |
return s.quarkUserCreateSubscription(args...) |
| 23 |
|
| 24 |
default: |
| 25 |
return nil, fmt.Errorf("unknown command: %s", command) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func (s *Backend) quarkEncryptionID(args ...string) (string, error) { |
| 30 |
fs := flag.NewFlagSet("encryption:id", flag.ContinueOnError) |
| 31 |
|
| 32 |
// Positional arguments. |
| 33 |
// arg0: value |
| 34 |
|
| 35 |
decrypt := fs.Bool("decrypt", false, "decrypt the given encrypted ID") |
| 36 |
|
| 37 |
if err := fs.Parse(args); err != nil { |
| 38 |
return "", err |
| 39 |
} |
| 40 |
|
| 41 |
// TODO: Encrypt/decrypt are currently no-op. |
| 42 |
if *decrypt { |
| 43 |
return fs.Arg(0), nil |
| 44 |
} else { |
| 45 |
return fs.Arg(0), nil |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func (s *Backend) quarkUserCreate(args ...string) (proton.User, error) { |
| 50 |
fs := flag.NewFlagSet("user:create", flag.ContinueOnError) |
| 51 |
|
| 52 |
// Flag arguments. |
| 53 |
name := fs.String("name", "", "new user's name") |
| 54 |
pass := fs.String("password", "", "new user's password") |
| 55 |
newAddr := fs.Bool("create-address", false, "create the user's default address, will not automatically setup the address key") |
| 56 |
genKeys := fs.String("gen-keys", "", "generate new address keys for the user") |
| 57 |
status := fs.Int("status", 2, "User status") |
| 58 |
|
| 59 |
if err := fs.Parse(args); err != nil { |
| 60 |
return proton.User{}, err |
| 61 |
} |
| 62 |
|
| 63 |
addressStatus, err := quarkStatusToAddressStatus(*status) |
| 64 |
if err != nil { |
| 65 |
return proton.User{}, err |
| 66 |
} |
| 67 |
|
| 68 |
userID, err := s.CreateUser(*name, []byte(*pass)) |
| 69 |
if err != nil { |
| 70 |
return proton.User{}, fmt.Errorf("failed to create user: %w", err) |
| 71 |
} |
| 72 |
|
| 73 |
// TODO: Create keys of different types (we always use RSA2048). |
| 74 |
if *newAddr || *genKeys != "" { |
| 75 |
if _, err := s.CreateAddress(userID, *name+"@"+s.domain, []byte(*pass), *genKeys != "", addressStatus, proton.AddressTypeOriginal); err != nil { |
| 76 |
return proton.User{}, fmt.Errorf("failed to create address with keys: %w", err) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return s.GetUser(userID) |
| 81 |
} |
| 82 |
|
| 83 |
func (s *Backend) quarkUserCreateAddress(args ...string) (proton.Address, error) { |
| 84 |
fs := flag.NewFlagSet("user:create:address", flag.ContinueOnError) |
| 85 |
|
| 86 |
// Positional arguments. |
| 87 |
// arg0: userID |
| 88 |
// arg1: password |
| 89 |
// arg2: email |
| 90 |
|
| 91 |
// Flag arguments. |
| 92 |
genKeys := fs.String("gen-keys", "", "generate new address keys for the user") |
| 93 |
status := fs.Int("status", 2, "User status") |
| 94 |
|
| 95 |
if err := fs.Parse(args); err != nil { |
| 96 |
return proton.Address{}, err |
| 97 |
} |
| 98 |
|
| 99 |
addressStatus, err := quarkStatusToAddressStatus(*status) |
| 100 |
if err != nil { |
| 101 |
return proton.Address{}, err |
| 102 |
} |
| 103 |
|
| 104 |
// TODO: Create keys of different types (we always use RSA2048). |
| 105 |
addrID, err := s.CreateAddress(fs.Arg(0), fs.Arg(2), []byte(fs.Arg(1)), *genKeys != "", addressStatus, proton.AddressTypeOriginal) |
| 106 |
if err != nil { |
| 107 |
return proton.Address{}, fmt.Errorf("failed to create address with keys: %w", err) |
| 108 |
} |
| 109 |
|
| 110 |
return s.GetAddress(fs.Arg(0), addrID) |
| 111 |
} |
| 112 |
|
| 113 |
func (s *Backend) quarkUserCreateSubscription(args ...string) (any, error) { |
| 114 |
fs := flag.NewFlagSet("user:create:subscription", flag.ContinueOnError) |
| 115 |
|
| 116 |
// Positional arguments. |
| 117 |
// arg0: userID |
| 118 |
|
| 119 |
// Flag arguments. |
| 120 |
planID := fs.String("planID", "", "plan ID for the user") |
| 121 |
|
| 122 |
if err := fs.Parse(args); err != nil { |
| 123 |
return nil, err |
| 124 |
} |
| 125 |
|
| 126 |
if err := s.CreateSubscription(fs.Arg(0), *planID); err != nil { |
| 127 |
return proton.Address{}, fmt.Errorf("failed to create subscription: %w", err) |
| 128 |
} |
| 129 |
|
| 130 |
return nil, nil |
| 131 |
} |
| 132 |
|
| 133 |
func quarkStatusToAddressStatus(status int) (proton.AddressStatus, error) { |
| 134 |
switch status { |
| 135 |
case 0: |
| 136 |
return proton.AddressStatusDeleting, nil |
| 137 |
case 1: |
| 138 |
return proton.AddressStatusDisabled, nil |
| 139 |
case 2: |
| 140 |
fallthrough |
| 141 |
case 3: |
| 142 |
fallthrough |
| 143 |
case 4: |
| 144 |
fallthrough |
| 145 |
case 5: |
| 146 |
return proton.AddressStatusEnabled, nil |
| 147 |
} |
| 148 |
|
| 149 |
return 0, fmt.Errorf("invalid status value") |
| 150 |
} |