Add flag to register accounts on the testing server

also move account list to v1
This commit is contained in:
Finn 2021-07-09 00:04:46 -07:00
parent fdcaabfba4
commit eae4dbc944
3 changed files with 48 additions and 54 deletions

View file

@ -17,6 +17,7 @@ package link
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"os" "os"
@ -27,36 +28,35 @@ import (
"gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/signald" "gitlab.com/signald/signald-go/signald"
"gitlab.com/signald/signald-go/signald/client-protocol/v0" v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1"
) )
var ( var (
noWait bool testing bool
deviceName string
LinkAccountCmd = &cobra.Command{ LinkAccountCmd = &cobra.Command{
Use: "link", Use: "link",
Short: "create a local account by linking to an existing Signal account", Short: "create a local account by linking to an existing Signal account",
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, _ []string) {
requestID := signald.GenerateID() go common.Signald.Listen(nil)
err := common.Signald.RawRequest(v0.LegacyRequest{Type: "link", ID: requestID, Version: "v0"}) req := v1.GenerateLinkingURIRequest{}
if err != nil { if testing {
log.Fatal("error sending request: ", err) req.Server = signald.StagingServerUUID
}
c := make(chan v0.LegacyResponse)
go common.Signald.Listen(c)
uri := signald.GetLegacyResponse(c, requestID)
if uri.Type != "linking_uri" {
log.Fatalf("unexpected response from signald when requesting link: %+v", uri)
} }
response, err := req.Submit(common.Signald)
if err != nil {
log.Fatal(err)
}
switch common.OutputFormat { switch common.OutputFormat {
case common.OutputFormatJSON: case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(uri.Data.URI) err := json.NewEncoder(os.Stdout).Encode(response.Uri)
if err != nil { if err != nil {
log.Fatal(err, "error encoding response to stdout") log.Fatal(err, "error encoding response to stdout")
} }
case common.OutputFormatYAML: case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(uri.Data.URI) err := yaml.NewEncoder(os.Stdout).Encode(response.Uri)
if err != nil { if err != nil {
log.Fatal(err, "error encoding response to stdout") log.Fatal(err, "error encoding response to stdout")
} }
@ -64,7 +64,7 @@ var (
t := table.NewWriter() t := table.NewWriter()
t.SetOutputMirror(os.Stdout) t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"URI"}) t.AppendHeader(table.Row{"URI"})
t.AppendRow(table.Row{uri.Data.URI}) t.AppendRow(table.Row{response.Uri})
if common.OutputFormat == common.OutputFormatCSV { if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV() t.RenderCSV()
} else { } else {
@ -72,29 +72,31 @@ var (
t.Render() t.Render()
} }
case common.OutputFormatQR, common.OutputFormatDefault: case common.OutputFormatQR, common.OutputFormatDefault:
qrterminal.Generate(uri.Data.URI, qrterminal.M, os.Stdout) qrterminal.Generate(response.Uri, qrterminal.M, os.Stdout)
default: default:
log.Fatal("unsupported output format") log.Fatal("unsupported output format")
} }
if noWait { finishReq := v1.FinishLinkRequest{
return DeviceName: deviceName,
SessionId: response.SessionId,
} }
finish := signald.GetLegacyResponse(c, requestID) _, err = finishReq.Submit(common.Signald)
if finish.Type == "linking_successful" {
log.Println("linking successful") if err != nil {
return log.Fatal(err)
}
if finish.Type == "linking_error" {
log.Fatal("error from signald:", finish.Data.Message)
} else {
log.Fatal("unexpected message from signald:", finish.Data.Message)
} }
log.Println("linking successful")
}, },
} }
) )
func init() { func init() {
LinkAccountCmd.Flags().BoolVar(&noWait, "no-wait", false, "return after the linking URI is printed to stdout. By default, signaldctl will wait until the server acknowledges that the linking has completed before exiting") name := "signald"
if hostname, err := os.Hostname(); err == nil {
name = fmt.Sprintf("signald on %s", hostname)
}
LinkAccountCmd.Flags().BoolVarP(&testing, "testing", "t", false, "use the Signal testing server")
LinkAccountCmd.Flags().StringVarP(&deviceName, "device-name", "n", name, "the name of this device. shown to other devices on the signal account")
} }

