Skip to content

Commit

Permalink
Merge pull request #445 from kxait/jsonrpc_linkdevice
Browse files Browse the repository at this point in the history
Link device in JSON-RPC mode
  • Loading branch information
bbernhard authored Nov 13, 2023
2 parents c493655 + 3ea7949 commit e446c8f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
55 changes: 54 additions & 1 deletion src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -1053,7 +1054,59 @@ func (s *SignalClient) DeleteGroup(number string, groupId string) error {

func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int) ([]byte, error) {
if s.signalCliMode == JsonRpc {
return []byte{}, errors.New(endpointNotSupportedInJsonRpcMode)
jsonRpc2Client, err := s.getJsonRpc2Client()
if err != nil {
return []byte{}, err
}

type StartRequest struct{}
type Response struct {
DeviceLinkUri string `json:"deviceLinkUri"`
}

result, err := jsonRpc2Client.getRaw("startLink", nil, &StartRequest{})
if err != nil {
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
}

var resp Response
err = json.Unmarshal([]byte(result), &resp)
if err != nil {
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
}

q, err := qrcode.NewWithForcedVersion(string(resp.DeviceLinkUri), qrCodeVersion, qrcode.Highest)
if err != nil {
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
}

var png []byte
png, err = q.PNG(256)
if err != nil {
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
}

go (func() {
type FinishRequest struct {
DeviceLinkUri string `json:"deviceLinkUri"`
DeviceName string `json:"deviceName"`
}

req := FinishRequest{
DeviceLinkUri: resp.DeviceLinkUri,
DeviceName: deviceName,
}

result, err := jsonRpc2Client.getRaw("finishLink", nil, &req)
if err != nil {
log.Debug("Error linking device: ", err.Error())
return
}
log.Debug("Linking device result: ", result)
s.signalCliApiConfig.Load(s.signalCliApiConfigPath)
})()

return png, nil
}
command := []string{"--config", s.signalCliConfig, "link", "-n", deviceName}

Expand Down
25 changes: 13 additions & 12 deletions src/client/jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ type JsonRpc2ReceivedMessage struct {

type JsonRpc2Client struct {
conn net.Conn
receivedMessageResponses chan JsonRpc2MessageResponse
receivedResponsesById map[string]chan JsonRpc2MessageResponse
receivedMessages chan JsonRpc2ReceivedMessage
lastTimeErrorMessageSent time.Time
signalCliApiConfig *utils.SignalCliApiConfig
number string
number string
}

func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client {
return &JsonRpc2Client{
signalCliApiConfig: signalCliApiConfig,
number: number,
signalCliApiConfig: signalCliApiConfig,
number: number,
receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse),
}
}

Expand All @@ -53,7 +54,6 @@ func (r *JsonRpc2Client) Dial(address string) error {
return err
}

r.receivedMessageResponses = make(chan JsonRpc2MessageResponse)
r.receivedMessages = make(chan JsonRpc2ReceivedMessage)

return nil
Expand Down Expand Up @@ -113,13 +113,12 @@ func (r *JsonRpc2Client) getRaw(command string, account *string, args interface{
return "", err
}

responseChan := make(chan JsonRpc2MessageResponse)
r.receivedResponsesById[u.String()] = responseChan

var resp JsonRpc2MessageResponse
for {
resp = <-r.receivedMessageResponses
if resp.Id == u.String() {
break
}
}
resp = <-responseChan
delete(r.receivedResponsesById, u.String())

if resp.Err.Code != 0 {
return "", errors.New(resp.Err.Message)
Expand Down Expand Up @@ -157,7 +156,9 @@ func (r *JsonRpc2Client) ReceiveData(number string) {
err = json.Unmarshal([]byte(str), &resp2)
if err == nil {
if resp2.Id != "" {
r.receivedMessageResponses <- resp2
if responseChan, ok := r.receivedResponsesById[resp2.Id]; ok {
responseChan <- resp2
}
}
} else {
log.Error("Received unparsable message: ", str)
Expand Down

0 comments on commit e446c8f

Please sign in to comment.