WIP IQ for avatars

This commit is contained in:
Alex 2020-02-17 18:11:34 +01:00
parent 86942a34a2
commit 4d16a3e436
4 changed files with 122 additions and 4 deletions

51
connector/xmpp/iq.go Normal file
View File

@ -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"`
}

View File

@ -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 {

4
go.mod
View File

@ -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

8
go.sum
View File

@ -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=