View file

@ -25,8 +25,7 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/signald" v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1"
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
) )
var ( var (
@ -34,46 +33,33 @@ var (
Use: "list", Use: "list",
Short: "return a list of accounts", Short: "return a list of accounts",
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, _ []string) {
requestID := signald.GenerateID() go common.Signald.Listen(nil)
err := common.Signald.RawRequest(v0.LegacyRequest{
Type: "list_accounts", req := v1.ListAccountsRequest{}
ID: requestID, accounts, err := req.Submit(common.Signald)
Version: "v0",
})
if err != nil { if err != nil {
log.Fatal("error sending request: ", err) log.Fatal(err)
}
c := make(chan v0.LegacyResponse)
go common.Signald.Listen(c)
var accounts []v0.Account
for {
message := <-c
if message.ID == requestID {
accounts = message.Data.Accounts
break
}
} }
switch common.OutputFormat { switch common.OutputFormat {
case common.OutputFormatJSON: case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(accounts) err := json.NewEncoder(os.Stdout).Encode(accounts.Accounts)
if err != nil { if err != nil {
log.Fatal(err, "error encoding response to stdout") log.Fatal(err, "error encoding response to stdout")
} }
case common.OutputFormatYAML: case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(accounts) err := yaml.NewEncoder(os.Stdout).Encode(accounts.Accounts)
if err != nil { if err != nil {
log.Fatal(err, "error encoding response to stdout") log.Fatal(err, "error encoding response to stdout")
} }
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter() t := table.NewWriter()
t.SetOutputMirror(os.Stdout) t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Phone Number", "UUID", "Device ID", "Subscribed"}) t.AppendHeader(table.Row{"Phone Number", "UUID", "Device ID"})
for _, account := range accounts { for _, account := range accounts.Accounts {
t.AppendRow(table.Row{account.Username, account.UUID, account.DeviceID, account.Subscribed}) t.AppendRow(table.Row{account.Address.Number, account.Address.UUID, account.DeviceId})
} }
if common.OutputFormat == common.OutputFormatCSV { if common.OutputFormat == common.OutputFormatCSV {

View file

@ -25,12 +25,14 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/signald"
v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1" v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1"
) )
var ( var (
voice bool voice bool
captcha string captcha string
testing bool
RegisterAccountCmd = &cobra.Command{ RegisterAccountCmd = &cobra.Command{
Use: "register [phone number]", Use: "register [phone number]",
@ -48,6 +50,9 @@ var (
Voice: voice, Voice: voice,
Captcha: captcha, Captcha: captcha,
} }
if testing {
req.Server = signald.StagingServerUUID
}
resp, err := req.Submit(common.Signald) resp, err := req.Submit(common.Signald)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -76,4 +81,5 @@ var (
func init() { func init() {
RegisterAccountCmd.Flags().BoolVarP(&voice, "voice", "V", false, "request verification code be sent via an automated voice call (code is sent via SMS by default)") RegisterAccountCmd.Flags().BoolVarP(&voice, "voice", "V", false, "request verification code be sent via an automated voice call (code is sent via SMS by default)")
RegisterAccountCmd.Flags().StringVarP(&captcha, "captcha", "c", "", "a captcha token may be required to register, see https://gitlab.com/signald/signald/-/wikis/Captchas for how to get one") RegisterAccountCmd.Flags().StringVarP(&captcha, "captcha", "c", "", "a captcha token may be required to register, see https://gitlab.com/signald/signald/-/wikis/Captchas for how to get one")
RegisterAccountCmd.Flags().BoolVarP(&testing, "testing", "t", false, "use the Signal testing server")
} }