diff --git a/cmd/signaldctl/cmd/get/account/account.go b/cmd/signaldctl/cmd/get/account/account.go new file mode 100644 index 0000000..e58634c --- /dev/null +++ b/cmd/signaldctl/cmd/get/account/account.go @@ -0,0 +1,88 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package account + +import ( + "encoding/json" + "log" + "os" + + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/signald" + "gitlab.com/signald/signald-go/signald/client-protocol/v0" +) + +var ( + GetAccountCmd = &cobra.Command{ + Use: "account", + Aliases: []string{"accounts"}, + Short: "return a list of accounts", + Run: func(_ *cobra.Command, _ []string) { + requestID := signald.GenerateID() + err := common.Signald.RawRequest(v0.LegacyRequest{ + Type: "list_accounts", + ID: requestID, + }) + if err != nil { + log.Fatal("error sending request: ", 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 { + case "json": + err := json.NewEncoder(os.Stdout).Encode(accounts) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + break + case "yaml": + err := yaml.NewEncoder(os.Stdout).Encode(accounts) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + break + case "table": + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"Phone Number", "UUID", "Device ID", "Subscribed"}) + + for _, account := range accounts { + t.AppendRow(table.Row{account.Username, account.UUID, account.DeviceID, account.Subscribed}) + } + + t.Render() + break + default: + log.Fatal("Unsupported output format") + } + }, + } +) diff --git a/cmd/signaldctl/cmd/get/group.go b/cmd/signaldctl/cmd/get/group/group.go similarity index 92% rename from cmd/signaldctl/cmd/get/group.go rename to cmd/signaldctl/cmd/get/group/group.go index fc27c40..3657b9e 100644 --- a/cmd/signaldctl/cmd/get/group.go +++ b/cmd/signaldctl/cmd/get/group/group.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package get +package group import ( "encoding/json" @@ -31,7 +31,7 @@ import ( var ( account string groupID string - getGroupCmd = &cobra.Command{ + GetGroupCmd = &cobra.Command{ Use: "group", Aliases: []string{"groups"}, Short: "return a list of Signal groups", @@ -102,9 +102,7 @@ var ( ) func init() { - GetCmd.AddCommand(getGroupCmd) - getGroupCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") - getGroupCmd.Flags().StringVarP(&groupID, "group-id", "g", "", "if set, fetches the latest state of a group from the server (local state is used for legacy groups). If unset, return all groups") - common.Must(getGroupCmd.MarkFlagRequired("account")) - + GetGroupCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") + GetGroupCmd.Flags().StringVarP(&groupID, "group-id", "g", "", "if set, fetches the latest state of a group from the server (local state is used for legacy groups). If unset, return all groups") + common.Must(GetGroupCmd.MarkFlagRequired("account")) } diff --git a/cmd/signaldctl/cmd/get/root.go b/cmd/signaldctl/cmd/get/root.go index a599851..47bff8f 100644 --- a/cmd/signaldctl/cmd/get/root.go +++ b/cmd/signaldctl/cmd/get/root.go @@ -17,6 +17,14 @@ package get import ( "github.com/spf13/cobra" + + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/get/account" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/get/group" ) var GetCmd = &cobra.Command{Use: "get"} + +func init() { + GetCmd.AddCommand(group.GetGroupCmd) + GetCmd.AddCommand(account.GetAccountCmd) +} diff --git a/cmd/signaldctl/cmd/list-accounts.go b/cmd/signaldctl/cmd/list-accounts.go deleted file mode 100644 index 136bc1b..0000000 --- a/cmd/signaldctl/cmd/list-accounts.go +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright © 2018 Finn Herzfeld -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -package cmd - -import ( - "fmt" - "log" - "math/rand" - - "github.com/spf13/cobra" - - "gitlab.com/signald/signald-go/cmd/signaldctl/common" - "gitlab.com/signald/signald-go/signald/client-protocol/v0" -) - -// listAccountsCmd represents the listAccounts command -var listAccountsCmd = &cobra.Command{ - Use: "list-accounts", - Short: "list of all the accounts registered to this signald instance.", - Long: `Prints a list of all users to stdout.`, - Run: func(cmd *cobra.Command, args []string) { - requestID := fmt.Sprint("signaldctl-", rand.Intn(1000)) - err := common.Signald.RawRequest(v0.LegacyRequest{ - Type: "list_accounts", - ID: requestID, - }) - if err != nil { - log.Fatal("error sending request: ", err) - } - - c := make(chan v0.LegacyResponse) - go common.Signald.Listen(c) - for { - message := <-c - if message.ID == requestID { - for _, account := range message.Data.Accounts { - fmt.Println(account.Username) - } - break - } - } - }, -} - -func init() { - RootCmd.AddCommand(listAccountsCmd) -} diff --git a/signald/client-protocol/v0/signaldresponse.go b/signald/client-protocol/v0/signaldresponse.go index 7a59ff6..1ac0349 100644 --- a/signald/client-protocol/v0/signaldresponse.go +++ b/signald/client-protocol/v0/signaldresponse.go @@ -38,6 +38,7 @@ type Account struct { Registered bool `json:",omitempty"` HasKeys bool `json:"has_keys,omitempty"` Subscribed bool + UUID string `json:",omitempty"` } // DataMessage is the main component of incoming text messages diff --git a/signald/client-protocol/v1/requests.go b/signald/client-protocol/v1/requests.go index d0edb38..d1935a8 100644 --- a/signald/client-protocol/v1/requests.go +++ b/signald/client-protocol/v1/requests.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "log" - "math/rand" "gitlab.com/signald/signald-go/signald" ) @@ -16,7 +15,7 @@ func (r *AcceptInvitationRequest) Submit(conn *signald.Signald) (response JsonGr r.Version = "v1" r.Type = "accept_invitation" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -49,7 +48,7 @@ func (r *ApproveMembershipRequest) Submit(conn *signald.Signald) (response JsonG r.Version = "v1" r.Type = "approve_membership" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -82,7 +81,7 @@ func (r *GetGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2Inf r.Version = "v1" r.Type = "get_group" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -115,7 +114,7 @@ func (r *GetLinkedDevicesRequest) Submit(conn *signald.Signald) (response Linked r.Version = "v1" r.Type = "get_linked_devices" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -148,7 +147,7 @@ func (r *GetProfileRequest) Submit(conn *signald.Signald) (response Profile, err r.Version = "v1" r.Type = "get_profile" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -181,7 +180,7 @@ func (r *JoinGroupRequest) Submit(conn *signald.Signald) (response JsonGroupJoin r.Version = "v1" r.Type = "join_group" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -213,7 +212,7 @@ func (r *ListContactsRequest) Submit(conn *signald.Signald) (response ProfileLis r.Version = "v1" r.Type = "list_contacts" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -245,7 +244,7 @@ func (r *ListGroupsRequest) Submit(conn *signald.Signald) (response GroupList, e r.Version = "v1" r.Type = "list_groups" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -277,7 +276,7 @@ func (r *MarkReadRequest) Submit(conn *signald.Signald) error { r.Version = "v1" r.Type = "mark_read" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } return conn.RawRequest(r) @@ -288,7 +287,7 @@ func (r *ProtocolRequest) Submit(conn *signald.Signald) error { r.Version = "v1" r.Type = "protocol" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } return conn.RawRequest(r) @@ -300,7 +299,7 @@ func (r *ReactRequest) Submit(conn *signald.Signald) (response SendResponse, err r.Version = "v1" r.Type = "react" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -333,7 +332,7 @@ func (r *RemoveLinkedDeviceRequest) Submit(conn *signald.Signald) error { r.Version = "v1" r.Type = "remove_linked_device" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } return conn.RawRequest(r) @@ -345,7 +344,7 @@ func (r *ResolveAddressRequest) Submit(conn *signald.Signald) (response JsonAddr r.Version = "v1" r.Type = "resolve_address" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -377,7 +376,7 @@ func (r *SendRequest) Submit(conn *signald.Signald) (response SendResponse, err r.Version = "v1" r.Type = "send" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -409,7 +408,7 @@ func (r *SetProfile) Submit(conn *signald.Signald) error { r.Version = "v1" r.Type = "set_profile" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } return conn.RawRequest(r) @@ -421,7 +420,7 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo, r.Version = "v1" r.Type = "update_group" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -453,7 +452,7 @@ func (r *VersionRequest) Submit(conn *signald.Signald) (response JsonVersionMess r.Version = "v1" r.Type = "version" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -480,15 +479,3 @@ func (r *VersionRequest) Submit(conn *signald.Signald) (response JsonVersionMess return response, nil } - -const idsize = 10 - -var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") - -func generateID() string { - id := make([]rune, idsize) - for i := range id { - id[i] = charset[rand.Intn(len(charset))] - } - return string(id) -} diff --git a/signald/client-protocol/v1alpha1/requests.go b/signald/client-protocol/v1alpha1/requests.go index 4f8e6f5..65d3312 100644 --- a/signald/client-protocol/v1alpha1/requests.go +++ b/signald/client-protocol/v1alpha1/requests.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "log" - "math/rand" "gitlab.com/signald/signald-go/signald" ) @@ -16,7 +15,7 @@ func (r *AcceptInvitationRequest) Submit(conn *signald.Signald) (response JsonGr r.Version = "v1alpha1" r.Type = "accept_invitation" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -49,7 +48,7 @@ func (r *ApproveMembershipRequest) Submit(conn *signald.Signald) (response JsonG r.Version = "v1alpha1" r.Type = "approve_membership" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -82,7 +81,7 @@ func (r *GetGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2Inf r.Version = "v1alpha1" r.Type = "get_group" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -115,7 +114,7 @@ func (r *GetLinkedDevicesRequest) Submit(conn *signald.Signald) (response Linked r.Version = "v1alpha1" r.Type = "get_linked_devices" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -148,7 +147,7 @@ func (r *JoinGroupRequest) Submit(conn *signald.Signald) (response JsonGroupJoin r.Version = "v1alpha1" r.Type = "join_group" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -180,7 +179,7 @@ func (r *ProtocolRequest) Submit(conn *signald.Signald) error { r.Version = "v1alpha1" r.Type = "protocol" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } return conn.RawRequest(r) @@ -192,7 +191,7 @@ func (r *RemoveLinkedDeviceRequest) Submit(conn *signald.Signald) error { r.Version = "v1alpha1" r.Type = "remove_linked_device" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } return conn.RawRequest(r) @@ -204,7 +203,7 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2 r.Version = "v1alpha1" r.Type = "update_group" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -231,15 +230,3 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2 return response, nil } - -const idsize = 10 - -var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") - -func generateID() string { - id := make([]rune, idsize) - for i := range id { - id[i] = charset[rand.Intn(len(charset))] - } - return string(id) -} diff --git a/signald/client-protocol/v1alpha2/requests.go b/signald/client-protocol/v1alpha2/requests.go index 8f237a5..b5b99ba 100644 --- a/signald/client-protocol/v1alpha2/requests.go +++ b/signald/client-protocol/v1alpha2/requests.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "log" - "math/rand" "gitlab.com/signald/signald-go/signald" ) @@ -16,7 +15,7 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo, r.Version = "v1alpha2" r.Type = "update_group" if r.ID == "" { - r.ID = generateID() + r.ID = signald.GenerateID() } err = conn.RawRequest(r) @@ -43,15 +42,3 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo, return response, nil } - -const idsize = 10 - -var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") - -func generateID() string { - id := make([]rune, idsize) - for i := range id { - id[i] = charset[rand.Intn(len(charset))] - } - return string(id) -} diff --git a/signald/utils.go b/signald/utils.go new file mode 100644 index 0000000..4e0fa3d --- /dev/null +++ b/signald/utils.go @@ -0,0 +1,18 @@ +package signald + +import ( + "math/rand" +) + +const idsize = 10 + +var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") + +// GenerateID is a helper function to generate random request IDs. +func GenerateID() string { + id := make([]rune, idsize) + for i := range id { + id[i] = charset[rand.Intn(len(charset))] + } + return string(id) +} diff --git a/tools/generator/requests.go.tmpl b/tools/generator/requests.go.tmpl index 962d499..1b5b7f2 100644 --- a/tools/generator/requests.go.tmpl +++ b/tools/generator/requests.go.tmpl @@ -6,7 +6,6 @@ import ({{if .Responses}} "encoding/json" "fmt" "log"{{end}} - "math/rand" "gitlab.com/signald/signald-go/signald" ) @@ -22,7 +21,7 @@ func {{$action.FnName}}(conn *signald.Signald) ({{if ne $action.Response ""}}res r := Request{Version: "{{.Version}}"} {{end}} r.Type = "{{$type}}" if(r.ID == "") { - r.ID = generateID() + r.ID = signald.GenerateID() } {{if ne $action.Response ""}} @@ -52,16 +51,4 @@ func {{$action.FnName}}(conn *signald.Signald) ({{if ne $action.Response ""}}res return conn.RawRequest(r) {{end}} } -{{end}} - - -const idsize = 10 -var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") - -func generateID() string { - id := make([]rune, idsize) - for i := range id { - id[i]= charset[rand.Intn(len(charset))] - } - return string(id) -} +{{end}} \ No newline at end of file