From 4d16a3e43603c6456f9137fca8bcfe3059069108 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 17 Feb 2020 18:11:34 +0100 Subject: WIP IQ for avatars --- connector/xmpp/iq.go | 51 ++++++++++++++++++++++++++++++++++++++++ connector/xmpp/xmpp.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++---- go.mod | 4 ++++ go.sum | 8 +++++++ 4 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 connector/xmpp/iq.go diff --git a/connector/xmpp/iq.go b/connector/xmpp/iq.go new file mode 100644 index 0000000..1405614 --- /dev/null +++ b/connector/xmpp/iq.go @@ -0,0 +1,51 @@ +package xmpp + +import ( + "encoding/xml" +) + +type DiscoQuery struct { + XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items query"` + Items []Node `xml:"item"` +} + +type Node struct { + XMLName xml.Name `xml:"item"` + Jid string `xml:"jid,attr"` + Node string `xml:"node,attr"` +} + +type PubSub struct { + XMLName xml.Name `xml:"http://jabber.org/protocol/pubsub pubsub"` + Subscribe []Subscribe `xml:"subscribe"` + Subscription []Subscription `xml:"subscription"` + Subscriptions []Subscription `xml:"subscriptions"` + Publish *Publish `xml:"publish"` +} + +type Subscribe struct { + XMLName xml.Name `xml:"subscribe"` + Jid string `xml:"jid,attr"` + Node string `xml:"node,attr"` +} + +type Subscription struct { + XMLName xml.Name `xml:"subscription"` + Jid string `xml:"jid,attr"` + Node string `xml:"node,attr"` + SubID string `xml:"subid,attr"` + Subscription string `xml:"subscription,attr"` +} + +type Publish struct { + XMLName xml.Name `xml:"publish"` + Node string `xml:"node,attr"` + Item []Item `xml:"item"` + Items []Item `xml:"items"` +} + +type Item struct { + XMLName xml.Name `xml:"publish"` + Id string `xml:"id,attr"` + Data string `xml:",innerxml"` +} diff --git a/connector/xmpp/xmpp.go b/connector/xmpp/xmpp.go index e50bb58..a62cdc4 100644 --- a/connector/xmpp/xmpp.go +++ b/connector/xmpp/xmpp.go @@ -6,9 +6,11 @@ import ( "strings" "fmt" "crypto/tls" + "encoding/xml" log "github.com/sirupsen/logrus" - gxmpp "github.com/mattn/go-xmpp" + //gxmpp "github.com/mattn/go-xmpp" + gxmpp "git.deuxfleurs.fr/lx/go-xmpp" . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" ) @@ -177,10 +179,10 @@ func (xm *XMPP) handleXMPP() error { return err } - fmt.Printf("XMPP: %#v\n", m) switch v := m.(type) { case gxmpp.Chat: + fmt.Printf("XMPP chat: %#v\n", v) remote_sp := strings.Split(v.Remote, "/") // Skip self-sent events @@ -228,6 +230,8 @@ func (xm *XMPP) handleXMPP() error { } } case gxmpp.Presence: + fmt.Printf("XMPP presence: %#v\n", v) + remote := strings.Split(v.From, "/") if ismuc, ok := xm.isMUC[remote[0]]; ok && ismuc { // skip presence with no user and self-presence @@ -248,8 +252,59 @@ func (xm *XMPP) handleXMPP() error { xm.handler.UserInfoUpdated(user, &UserInfo{ DisplayName: remote[1], }) + } else { + // Send discovery query + iq, err := xml.Marshal(&DiscoQuery{}) + if err != nil { + fmt.Printf("XML marshall error: %s\n", err) + } else { + xm.conn.SendIQ(gxmpp.IQ{ + Type: "get", + To: remote[0], + ID: "items1", + Query: iq, + }) + } + } + case gxmpp.IQ: + fmt.Printf("XMPP iq: from=%s to=%s id=%s type=%s\n", v.From, v.To, v.ID, v.Type) + if len(v.Query) > 0 { + fmt.Printf("Query data: %s\n", string(v.Query)) + } + + if v.Type == "result" && v.ID == "items1" { + var q DiscoQuery + err := xml.Unmarshal(v.Query, &q) + if err != nil { + fmt.Printf("XML unmarshall error: %s\n", err) + continue + } + + for _, item := range q.Items { + if item.Node == "urn:xmpp:avatar:metadata" || item.Node == "urn:xmpp:avatar:data" { + sub := &PubSub{ + Subscribe: []Subscribe{ + Subscribe{ + Jid: xm.jid, + Node: item.Node, + }, + }, + } + iq, err := xml.Marshal(sub) + if err != nil { + fmt.Printf("XML marshall error: %s\n", err) + } else { + fmt.Printf("IQ AVATAR SUB: %s\n", iq) + xm.conn.SendIQ(gxmpp.IQ{ + To: v.From, + Type: "set", + ID: "sub1", + Query: iq, + }) + } + } + } } - // Do nothing. } } } @@ -297,7 +352,7 @@ func (xm *XMPP) Invite(userId UserID, roomId RoomID) error { } func (xm *XMPP) Leave(roomId RoomID) { - // TODO + xm.conn.LeaveMUC(string(roomId)) } func (xm *XMPP) Send(event *Event) error { diff --git a/go.mod b/go.mod index 9c826c6..2d294c7 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,14 @@ module git.deuxfleurs.fr/Deuxfleurs/easybridge go 1.13 require ( + git.deuxfleurs.fr/lx/go-xmpp v0.0.0-20200217161715-21c9a1d8b8fd + git.deuxfleurs.fr/lx/gxmpp v0.0.0-20200217161715-21c9a1d8b8fd github.com/gorilla/mux v1.7.4 github.com/jinzhu/gorm v1.9.12 github.com/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7 github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6 + github.com/mattn/go-gtk v0.0.0-20191030024613-af2e013261f5 + github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f // indirect github.com/mattn/go-xmpp v0.0.0-20200128155807-a86b6abcb3ad github.com/sirupsen/logrus v1.4.2 gopkg.in/yaml.v2 v2.2.8 diff --git a/go.sum b/go.sum index 2b37958..cc833b6 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +git.deuxfleurs.fr/lx/go-xmpp v0.0.0-20200217161715-21c9a1d8b8fd h1:lG3g6pY8MiSebJRyFSn+aPndtOoQxFCpy81Cuez661U= +git.deuxfleurs.fr/lx/go-xmpp v0.0.0-20200217161715-21c9a1d8b8fd/go.mod h1:IFE41QLqJ1CiGufB6JV0nh6B7kQt94I3/aAWo5xNFEo= +git.deuxfleurs.fr/lx/gxmpp v0.0.0-20200217161715-21c9a1d8b8fd h1:ZpGuco7meCBnn6+IWl/PrIDNy+Z/iCSxJ1BAGcd28NA= +git.deuxfleurs.fr/lx/gxmpp v0.0.0-20200217161715-21c9a1d8b8fd/go.mod h1:yvP+HMKQoSlb+EiqrxQzl0YPKtdHN5gv7ElMB0IlHy4= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= @@ -19,6 +23,10 @@ github.com/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7 h1:BS9tqL0OCiOGuy/C github.com/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7/go.mod h1:liX5MxHPrwgHaKowoLkYGwbXfYABh1jbZ6FpElbGF1I= github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6 h1:GDh7egrbDEzP41mScMt7Q/uPM2nJENh9LNFXjUOGts8= github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6/go.mod h1:ECDRehsR9TYTKCAsRS8/wLeOk6UUqDydw47ln7wG41Q= +github.com/mattn/go-gtk v0.0.0-20191030024613-af2e013261f5 h1:GMB3MVJnxysGrSvjWGsgK8L3XGI3F4etQQq37Py6W5A= +github.com/mattn/go-gtk v0.0.0-20191030024613-af2e013261f5/go.mod h1:PwzwfeB5syFHXORC3MtPylVcjIoTDT/9cvkKpEndGVI= +github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f h1:QTRRO+ozoYgT3CQRIzNVYJRU3DB8HRnkZv6mr4ISmMA= +github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc= github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-xmpp v0.0.0-20200128155807-a86b6abcb3ad h1:ntj2CDcRNjFht20llTwIwwguKa00u0UCLtF2J5+Gmxo= -- cgit v1.2.3