diff --git a/cmd/signaldctl/cmd/subscribe.go b/cmd/signaldctl/cmd/subscribe.go new file mode 100644 index 0000000..dfa0638 --- /dev/null +++ b/cmd/signaldctl/cmd/subscribe.go @@ -0,0 +1,96 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "log" + "os" + + "github.com/spf13/cobra" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/cmd/signaldctl/config" + client_protocol "gitlab.com/signald/signald-go/signald/client-protocol" + v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + accountIdentifier string + subscribeCmd = &cobra.Command{ + Use: "subscribe", + Short: "subscribe to incoming messages from signald", + Long: `subscribe to incoming messages from signald. + + if no default account is set, the -a/--account argument is required. + + the default output format (-o default) is being worked on, subject to change and should not be relied upon. Thoughts? come by the #signald IRC/matrix room + + if you want to future-proof your scripts, use json output (-o json) and parse it.`, + PreRun: func(cmd *cobra.Command, args []string) { + if accountIdentifier == "" { + accountIdentifier = config.Config.DefaultAccount + } + if accountIdentifier == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + }, + RunE: func(cmd *cobra.Command, args []string) error { + incoming := make(chan client_protocol.BasicResponse) + go common.Signald.Listen(incoming) + + req := v1.SubscribeRequest{Account: accountIdentifier} + err := req.Submit(common.Signald) + if err != nil { + panic(err) + } + + for msg := range incoming { + switch common.OutputFormat { + case common.OutputFormatDefault: + if msg.Type != "IncomingMessage" { + continue + } + var data v1.IncomingMessage + err := json.Unmarshal(msg.Data, &data) + if err != nil { + panic(err) + } + + if data.DataMessage == nil { + continue + } + + group := "-" + if data.DataMessage.GroupV2 != nil { + group = fmt.Sprintf("%s|%d", data.DataMessage.GroupV2.ID, data.DataMessage.GroupV2.Revision) + } + + attachment := "-" + if len(data.DataMessage.Attachments) > 0 { + attachment = data.DataMessage.Attachments[0].StoredFilename + } + + body := "-" + if data.DataMessage.Body != "" { + body = data.DataMessage.Body + } + fmt.Println(data.DataMessage.Timestamp, data.Account, data.Source.UUID, group, attachment, body) + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(msg) + if err != nil { + panic(err) + } + default: + log.Fatal("unsupported output format") + } + } + return nil + }, + } +) + +func init() { + subscribeCmd.Flags().StringVarP(&accountIdentifier, "account", "a", "", "the signald account to use") + RootCmd.AddCommand(subscribeCmd) +} diff --git a/signald/client-protocol/protocol.go b/signald/client-protocol/protocol.go index 1403b18..c0cb562 100644 --- a/signald/client-protocol/protocol.go +++ b/signald/client-protocol/protocol.go @@ -5,10 +5,10 @@ import ( ) type BasicResponse struct { - ID string - Type string - ErrorType string - Error json.RawMessage - Data json.RawMessage - Account string + ID string `json:"id,omitempty"` + Type string `json:"type,omitempty"` + ErrorType string `json:"error_type,omitempty"` + Error json.RawMessage `json:"error,omitempty"` + Data json.RawMessage `json:"data,omitempty"` + Account string `json:"account,omitempty"` } diff --git a/signald/signald.go b/signald/signald.go index 0cfd740..9f646f0 100644 --- a/signald/signald.go +++ b/signald/signald.go @@ -125,7 +125,7 @@ func (s *Signald) Listen(c chan client_protocol.BasicResponse) { subscribers <- msg } - if c != nil && msg.ID == "" && msg.Type != "version" { + if c != nil && !(msg.ID == "" && msg.Type == "version") { c <- msg } }