From c69745b52b88d68c2d292e916860e81653552872 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 8 Oct 2018 20:35:04 -0700 Subject: [PATCH] listGroups command and a light sprinkling of gofmt --- cmd/signald-cli/cmd/listGroups.go | 59 +++++++++++++++++++++++++++++++ cmd/signald-cli/cmd/send.go | 12 +++---- signald/signald.go | 5 ++- signald/signaldresponse.go | 20 +++++++++++ 4 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 cmd/signald-cli/cmd/listGroups.go create mode 100644 signald/signaldresponse.go diff --git a/cmd/signald-cli/cmd/listGroups.go b/cmd/signald-cli/cmd/listGroups.go new file mode 100644 index 0000000..cdfbf8d --- /dev/null +++ b/cmd/signald-cli/cmd/listGroups.go @@ -0,0 +1,59 @@ +// 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" + "math/rand" + + "github.com/spf13/cobra" + + "git.callpipe.com/finn/signald-go/signald" +) + +// listGroupsCmd represents the listGroups command +var listGroupsCmd = &cobra.Command{ + Use: "listGroups", + Short: "list of all the groups that the user is in.", + Long: `Prints a list of all groups the user is in to stdout.`, + Run: func(cmd *cobra.Command, args []string) { + requestID := fmt.Sprint("signald-cli-", rand.Intn(1000)) + s.SendRequest(signald.Request{ + Type: "list_groups", + Username: username, + ID: requestID, + }) + + c := make(chan signald.Response) + go s.Listen(c) + for { + message := <- c + if message.ID == requestID { + for _, group := range message.Data.Groups { + fmt.Println(group.Name) + } + break + } + } + }, +} + +func init() { + RootCmd.AddCommand(listGroupsCmd) + + listGroupsCmd.Flags().StringVarP(&username, "username", "u", "", "The username of the account to use)") + listGroupsCmd.MarkFlagRequired("username") +} diff --git a/cmd/signald-cli/cmd/send.go b/cmd/signald-cli/cmd/send.go index f8704a1..3efd4ce 100644 --- a/cmd/signald-cli/cmd/send.go +++ b/cmd/signald-cli/cmd/send.go @@ -24,21 +24,21 @@ import ( ) var ( - username string - toUser string - toGroup string + username string + toUser string + toGroup string messageBody string - attachment string + attachment string ) // sendCmd represents the send command var sendCmd = &cobra.Command{ Use: "send", Short: "send a message to another user or group", - Long: `send a message to another user or group on Signal`, + Long: `send a message to another user or group on Signal`, Run: func(cmd *cobra.Command, args []string) { request := signald.Request{ - Type: "send", + Type: "send", Username: username, } diff --git a/signald/signald.go b/signald/signald.go index 8ffc240..f1b8144 100644 --- a/signald/signald.go +++ b/signald/signald.go @@ -45,15 +45,14 @@ func (s *Signald) Connect() { } // Listen listens for events from signald -func (s *Signald) Listen(c chan interface{}) { +func (s *Signald) Listen(c chan Response) { // we create a decoder that reads directly from the socket d := json.NewDecoder(s.socket) - var msg interface{} + var msg Response for { crash(d.Decode(&msg)) - log.Print(msg) c <- msg } } diff --git a/signald/signaldresponse.go b/signald/signaldresponse.go new file mode 100644 index 0000000..5b49df1 --- /dev/null +++ b/signald/signaldresponse.go @@ -0,0 +1,20 @@ +package signald + +// Response is a response to a request to signald, or a new inbound message +type Response struct { + ID string + Data ResponseData +} + +// ResponseData is where most of the data in the response is stored. +type ResponseData struct { + Groups []Group +} + +// Group represents a group in signal +type Group struct { + GroupID string + Members []string + Name string + AvatarID int +}