Add subscribe
This commit is contained in:
parent
0b523fcc4f
commit
c11cd4a4e1
3 changed files with 112 additions and 37 deletions
71
cmd/signaldctl/cmd/subscribe.go
Normal file
71
cmd/signaldctl/cmd/subscribe.go
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
// Copyright © 2020 Finn Herzfeld <finn@janky.solutions>
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
|
||||||
|
)
|
||||||
|
|
||||||
|
// subscribeCmd represents the subscribe command
|
||||||
|
var subscribeCmd = &cobra.Command{
|
||||||
|
Use: "subscribe",
|
||||||
|
Short: "subscribe to incoming messages",
|
||||||
|
Long: `receive incoming messages from Signal`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
requestID := fmt.Sprint("signaldctl-", rand.Intn(1000))
|
||||||
|
err := s.RawRequest(v0.LegacyRequest{
|
||||||
|
Type: "subscribe",
|
||||||
|
Username: username,
|
||||||
|
ID: requestID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("error sending request: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c := make(chan v0.LegacyResponse)
|
||||||
|
go s.Listen(c)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer s.CloseResponseListener(requestID)
|
||||||
|
r := <-s.GetResponseListener(requestID)
|
||||||
|
if r.GetError() != nil {
|
||||||
|
log.Fatal("error subscribing: ", r.GetError())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
msg := <-c
|
||||||
|
if err := json.NewEncoder(os.Stdout).Encode(msg); err != nil {
|
||||||
|
log.Fatal("error encoding response to JSON. this should never happen", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(subscribeCmd)
|
||||||
|
|
||||||
|
subscribeCmd.Flags().StringVarP(&username, "username", "u", "", "The phone number to subscribe to")
|
||||||
|
must(subscribeCmd.MarkFlagRequired("username"))
|
||||||
|
}
|
|
@ -1,55 +1,55 @@
|
||||||
package v0
|
package v0
|
||||||
|
|
||||||
type LegacyResponse struct {
|
type LegacyResponse struct {
|
||||||
ID string
|
ID string `json:",omitempty"`
|
||||||
Data LegacyResponseData
|
Data LegacyResponseData `json:",omitempty"`
|
||||||
Type string
|
Type string `json:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResponseData is where most of the data in the response is stored.
|
// ResponseData is where most of the data in the response is stored.
|
||||||
type LegacyResponseData struct {
|
type LegacyResponseData struct {
|
||||||
Groups []Group
|
Groups []Group `json:",omitempty"`
|
||||||
Accounts []Account
|
Accounts []Account `json:",omitempty"`
|
||||||
URI string
|
URI string `json:",omitempty"`
|
||||||
DataMessage DataMessage
|
DataMessage DataMessage `json:",omitempty"`
|
||||||
Message string
|
Message string `json:",omitempty"`
|
||||||
Username string
|
Username string `json:",omitempty"`
|
||||||
Source JsonAddress
|
Source JsonAddress `json:",omitempty"`
|
||||||
SourceDevice int
|
SourceDevice int `json:",omitempty"`
|
||||||
Type string
|
Type string `json:",omitempty"`
|
||||||
IsReceipt bool
|
IsReceipt bool `json:",omitempty"`
|
||||||
Timestamp int64
|
Timestamp int64 `json:",omitempty"`
|
||||||
ServerTimestamp int64
|
ServerTimestamp int64 `json:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group represents a group in signal
|
// Group represents a group in signal
|
||||||
type Group struct {
|
type Group struct {
|
||||||
GroupID string
|
GroupID string `json:",omitempty"`
|
||||||
Members []string
|
Members []string `json:",omitempty"`
|
||||||
Name string
|
Name string `json:",omitempty"`
|
||||||
AvatarID int
|
AvatarID int `json:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account represents a user account registered to signald
|
// Account represents a user account registered to signald
|
||||||
type Account struct {
|
type Account struct {
|
||||||
Username string
|
Username string `json:",omitempty"`
|
||||||
DeviceID int
|
DeviceID int `json:",omitempty"`
|
||||||
Filename string
|
Filename string `json:",omitempty"`
|
||||||
Registered bool
|
Registered bool `json:",omitempty"`
|
||||||
HasKeys bool `json:"has_keys"`
|
HasKeys bool `json:"has_keys,omitempty"`
|
||||||
Subscribed bool
|
Subscribed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// DataMessage is the main component of incoming text messages
|
// DataMessage is the main component of incoming text messages
|
||||||
type DataMessage struct {
|
type DataMessage struct {
|
||||||
Timestamp int64
|
Timestamp int64 `json:",omitempty"`
|
||||||
Body string
|
Body string `json:",omitempty"`
|
||||||
ExpiresInSeconds int64
|
ExpiresInSeconds int64 `json:",omitempty"`
|
||||||
GroupInfo IncomingGroupInfo `json:"group"`
|
GroupInfo IncomingGroupInfo `json:"group,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncomingGroupInfo is information about a particular group
|
// IncomingGroupInfo is information about a particular group
|
||||||
type IncomingGroupInfo struct {
|
type IncomingGroupInfo struct {
|
||||||
GroupID string
|
GroupID string `json:",omitempty"`
|
||||||
Type string
|
Type string `json:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
|
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
|
||||||
)
|
)
|
||||||
|
@ -70,6 +71,9 @@ func (s *Signald) Listen(c chan v0.LegacyResponse) {
|
||||||
msg, err := s.readNext()
|
msg, err := s.readNext()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
log.Println("signald-go: socket disconnected!")
|
log.Println("signald-go: socket disconnected!")
|
||||||
|
if c != nil {
|
||||||
|
close(c)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,21 +92,21 @@ func (s *Signald) Listen(c chan v0.LegacyResponse) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if c != nil {
|
if c != nil {
|
||||||
legacyResponse := v0.LegacyResponse{
|
legacyResponse := v0.LegacyResponse{ID: msg.ID, Type: msg.Type}
|
||||||
ID: msg.ID,
|
if err := json.Unmarshal(msg.Data, &legacyResponse.Data); err != nil {
|
||||||
Type: msg.Type,
|
log.Println("signald-go receive error: ", err)
|
||||||
}
|
} else {
|
||||||
_ = json.Unmarshal(msg.Data, &legacyResponse.Data)
|
|
||||||
c <- legacyResponse
|
c <- legacyResponse
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Signald) RawRequest(request interface{}) error {
|
func (s *Signald) RawRequest(request interface{}) error {
|
||||||
if debugSignaldIO {
|
if debugSignaldIO {
|
||||||
buffer := bytes.Buffer{}
|
buffer := bytes.Buffer{}
|
||||||
if err := json.NewEncoder(&buffer).Encode(request); err == nil {
|
if err := json.NewEncoder(&buffer).Encode(request); err == nil {
|
||||||
log.Println("[to signald]", buffer.String())
|
log.Println("[to signald]", strings.TrimSpace(buffer.String()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return json.NewEncoder(s.socket).Encode(request)
|
return json.NewEncoder(s.socket).Encode(request)
|
||||||
|
@ -133,7 +137,7 @@ func (s *Signald) readNext() (b BasicResponse, err error) {
|
||||||
if debugSignaldIO {
|
if debugSignaldIO {
|
||||||
buffer := bytes.Buffer{}
|
buffer := bytes.Buffer{}
|
||||||
err = json.NewDecoder(io.TeeReader(s.socket, &buffer)).Decode(&b)
|
err = json.NewDecoder(io.TeeReader(s.socket, &buffer)).Decode(&b)
|
||||||
log.Println("[from signald]", buffer.String())
|
log.Println("[from signald]", strings.TrimSpace(buffer.String()))
|
||||||
} else {
|
} else {
|
||||||
err = json.NewDecoder(s.socket).Decode(&b)
|
err = json.NewDecoder(s.socket).Decode(&b)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue