summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Rhymen/go-whatsapp/binary/node.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2022-03-20 02:20:54 +0100
committerWim <wim@42.be>2022-03-20 14:57:48 +0100
commit496d5b4ec7f5f4afae6199f928675d14d18de015 (patch)
tree3a649c80a76f0ffc33e6b3a63c0e65e892169cde /vendor/github.com/Rhymen/go-whatsapp/binary/node.go
parent2623a412c42a81104b97ae8c81a5f66760fee4b6 (diff)
downloadmatterbridge-msglm-496d5b4ec7f5f4afae6199f928675d14d18de015.tar.gz
matterbridge-msglm-496d5b4ec7f5f4afae6199f928675d14d18de015.tar.bz2
matterbridge-msglm-496d5b4ec7f5f4afae6199f928675d14d18de015.zip
Add whatsappmulti buildflag for whatsapp with multidevice support (whatsapp)
Diffstat (limited to 'vendor/github.com/Rhymen/go-whatsapp/binary/node.go')
-rw-r--r--vendor/github.com/Rhymen/go-whatsapp/binary/node.go106
1 files changed, 106 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..66b28d88
--- /dev/null
+++ b/vendor/github.com/Rhymen/go-whatsapp/binary/node.go
@@ -0,0 +1,106 @@
+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 {
+ nContent, ok := n.Content.([]Node)
+ if ok {
+ n.Content, err = unmarshalMessageArray(nContent)
+ 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
+}