summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/matterbridge/go-whatsapp/binary/node.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-03-02 13:04:28 +0100
committerGitHub <noreply@github.com>2019-03-02 13:04:28 +0100
commitdf3fdc26a01a20c3568b824f4f15f9b56a2f3db3 (patch)
tree053e3caf197063f94af386e60c67e4bef69a7863 /vendor/github.com/matterbridge/go-whatsapp/binary/node.go
parentaf00c34aaccb3521b8dbf0bd8876a5b38d5d42f4 (diff)
downloadmatterbridge-msglm-df3fdc26a01a20c3568b824f4f15f9b56a2f3db3.tar.gz
matterbridge-msglm-df3fdc26a01a20c3568b824f4f15f9b56a2f3db3.tar.bz2
matterbridge-msglm-df3fdc26a01a20c3568b824f4f15f9b56a2f3db3.zip
Use whatsapp forks (#750)
Diffstat (limited to 'vendor/github.com/matterbridge/go-whatsapp/binary/node.go')
-rw-r--r--vendor/github.com/matterbridge/go-whatsapp/binary/node.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/github.com/matterbridge/go-whatsapp/binary/node.go b/vendor/github.com/matterbridge/go-whatsapp/binary/node.go
new file mode 100644
index 00000000..0e55656e
--- /dev/null
+++ b/vendor/github.com/matterbridge/go-whatsapp/binary/node.go
@@ -0,0 +1,103 @@
+package binary
+
+import (
+ "fmt"
+ pb "github.com/matterbridge/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
+}