summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Rhymen/go-whatsapp/binary/node.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-05-30 12:20:56 +0200
committerGitHub <noreply@github.com>2019-05-30 12:20:56 +0200
commit3418e8c9afbdf3e94ab26a20d8f12c042ae29fc4 (patch)
treec5358b971a95749bece9469e959041d4f2e54cc3 /vendor/github.com/Rhymen/go-whatsapp/binary/node.go
parent9619dff33417548a50e51a4f75f41b9de4a73327 (diff)
downloadmatterbridge-msglm-3418e8c9afbdf3e94ab26a20d8f12c042ae29fc4.tar.gz
matterbridge-msglm-3418e8c9afbdf3e94ab26a20d8f12c042ae29fc4.tar.bz2
matterbridge-msglm-3418e8c9afbdf3e94ab26a20d8f12c042ae29fc4.zip
Use upstream whatsapp again (#809)
Diffstat (limited to 'vendor/github.com/Rhymen/go-whatsapp/binary/node.go')
-rw-r--r--vendor/github.com/Rhymen/go-whatsapp/binary/node.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/github.com/Rhymen/go-whatsapp/binary/node.go b/vendor/github.com/Rhymen/go-whatsapp/binary/node.go
new file mode 100644
index 00000000..2139e385
--- /dev/null
+++ b/vendor/github.com/Rhymen/go-whatsapp/binary/node.go
@@ -0,0 +1,103 @@
+package binary
+
+import (
+ "fmt"
+ pb "github.com/Rhymen/go-whatsapp/binary/proto"
+ "github.com/golang/protobuf/proto"
+)
+
+type Node struct {
+ Description string
+ Attributes map[string]string
+ Content interface{}
+}
+
+func Marshal(n Node) ([]byte, error) {
+ if n.Attributes != nil && n.Content != nil {
+ a, err := marshalMessageArray(n.Content.([]interface{}))
+ if err != nil {
+ return nil, err
+ }
+ n.Content = a
+ }
+
+ w := NewEncoder()
+ if err := w.WriteNode(n); err != nil {
+ return nil, err
+ }
+
+ return w.GetData(), nil
+}
+
+func marshalMessageArray(messages []interface{}) ([]Node, error) {
+ ret := make([]Node, len(messages))
+
+ for i, m := range messages {
+ if wmi, ok := m.(*pb.WebMessageInfo); ok {
+ b, err := marshalWebMessageInfo(wmi)
+ if err != nil {
+ return nil, nil
+ }
+ ret[i] = Node{"message", nil, b}
+ } else {
+ ret[i], ok = m.(Node)
+ if !ok {
+ return nil, fmt.Errorf("invalid Node")
+ }
+ }
+ }
+
+ return ret, nil
+}
+
+func marshalWebMessageInfo(p *pb.WebMessageInfo) ([]byte, error) {
+ b, err := proto.Marshal(p)
+ if err != nil {
+ return nil, err
+ }
+ return b, nil
+}
+
+func Unmarshal(data []byte) (*Node, error) {
+ r := NewDecoder(data)
+ n, err := r.ReadNode()
+ if err != nil {
+ return nil, err
+ }
+
+ if n != nil && n.Attributes != nil && n.Content != nil {
+ n.Content, err = unmarshalMessageArray(n.Content.([]Node))
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ return n, nil
+}
+
+func unmarshalMessageArray(messages []Node) ([]interface{}, error) {
+ ret := make([]interface{}, len(messages))
+
+ for i, msg := range messages {
+ if msg.Description == "message" {
+ info, err := unmarshalWebMessageInfo(msg.Content.([]byte))
+ if err != nil {
+ return nil, err
+ }
+ ret[i] = info
+ } else {
+ ret[i] = msg
+ }
+ }
+
+ return ret, nil
+}
+
+func unmarshalWebMessageInfo(msg []byte) (*pb.WebMessageInfo, error) {
+ message := &pb.WebMessageInfo{}
+ err := proto.Unmarshal(msg, message)
+ if err != nil {
+ return nil, err
+ }
+ return message, nil
+}